bgfx/examples/04-mesh/mesh.cpp

161 lines
3.5 KiB
C++
Raw Normal View History

2012-10-08 07:41:18 +04:00
/*
2020-01-15 08:37:06 +03:00
* Copyright 2011-2020 Branimir Karadzic. All rights reserved.
2016-01-01 11:11:04 +03:00
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
2012-10-08 07:41:18 +04:00
*/
#include "common.h"
2014-05-04 02:18:28 +04:00
#include "bgfx_utils.h"
2017-06-26 07:44:04 +03:00
#include "imgui/imgui.h"
namespace
{
2012-10-08 07:41:18 +04:00
2016-03-07 02:29:22 +03:00
class ExampleMesh : public entry::AppI
2012-10-08 07:41:18 +04:00
{
2017-06-26 07:44:04 +03:00
public:
2019-08-15 18:11:34 +03:00
ExampleMesh(const char* _name, const char* _description, const char* _url)
: entry::AppI(_name, _description, _url)
2017-06-26 07:44:04 +03:00
{
}
2017-07-15 10:17:29 +03:00
void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
2016-03-07 02:29:22 +03:00
{
Args args(_argc, _argv);
2016-04-06 05:33:05 +03:00
2017-06-30 08:23:18 +03:00
m_width = _width;
m_height = _height;
2017-06-26 07:44:04 +03:00
m_debug = BGFX_DEBUG_NONE;
2016-03-07 02:29:22 +03:00
m_reset = BGFX_RESET_VSYNC;
bgfx::Init init;
init.type = args.m_type;
init.vendorId = args.m_pciId;
init.resolution.width = m_width;
init.resolution.height = m_height;
init.resolution.reset = m_reset;
bgfx::init(init);
2012-10-08 07:41:18 +04:00
2016-03-07 02:29:22 +03:00
// Enable debug text.
bgfx::setDebug(m_debug);
2012-10-08 07:41:18 +04:00
2016-03-07 02:29:22 +03:00
// Set view 0 clear state.
bgfx::setViewClear(0
, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
, 0x303030ff
, 1.0f
, 0
);
2012-10-08 07:41:18 +04:00
2016-03-07 02:29:22 +03:00
u_time = bgfx::createUniform("u_time", bgfx::UniformType::Vec4);
2012-10-08 07:41:18 +04:00
2016-03-07 02:29:22 +03:00
// Create program from shaders.
m_program = loadProgram("vs_mesh", "fs_mesh");
2012-10-08 07:41:18 +04:00
2016-03-07 02:29:22 +03:00
m_mesh = meshLoad("meshes/bunny.bin");
2012-10-08 07:41:18 +04:00
2016-03-07 02:29:22 +03:00
m_timeOffset = bx::getHPCounter();
2017-06-26 07:44:04 +03:00
imguiCreate();
2016-03-07 02:29:22 +03:00
}
2017-07-15 10:17:29 +03:00
int shutdown() override
2012-10-08 07:41:18 +04:00
{
2017-06-26 07:44:04 +03:00
imguiDestroy();
2016-03-07 02:29:22 +03:00
meshUnload(m_mesh);
2016-03-07 02:29:22 +03:00
// Cleanup.
bgfx::destroy(m_program);
bgfx::destroy(u_time);
2016-03-07 02:29:22 +03:00
// Shutdown bgfx.
bgfx::shutdown();
2016-03-07 02:29:22 +03:00
return 0;
}
2017-07-15 10:17:29 +03:00
bool update() override
2016-03-07 02:29:22 +03:00
{
2017-06-26 07:44:04 +03:00
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
2016-03-07 02:29:22 +03:00
{
2017-06-26 07:44:04 +03:00
imguiBeginFrame(m_mouseState.m_mx
, m_mouseState.m_my
, (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
| (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
| (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
, m_mouseState.m_mz
, uint16_t(m_width)
, uint16_t(m_height)
);
2017-06-30 09:19:20 +03:00
showExampleDialog(this);
2017-06-26 07:44:04 +03:00
imguiEndFrame();
// Set view 0 default viewport.
2016-05-07 07:29:47 +03:00
bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
2016-03-07 02:29:22 +03:00
// This dummy draw call is here to make sure that view 0 is cleared
// if no other draw calls are submitted to view 0.
bgfx::touch(0);
float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ) );
bgfx::setUniform(u_time, &time);
2018-11-17 08:54:20 +03:00
const bx::Vec3 at = { 0.0f, 1.0f, 0.0f };
const bx::Vec3 eye = { 0.0f, 1.0f, -2.5f };
2016-03-07 02:29:22 +03:00
// Set view and projection matrix for view 0.
{
float view[16];
bx::mtxLookAt(view, eye, at);
float proj[16];
2017-02-23 09:17:44 +03:00
bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
2016-03-07 02:29:22 +03:00
bgfx::setViewTransform(0, view, proj);
// Set view 0 default viewport.
2016-05-07 07:29:47 +03:00
bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
2016-03-07 02:29:22 +03:00
}
float mtx[16];
bx::mtxRotateXY(mtx
, 0.0f
, time*0.37f
);
meshSubmit(m_mesh, 0, m_program, mtx);
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
bgfx::frame();
2017-06-30 09:19:20 +03:00
return true;
}
2012-10-08 07:41:18 +04:00
2016-03-07 02:29:22 +03:00
return false;
2012-10-08 07:41:18 +04:00
}
2017-06-26 07:44:04 +03:00
entry::MouseState m_mouseState;
2016-03-07 02:29:22 +03:00
uint32_t m_width;
uint32_t m_height;
uint32_t m_debug;
uint32_t m_reset;
2012-10-08 07:41:18 +04:00
2016-03-07 02:29:22 +03:00
int64_t m_timeOffset;
Mesh* m_mesh;
bgfx::ProgramHandle m_program;
bgfx::UniformHandle u_time;
};
2012-10-08 07:41:18 +04:00
2017-06-26 07:44:04 +03:00
} // namespace
2019-08-15 18:11:34 +03:00
ENTRY_IMPLEMENT_MAIN(
ExampleMesh
, "04-mesh"
, "Loading meshes."
, "https://bkaradzic.github.io/bgfx/examples.html#mesh"
);