From 086285a3e2806c6275a50aa67bbac70e477124da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Sun, 6 Mar 2016 15:29:22 -0800 Subject: [PATCH] Cleanup. --- examples/01-cubes/cubes.cpp | 4 +- examples/02-metaballs/metaballs.cpp | 4 +- examples/03-raymarch/raymarch.cpp | 267 ++++++------ examples/04-mesh/mesh.cpp | 225 +++++----- examples/05-instancing/instancing.cpp | 4 +- examples/06-bump/bump.cpp | 4 +- examples/08-update/update.cpp | 6 +- examples/09-hdr/hdr.cpp | 4 +- examples/12-lod/lod.cpp | 4 +- examples/17-drawstress/drawstress.cpp | 4 +- examples/21-deferred/deferred.cpp | 4 +- examples/26-occlusion/occlusion.cpp | 4 +- examples/27-terrain/terrain.cpp | 4 +- examples/28-wireframe/wireframe.cpp | 566 +++++++++++++------------- 14 files changed, 577 insertions(+), 527 deletions(-) diff --git a/examples/01-cubes/cubes.cpp b/examples/01-cubes/cubes.cpp index de74b89e6..bf88eb3f7 100644 --- a/examples/01-cubes/cubes.cpp +++ b/examples/01-cubes/cubes.cpp @@ -55,7 +55,7 @@ static const uint16_t s_cubeIndices[36] = 6, 3, 7, }; -class Cubes : public entry::AppI +class ExampleCubes : public entry::AppI { void init(int _argc, char** _argv) BX_OVERRIDE { @@ -218,4 +218,4 @@ class Cubes : public entry::AppI int64_t m_timeOffset; }; -ENTRY_IMPLEMENT_MAIN(Cubes); +ENTRY_IMPLEMENT_MAIN(ExampleCubes); diff --git a/examples/02-metaballs/metaballs.cpp b/examples/02-metaballs/metaballs.cpp index a4ce1760f..bedf55497 100644 --- a/examples/02-metaballs/metaballs.cpp +++ b/examples/02-metaballs/metaballs.cpp @@ -462,7 +462,7 @@ uint32_t triangulate(uint8_t* _result, uint32_t _stride, const float* __restrict #define DIMS 32 -class Metaballs : public entry::AppI +class ExampleMetaballs : public entry::AppI { void init(int _argc, char** _argv) BX_OVERRIDE { @@ -780,4 +780,4 @@ class Metaballs : public entry::AppI int64_t m_timeOffset; }; -ENTRY_IMPLEMENT_MAIN(Metaballs); +ENTRY_IMPLEMENT_MAIN(ExampleMetaballs); diff --git a/examples/03-raymarch/raymarch.cpp b/examples/03-raymarch/raymarch.cpp index df07fc7ea..60a466454 100644 --- a/examples/03-raymarch/raymarch.cpp +++ b/examples/03-raymarch/raymarch.cpp @@ -102,138 +102,163 @@ void renderScreenSpaceQuad(uint32_t _view, bgfx::ProgramHandle _program, float _ } } -int _main_(int _argc, char** _argv) +class ExampleRaymarch : public entry::AppI { - Args args(_argc, _argv); - - uint32_t width = 1280; - uint32_t height = 720; - uint32_t debug = BGFX_DEBUG_TEXT; - uint32_t reset = BGFX_RESET_VSYNC; - - bgfx::init(args.m_type, args.m_pciId); - bgfx::reset(width, height, reset); - - // Enable debug text. - bgfx::setDebug(debug); - - // Set view 0 clear state. - bgfx::setViewClear(0 - , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH - , 0x303030ff - , 1.0f - , 0 - ); - - // Setup root path for binary shaders. Shader binaries are different - // for each renderer. - switch (bgfx::getRendererType() ) + void init(int _argc, char** _argv) BX_OVERRIDE { - default: - break; + Args args(_argc, _argv); + + m_width = 1280; + m_height = 720; + m_debug = BGFX_DEBUG_TEXT; + m_reset = BGFX_RESET_VSYNC; - case bgfx::RendererType::OpenGL: - case bgfx::RendererType::OpenGLES: - s_oglNdc = true; - break; + bgfx::init(args.m_type, args.m_pciId); + bgfx::reset(m_width, m_height, m_reset); + + // Enable debug text. + bgfx::setDebug(m_debug); + + // Set view 0 clear state. + bgfx::setViewClear(0 + , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH + , 0x303030ff + , 1.0f + , 0 + ); + + // Setup root path for binary shaders. Shader binaries are different + // for each renderer. + switch (bgfx::getRendererType() ) + { + default: + break; + + case bgfx::RendererType::OpenGL: + case bgfx::RendererType::OpenGLES: + s_oglNdc = true; + break; + } + + // Create vertex stream declaration. + PosColorTexCoord0Vertex::init(); + + u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4); + u_lightDirTime = bgfx::createUniform("u_lightDirTime", bgfx::UniformType::Vec4); + + // Create program from shaders. + m_program = loadProgram("vs_raymarching", "fs_raymarching"); + + m_timeOffset = bx::getHPCounter(); } - // Create vertex stream declaration. - PosColorTexCoord0Vertex::init(); - - bgfx::UniformHandle u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4); - bgfx::UniformHandle u_lightDirTime = bgfx::createUniform("u_lightDirTime", bgfx::UniformType::Vec4); - - // Create program from shaders. - bgfx::ProgramHandle raymarching = loadProgram("vs_raymarching", "fs_raymarching"); - - int64_t timeOffset = bx::getHPCounter(); - - while (!entry::processEvents(width, height, debug, reset) ) + int shutdown() BX_OVERRIDE { - // Set view 0 default viewport. - bgfx::setViewRect(0, 0, 0, width, height); + // Cleanup. + bgfx::destroyProgram(m_program); - // Set view 1 default viewport. - bgfx::setViewRect(1, 0, 0, width, height); + bgfx::destroyUniform(u_mtx); + bgfx::destroyUniform(u_lightDirTime); - // This dummy draw call is here to make sure that view 0 is cleared - // if no other draw calls are submitted to viewZ 0. - bgfx::touch(0); + // Shutdown bgfx. + bgfx::shutdown(); - int64_t now = bx::getHPCounter(); - static int64_t last = now; - const int64_t frameTime = now - last; - last = now; - const double freq = double(bx::getHPFrequency() ); - const double toMs = 1000.0/freq; - - // Use debug font to print information about this example. - bgfx::dbgTextClear(); - bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/03-raymarch"); - bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Updating shader uniforms."); - bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs); - - float at[3] = { 0.0f, 0.0f, 0.0f }; - float eye[3] = { 0.0f, 0.0f, -15.0f }; - - float view[16]; - float proj[16]; - bx::mtxLookAt(view, eye, at); - mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f); - - // Set view and projection matrix for view 1. - bgfx::setViewTransform(0, view, proj); - - float ortho[16]; - bx::mtxOrtho(ortho, 0.0f, 1280.0f, 720.0f, 0.0f, 0.0f, 100.0f); - - // Set view and projection matrix for view 0. - bgfx::setViewTransform(1, NULL, ortho); - - float time = (float)( (bx::getHPCounter()-timeOffset)/double(bx::getHPFrequency() ) ); - - float vp[16]; - bx::mtxMul(vp, view, proj); - - float mtx[16]; - bx::mtxRotateXY(mtx - , time - , time*0.37f - ); - - float mtxInv[16]; - bx::mtxInverse(mtxInv, mtx); - float lightDirModel[4] = { -0.4f, -0.5f, -1.0f, 0.0f }; - float lightDirModelN[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; - bx::vec3Norm(lightDirModelN, lightDirModel); - float lightDirTime[4]; - bx::vec4MulMtx(lightDirTime, lightDirModelN, mtxInv); - lightDirTime[3] = time; - bgfx::setUniform(u_lightDirTime, lightDirTime); - - float mvp[16]; - bx::mtxMul(mvp, mtx, vp); - - float invMvp[16]; - bx::mtxInverse(invMvp, mvp); - bgfx::setUniform(u_mtx, invMvp); - - renderScreenSpaceQuad(1, raymarching, 0.0f, 0.0f, 1280.0f, 720.0f); - - // Advance to next frame. Rendering thread will be kicked to - // process submitted rendering primitives. - bgfx::frame(); + return 0; } - // Cleanup. - bgfx::destroyProgram(raymarching); + bool update() BX_OVERRIDE + { + if (!entry::processEvents(m_width, m_height, m_debug, m_reset) ) + { + // Set view 0 default viewport. + bgfx::setViewRect(0, 0, 0, m_width, m_height); - bgfx::destroyUniform(u_mtx); - bgfx::destroyUniform(u_lightDirTime); + // Set view 1 default viewport. + bgfx::setViewRect(1, 0, 0, m_width, m_height); - // Shutdown bgfx. - bgfx::shutdown(); + // This dummy draw call is here to make sure that view 0 is cleared + // if no other draw calls are submitted to viewZ 0. + bgfx::touch(0); - return 0; -} + int64_t now = bx::getHPCounter(); + static int64_t last = now; + const int64_t frameTime = now - last; + last = now; + const double freq = double(bx::getHPFrequency() ); + const double toMs = 1000.0/freq; + + // Use debug font to print information about this example. + bgfx::dbgTextClear(); + bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/03-raymarch"); + bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Updating shader uniforms."); + bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs); + + float at[3] = { 0.0f, 0.0f, 0.0f }; + float eye[3] = { 0.0f, 0.0f, -15.0f }; + + float view[16]; + float proj[16]; + bx::mtxLookAt(view, eye, at); + mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f); + + // Set view and projection matrix for view 1. + bgfx::setViewTransform(0, view, proj); + + float ortho[16]; + bx::mtxOrtho(ortho, 0.0f, 1280.0f, 720.0f, 0.0f, 0.0f, 100.0f); + + // Set view and projection matrix for view 0. + bgfx::setViewTransform(1, NULL, ortho); + + float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ) ); + + float vp[16]; + bx::mtxMul(vp, view, proj); + + float mtx[16]; + bx::mtxRotateXY(mtx + , time + , time*0.37f + ); + + float mtxInv[16]; + bx::mtxInverse(mtxInv, mtx); + float lightDirModel[4] = { -0.4f, -0.5f, -1.0f, 0.0f }; + float lightDirModelN[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; + bx::vec3Norm(lightDirModelN, lightDirModel); + float lightDirTime[4]; + bx::vec4MulMtx(lightDirTime, lightDirModelN, mtxInv); + lightDirTime[3] = time; + bgfx::setUniform(u_lightDirTime, lightDirTime); + + float mvp[16]; + bx::mtxMul(mvp, mtx, vp); + + float invMvp[16]; + bx::mtxInverse(invMvp, mvp); + bgfx::setUniform(u_mtx, invMvp); + + renderScreenSpaceQuad(1, m_program, 0.0f, 0.0f, 1280.0f, 720.0f); + + // Advance to next frame. Rendering thread will be kicked to + // process submitted rendering primitives. + bgfx::frame(); + + return true; + } + + return false; + } + + uint32_t m_width; + uint32_t m_height; + uint32_t m_debug; + uint32_t m_reset; + + int64_t m_timeOffset; + bgfx::UniformHandle u_mtx; + bgfx::UniformHandle u_lightDirTime; + bgfx::ProgramHandle m_program; +}; + +ENTRY_IMPLEMENT_MAIN(ExampleRaymarch); diff --git a/examples/04-mesh/mesh.cpp b/examples/04-mesh/mesh.cpp index 854cf1a23..bfe61df6a 100644 --- a/examples/04-mesh/mesh.cpp +++ b/examples/04-mesh/mesh.cpp @@ -6,118 +6,143 @@ #include "common.h" #include "bgfx_utils.h" -int _main_(int _argc, char** _argv) +class ExampleMesh : public entry::AppI { - Args args(_argc, _argv); - - uint32_t width = 1280; - uint32_t height = 720; - uint32_t debug = BGFX_DEBUG_TEXT; - uint32_t reset = BGFX_RESET_VSYNC; - - bgfx::init(args.m_type, args.m_pciId); - bgfx::reset(width, height, reset); - - // Enable debug text. - bgfx::setDebug(debug); - - // Set view 0 clear state. - bgfx::setViewClear(0 - , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH - , 0x303030ff - , 1.0f - , 0 - ); - - bgfx::UniformHandle u_time = bgfx::createUniform("u_time", bgfx::UniformType::Vec4); - - // Create program from shaders. - bgfx::ProgramHandle program = loadProgram("vs_mesh", "fs_mesh"); - - Mesh* mesh = meshLoad("meshes/bunny.bin"); - - int64_t timeOffset = bx::getHPCounter(); - - while (!entry::processEvents(width, height, debug, reset) ) + void init(int _argc, char** _argv) BX_OVERRIDE { - // Set view 0 default viewport. - bgfx::setViewRect(0, 0, 0, width, height); + Args args(_argc, _argv); + + m_width = 1280; + m_height = 720; + m_debug = BGFX_DEBUG_TEXT; + m_reset = BGFX_RESET_VSYNC; - // 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); + bgfx::init(args.m_type, args.m_pciId); + bgfx::reset(m_width, m_height, m_reset); - int64_t now = bx::getHPCounter(); - static int64_t last = now; - const int64_t frameTime = now - last; - last = now; - const double freq = double(bx::getHPFrequency() ); - const double toMs = 1000.0/freq; - float time = (float)( (bx::getHPCounter()-timeOffset)/double(bx::getHPFrequency() ) ); - bgfx::setUniform(u_time, &time); + // Enable debug text. + bgfx::setDebug(m_debug); - // Use debug font to print information about this example. - bgfx::dbgTextClear(); - bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/04-mesh"); - bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Loading meshes."); - bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs); + // Set view 0 clear state. + bgfx::setViewClear(0 + , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH + , 0x303030ff + , 1.0f + , 0 + ); - float at[3] = { 0.0f, 1.0f, 0.0f }; - float eye[3] = { 0.0f, 1.0f, -2.5f }; + u_time = bgfx::createUniform("u_time", bgfx::UniformType::Vec4); - // Set view and projection matrix for view 0. - const bgfx::HMD* hmd = bgfx::getHMD(); - if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) ) - { - float view[16]; - bx::mtxQuatTranslationHMD(view, hmd->eye[0].rotation, eye); + // Create program from shaders. + m_program = loadProgram("vs_mesh", "fs_mesh"); - float proj[16]; - bx::mtxProj(proj, hmd->eye[0].fov, 0.1f, 100.0f); + m_mesh = meshLoad("meshes/bunny.bin"); - bgfx::setViewTransform(0, view, proj); - - // Set view 0 default viewport. - // - // Use HMD's width/height since HMD's internal frame buffer size - // might be much larger than window size. - bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height); - } - else - { - float view[16]; - bx::mtxLookAt(view, eye, at); - - float proj[16]; - bx::mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f); - bgfx::setViewTransform(0, view, proj); - - // Set view 0 default viewport. - bgfx::setViewRect(0, 0, 0, width, height); - } - - float mtx[16]; - bx::mtxRotateXY(mtx - , 0.0f - , time*0.37f - ); - - meshSubmit(mesh, 0, program, mtx); - - // Advance to next frame. Rendering thread will be kicked to - // process submitted rendering primitives. - bgfx::frame(); + m_timeOffset = bx::getHPCounter(); } - meshUnload(mesh); + int shutdown() BX_OVERRIDE + { + meshUnload(m_mesh); - // Cleanup. - bgfx::destroyProgram(program); + // Cleanup. + bgfx::destroyProgram(m_program); - bgfx::destroyUniform(u_time); + bgfx::destroyUniform(u_time); - // Shutdown bgfx. - bgfx::shutdown(); + // Shutdown bgfx. + bgfx::shutdown(); - return 0; -} + return 0; + } + + bool update() BX_OVERRIDE + { + if (!entry::processEvents(m_width, m_height, m_debug, m_reset) ) + { + // Set view 0 default viewport. + bgfx::setViewRect(0, 0, 0, m_width, m_height); + + // 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); + + int64_t now = bx::getHPCounter(); + static int64_t last = now; + const int64_t frameTime = now - last; + last = now; + const double freq = double(bx::getHPFrequency() ); + const double toMs = 1000.0/freq; + float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ) ); + bgfx::setUniform(u_time, &time); + + // Use debug font to print information about this example. + bgfx::dbgTextClear(); + bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/04-mesh"); + bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Loading meshes."); + bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs); + + float at[3] = { 0.0f, 1.0f, 0.0f }; + float eye[3] = { 0.0f, 1.0f, -2.5f }; + + // Set view and projection matrix for view 0. + const bgfx::HMD* hmd = bgfx::getHMD(); + if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) ) + { + float view[16]; + bx::mtxQuatTranslationHMD(view, hmd->eye[0].rotation, eye); + + float proj[16]; + bx::mtxProj(proj, hmd->eye[0].fov, 0.1f, 100.0f); + + bgfx::setViewTransform(0, view, proj); + + // Set view 0 default viewport. + // + // Use HMD's width/height since HMD's internal frame buffer size + // might be much larger than window size. + bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height); + } + else + { + float view[16]; + bx::mtxLookAt(view, eye, at); + + float proj[16]; + bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f); + bgfx::setViewTransform(0, view, proj); + + // Set view 0 default viewport. + bgfx::setViewRect(0, 0, 0, m_width, m_height); + } + + 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(); + + return true; + } + + return false; + } + + uint32_t m_width; + uint32_t m_height; + uint32_t m_debug; + uint32_t m_reset; + + int64_t m_timeOffset; + Mesh* m_mesh; + bgfx::ProgramHandle m_program; + bgfx::UniformHandle u_time; +}; + +ENTRY_IMPLEMENT_MAIN(ExampleMesh); diff --git a/examples/05-instancing/instancing.cpp b/examples/05-instancing/instancing.cpp index d0e415bb4..6e854b207 100644 --- a/examples/05-instancing/instancing.cpp +++ b/examples/05-instancing/instancing.cpp @@ -55,7 +55,7 @@ static const uint16_t s_cubeIndices[36] = 6, 3, 7, }; -class Instancing : public entry::AppI +class ExampleInstancing : public entry::AppI { void init(int _argc, char** _argv) BX_OVERRIDE { @@ -247,4 +247,4 @@ class Instancing : public entry::AppI int64_t m_timeOffset; }; -ENTRY_IMPLEMENT_MAIN(Instancing); +ENTRY_IMPLEMENT_MAIN(ExampleInstancing); diff --git a/examples/06-bump/bump.cpp b/examples/06-bump/bump.cpp index 2956caddf..bc5f9e74b 100644 --- a/examples/06-bump/bump.cpp +++ b/examples/06-bump/bump.cpp @@ -103,7 +103,7 @@ static const uint16_t s_cubeIndices[36] = 21, 23, 22, }; -class Bump : public entry::AppI +class ExampleBump : public entry::AppI { void init(int _argc, char** _argv) BX_OVERRIDE { @@ -387,4 +387,4 @@ class Bump : public entry::AppI int64_t m_timeOffset; }; -ENTRY_IMPLEMENT_MAIN(Bump); +ENTRY_IMPLEMENT_MAIN(ExampleBump); diff --git a/examples/08-update/update.cpp b/examples/08-update/update.cpp index 8a5d851ba..baa54c17e 100644 --- a/examples/08-update/update.cpp +++ b/examples/08-update/update.cpp @@ -115,10 +115,10 @@ static void updateTextureCubeRectBgra8(bgfx::TextureHandle _handle, uint8_t _sid static const uint32_t m_textureside = 512; static const uint32_t m_texture2dSize = 256; -class Update : public entry::AppI +class ExampleUpdate : public entry::AppI { public: - Update() + ExampleUpdate() : m_cube(m_textureside) { } @@ -575,4 +575,4 @@ public: }; -ENTRY_IMPLEMENT_MAIN(Update); +ENTRY_IMPLEMENT_MAIN(ExampleUpdate); diff --git a/examples/09-hdr/hdr.cpp b/examples/09-hdr/hdr.cpp index 047fb0aa5..bee42aed2 100644 --- a/examples/09-hdr/hdr.cpp +++ b/examples/09-hdr/hdr.cpp @@ -139,7 +139,7 @@ inline float square(float _x) return _x*_x; } -class HDR : public entry::AppI +class ExampleHDR : public entry::AppI { void init(int _argc, char** _argv) BX_OVERRIDE { @@ -561,4 +561,4 @@ class HDR : public entry::AppI float m_time; }; -ENTRY_IMPLEMENT_MAIN(HDR); +ENTRY_IMPLEMENT_MAIN(ExampleHDR); diff --git a/examples/12-lod/lod.cpp b/examples/12-lod/lod.cpp index e13387249..ef37d254c 100644 --- a/examples/12-lod/lod.cpp +++ b/examples/12-lod/lod.cpp @@ -23,7 +23,7 @@ static const KnightPos knightTour[8*4] = {7,1}, {6,3}, {5,1}, {7,0}, {6,2}, {4,3}, {3,1}, {2,3}, }; -class Lod : public entry::AppI +class ExampleLod : public entry::AppI { void init(int _argc, char** _argv) BX_OVERRIDE { @@ -316,4 +316,4 @@ class Lod : public entry::AppI bool m_transitions; }; -ENTRY_IMPLEMENT_MAIN(Lod); +ENTRY_IMPLEMENT_MAIN(ExampleLod); diff --git a/examples/17-drawstress/drawstress.cpp b/examples/17-drawstress/drawstress.cpp index 258c00b51..2e933c1cb 100644 --- a/examples/17-drawstress/drawstress.cpp +++ b/examples/17-drawstress/drawstress.cpp @@ -70,7 +70,7 @@ static const int64_t highwm = 1000000/65; static const int64_t lowwm = 1000000/57; #endif // BX_PLATFORM_EMSCRIPTEN || BX_PLATFORM_NACL -class DrawStress : public entry::AppI +class ExampleDrawStress : public entry::AppI { void init(int _argc, char** _argv) BX_OVERRIDE { @@ -350,4 +350,4 @@ class DrawStress : public entry::AppI bgfx::IndexBufferHandle m_ibh; }; -ENTRY_IMPLEMENT_MAIN(DrawStress); +ENTRY_IMPLEMENT_MAIN(ExampleDrawStress); diff --git a/examples/21-deferred/deferred.cpp b/examples/21-deferred/deferred.cpp index 5a97b7f21..2f8fb5d38 100644 --- a/examples/21-deferred/deferred.cpp +++ b/examples/21-deferred/deferred.cpp @@ -213,7 +213,7 @@ void screenSpaceQuad(float _textureWidth, float _textureHeight, float _texelHalf } } -class Deferred : public entry::AppI +class ExampleDeferred : public entry::AppI { void init(int _argc, char** _argv) BX_OVERRIDE { @@ -792,4 +792,4 @@ class Deferred : public entry::AppI int64_t m_timeOffset; }; -ENTRY_IMPLEMENT_MAIN(Deferred); +ENTRY_IMPLEMENT_MAIN(ExampleDeferred); diff --git a/examples/26-occlusion/occlusion.cpp b/examples/26-occlusion/occlusion.cpp index a6971cdd5..81e00f0c5 100644 --- a/examples/26-occlusion/occlusion.cpp +++ b/examples/26-occlusion/occlusion.cpp @@ -58,7 +58,7 @@ static const uint16_t s_cubeIndices[36] = 6, 3, 7, }; -class Occlusion : public entry::AppI +class ExampleOcclusion : public entry::AppI { void init(int _argc, char** _argv) BX_OVERRIDE { @@ -282,4 +282,4 @@ class Occlusion : public entry::AppI entry::WindowState m_state; }; -ENTRY_IMPLEMENT_MAIN(Occlusion); +ENTRY_IMPLEMENT_MAIN(ExampleOcclusion); diff --git a/examples/27-terrain/terrain.cpp b/examples/27-terrain/terrain.cpp index 067684b19..74d88ac35 100644 --- a/examples/27-terrain/terrain.cpp +++ b/examples/27-terrain/terrain.cpp @@ -58,7 +58,7 @@ struct BrushData float m_power; }; -class Terrain : public entry::AppI +class ExampleTerrain : public entry::AppI { void init(int _argc, char** _argv) BX_OVERRIDE { @@ -531,4 +531,4 @@ class Terrain : public entry::AppI int64_t m_timeOffset; }; -ENTRY_IMPLEMENT_MAIN(Terrain); +ENTRY_IMPLEMENT_MAIN(ExampleTerrain); diff --git a/examples/28-wireframe/wireframe.cpp b/examples/28-wireframe/wireframe.cpp index b7ecaf249..df464f51f 100644 --- a/examples/28-wireframe/wireframe.cpp +++ b/examples/28-wireframe/wireframe.cpp @@ -7,305 +7,305 @@ #include "bgfx_utils.h" #include "imgui/imgui.h" -class Wireframe : public entry::AppI +struct DrawMode { - struct DrawMode + enum { - enum - { - WireframeShaded, - Wireframe, - Shaded, - }; + WireframeShaded, + Wireframe, + Shaded, }; +}; - struct Camera +struct Camera +{ + Camera() { - Camera() + reset(); + } + + void reset() + { + m_target.curr[0] = 0.0f; + m_target.curr[1] = 0.0f; + m_target.curr[2] = 0.0f; + m_target.dest[0] = 0.0f; + m_target.dest[1] = 0.0f; + m_target.dest[2] = 0.0f; + + m_pos.curr[0] = 0.0f; + m_pos.curr[1] = 0.0f; + m_pos.curr[2] = -2.0f; + m_pos.dest[0] = 0.0f; + m_pos.dest[1] = 0.0f; + m_pos.dest[2] = -2.0f; + + m_orbit[0] = 0.0f; + m_orbit[1] = 0.0f; + } + + void mtxLookAt(float* _outViewMtx) + { + bx::mtxLookAt(_outViewMtx, m_pos.curr, m_target.curr); + } + + void orbit(float _dx, float _dy) + { + m_orbit[0] += _dx; + m_orbit[1] += _dy; + } + + void dolly(float _dz) + { + const float cnear = 0.01f; + const float cfar = 10.0f; + + const float toTarget[3] = { - reset(); - } - - void reset() + m_target.dest[0] - m_pos.dest[0], + m_target.dest[1] - m_pos.dest[1], + m_target.dest[2] - m_pos.dest[2], + }; + const float toTargetLen = bx::vec3Length(toTarget); + const float invToTargetLen = 1.0f/(toTargetLen+FLT_MIN); + const float toTargetNorm[3] = { - m_target.curr[0] = 0.0f; - m_target.curr[1] = 0.0f; - m_target.curr[2] = 0.0f; - m_target.dest[0] = 0.0f; - m_target.dest[1] = 0.0f; - m_target.dest[2] = 0.0f; - - m_pos.curr[0] = 0.0f; - m_pos.curr[1] = 0.0f; - m_pos.curr[2] = -2.0f; - m_pos.dest[0] = 0.0f; - m_pos.dest[1] = 0.0f; - m_pos.dest[2] = -2.0f; - - m_orbit[0] = 0.0f; - m_orbit[1] = 0.0f; - } - - void mtxLookAt(float* _outViewMtx) - { - bx::mtxLookAt(_outViewMtx, m_pos.curr, m_target.curr); - } - - void orbit(float _dx, float _dy) - { - m_orbit[0] += _dx; - m_orbit[1] += _dy; - } - - void dolly(float _dz) - { - const float cnear = 0.01f; - const float cfar = 10.0f; - - const float toTarget[3] = - { - m_target.dest[0] - m_pos.dest[0], - m_target.dest[1] - m_pos.dest[1], - m_target.dest[2] - m_pos.dest[2], - }; - const float toTargetLen = bx::vec3Length(toTarget); - const float invToTargetLen = 1.0f/(toTargetLen+FLT_MIN); - const float toTargetNorm[3] = - { - toTarget[0]*invToTargetLen, - toTarget[1]*invToTargetLen, - toTarget[2]*invToTargetLen, - }; - - float delta = toTargetLen*_dz; - float newLen = toTargetLen + delta; - if ( (cnear < newLen || _dz < 0.0f) - && (newLen < cfar || _dz > 0.0f) ) - { - m_pos.dest[0] += toTargetNorm[0]*delta; - m_pos.dest[1] += toTargetNorm[1]*delta; - m_pos.dest[2] += toTargetNorm[2]*delta; - } - } - - void consumeOrbit(float _amount) - { - float consume[2]; - consume[0] = m_orbit[0]*_amount; - consume[1] = m_orbit[1]*_amount; - m_orbit[0] -= consume[0]; - m_orbit[1] -= consume[1]; - - const float toPos[3] = - { - m_pos.curr[0] - m_target.curr[0], - m_pos.curr[1] - m_target.curr[1], - m_pos.curr[2] - m_target.curr[2], - }; - const float toPosLen = bx::vec3Length(toPos); - const float invToPosLen = 1.0f/(toPosLen+FLT_MIN); - const float toPosNorm[3] = - { - toPos[0]*invToPosLen, - toPos[1]*invToPosLen, - toPos[2]*invToPosLen, - }; - - float ll[2]; - latLongFromVec(ll[0], ll[1], toPosNorm); - ll[0] += consume[0]; - ll[1] -= consume[1]; - ll[1] = bx::fclamp(ll[1], 0.02f, 0.98f); - - float tmp[3]; - vecFromLatLong(tmp, ll[0], ll[1]); - - float diff[3]; - diff[0] = (tmp[0]-toPosNorm[0])*toPosLen; - diff[1] = (tmp[1]-toPosNorm[1])*toPosLen; - diff[2] = (tmp[2]-toPosNorm[2])*toPosLen; - - m_pos.curr[0] += diff[0]; - m_pos.curr[1] += diff[1]; - m_pos.curr[2] += diff[2]; - m_pos.dest[0] += diff[0]; - m_pos.dest[1] += diff[1]; - m_pos.dest[2] += diff[2]; - } - - void update(float _dt) - { - const float amount = bx::fmin(_dt/0.12f, 1.0f); - - consumeOrbit(amount); - - m_target.curr[0] = bx::flerp(m_target.curr[0], m_target.dest[0], amount); - m_target.curr[1] = bx::flerp(m_target.curr[1], m_target.dest[1], amount); - m_target.curr[2] = bx::flerp(m_target.curr[2], m_target.dest[2], amount); - m_pos.curr[0] = bx::flerp(m_pos.curr[0], m_pos.dest[0], amount); - m_pos.curr[1] = bx::flerp(m_pos.curr[1], m_pos.dest[1], amount); - m_pos.curr[2] = bx::flerp(m_pos.curr[2], m_pos.dest[2], amount); - } - - static inline void vecFromLatLong(float _vec[3], float _u, float _v) - { - const float phi = _u * 2.0f*bx::pi; - const float theta = _v * bx::pi; - - const float st = bx::fsin(theta); - const float sp = bx::fsin(phi); - const float ct = bx::fcos(theta); - const float cp = bx::fcos(phi); - - _vec[0] = -st*sp; - _vec[1] = ct; - _vec[2] = -st*cp; - } - - static inline void latLongFromVec(float& _u, float& _v, const float _vec[3]) - { - const float phi = atan2f(_vec[0], _vec[2]); - const float theta = acosf(_vec[1]); - - _u = (bx::pi + phi)*bx::invPi*0.5f; - _v = theta*bx::invPi; - } - - struct Interp3f - { - float curr[3]; - float dest[3]; + toTarget[0]*invToTargetLen, + toTarget[1]*invToTargetLen, + toTarget[2]*invToTargetLen, }; - Interp3f m_target; - Interp3f m_pos; - float m_orbit[2]; - }; + float delta = toTargetLen*_dz; + float newLen = toTargetLen + delta; + if ( (cnear < newLen || _dz < 0.0f) + && (newLen < cfar || _dz > 0.0f) ) + { + m_pos.dest[0] += toTargetNorm[0]*delta; + m_pos.dest[1] += toTargetNorm[1]*delta; + m_pos.dest[2] += toTargetNorm[2]*delta; + } + } - struct Mouse + void consumeOrbit(float _amount) { - Mouse() + float consume[2]; + consume[0] = m_orbit[0]*_amount; + consume[1] = m_orbit[1]*_amount; + m_orbit[0] -= consume[0]; + m_orbit[1] -= consume[1]; + + const float toPos[3] = { - m_dx = 0.0f; - m_dy = 0.0f; - m_prevMx = 0.0f; - m_prevMx = 0.0f; - m_scroll = 0; - m_scrollPrev = 0; - } - - void update(float _mx, float _my, int32_t _mz, uint32_t _width, uint32_t _height) + m_pos.curr[0] - m_target.curr[0], + m_pos.curr[1] - m_target.curr[1], + m_pos.curr[2] - m_target.curr[2], + }; + const float toPosLen = bx::vec3Length(toPos); + const float invToPosLen = 1.0f/(toPosLen+FLT_MIN); + const float toPosNorm[3] = { - const float widthf = float(int32_t(_width)); - const float heightf = float(int32_t(_height)); - - // Delta movement. - m_dx = float(_mx - m_prevMx)/widthf; - m_dy = float(_my - m_prevMy)/heightf; - - m_prevMx = _mx; - m_prevMy = _my; - - // Scroll. - m_scroll = _mz - m_scrollPrev; - m_scrollPrev = _mz; - } - - float m_dx; // Screen space. - float m_dy; - float m_prevMx; - float m_prevMy; - int32_t m_scroll; - int32_t m_scrollPrev; - }; - - struct MeshMtx - { - MeshMtx() - { - m_mesh = NULL; - } - - void init(const char* _path - , float _scale = 1.0f - , float _rotX = 0.0f - , float _rotY = 0.0f - , float _rotZ = 0.0f - , float _transX = 0.0f - , float _transY = 0.0f - , float _transZ = 0.0f - ) - { - m_mesh = meshLoad(_path); - bx::mtxSRT(m_mtx - , _scale - , _scale - , _scale - , _rotX - , _rotY - , _rotZ - , _transX - , _transY - , _transZ - ); - } - - void destroy() - { - if (NULL != m_mesh) - { - meshUnload(m_mesh); - } - } - - Mesh* m_mesh; - float m_mtx[16]; - }; - - struct Uniforms - { - enum { NumVec4 = 3 }; - - void init() - { - m_camPos[0] = 0.0f; - m_camPos[1] = 1.0f; - m_camPos[2] = -2.5f; - m_wfColor[0] = 1.0f; - m_wfColor[1] = 0.0f; - m_wfColor[2] = 0.0f; - m_wfOpacity = 0.7f; - m_drawEdges = 0.0f; - m_wfThickness = 1.5f; - - u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4, NumVec4); - } - - void submit() - { - bgfx::setUniform(u_params, m_params, NumVec4); - } - - void destroy() - { - bgfx::destroyUniform(u_params); - } - - union - { - struct - { - /*0*/struct { float m_camPos[3], m_unused0; }; - /*1*/struct { float m_wfColor[3], m_wfOpacity; }; - /*2*/struct { float m_drawEdges, m_wfThickness, m_unused2[2]; }; - }; - - float m_params[NumVec4*4]; + toPos[0]*invToPosLen, + toPos[1]*invToPosLen, + toPos[2]*invToPosLen, }; - bgfx::UniformHandle u_params; + float ll[2]; + latLongFromVec(ll[0], ll[1], toPosNorm); + ll[0] += consume[0]; + ll[1] -= consume[1]; + ll[1] = bx::fclamp(ll[1], 0.02f, 0.98f); + + float tmp[3]; + vecFromLatLong(tmp, ll[0], ll[1]); + + float diff[3]; + diff[0] = (tmp[0]-toPosNorm[0])*toPosLen; + diff[1] = (tmp[1]-toPosNorm[1])*toPosLen; + diff[2] = (tmp[2]-toPosNorm[2])*toPosLen; + + m_pos.curr[0] += diff[0]; + m_pos.curr[1] += diff[1]; + m_pos.curr[2] += diff[2]; + m_pos.dest[0] += diff[0]; + m_pos.dest[1] += diff[1]; + m_pos.dest[2] += diff[2]; + } + + void update(float _dt) + { + const float amount = bx::fmin(_dt/0.12f, 1.0f); + + consumeOrbit(amount); + + m_target.curr[0] = bx::flerp(m_target.curr[0], m_target.dest[0], amount); + m_target.curr[1] = bx::flerp(m_target.curr[1], m_target.dest[1], amount); + m_target.curr[2] = bx::flerp(m_target.curr[2], m_target.dest[2], amount); + m_pos.curr[0] = bx::flerp(m_pos.curr[0], m_pos.dest[0], amount); + m_pos.curr[1] = bx::flerp(m_pos.curr[1], m_pos.dest[1], amount); + m_pos.curr[2] = bx::flerp(m_pos.curr[2], m_pos.dest[2], amount); + } + + static inline void vecFromLatLong(float _vec[3], float _u, float _v) + { + const float phi = _u * 2.0f*bx::pi; + const float theta = _v * bx::pi; + + const float st = bx::fsin(theta); + const float sp = bx::fsin(phi); + const float ct = bx::fcos(theta); + const float cp = bx::fcos(phi); + + _vec[0] = -st*sp; + _vec[1] = ct; + _vec[2] = -st*cp; + } + + static inline void latLongFromVec(float& _u, float& _v, const float _vec[3]) + { + const float phi = atan2f(_vec[0], _vec[2]); + const float theta = acosf(_vec[1]); + + _u = (bx::pi + phi)*bx::invPi*0.5f; + _v = theta*bx::invPi; + } + + struct Interp3f + { + float curr[3]; + float dest[3]; }; + Interp3f m_target; + Interp3f m_pos; + float m_orbit[2]; +}; + +struct Mouse +{ + Mouse() + { + m_dx = 0.0f; + m_dy = 0.0f; + m_prevMx = 0.0f; + m_prevMx = 0.0f; + m_scroll = 0; + m_scrollPrev = 0; + } + + void update(float _mx, float _my, int32_t _mz, uint32_t _width, uint32_t _height) + { + const float widthf = float(int32_t(_width)); + const float heightf = float(int32_t(_height)); + + // Delta movement. + m_dx = float(_mx - m_prevMx)/widthf; + m_dy = float(_my - m_prevMy)/heightf; + + m_prevMx = _mx; + m_prevMy = _my; + + // Scroll. + m_scroll = _mz - m_scrollPrev; + m_scrollPrev = _mz; + } + + float m_dx; // Screen space. + float m_dy; + float m_prevMx; + float m_prevMy; + int32_t m_scroll; + int32_t m_scrollPrev; +}; + +struct MeshMtx +{ + MeshMtx() + { + m_mesh = NULL; + } + + void init(const char* _path + , float _scale = 1.0f + , float _rotX = 0.0f + , float _rotY = 0.0f + , float _rotZ = 0.0f + , float _transX = 0.0f + , float _transY = 0.0f + , float _transZ = 0.0f + ) + { + m_mesh = meshLoad(_path); + bx::mtxSRT(m_mtx + , _scale + , _scale + , _scale + , _rotX + , _rotY + , _rotZ + , _transX + , _transY + , _transZ + ); + } + + void destroy() + { + if (NULL != m_mesh) + { + meshUnload(m_mesh); + } + } + + Mesh* m_mesh; + float m_mtx[16]; +}; + +struct Uniforms +{ + enum { NumVec4 = 3 }; + + void init() + { + m_camPos[0] = 0.0f; + m_camPos[1] = 1.0f; + m_camPos[2] = -2.5f; + m_wfColor[0] = 1.0f; + m_wfColor[1] = 0.0f; + m_wfColor[2] = 0.0f; + m_wfOpacity = 0.7f; + m_drawEdges = 0.0f; + m_wfThickness = 1.5f; + + u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4, NumVec4); + } + + void submit() + { + bgfx::setUniform(u_params, m_params, NumVec4); + } + + void destroy() + { + bgfx::destroyUniform(u_params); + } + + union + { + struct + { + /*0*/struct { float m_camPos[3], m_unused0; }; + /*1*/struct { float m_wfColor[3], m_wfOpacity; }; + /*2*/struct { float m_drawEdges, m_wfThickness, m_unused2[2]; }; + }; + + float m_params[NumVec4*4]; + }; + + bgfx::UniformHandle u_params; +}; + +class ExampleWireframe : public entry::AppI +{ void init(int _argc, char** _argv) BX_OVERRIDE { Args args(_argc, _argv); @@ -563,4 +563,4 @@ class Wireframe : public entry::AppI int32_t m_scrollArea; }; -ENTRY_IMPLEMENT_MAIN(Wireframe); +ENTRY_IMPLEMENT_MAIN(ExampleWireframe);