bump.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * Copyright 2011-2025 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include "common.h"
  6. #include "bgfx_utils.h"
  7. #include "imgui/imgui.h"
  8. namespace
  9. {
  10. struct PosNormalTangentTexcoordVertex
  11. {
  12. float m_x;
  13. float m_y;
  14. float m_z;
  15. uint32_t m_normal;
  16. uint32_t m_tangent;
  17. int16_t m_u;
  18. int16_t m_v;
  19. static void init()
  20. {
  21. ms_layout
  22. .begin()
  23. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  24. .add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true)
  25. .add(bgfx::Attrib::Tangent, 4, bgfx::AttribType::Uint8, true, true)
  26. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Int16, true, true)
  27. .end();
  28. }
  29. static bgfx::VertexLayout ms_layout;
  30. };
  31. bgfx::VertexLayout PosNormalTangentTexcoordVertex::ms_layout;
  32. static PosNormalTangentTexcoordVertex s_cubeVertices[24] =
  33. {
  34. {-1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0, 0, 0 },
  35. { 1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0, 0x7fff, 0 },
  36. {-1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0, 0, 0x7fff },
  37. { 1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0, 0x7fff, 0x7fff },
  38. {-1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0, 0, 0 },
  39. { 1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0, 0x7fff, 0 },
  40. {-1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0, 0, 0x7fff },
  41. { 1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0, 0x7fff, 0x7fff },
  42. {-1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0, 0, 0 },
  43. { 1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0, 0x7fff, 0 },
  44. {-1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0, 0, 0x7fff },
  45. { 1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0, 0x7fff, 0x7fff },
  46. {-1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0, 0, 0 },
  47. { 1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0, 0x7fff, 0 },
  48. {-1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0, 0, 0x7fff },
  49. { 1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0, 0x7fff, 0x7fff },
  50. { 1.0f, -1.0f, 1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0, 0, 0 },
  51. { 1.0f, 1.0f, 1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0, 0x7fff, 0 },
  52. { 1.0f, -1.0f, -1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0, 0, 0x7fff },
  53. { 1.0f, 1.0f, -1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0, 0x7fff, 0x7fff },
  54. {-1.0f, -1.0f, 1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0, 0, 0 },
  55. {-1.0f, 1.0f, 1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0, 0x7fff, 0 },
  56. {-1.0f, -1.0f, -1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0, 0, 0x7fff },
  57. {-1.0f, 1.0f, -1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0, 0x7fff, 0x7fff },
  58. };
  59. static const uint16_t s_cubeIndices[36] =
  60. {
  61. 0, 2, 1,
  62. 1, 2, 3,
  63. 4, 5, 6,
  64. 5, 7, 6,
  65. 8, 10, 9,
  66. 9, 10, 11,
  67. 12, 13, 14,
  68. 13, 15, 14,
  69. 16, 18, 17,
  70. 17, 18, 19,
  71. 20, 21, 22,
  72. 21, 23, 22,
  73. };
  74. class ExampleBump : public entry::AppI
  75. {
  76. public:
  77. ExampleBump(const char* _name, const char* _description, const char* _url)
  78. : entry::AppI(_name, _description, _url)
  79. {
  80. }
  81. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  82. {
  83. Args args(_argc, _argv);
  84. m_width = _width;
  85. m_height = _height;
  86. m_debug = BGFX_DEBUG_NONE;
  87. m_reset = BGFX_RESET_VSYNC;
  88. bgfx::Init init;
  89. init.type = args.m_type;
  90. init.vendorId = args.m_pciId;
  91. init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  92. init.platformData.ndt = entry::getNativeDisplayHandle();
  93. init.platformData.type = entry::getNativeWindowHandleType();
  94. init.resolution.width = m_width;
  95. init.resolution.height = m_height;
  96. init.resolution.reset = m_reset;
  97. bgfx::init(init);
  98. // Enable debug text.
  99. bgfx::setDebug(m_debug);
  100. // Set view 0 clear state.
  101. bgfx::setViewClear(0
  102. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  103. , 0x303030ff
  104. , 1.0f
  105. , 0
  106. );
  107. // Get renderer capabilities info.
  108. const bgfx::Caps* caps = bgfx::getCaps();
  109. m_instancingSupported = 0 != (caps->supported & BGFX_CAPS_INSTANCING);
  110. // Create vertex stream declaration.
  111. PosNormalTangentTexcoordVertex::init();
  112. calcTangents(s_cubeVertices
  113. , BX_COUNTOF(s_cubeVertices)
  114. , PosNormalTangentTexcoordVertex::ms_layout
  115. , s_cubeIndices
  116. , BX_COUNTOF(s_cubeIndices)
  117. );
  118. // Create static vertex buffer.
  119. m_vbh = bgfx::createVertexBuffer(
  120. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  121. , PosNormalTangentTexcoordVertex::ms_layout
  122. );
  123. // Create static index buffer.
  124. m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
  125. // Create texture sampler uniforms.
  126. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
  127. s_texNormal = bgfx::createUniform("s_texNormal", bgfx::UniformType::Sampler);
  128. m_numLights = 4;
  129. u_lightPosRadius = bgfx::createUniform("u_lightPosRadius", bgfx::UniformType::Vec4, m_numLights);
  130. u_lightRgbInnerR = bgfx::createUniform("u_lightRgbInnerR", bgfx::UniformType::Vec4, m_numLights);
  131. // Create program from shaders.
  132. m_program = loadProgram(m_instancingSupported ? "vs_bump_instanced" : "vs_bump", "fs_bump");
  133. // Load diffuse texture.
  134. m_textureColor = loadTexture("textures/fieldstone-rgba.dds");
  135. // Load normal texture.
  136. m_textureNormal = loadTexture("textures/fieldstone-n.dds");
  137. m_timeOffset = bx::getHPCounter();
  138. imguiCreate();
  139. }
  140. virtual int shutdown() override
  141. {
  142. imguiDestroy();
  143. // Cleanup.
  144. bgfx::destroy(m_ibh);
  145. bgfx::destroy(m_vbh);
  146. bgfx::destroy(m_program);
  147. bgfx::destroy(m_textureColor);
  148. bgfx::destroy(m_textureNormal);
  149. bgfx::destroy(s_texColor);
  150. bgfx::destroy(s_texNormal);
  151. bgfx::destroy(u_lightPosRadius);
  152. bgfx::destroy(u_lightRgbInnerR);
  153. // Shutdown bgfx.
  154. bgfx::shutdown();
  155. return 0;
  156. }
  157. bool update() override
  158. {
  159. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  160. {
  161. imguiBeginFrame(m_mouseState.m_mx
  162. , m_mouseState.m_my
  163. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  164. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  165. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  166. , m_mouseState.m_mz
  167. , uint16_t(m_width)
  168. , uint16_t(m_height)
  169. );
  170. showExampleDialog(this);
  171. imguiEndFrame();
  172. // Set view 0 default viewport.
  173. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  174. // This dummy draw call is here to make sure that view 0 is cleared
  175. // if no other draw calls are submitted to view 0.
  176. bgfx::touch(0);
  177. float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ));
  178. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  179. const bx::Vec3 eye = { 0.0f, 0.0f, -7.0f };
  180. // Set view and projection matrix for view 0.
  181. {
  182. float view[16];
  183. bx::mtxLookAt(view, eye, at);
  184. float proj[16];
  185. bx::mtxProj(proj, 60.0f, float(m_width) / float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  186. bgfx::setViewTransform(0, view, proj);
  187. // Set view 0 default viewport.
  188. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  189. }
  190. float lightPosRadius[4][4];
  191. for (uint32_t ii = 0; ii < m_numLights; ++ii)
  192. {
  193. lightPosRadius[ii][0] = bx::sin( (time*(0.1f + ii*0.17f) + ii*bx::kPiHalf*1.37f ) )*3.0f;
  194. lightPosRadius[ii][1] = bx::cos( (time*(0.2f + ii*0.29f) + ii*bx::kPiHalf*1.49f ) )*3.0f;
  195. lightPosRadius[ii][2] = -2.5f;
  196. lightPosRadius[ii][3] = 3.0f;
  197. }
  198. bgfx::setUniform(u_lightPosRadius, lightPosRadius, m_numLights);
  199. float lightRgbInnerR[4][4] =
  200. {
  201. { 1.0f, 0.7f, 0.2f, 0.8f },
  202. { 0.7f, 0.2f, 1.0f, 0.8f },
  203. { 0.2f, 1.0f, 0.7f, 0.8f },
  204. { 1.0f, 0.4f, 0.2f, 0.8f },
  205. };
  206. bgfx::setUniform(u_lightRgbInnerR, lightRgbInnerR, m_numLights);
  207. const uint16_t instanceStride = 64;
  208. const uint16_t numInstances = 3;
  209. if (m_instancingSupported)
  210. {
  211. // Write instance data for 3x3 cubes.
  212. for (uint32_t yy = 0; yy < 3; ++yy)
  213. {
  214. if (numInstances == bgfx::getAvailInstanceDataBuffer(numInstances, instanceStride) )
  215. {
  216. bgfx::InstanceDataBuffer idb;
  217. bgfx::allocInstanceDataBuffer(&idb, numInstances, instanceStride);
  218. uint8_t* data = idb.data;
  219. for (uint32_t xx = 0; xx < 3; ++xx)
  220. {
  221. float* mtx = (float*)data;
  222. bx::mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f);
  223. mtx[12] = -3.0f + float(xx)*3.0f;
  224. mtx[13] = -3.0f + float(yy)*3.0f;
  225. mtx[14] = 0.0f;
  226. data += instanceStride;
  227. }
  228. // Set instance data buffer.
  229. bgfx::setInstanceDataBuffer(&idb, 0, numInstances);
  230. // Set vertex and index buffer.
  231. bgfx::setVertexBuffer(0, m_vbh);
  232. bgfx::setIndexBuffer(m_ibh);
  233. // Bind textures.
  234. bgfx::setTexture(0, s_texColor, m_textureColor);
  235. bgfx::setTexture(1, s_texNormal, m_textureNormal);
  236. // Set render states.
  237. bgfx::setState(0
  238. | BGFX_STATE_WRITE_RGB
  239. | BGFX_STATE_WRITE_A
  240. | BGFX_STATE_WRITE_Z
  241. | BGFX_STATE_DEPTH_TEST_LESS
  242. | BGFX_STATE_MSAA
  243. );
  244. // Submit primitive for rendering to view 0.
  245. bgfx::submit(0, m_program);
  246. }
  247. }
  248. }
  249. else
  250. {
  251. for (uint32_t yy = 0; yy < 3; ++yy)
  252. {
  253. for (uint32_t xx = 0; xx < 3; ++xx)
  254. {
  255. float mtx[16];
  256. bx::mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f);
  257. mtx[12] = -3.0f + float(xx)*3.0f;
  258. mtx[13] = -3.0f + float(yy)*3.0f;
  259. mtx[14] = 0.0f;
  260. // Set transform for draw call.
  261. bgfx::setTransform(mtx);
  262. // Set vertex and index buffer.
  263. bgfx::setVertexBuffer(0, m_vbh);
  264. bgfx::setIndexBuffer(m_ibh);
  265. // Bind textures.
  266. bgfx::setTexture(0, s_texColor, m_textureColor);
  267. bgfx::setTexture(1, s_texNormal, m_textureNormal);
  268. // Set render states.
  269. bgfx::setState(0
  270. | BGFX_STATE_WRITE_RGB
  271. | BGFX_STATE_WRITE_A
  272. | BGFX_STATE_WRITE_Z
  273. | BGFX_STATE_DEPTH_TEST_LESS
  274. | BGFX_STATE_MSAA
  275. );
  276. // Submit primitive for rendering to view 0.
  277. bgfx::submit(0, m_program);
  278. }
  279. }
  280. }
  281. // Advance to next frame. Rendering thread will be kicked to
  282. // process submitted rendering primitives.
  283. bgfx::frame();
  284. return true;
  285. }
  286. return false;
  287. }
  288. entry::MouseState m_mouseState;
  289. bgfx::VertexBufferHandle m_vbh;
  290. bgfx::IndexBufferHandle m_ibh;
  291. bgfx::UniformHandle s_texColor;
  292. bgfx::UniformHandle s_texNormal;
  293. bgfx::UniformHandle u_lightPosRadius;
  294. bgfx::UniformHandle u_lightRgbInnerR;
  295. bgfx::ProgramHandle m_program;
  296. bgfx::TextureHandle m_textureColor;
  297. bgfx::TextureHandle m_textureNormal;
  298. uint16_t m_numLights;
  299. bool m_instancingSupported;
  300. uint32_t m_width;
  301. uint32_t m_height;
  302. uint32_t m_debug;
  303. uint32_t m_reset;
  304. int64_t m_timeOffset;
  305. };
  306. } // namespace
  307. ENTRY_IMPLEMENT_MAIN(
  308. ExampleBump
  309. , "06-bump"
  310. , "Loading textures."
  311. , "https://bkaradzic.github.io/bgfx/examples.html#bump"
  312. );