bump.cpp 11 KB

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