bump.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. {
  179. float view[16];
  180. bx::mtxLookAt(view, eye, at);
  181. float proj[16];
  182. bx::mtxProj(proj, 60.0f, float(m_width) / float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  183. bgfx::setViewTransform(0, view, proj);
  184. // Set view 0 default viewport.
  185. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  186. }
  187. float lightPosRadius[4][4];
  188. for (uint32_t ii = 0; ii < m_numLights; ++ii)
  189. {
  190. lightPosRadius[ii][0] = bx::sin( (time*(0.1f + ii*0.17f) + ii*bx::kPiHalf*1.37f ) )*3.0f;
  191. lightPosRadius[ii][1] = bx::cos( (time*(0.2f + ii*0.29f) + ii*bx::kPiHalf*1.49f ) )*3.0f;
  192. lightPosRadius[ii][2] = -2.5f;
  193. lightPosRadius[ii][3] = 3.0f;
  194. }
  195. bgfx::setUniform(u_lightPosRadius, lightPosRadius, m_numLights);
  196. float lightRgbInnerR[4][4] =
  197. {
  198. { 1.0f, 0.7f, 0.2f, 0.8f },
  199. { 0.7f, 0.2f, 1.0f, 0.8f },
  200. { 0.2f, 1.0f, 0.7f, 0.8f },
  201. { 1.0f, 0.4f, 0.2f, 0.8f },
  202. };
  203. bgfx::setUniform(u_lightRgbInnerR, lightRgbInnerR, m_numLights);
  204. const uint16_t instanceStride = 64;
  205. const uint16_t numInstances = 3;
  206. if (m_instancingSupported)
  207. {
  208. // Write instance data for 3x3 cubes.
  209. for (uint32_t yy = 0; yy < 3; ++yy)
  210. {
  211. if (numInstances == bgfx::getAvailInstanceDataBuffer(numInstances, instanceStride) )
  212. {
  213. bgfx::InstanceDataBuffer idb;
  214. bgfx::allocInstanceDataBuffer(&idb, numInstances, instanceStride);
  215. uint8_t* data = idb.data;
  216. for (uint32_t xx = 0; xx < 3; ++xx)
  217. {
  218. float* mtx = (float*)data;
  219. bx::mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f);
  220. mtx[12] = -3.0f + float(xx)*3.0f;
  221. mtx[13] = -3.0f + float(yy)*3.0f;
  222. mtx[14] = 0.0f;
  223. data += instanceStride;
  224. }
  225. // Set instance data buffer.
  226. bgfx::setInstanceDataBuffer(&idb, 0, numInstances);
  227. // Set vertex and index buffer.
  228. bgfx::setVertexBuffer(0, m_vbh);
  229. bgfx::setIndexBuffer(m_ibh);
  230. // Bind textures.
  231. bgfx::setTexture(0, s_texColor, m_textureColor);
  232. bgfx::setTexture(1, s_texNormal, m_textureNormal);
  233. // Set render states.
  234. bgfx::setState(0
  235. | BGFX_STATE_WRITE_RGB
  236. | BGFX_STATE_WRITE_A
  237. | BGFX_STATE_WRITE_Z
  238. | BGFX_STATE_DEPTH_TEST_LESS
  239. | BGFX_STATE_MSAA
  240. );
  241. // Submit primitive for rendering to view 0.
  242. bgfx::submit(0, m_program);
  243. }
  244. }
  245. }
  246. else
  247. {
  248. for (uint32_t yy = 0; yy < 3; ++yy)
  249. {
  250. for (uint32_t xx = 0; xx < 3; ++xx)
  251. {
  252. float mtx[16];
  253. bx::mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f);
  254. mtx[12] = -3.0f + float(xx)*3.0f;
  255. mtx[13] = -3.0f + float(yy)*3.0f;
  256. mtx[14] = 0.0f;
  257. // Set transform for draw call.
  258. bgfx::setTransform(mtx);
  259. // Set vertex and index buffer.
  260. bgfx::setVertexBuffer(0, m_vbh);
  261. bgfx::setIndexBuffer(m_ibh);
  262. // Bind textures.
  263. bgfx::setTexture(0, s_texColor, m_textureColor);
  264. bgfx::setTexture(1, s_texNormal, m_textureNormal);
  265. // Set render states.
  266. bgfx::setState(0
  267. | BGFX_STATE_WRITE_RGB
  268. | BGFX_STATE_WRITE_A
  269. | BGFX_STATE_WRITE_Z
  270. | BGFX_STATE_DEPTH_TEST_LESS
  271. | BGFX_STATE_MSAA
  272. );
  273. // Submit primitive for rendering to view 0.
  274. bgfx::submit(0, m_program);
  275. }
  276. }
  277. }
  278. // Advance to next frame. Rendering thread will be kicked to
  279. // process submitted rendering primitives.
  280. bgfx::frame();
  281. return true;
  282. }
  283. return false;
  284. }
  285. entry::MouseState m_mouseState;
  286. bgfx::VertexBufferHandle m_vbh;
  287. bgfx::IndexBufferHandle m_ibh;
  288. bgfx::UniformHandle s_texColor;
  289. bgfx::UniformHandle s_texNormal;
  290. bgfx::UniformHandle u_lightPosRadius;
  291. bgfx::UniformHandle u_lightRgbInnerR;
  292. bgfx::ProgramHandle m_program;
  293. bgfx::TextureHandle m_textureColor;
  294. bgfx::TextureHandle m_textureNormal;
  295. uint16_t m_numLights;
  296. bool m_instancingSupported;
  297. uint32_t m_width;
  298. uint32_t m_height;
  299. uint32_t m_debug;
  300. uint32_t m_reset;
  301. int64_t m_timeOffset;
  302. };
  303. } // namespace
  304. ENTRY_IMPLEMENT_MAIN(ExampleBump, "06-bump", "Loading textures.");