shadowmaps_simple.cpp 17 KB

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