bump.cpp 11 KB

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