shadowmaps_simple.cpp 17 KB

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