summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/examples/06-bump/bump.cpp
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/bgfx/examples/06-bump/bump.cpp')
-rw-r--r--3rdparty/bgfx/examples/06-bump/bump.cpp174
1 files changed, 85 insertions, 89 deletions
diff --git a/3rdparty/bgfx/examples/06-bump/bump.cpp b/3rdparty/bgfx/examples/06-bump/bump.cpp
index 101bf8fc96d..e60fb042fba 100644
--- a/3rdparty/bgfx/examples/06-bump/bump.cpp
+++ b/3rdparty/bgfx/examples/06-bump/bump.cpp
@@ -5,6 +5,10 @@
#include "common.h"
#include "bgfx_utils.h"
+#include "imgui/imgui.h"
+
+namespace
+{
struct PosNormalTangentTexcoordVertex
{
@@ -32,57 +36,32 @@ struct PosNormalTangentTexcoordVertex
bgfx::VertexDecl PosNormalTangentTexcoordVertex::ms_decl;
-uint32_t packUint32(uint8_t _x, uint8_t _y, uint8_t _z, uint8_t _w)
-{
- union
- {
- uint32_t ui32;
- uint8_t arr[4];
- } un;
-
- un.arr[0] = _x;
- un.arr[1] = _y;
- un.arr[2] = _z;
- un.arr[3] = _w;
-
- return un.ui32;
-}
-
-uint32_t packF4u(float _x, float _y = 0.0f, float _z = 0.0f, float _w = 0.0f)
-{
- const uint8_t xx = uint8_t(_x*127.0f + 128.0f);
- const uint8_t yy = uint8_t(_y*127.0f + 128.0f);
- const uint8_t zz = uint8_t(_z*127.0f + 128.0f);
- const uint8_t ww = uint8_t(_w*127.0f + 128.0f);
- return packUint32(xx, yy, zz, ww);
-}
-
static PosNormalTangentTexcoordVertex s_cubeVertices[24] =
{
- {-1.0f, 1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0, 0, 0 },
- { 1.0f, 1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0, 0x7fff, 0 },
- {-1.0f, -1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0, 0, 0x7fff },
- { 1.0f, -1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0, 0x7fff, 0x7fff },
- {-1.0f, 1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0, 0, 0 },
- { 1.0f, 1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0, 0x7fff, 0 },
- {-1.0f, -1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0, 0, 0x7fff },
- { 1.0f, -1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0, 0x7fff, 0x7fff },
- {-1.0f, 1.0f, 1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0, 0, 0 },
- { 1.0f, 1.0f, 1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0, 0x7fff, 0 },
- {-1.0f, 1.0f, -1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0, 0, 0x7fff },
- { 1.0f, 1.0f, -1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0, 0x7fff, 0x7fff },
- {-1.0f, -1.0f, 1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0, 0, 0 },
- { 1.0f, -1.0f, 1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0, 0x7fff, 0 },
- {-1.0f, -1.0f, -1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0, 0, 0x7fff },
- { 1.0f, -1.0f, -1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0, 0x7fff, 0x7fff },
- { 1.0f, -1.0f, 1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0, 0, 0 },
- { 1.0f, 1.0f, 1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0, 0x7fff, 0 },
- { 1.0f, -1.0f, -1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0, 0, 0x7fff },
- { 1.0f, 1.0f, -1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0, 0x7fff, 0x7fff },
- {-1.0f, -1.0f, 1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0, 0, 0 },
- {-1.0f, 1.0f, 1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0, 0x7fff, 0 },
- {-1.0f, -1.0f, -1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0, 0, 0x7fff },
- {-1.0f, 1.0f, -1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0, 0x7fff, 0x7fff },
+ {-1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0, 0, 0 },
+ { 1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0, 0x7fff, 0 },
+ {-1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0, 0, 0x7fff },
+ { 1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0, 0x7fff, 0x7fff },
+ {-1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0, 0, 0 },
+ { 1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0, 0x7fff, 0 },
+ {-1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0, 0, 0x7fff },
+ { 1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0, 0x7fff, 0x7fff },
+ {-1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0, 0, 0 },
+ { 1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0, 0x7fff, 0 },
+ {-1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0, 0, 0x7fff },
+ { 1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0, 0x7fff, 0x7fff },
+ {-1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0, 0, 0 },
+ { 1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0, 0x7fff, 0 },
+ {-1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0, 0, 0x7fff },
+ { 1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0, 0x7fff, 0x7fff },
+ { 1.0f, -1.0f, 1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0, 0, 0 },
+ { 1.0f, 1.0f, 1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0, 0x7fff, 0 },
+ { 1.0f, -1.0f, -1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0, 0, 0x7fff },
+ { 1.0f, 1.0f, -1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0, 0x7fff, 0x7fff },
+ {-1.0f, -1.0f, 1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0, 0, 0 },
+ {-1.0f, 1.0f, 1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0, 0x7fff, 0 },
+ {-1.0f, -1.0f, -1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0, 0, 0x7fff },
+ {-1.0f, 1.0f, -1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0, 0x7fff, 0x7fff },
};
static const uint16_t s_cubeIndices[36] =
@@ -105,13 +84,19 @@ static const uint16_t s_cubeIndices[36] =
class ExampleBump : public entry::AppI
{
- void init(int _argc, char** _argv) BX_OVERRIDE
+public:
+ ExampleBump(const char* _name, const char* _description)
+ : entry::AppI(_name, _description)
+ {
+ }
+
+ void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
{
Args args(_argc, _argv);
- m_width = 1280;
- m_height = 720;
- m_debug = BGFX_DEBUG_TEXT;
+ m_width = _width;
+ m_height = _height;
+ m_debug = BGFX_DEBUG_NONE;
m_reset = BGFX_RESET_VSYNC;
bgfx::init(args.m_type, args.m_pciId);
@@ -169,20 +154,24 @@ class ExampleBump : public entry::AppI
m_textureNormal = loadTexture("textures/fieldstone-n.dds");
m_timeOffset = bx::getHPCounter();
+
+ imguiCreate();
}
- virtual int shutdown() BX_OVERRIDE
+ virtual int shutdown() override
{
+ imguiDestroy();
+
// Cleanup.
- bgfx::destroyIndexBuffer(m_ibh);
- bgfx::destroyVertexBuffer(m_vbh);
- bgfx::destroyProgram(m_program);
- bgfx::destroyTexture(m_textureColor);
- bgfx::destroyTexture(m_textureNormal);
- bgfx::destroyUniform(s_texColor);
- bgfx::destroyUniform(s_texNormal);
- bgfx::destroyUniform(u_lightPosRadius);
- bgfx::destroyUniform(u_lightRgbInnerR);
+ bgfx::destroy(m_ibh);
+ bgfx::destroy(m_vbh);
+ bgfx::destroy(m_program);
+ bgfx::destroy(m_textureColor);
+ bgfx::destroy(m_textureNormal);
+ bgfx::destroy(s_texColor);
+ bgfx::destroy(s_texNormal);
+ bgfx::destroy(u_lightPosRadius);
+ bgfx::destroy(u_lightRgbInnerR);
// Shutdown bgfx.
bgfx::shutdown();
@@ -190,10 +179,24 @@ class ExampleBump : public entry::AppI
return 0;
}
- bool update() BX_OVERRIDE
+ bool update() override
{
- if (!entry::processEvents(m_width, m_height, m_debug, m_reset) )
+ if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
{
+ 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)
+ );
+
+ showExampleDialog(this);
+
+ imguiEndFrame();
+
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
@@ -201,20 +204,7 @@ class ExampleBump : public entry::AppI
// 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)( (now-m_timeOffset)/freq);
-
- // Use debug font to print information about this example.
- bgfx::dbgTextClear();
- bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/06-bump");
- bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Loading textures.");
- bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
+ float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ));
float at[3] = { 0.0f, 0.0f, 0.0f };
float eye[3] = { 0.0f, 0.0f, -7.0f };
@@ -249,8 +239,8 @@ class ExampleBump : public entry::AppI
float lightPosRadius[4][4];
for (uint32_t ii = 0; ii < m_numLights; ++ii)
{
- lightPosRadius[ii][0] = bx::fsin( (time*(0.1f + ii*0.17f) + ii*bx::piHalf*1.37f ) )*3.0f;
- lightPosRadius[ii][1] = bx::fcos( (time*(0.2f + ii*0.29f) + ii*bx::piHalf*1.49f ) )*3.0f;
+ lightPosRadius[ii][0] = bx::fsin( (time*(0.1f + ii*0.17f) + ii*bx::kPiHalf*1.37f ) )*3.0f;
+ lightPosRadius[ii][1] = bx::fcos( (time*(0.2f + ii*0.29f) + ii*bx::kPiHalf*1.49f ) )*3.0f;
lightPosRadius[ii][2] = -2.5f;
lightPosRadius[ii][3] = 3.0f;
}
@@ -268,17 +258,19 @@ class ExampleBump : public entry::AppI
bgfx::setUniform(u_lightRgbInnerR, lightRgbInnerR, m_numLights);
const uint16_t instanceStride = 64;
- const uint16_t numInstances = 3;
+ const uint16_t numInstances = 3;
if (m_instancingSupported)
{
// Write instance data for 3x3 cubes.
for (uint32_t yy = 0; yy < 3; ++yy)
{
- const bgfx::InstanceDataBuffer* idb = bgfx::allocInstanceDataBuffer(numInstances, instanceStride);
- if (NULL != idb)
+ if (numInstances == bgfx::getAvailInstanceDataBuffer(numInstances, instanceStride) )
{
- uint8_t* data = idb->data;
+ bgfx::InstanceDataBuffer idb;
+ bgfx::allocInstanceDataBuffer(&idb, numInstances, instanceStride);
+
+ uint8_t* data = idb.data;
for (uint32_t xx = 0; xx < 3; ++xx)
{
@@ -292,10 +284,10 @@ class ExampleBump : public entry::AppI
}
// Set instance data buffer.
- bgfx::setInstanceDataBuffer(idb, numInstances);
+ bgfx::setInstanceDataBuffer(&idb, numInstances);
// Set vertex and index buffer.
- bgfx::setVertexBuffer(m_vbh);
+ bgfx::setVertexBuffer(0, m_vbh);
bgfx::setIndexBuffer(m_ibh);
// Bind textures.
@@ -332,7 +324,7 @@ class ExampleBump : public entry::AppI
bgfx::setTransform(mtx);
// Set vertex and index buffer.
- bgfx::setVertexBuffer(m_vbh);
+ bgfx::setVertexBuffer(0, m_vbh);
bgfx::setIndexBuffer(m_ibh);
// Bind textures.
@@ -364,6 +356,8 @@ class ExampleBump : public entry::AppI
return false;
}
+ entry::MouseState m_mouseState;
+
bgfx::VertexBufferHandle m_vbh;
bgfx::IndexBufferHandle m_ibh;
bgfx::UniformHandle s_texColor;
@@ -383,4 +377,6 @@ class ExampleBump : public entry::AppI
int64_t m_timeOffset;
};
-ENTRY_IMPLEMENT_MAIN(ExampleBump);
+} // namespace
+
+ENTRY_IMPLEMENT_MAIN(ExampleBump, "06-bump", "Loading textures.");