bump.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Copyright 2011-2023 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.resolution.width = m_width;
  94. init.resolution.height = m_height;
  95. init.resolution.reset = m_reset;
  96. bgfx::init(init);
  97. // Enable debug text.
  98. bgfx::setDebug(m_debug);
  99. // Set view 0 clear state.
  100. bgfx::setViewClear(0
  101. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  102. , 0x303030ff
  103. , 1.0f
  104. , 0
  105. );
  106. // Get renderer capabilities info.
  107. const bgfx::Caps* caps = bgfx::getCaps();
  108. m_instancingSupported = 0 != (caps->supported & BGFX_CAPS_INSTANCING);
  109. // Create vertex stream declaration.
  110. PosNormalTangentTexcoordVertex::init();
  111. calcTangents(s_cubeVertices
  112. , BX_COUNTOF(s_cubeVertices)
  113. , PosNormalTangentTexcoordVertex::ms_layout
  114. , s_cubeIndices
  115. , BX_COUNTOF(s_cubeIndices)
  116. );
  117. // Create static vertex buffer.
  118. m_vbh = bgfx::createVertexBuffer(
  119. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  120. , PosNormalTangentTexcoordVertex::ms_layout
  121. );
  122. // Create static index buffer.
  123. m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
  124. // Create texture sampler uniforms.
  125. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
  126. s_texNormal = bgfx::createUniform("s_texNormal", bgfx::UniformType::Sampler);
  127. m_numLights = 4;
  128. u_lightPosRadius = bgfx::createUniform("u_lightPosRadius", bgfx::UniformType::Vec4, m_numLights);
  129. u_lightRgbInnerR = bgfx::createUniform("u_lightRgbInnerR", bgfx::UniformType::Vec4, m_numLights);
  130. // Create program from shaders.
  131. m_program = loadProgram(m_instancingSupported ? "vs_bump_instanced" : "vs_bump", "fs_bump");
  132. // Load diffuse texture.
  133. m_textureColor = loadTexture("textures/fieldstone-rgba.dds");
  134. // Load normal texture.
  135. m_textureNormal = loadTexture("textures/fieldstone-n.dds");
  136. m_timeOffset = bx::getHPCounter();
  137. imguiCreate();
  138. }
  139. virtual int shutdown() override
  140. {
  141. imguiDestroy();
  142. // Cleanup.
  143. bgfx::destroy(m_ibh);
  144. bgfx::destroy(m_vbh);
  145. bgfx::destroy(m_program);
  146. bgfx::destroy(m_textureColor);
  147. bgfx::destroy(m_textureNormal);
  148. bgfx::destroy(s_texColor);
  149. bgfx::destroy(s_texNormal);
  150. bgfx::destroy(u_lightPosRadius);
  151. bgfx::destroy(u_lightRgbInnerR);
  152. // Shutdown bgfx.
  153. bgfx::shutdown();
  154. return 0;
  155. }
  156. bool update() override
  157. {
  158. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  159. {
  160. imguiBeginFrame(m_mouseState.m_mx
  161. , m_mouseState.m_my
  162. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  163. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  164. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  165. , m_mouseState.m_mz
  166. , uint16_t(m_width)
  167. , uint16_t(m_height)
  168. );
  169. showExampleDialog(this);
  170. imguiEndFrame();
  171. // Set view 0 default viewport.
  172. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  173. // This dummy draw call is here to make sure that view 0 is cleared
  174. // if no other draw calls are submitted to view 0.
  175. bgfx::touch(0);
  176. float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ));
  177. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  178. const bx::Vec3 eye = { 0.0f, 0.0f, -7.0f };
  179. // Set view and projection matrix for view 0.
  180. {
  181. float view[16];
  182. bx::mtxLookAt(view, eye, at);
  183. float proj[16];
  184. bx::mtxProj(proj, 60.0f, float(m_width) / float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  185. bgfx::setViewTransform(0, view, proj);
  186. // Set view 0 default viewport.
  187. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  188. }
  189. float lightPosRadius[4][4];
  190. for (uint32_t ii = 0; ii < m_numLights; ++ii)
  191. {
  192. lightPosRadius[ii][0] = bx::sin( (time*(0.1f + ii*0.17f) + ii*bx::kPiHalf*1.37f ) )*3.0f;
  193. lightPosRadius[ii][1] = bx::cos( (time*(0.2f + ii*0.29f) + ii*bx::kPiHalf*1.49f ) )*3.0f;
  194. lightPosRadius[ii][2] = -2.5f;
  195. lightPosRadius[ii][3] = 3.0f;
  196. }
  197. bgfx::setUniform(u_lightPosRadius, lightPosRadius, m_numLights);
  198. float lightRgbInnerR[4][4] =
  199. {
  200. { 1.0f, 0.7f, 0.2f, 0.8f },
  201. { 0.7f, 0.2f, 1.0f, 0.8f },
  202. { 0.2f, 1.0f, 0.7f, 0.8f },
  203. { 1.0f, 0.4f, 0.2f, 0.8f },
  204. };
  205. bgfx::setUniform(u_lightRgbInnerR, lightRgbInnerR, m_numLights);
  206. const uint16_t instanceStride = 64;
  207. const uint16_t numInstances = 3;
  208. if (m_instancingSupported)
  209. {
  210. // Write instance data for 3x3 cubes.
  211. for (uint32_t yy = 0; yy < 3; ++yy)
  212. {
  213. if (numInstances == bgfx::getAvailInstanceDataBuffer(numInstances, instanceStride) )
  214. {
  215. bgfx::InstanceDataBuffer idb;
  216. bgfx::allocInstanceDataBuffer(&idb, numInstances, instanceStride);
  217. uint8_t* data = idb.data;
  218. for (uint32_t xx = 0; xx < 3; ++xx)
  219. {
  220. float* mtx = (float*)data;
  221. bx::mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f);
  222. mtx[12] = -3.0f + float(xx)*3.0f;
  223. mtx[13] = -3.0f + float(yy)*3.0f;
  224. mtx[14] = 0.0f;
  225. data += instanceStride;
  226. }
  227. // Set instance data buffer.
  228. bgfx::setInstanceDataBuffer(&idb, 0, numInstances);
  229. // Set vertex and index buffer.
  230. bgfx::setVertexBuffer(0, m_vbh);
  231. bgfx::setIndexBuffer(m_ibh);
  232. // Bind textures.
  233. bgfx::setTexture(0, s_texColor, m_textureColor);
  234. bgfx::setTexture(1, s_texNormal, m_textureNormal);
  235. // Set render states.
  236. bgfx::setState(0
  237. | BGFX_STATE_WRITE_RGB
  238. | BGFX_STATE_WRITE_A
  239. | BGFX_STATE_WRITE_Z
  240. | BGFX_STATE_DEPTH_TEST_LESS
  241. | BGFX_STATE_MSAA
  242. );
  243. // Submit primitive for rendering to view 0.
  244. bgfx::submit(0, m_program);
  245. }
  246. }
  247. }
  248. else
  249. {
  250. for (uint32_t yy = 0; yy < 3; ++yy)
  251. {
  252. for (uint32_t xx = 0; xx < 3; ++xx)
  253. {
  254. float mtx[16];
  255. bx::mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f);
  256. mtx[12] = -3.0f + float(xx)*3.0f;
  257. mtx[13] = -3.0f + float(yy)*3.0f;
  258. mtx[14] = 0.0f;
  259. // Set transform for draw call.
  260. bgfx::setTransform(mtx);
  261. // Set vertex and index buffer.
  262. bgfx::setVertexBuffer(0, m_vbh);
  263. bgfx::setIndexBuffer(m_ibh);
  264. // Bind textures.
  265. bgfx::setTexture(0, s_texColor, m_textureColor);
  266. bgfx::setTexture(1, s_texNormal, m_textureNormal);
  267. // Set render states.
  268. bgfx::setState(0
  269. | BGFX_STATE_WRITE_RGB
  270. | BGFX_STATE_WRITE_A
  271. | BGFX_STATE_WRITE_Z
  272. | BGFX_STATE_DEPTH_TEST_LESS
  273. | BGFX_STATE_MSAA
  274. );
  275. // Submit primitive for rendering to view 0.
  276. bgfx::submit(0, m_program);
  277. }
  278. }
  279. }
  280. // Advance to next frame. Rendering thread will be kicked to
  281. // process submitted rendering primitives.
  282. bgfx::frame();
  283. return true;
  284. }
  285. return false;
  286. }
  287. entry::MouseState m_mouseState;
  288. bgfx::VertexBufferHandle m_vbh;
  289. bgfx::IndexBufferHandle m_ibh;
  290. bgfx::UniformHandle s_texColor;
  291. bgfx::UniformHandle s_texNormal;
  292. bgfx::UniformHandle u_lightPosRadius;
  293. bgfx::UniformHandle u_lightRgbInnerR;
  294. bgfx::ProgramHandle m_program;
  295. bgfx::TextureHandle m_textureColor;
  296. bgfx::TextureHandle m_textureNormal;
  297. uint16_t m_numLights;
  298. bool m_instancingSupported;
  299. uint32_t m_width;
  300. uint32_t m_height;
  301. uint32_t m_debug;
  302. uint32_t m_reset;
  303. int64_t m_timeOffset;
  304. };
  305. } // namespace
  306. ENTRY_IMPLEMENT_MAIN(
  307. ExampleBump
  308. , "06-bump"
  309. , "Loading textures."
  310. , "https://bkaradzic.github.io/bgfx/examples.html#bump"
  311. );