bump.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Copyright 2011-2017 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. struct PosNormalTangentTexcoordVertex
  8. {
  9. float m_x;
  10. float m_y;
  11. float m_z;
  12. uint32_t m_normal;
  13. uint32_t m_tangent;
  14. int16_t m_u;
  15. int16_t m_v;
  16. static void init()
  17. {
  18. ms_decl
  19. .begin()
  20. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  21. .add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true)
  22. .add(bgfx::Attrib::Tangent, 4, bgfx::AttribType::Uint8, true, true)
  23. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Int16, true, true)
  24. .end();
  25. }
  26. static bgfx::VertexDecl ms_decl;
  27. };
  28. bgfx::VertexDecl PosNormalTangentTexcoordVertex::ms_decl;
  29. static PosNormalTangentTexcoordVertex s_cubeVertices[24] =
  30. {
  31. {-1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0, 0, 0 },
  32. { 1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0, 0x7fff, 0 },
  33. {-1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0, 0, 0x7fff },
  34. { 1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0, 0x7fff, 0x7fff },
  35. {-1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0, 0, 0 },
  36. { 1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0, 0x7fff, 0 },
  37. {-1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0, 0, 0x7fff },
  38. { 1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0, 0x7fff, 0x7fff },
  39. {-1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0, 0, 0 },
  40. { 1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0, 0x7fff, 0 },
  41. {-1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0, 0, 0x7fff },
  42. { 1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0, 0x7fff, 0x7fff },
  43. {-1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0, 0, 0 },
  44. { 1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0, 0x7fff, 0 },
  45. {-1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0, 0, 0x7fff },
  46. { 1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0, 0x7fff, 0x7fff },
  47. { 1.0f, -1.0f, 1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0, 0, 0 },
  48. { 1.0f, 1.0f, 1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0, 0x7fff, 0 },
  49. { 1.0f, -1.0f, -1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0, 0, 0x7fff },
  50. { 1.0f, 1.0f, -1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0, 0x7fff, 0x7fff },
  51. {-1.0f, -1.0f, 1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0, 0, 0 },
  52. {-1.0f, 1.0f, 1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0, 0x7fff, 0 },
  53. {-1.0f, -1.0f, -1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0, 0, 0x7fff },
  54. {-1.0f, 1.0f, -1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0, 0x7fff, 0x7fff },
  55. };
  56. static const uint16_t s_cubeIndices[36] =
  57. {
  58. 0, 2, 1,
  59. 1, 2, 3,
  60. 4, 5, 6,
  61. 5, 7, 6,
  62. 8, 10, 9,
  63. 9, 10, 11,
  64. 12, 13, 14,
  65. 13, 15, 14,
  66. 16, 18, 17,
  67. 17, 18, 19,
  68. 20, 21, 22,
  69. 21, 23, 22,
  70. };
  71. class ExampleBump : public entry::AppI
  72. {
  73. void init(int _argc, char** _argv) BX_OVERRIDE
  74. {
  75. Args args(_argc, _argv);
  76. m_width = 1280;
  77. m_height = 720;
  78. m_debug = BGFX_DEBUG_TEXT;
  79. m_reset = BGFX_RESET_VSYNC;
  80. bgfx::init(args.m_type, args.m_pciId);
  81. bgfx::reset(m_width, m_height, m_reset);
  82. // Enable debug text.
  83. bgfx::setDebug(m_debug);
  84. // Set view 0 clear state.
  85. bgfx::setViewClear(0
  86. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  87. , 0x303030ff
  88. , 1.0f
  89. , 0
  90. );
  91. // Get renderer capabilities info.
  92. const bgfx::Caps* caps = bgfx::getCaps();
  93. m_instancingSupported = 0 != (caps->supported & BGFX_CAPS_INSTANCING);
  94. // Create vertex stream declaration.
  95. PosNormalTangentTexcoordVertex::init();
  96. calcTangents(s_cubeVertices
  97. , BX_COUNTOF(s_cubeVertices)
  98. , PosNormalTangentTexcoordVertex::ms_decl
  99. , s_cubeIndices
  100. , BX_COUNTOF(s_cubeIndices)
  101. );
  102. // Create static vertex buffer.
  103. m_vbh = bgfx::createVertexBuffer(
  104. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  105. , PosNormalTangentTexcoordVertex::ms_decl
  106. );
  107. // Create static index buffer.
  108. m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
  109. // Create texture sampler uniforms.
  110. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  111. s_texNormal = bgfx::createUniform("s_texNormal", bgfx::UniformType::Int1);
  112. m_numLights = 4;
  113. u_lightPosRadius = bgfx::createUniform("u_lightPosRadius", bgfx::UniformType::Vec4, m_numLights);
  114. u_lightRgbInnerR = bgfx::createUniform("u_lightRgbInnerR", bgfx::UniformType::Vec4, m_numLights);
  115. // Create program from shaders.
  116. m_program = loadProgram(m_instancingSupported ? "vs_bump_instanced" : "vs_bump", "fs_bump");
  117. // Load diffuse texture.
  118. m_textureColor = loadTexture("textures/fieldstone-rgba.dds");
  119. // Load normal texture.
  120. m_textureNormal = loadTexture("textures/fieldstone-n.dds");
  121. m_timeOffset = bx::getHPCounter();
  122. }
  123. virtual int shutdown() BX_OVERRIDE
  124. {
  125. // Cleanup.
  126. bgfx::destroyIndexBuffer(m_ibh);
  127. bgfx::destroyVertexBuffer(m_vbh);
  128. bgfx::destroyProgram(m_program);
  129. bgfx::destroyTexture(m_textureColor);
  130. bgfx::destroyTexture(m_textureNormal);
  131. bgfx::destroyUniform(s_texColor);
  132. bgfx::destroyUniform(s_texNormal);
  133. bgfx::destroyUniform(u_lightPosRadius);
  134. bgfx::destroyUniform(u_lightRgbInnerR);
  135. // Shutdown bgfx.
  136. bgfx::shutdown();
  137. return 0;
  138. }
  139. bool update() BX_OVERRIDE
  140. {
  141. if (!entry::processEvents(m_width, m_height, m_debug, m_reset) )
  142. {
  143. // Set view 0 default viewport.
  144. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  145. // This dummy draw call is here to make sure that view 0 is cleared
  146. // if no other draw calls are submitted to view 0.
  147. bgfx::touch(0);
  148. int64_t now = bx::getHPCounter();
  149. static int64_t last = now;
  150. const int64_t frameTime = now - last;
  151. last = now;
  152. const double freq = double(bx::getHPFrequency() );
  153. const double toMs = 1000.0/freq;
  154. float time = (float)( (now-m_timeOffset)/freq);
  155. // Use debug font to print information about this example.
  156. bgfx::dbgTextClear();
  157. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/06-bump");
  158. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Loading textures.");
  159. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  160. float at[3] = { 0.0f, 0.0f, 0.0f };
  161. float eye[3] = { 0.0f, 0.0f, -7.0f };
  162. // Set view and projection matrix for view 0.
  163. const bgfx::HMD* hmd = bgfx::getHMD();
  164. if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  165. {
  166. float view[16];
  167. bx::mtxQuatTranslationHMD(view, hmd->eye[0].rotation, eye);
  168. bgfx::setViewTransform(0, view, hmd->eye[0].projection, BGFX_VIEW_STEREO, hmd->eye[1].projection);
  169. // Set view 0 default viewport.
  170. //
  171. // Use HMD's width/height since HMD's internal frame buffer size
  172. // might be much larger than window size.
  173. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  174. }
  175. else
  176. {
  177. float view[16];
  178. bx::mtxLookAt(view, eye, at);
  179. float proj[16];
  180. bx::mtxProj(proj, 60.0f, float(m_width) / float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  181. bgfx::setViewTransform(0, view, proj);
  182. // Set view 0 default viewport.
  183. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  184. }
  185. float lightPosRadius[4][4];
  186. for (uint32_t ii = 0; ii < m_numLights; ++ii)
  187. {
  188. lightPosRadius[ii][0] = bx::fsin( (time*(0.1f + ii*0.17f) + ii*bx::piHalf*1.37f ) )*3.0f;
  189. lightPosRadius[ii][1] = bx::fcos( (time*(0.2f + ii*0.29f) + ii*bx::piHalf*1.49f ) )*3.0f;
  190. lightPosRadius[ii][2] = -2.5f;
  191. lightPosRadius[ii][3] = 3.0f;
  192. }
  193. bgfx::setUniform(u_lightPosRadius, lightPosRadius, m_numLights);
  194. float lightRgbInnerR[4][4] =
  195. {
  196. { 1.0f, 0.7f, 0.2f, 0.8f },
  197. { 0.7f, 0.2f, 1.0f, 0.8f },
  198. { 0.2f, 1.0f, 0.7f, 0.8f },
  199. { 1.0f, 0.4f, 0.2f, 0.8f },
  200. };
  201. bgfx::setUniform(u_lightRgbInnerR, lightRgbInnerR, m_numLights);
  202. const uint16_t instanceStride = 64;
  203. const uint16_t numInstances = 3;
  204. if (m_instancingSupported)
  205. {
  206. // Write instance data for 3x3 cubes.
  207. for (uint32_t yy = 0; yy < 3; ++yy)
  208. {
  209. const bgfx::InstanceDataBuffer* idb = bgfx::allocInstanceDataBuffer(numInstances, instanceStride);
  210. if (NULL != idb)
  211. {
  212. uint8_t* data = idb->data;
  213. for (uint32_t xx = 0; xx < 3; ++xx)
  214. {
  215. float* mtx = (float*)data;
  216. bx::mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f);
  217. mtx[12] = -3.0f + float(xx)*3.0f;
  218. mtx[13] = -3.0f + float(yy)*3.0f;
  219. mtx[14] = 0.0f;
  220. data += instanceStride;
  221. }
  222. // Set instance data buffer.
  223. bgfx::setInstanceDataBuffer(idb, numInstances);
  224. // Set vertex and index buffer.
  225. bgfx::setVertexBuffer(0, m_vbh);
  226. bgfx::setIndexBuffer(m_ibh);
  227. // Bind textures.
  228. bgfx::setTexture(0, s_texColor, m_textureColor);
  229. bgfx::setTexture(1, s_texNormal, m_textureNormal);
  230. // Set render states.
  231. bgfx::setState(0
  232. | BGFX_STATE_RGB_WRITE
  233. | BGFX_STATE_ALPHA_WRITE
  234. | BGFX_STATE_DEPTH_WRITE
  235. | BGFX_STATE_DEPTH_TEST_LESS
  236. | BGFX_STATE_MSAA
  237. );
  238. // Submit primitive for rendering to view 0.
  239. bgfx::submit(0, m_program);
  240. }
  241. }
  242. }
  243. else
  244. {
  245. for (uint32_t yy = 0; yy < 3; ++yy)
  246. {
  247. for (uint32_t xx = 0; xx < 3; ++xx)
  248. {
  249. float mtx[16];
  250. bx::mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f);
  251. mtx[12] = -3.0f + float(xx)*3.0f;
  252. mtx[13] = -3.0f + float(yy)*3.0f;
  253. mtx[14] = 0.0f;
  254. // Set transform for draw call.
  255. bgfx::setTransform(mtx);
  256. // Set vertex and index buffer.
  257. bgfx::setVertexBuffer(0, m_vbh);
  258. bgfx::setIndexBuffer(m_ibh);
  259. // Bind textures.
  260. bgfx::setTexture(0, s_texColor, m_textureColor);
  261. bgfx::setTexture(1, s_texNormal, m_textureNormal);
  262. // Set render states.
  263. bgfx::setState(0
  264. | BGFX_STATE_RGB_WRITE
  265. | BGFX_STATE_ALPHA_WRITE
  266. | BGFX_STATE_DEPTH_WRITE
  267. | BGFX_STATE_DEPTH_TEST_LESS
  268. | BGFX_STATE_MSAA
  269. );
  270. // Submit primitive for rendering to view 0.
  271. bgfx::submit(0, m_program);
  272. }
  273. }
  274. }
  275. // Advance to next frame. Rendering thread will be kicked to
  276. // process submitted rendering primitives.
  277. bgfx::frame();
  278. return true;
  279. }
  280. return false;
  281. }
  282. bgfx::VertexBufferHandle m_vbh;
  283. bgfx::IndexBufferHandle m_ibh;
  284. bgfx::UniformHandle s_texColor;
  285. bgfx::UniformHandle s_texNormal;
  286. bgfx::UniformHandle u_lightPosRadius;
  287. bgfx::UniformHandle u_lightRgbInnerR;
  288. bgfx::ProgramHandle m_program;
  289. bgfx::TextureHandle m_textureColor;
  290. bgfx::TextureHandle m_textureNormal;
  291. uint16_t m_numLights;
  292. bool m_instancingSupported;
  293. uint32_t m_width;
  294. uint32_t m_height;
  295. uint32_t m_debug;
  296. uint32_t m_reset;
  297. int64_t m_timeOffset;
  298. };
  299. ENTRY_IMPLEMENT_MAIN(ExampleBump);