shadowmaps_simple.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. * Copyright 2013-2014 Dario Manesku. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include <string>
  6. #include <vector>
  7. #include <algorithm>
  8. #include "common.h"
  9. #include <bgfx.h>
  10. #include <bx/timer.h>
  11. #include <bx/readerwriter.h>
  12. #include <bx/fpumath.h>
  13. #include "entry/entry.h"
  14. #define RENDER_SHADOW_PASS_ID 0
  15. #define RENDER_SCENE_PASS_ID 1
  16. uint32_t packUint32(uint8_t _x, uint8_t _y, uint8_t _z, uint8_t _w)
  17. {
  18. union
  19. {
  20. uint32_t ui32;
  21. uint8_t arr[4];
  22. } un;
  23. un.arr[0] = _x;
  24. un.arr[1] = _y;
  25. un.arr[2] = _z;
  26. un.arr[3] = _w;
  27. return un.ui32;
  28. }
  29. uint32_t packF4u(float _x, float _y = 0.0f, float _z = 0.0f, float _w = 0.0f)
  30. {
  31. const uint8_t xx = uint8_t(_x*127.0f + 128.0f);
  32. const uint8_t yy = uint8_t(_y*127.0f + 128.0f);
  33. const uint8_t zz = uint8_t(_z*127.0f + 128.0f);
  34. const uint8_t ww = uint8_t(_w*127.0f + 128.0f);
  35. return packUint32(xx, yy, zz, ww);
  36. }
  37. struct PosNormalVertex
  38. {
  39. float m_x;
  40. float m_y;
  41. float m_z;
  42. uint32_t m_normal;
  43. };
  44. static PosNormalVertex s_hplaneVertices[] =
  45. {
  46. { -1.0f, 0.0f, 1.0f, packF4u(0.0f, 1.0f, 0.0f) },
  47. { 1.0f, 0.0f, 1.0f, packF4u(0.0f, 1.0f, 0.0f) },
  48. { -1.0f, 0.0f, -1.0f, packF4u(0.0f, 1.0f, 0.0f) },
  49. { 1.0f, 0.0f, -1.0f, packF4u(0.0f, 1.0f, 0.0f) },
  50. };
  51. static const uint16_t s_planeIndices[] =
  52. {
  53. 0, 1, 2,
  54. 1, 3, 2,
  55. };
  56. static const char* s_shaderPath = NULL;
  57. static bool s_flipV = false;
  58. static float s_texelHalf = 0.0f;
  59. bgfx::FrameBufferHandle s_shadowMapFB;
  60. static bgfx::UniformHandle u_shadowMap;
  61. inline void mtxProj(float* _result, float _fovy, float _aspect, float _near, float _far)
  62. {
  63. bx::mtxProj(_result, _fovy, _aspect, _near, _far, s_flipV);
  64. }
  65. static void shaderFilePath(char* _out, const char* _name)
  66. {
  67. strcpy(_out, s_shaderPath);
  68. strcat(_out, _name);
  69. strcat(_out, ".bin");
  70. }
  71. long int fsize(FILE* _file)
  72. {
  73. long int pos = ftell(_file);
  74. fseek(_file, 0L, SEEK_END);
  75. long int size = ftell(_file);
  76. fseek(_file, pos, SEEK_SET);
  77. return size;
  78. }
  79. static const bgfx::Memory* load(const char* _filePath)
  80. {
  81. FILE* file = fopen(_filePath, "rb");
  82. if (NULL != file)
  83. {
  84. uint32_t size = (uint32_t)fsize(file);
  85. const bgfx::Memory* mem = bgfx::alloc(size+1);
  86. size_t ignore = fread(mem->data, 1, size, file);
  87. BX_UNUSED(ignore);
  88. fclose(file);
  89. mem->data[mem->size-1] = '\0';
  90. return mem;
  91. }
  92. return NULL;
  93. }
  94. static const bgfx::Memory* loadShader(const char* _name)
  95. {
  96. char filePath[512];
  97. shaderFilePath(filePath, _name);
  98. return load(filePath);
  99. }
  100. static bgfx::ProgramHandle loadProgram(const char* _vsName, const char* _fsName)
  101. {
  102. const bgfx::Memory* mem;
  103. // Load vertex shader.
  104. mem = loadShader(_vsName);
  105. bgfx::ShaderHandle vsh = bgfx::createShader(mem);
  106. // Load fragment shader.
  107. mem = loadShader(_fsName);
  108. bgfx::ShaderHandle fsh = bgfx::createShader(mem);
  109. // Create program from shaders.
  110. return bgfx::createProgram(vsh, fsh, true /* destroy shaders when program is destroyed */);
  111. }
  112. struct Aabb
  113. {
  114. float m_min[3];
  115. float m_max[3];
  116. };
  117. struct Obb
  118. {
  119. float m_mtx[16];
  120. };
  121. struct Sphere
  122. {
  123. float m_center[3];
  124. float m_radius;
  125. };
  126. struct Primitive
  127. {
  128. uint32_t m_startIndex;
  129. uint32_t m_numIndices;
  130. uint32_t m_startVertex;
  131. uint32_t m_numVertices;
  132. Sphere m_sphere;
  133. Aabb m_aabb;
  134. Obb m_obb;
  135. };
  136. typedef std::vector<Primitive> PrimitiveArray;
  137. struct Group
  138. {
  139. Group()
  140. {
  141. reset();
  142. }
  143. void reset()
  144. {
  145. m_vbh.idx = bgfx::invalidHandle;
  146. m_ibh.idx = bgfx::invalidHandle;
  147. m_prims.clear();
  148. }
  149. bgfx::VertexBufferHandle m_vbh;
  150. bgfx::IndexBufferHandle m_ibh;
  151. Sphere m_sphere;
  152. Aabb m_aabb;
  153. Obb m_obb;
  154. PrimitiveArray m_prims;
  155. };
  156. namespace bgfx
  157. {
  158. int32_t read(bx::ReaderI* _reader, bgfx::VertexDecl& _decl);
  159. }
  160. struct Mesh
  161. {
  162. void load(const void* _vertices, uint32_t _numVertices, const bgfx::VertexDecl _decl, const uint16_t* _indices, uint32_t _numIndices)
  163. {
  164. Group group;
  165. const bgfx::Memory* mem;
  166. uint32_t size;
  167. size = _numVertices*_decl.getStride();
  168. mem = bgfx::makeRef(_vertices, size);
  169. group.m_vbh = bgfx::createVertexBuffer(mem, _decl);
  170. size = _numIndices*2;
  171. mem = bgfx::makeRef(_indices, size);
  172. group.m_ibh = bgfx::createIndexBuffer(mem);
  173. //TODO:
  174. // group.m_sphere = ...
  175. // group.m_aabb = ...
  176. // group.m_obb = ...
  177. // group.m_prims = ...
  178. m_groups.push_back(group);
  179. }
  180. void load(const char* _filePath)
  181. {
  182. #define BGFX_CHUNK_MAGIC_VB BX_MAKEFOURCC('V', 'B', ' ', 0x1)
  183. #define BGFX_CHUNK_MAGIC_IB BX_MAKEFOURCC('I', 'B', ' ', 0x0)
  184. #define BGFX_CHUNK_MAGIC_PRI BX_MAKEFOURCC('P', 'R', 'I', 0x0)
  185. bx::CrtFileReader reader;
  186. reader.open(_filePath);
  187. Group group;
  188. uint32_t chunk;
  189. while (4 == bx::read(&reader, chunk) )
  190. {
  191. switch (chunk)
  192. {
  193. case BGFX_CHUNK_MAGIC_VB:
  194. {
  195. bx::read(&reader, group.m_sphere);
  196. bx::read(&reader, group.m_aabb);
  197. bx::read(&reader, group.m_obb);
  198. bgfx::read(&reader, m_decl);
  199. uint16_t stride = m_decl.getStride();
  200. uint16_t numVertices;
  201. bx::read(&reader, numVertices);
  202. const bgfx::Memory* mem = bgfx::alloc(numVertices*stride);
  203. bx::read(&reader, mem->data, mem->size);
  204. group.m_vbh = bgfx::createVertexBuffer(mem, m_decl);
  205. }
  206. break;
  207. case BGFX_CHUNK_MAGIC_IB:
  208. {
  209. uint32_t numIndices;
  210. bx::read(&reader, numIndices);
  211. const bgfx::Memory* mem = bgfx::alloc(numIndices*2);
  212. bx::read(&reader, mem->data, mem->size);
  213. group.m_ibh = bgfx::createIndexBuffer(mem);
  214. }
  215. break;
  216. case BGFX_CHUNK_MAGIC_PRI:
  217. {
  218. uint16_t len;
  219. bx::read(&reader, len);
  220. std::string material;
  221. material.resize(len);
  222. bx::read(&reader, const_cast<char*>(material.c_str() ), len);
  223. uint16_t num;
  224. bx::read(&reader, num);
  225. for (uint32_t ii = 0; ii < num; ++ii)
  226. {
  227. bx::read(&reader, len);
  228. std::string name;
  229. name.resize(len);
  230. bx::read(&reader, const_cast<char*>(name.c_str() ), len);
  231. Primitive prim;
  232. bx::read(&reader, prim.m_startIndex);
  233. bx::read(&reader, prim.m_numIndices);
  234. bx::read(&reader, prim.m_startVertex);
  235. bx::read(&reader, prim.m_numVertices);
  236. bx::read(&reader, prim.m_sphere);
  237. bx::read(&reader, prim.m_aabb);
  238. bx::read(&reader, prim.m_obb);
  239. group.m_prims.push_back(prim);
  240. }
  241. m_groups.push_back(group);
  242. group.reset();
  243. }
  244. break;
  245. default:
  246. DBG("%08x at %d", chunk, reader.seek() );
  247. break;
  248. }
  249. }
  250. reader.close();
  251. }
  252. void unload()
  253. {
  254. for (GroupArray::const_iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
  255. {
  256. const Group& group = *it;
  257. bgfx::destroyVertexBuffer(group.m_vbh);
  258. if (bgfx::isValid(group.m_ibh) )
  259. {
  260. bgfx::destroyIndexBuffer(group.m_ibh);
  261. }
  262. }
  263. m_groups.clear();
  264. }
  265. void submit(uint8_t _view, float* _mtx, bgfx::ProgramHandle _program)
  266. {
  267. for (GroupArray::const_iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
  268. {
  269. const Group& group = *it;
  270. // Set model matrix for rendering.
  271. bgfx::setTransform(_mtx);
  272. bgfx::setProgram(_program);
  273. bgfx::setIndexBuffer(group.m_ibh);
  274. bgfx::setVertexBuffer(group.m_vbh);
  275. // Set shadow map.
  276. bgfx::setTexture(4, u_shadowMap, s_shadowMapFB);
  277. // Set render states.
  278. bgfx::setState(0
  279. | BGFX_STATE_RGB_WRITE
  280. | BGFX_STATE_ALPHA_WRITE
  281. | BGFX_STATE_DEPTH_WRITE
  282. | BGFX_STATE_DEPTH_TEST_LESS
  283. | BGFX_STATE_CULL_CCW
  284. | BGFX_STATE_MSAA
  285. );
  286. // Submit primitive for rendering.
  287. bgfx::submit(_view);
  288. }
  289. }
  290. void submitShadow(uint8_t _view, float* _mtx, bgfx::ProgramHandle _program)
  291. {
  292. for (GroupArray::const_iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
  293. {
  294. const Group& group = *it;
  295. // Set model matrix for rendering.
  296. bgfx::setTransform(_mtx);
  297. bgfx::setProgram(_program);
  298. bgfx::setIndexBuffer(group.m_ibh);
  299. bgfx::setVertexBuffer(group.m_vbh);
  300. // Set render states.
  301. bgfx::setState(0
  302. | BGFX_STATE_RGB_WRITE
  303. | BGFX_STATE_ALPHA_WRITE
  304. | BGFX_STATE_DEPTH_WRITE
  305. | BGFX_STATE_DEPTH_TEST_LESS
  306. | BGFX_STATE_CULL_CCW
  307. | BGFX_STATE_MSAA
  308. );
  309. // Submit primitive for rendering.
  310. bgfx::submit(_view);
  311. }
  312. }
  313. bgfx::VertexDecl m_decl;
  314. typedef std::vector<Group> GroupArray;
  315. GroupArray m_groups;
  316. };
  317. int _main_(int /*_argc*/, char** /*_argv*/)
  318. {
  319. uint32_t width = 1280;
  320. uint32_t height = 720;
  321. uint32_t debug = BGFX_DEBUG_TEXT;
  322. uint32_t reset = BGFX_RESET_VSYNC;
  323. bgfx::init();
  324. bgfx::reset(width, height, reset);
  325. // Enable debug text.
  326. bgfx::setDebug(debug);
  327. // Setup root path for binary shaders. Shader binaries are different
  328. // for each renderer.
  329. switch (bgfx::getRendererType() )
  330. {
  331. default:
  332. case bgfx::RendererType::Direct3D9:
  333. s_shaderPath = "shaders/dx9/";
  334. s_texelHalf = 0.5f;
  335. break;
  336. case bgfx::RendererType::Direct3D11:
  337. s_shaderPath = "shaders/dx11/";
  338. break;
  339. case bgfx::RendererType::OpenGL:
  340. s_shaderPath = "shaders/glsl/";
  341. s_flipV = true;
  342. break;
  343. case bgfx::RendererType::OpenGLES:
  344. s_shaderPath = "shaders/gles/";
  345. s_flipV = true;
  346. break;
  347. }
  348. // Uniforms.
  349. u_shadowMap = bgfx::createUniform("u_shadowMap", bgfx::UniformType::Uniform1iv);
  350. bgfx::UniformHandle u_lightPos = bgfx::createUniform("u_lightPos", bgfx::UniformType::Uniform4fv);
  351. bgfx::UniformHandle u_lightMtx = bgfx::createUniform("u_lightMtx", bgfx::UniformType::Uniform4x4fv);
  352. // Vertex declarations.
  353. bgfx::VertexDecl PosNormalDecl;
  354. PosNormalDecl.begin()
  355. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  356. .add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true)
  357. .end();
  358. // Meshes.
  359. Mesh bunnyMesh;
  360. Mesh cubeMesh;
  361. Mesh hollowcubeMesh;
  362. Mesh hplaneMesh;
  363. bunnyMesh.load("meshes/bunny.bin");
  364. cubeMesh.load("meshes/cube.bin");
  365. hollowcubeMesh.load("meshes/hollowcube.bin");
  366. hplaneMesh.load(s_hplaneVertices, BX_COUNTOF(s_hplaneVertices), PosNormalDecl, s_planeIndices, BX_COUNTOF(s_planeIndices) );
  367. // Render targets.
  368. uint16_t shadowMapSize = 512;
  369. // Get renderer capabilities info.
  370. const bgfx::Caps* caps = bgfx::getCaps();
  371. // Shadow samplers are supported at least partially supported if texture
  372. // compare less equal feature is supported.
  373. bool shadowSamplerSupported = 0 != (caps->supported & BGFX_CAPS_TEXTURE_COMPARE_LEQUAL);
  374. bgfx::ProgramHandle progShadow;
  375. bgfx::ProgramHandle progMesh;
  376. if (shadowSamplerSupported)
  377. {
  378. // Depth textures and shadow samplers are supported.
  379. progShadow = loadProgram("vs_sms_shadow", "fs_sms_shadow");
  380. progMesh = loadProgram("vs_sms_mesh", "fs_sms_mesh");
  381. bgfx::TextureHandle fbtextures[] =
  382. {
  383. bgfx::createTexture2D(shadowMapSize, shadowMapSize, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_COMPARE_LEQUAL),
  384. };
  385. s_shadowMapFB = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
  386. }
  387. else
  388. {
  389. // Depth textures and shadow samplers are not supported. Use float
  390. // depth packing into color buffer instead.
  391. progShadow = loadProgram("vs_sms_shadow_pd", "fs_sms_shadow_pd");
  392. progMesh = loadProgram("vs_sms_mesh", "fs_sms_mesh_pd");
  393. bgfx::TextureHandle fbtextures[] =
  394. {
  395. bgfx::createTexture2D(shadowMapSize, shadowMapSize, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT),
  396. bgfx::createTexture2D(shadowMapSize, shadowMapSize, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_RT_BUFFER_ONLY),
  397. };
  398. s_shadowMapFB = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
  399. }
  400. // Set view and projection matrices.
  401. float view[16];
  402. float proj[16];
  403. const float eye[3] = { 0.0f, 30.0f, -60.0f };
  404. const float at[3] = { 0.0f, 5.0f, 0.0f };
  405. bx::mtxLookAt(view, eye, at);
  406. const float aspect = float(int32_t(width) ) / float(int32_t(height) );
  407. mtxProj(proj, 60.0f, aspect, 0.1f, 1000.0f);
  408. // Time acumulators.
  409. float timeAccumulatorLight = 0.0f;
  410. float timeAccumulatorScene = 0.0f;
  411. entry::MouseState mouseState;
  412. while (!entry::processEvents(width, height, debug, reset, &mouseState) )
  413. {
  414. // Time.
  415. int64_t now = bx::getHPCounter();
  416. static int64_t last = now;
  417. const int64_t frameTime = now - last;
  418. last = now;
  419. const double freq = double(bx::getHPFrequency() );
  420. const double toMs = 1000.0/freq;
  421. const float deltaTime = float(frameTime/freq);
  422. // Update time accumulators.
  423. timeAccumulatorLight += deltaTime;
  424. timeAccumulatorScene += deltaTime;
  425. // Use debug font to print information about this example.
  426. bgfx::dbgTextClear();
  427. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/15-shadowmaps-simple");
  428. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Shadow maps example (technique: %s).", shadowSamplerSupported ? "depth texture and shadow samplers" : "shadow depth packed into color texture");
  429. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  430. // Setup lights.
  431. float lightPos[4];
  432. lightPos[0] = -cos(timeAccumulatorLight);
  433. lightPos[1] = -1.0f;
  434. lightPos[2] = -sin(timeAccumulatorLight);
  435. lightPos[3] = 0.0f;
  436. bgfx::setUniform(u_lightPos, lightPos);
  437. // Setup instance matrices.
  438. float mtxFloor[16];
  439. bx::mtxSRT(mtxFloor
  440. , 30.0f, 30.0f, 30.0f
  441. , 0.0f, 0.0f, 0.0f
  442. , 0.0f, 0.0f, 0.0f
  443. );
  444. float mtxBunny[16];
  445. bx::mtxSRT(mtxBunny
  446. , 5.0f, 5.0f, 5.0f
  447. , 0.0f, bx::pi - timeAccumulatorScene, 0.0f
  448. , 15.0f, 5.0f, 0.0f
  449. );
  450. float mtxHollowcube[16];
  451. bx::mtxSRT(mtxHollowcube
  452. , 2.5f, 2.5f, 2.5f
  453. , 0.0f, 1.56f - timeAccumulatorScene, 0.0f
  454. , 0.0f, 10.0f, 0.0f
  455. );
  456. float mtxCube[16];
  457. bx::mtxSRT(mtxCube
  458. , 2.5f, 2.5f, 2.5f
  459. , 0.0f, 1.56f - timeAccumulatorScene, 0.0f
  460. , -15.0f, 5.0f, 0.0f
  461. );
  462. // Define matrices.
  463. float lightView[16];
  464. float lightProj[16];
  465. const float eye[3] =
  466. {
  467. -lightPos[0],
  468. -lightPos[1],
  469. -lightPos[2],
  470. };
  471. const float at[3] = { 0.0f, 0.0f, 0.0f };
  472. bx::mtxLookAt(lightView, eye, at);
  473. const float area = 30.0f;
  474. bx::mtxOrtho(lightProj, -area, area, -area, area, -100.0f, 100.0f);
  475. bgfx::setViewRect(RENDER_SHADOW_PASS_ID, 0, 0, shadowMapSize, shadowMapSize);
  476. bgfx::setViewFrameBuffer(RENDER_SHADOW_PASS_ID, s_shadowMapFB);
  477. bgfx::setViewTransform(RENDER_SHADOW_PASS_ID, lightView, lightProj);
  478. bgfx::setViewRect(RENDER_SCENE_PASS_ID, 0, 0, width, height);
  479. bgfx::setViewTransform(RENDER_SCENE_PASS_ID, view, proj);
  480. // Clear backbuffer and shadowmap framebuffer at beginning.
  481. bgfx::setViewClear(RENDER_SHADOW_PASS_ID
  482. , BGFX_CLEAR_COLOR_BIT | BGFX_CLEAR_DEPTH_BIT
  483. , 0x303030ff, 1.0f, 0
  484. );
  485. bgfx::setViewClear(RENDER_SCENE_PASS_ID
  486. , BGFX_CLEAR_COLOR_BIT | BGFX_CLEAR_DEPTH_BIT
  487. , 0x303030ff, 1.0f, 0
  488. );
  489. // Render.
  490. float mtxShadow[16];
  491. float lightMtx[16];
  492. const float sy = s_flipV ? 0.5f : -0.5f;
  493. const float mtxCrop[16] =
  494. {
  495. 0.5f, 0.0f, 0.0f, 0.0f,
  496. 0.0f, sy, 0.0f, 0.0f,
  497. 0.0f, 0.0f, 0.5f, 0.0f,
  498. 0.5f, 0.5f, 0.5f, 1.0f,
  499. };
  500. float mtxTmp[16];
  501. bx::mtxMul(mtxTmp, lightProj, mtxCrop);
  502. bx::mtxMul(mtxShadow, lightView, mtxTmp);
  503. // Floor.
  504. bx::mtxMul(lightMtx, mtxFloor, mtxShadow);
  505. bgfx::setUniform(u_lightMtx, lightMtx);
  506. hplaneMesh.submit(RENDER_SCENE_PASS_ID, mtxFloor, progMesh);
  507. hplaneMesh.submitShadow(RENDER_SHADOW_PASS_ID, mtxFloor, progShadow);
  508. // Bunny.
  509. bx::mtxMul(lightMtx, mtxBunny, mtxShadow);
  510. bgfx::setUniform(u_lightMtx, lightMtx);
  511. bunnyMesh.submit(RENDER_SCENE_PASS_ID, mtxBunny, progMesh);
  512. bunnyMesh.submitShadow(RENDER_SHADOW_PASS_ID, mtxBunny, progShadow);
  513. // Hollow cube.
  514. bx::mtxMul(lightMtx, mtxHollowcube, mtxShadow);
  515. bgfx::setUniform(u_lightMtx, lightMtx);
  516. hollowcubeMesh.submit(RENDER_SCENE_PASS_ID, mtxHollowcube, progMesh);
  517. hollowcubeMesh.submitShadow(RENDER_SHADOW_PASS_ID, mtxHollowcube, progShadow);
  518. // Cube.
  519. bx::mtxMul(lightMtx, mtxCube, mtxShadow);
  520. bgfx::setUniform(u_lightMtx, lightMtx);
  521. cubeMesh.submit(RENDER_SCENE_PASS_ID, mtxCube, progMesh);
  522. cubeMesh.submitShadow(RENDER_SHADOW_PASS_ID, mtxCube, progShadow);
  523. // Advance to next frame. Rendering thread will be kicked to
  524. // process submitted rendering primitives.
  525. bgfx::frame();
  526. }
  527. bunnyMesh.unload();
  528. cubeMesh.unload();
  529. hollowcubeMesh.unload();
  530. hplaneMesh.unload();
  531. bgfx::destroyProgram(progShadow);
  532. bgfx::destroyProgram(progMesh);
  533. bgfx::destroyFrameBuffer(s_shadowMapFB);
  534. bgfx::destroyUniform(u_shadowMap);
  535. bgfx::destroyUniform(u_lightPos);
  536. bgfx::destroyUniform(u_lightMtx);
  537. // Shutdown bgfx.
  538. bgfx::shutdown();
  539. return 0;
  540. }