stencil.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  1. /*
  2. * Copyright 2013 Dario Manesku. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "common.h"
  6. #include <bgfx.h>
  7. #include <bx/timer.h>
  8. #include <bx/readerwriter.h>
  9. #include "entry/entry.h"
  10. #include "fpumath.h"
  11. #include "imgui/imgui.h"
  12. #include <string>
  13. #include <vector>
  14. #define RENDER_VIEWID_RANGE1_PASS_0 1
  15. #define RENDER_VIEWID_RANGE1_PASS_1 2
  16. #define RENDER_VIEWID_RANGE1_PASS_2 3
  17. #define RENDER_VIEWID_RANGE1_PASS_3 4
  18. #define RENDER_VIEWID_RANGE1_PASS_4 5
  19. #define RENDER_VIEWID_RANGE1_PASS_5 6
  20. #define RENDER_VIEWID_RANGE5_PASS_6 7
  21. #define RENDER_VIEWID_RANGE1_PASS_7 13
  22. uint32_t packUint32(uint8_t _x, uint8_t _y, uint8_t _z, uint8_t _w)
  23. {
  24. union
  25. {
  26. uint32_t ui32;
  27. uint8_t arr[4];
  28. } un;
  29. un.arr[0] = _x;
  30. un.arr[1] = _y;
  31. un.arr[2] = _z;
  32. un.arr[3] = _w;
  33. return un.ui32;
  34. }
  35. uint32_t packF4u(float _x, float _y = 0.0f, float _z = 0.0f, float _w = 0.0f)
  36. {
  37. const uint8_t xx = uint8_t(_x*127.0f + 128.0f);
  38. const uint8_t yy = uint8_t(_y*127.0f + 128.0f);
  39. const uint8_t zz = uint8_t(_z*127.0f + 128.0f);
  40. const uint8_t ww = uint8_t(_w*127.0f + 128.0f);
  41. return packUint32(xx, yy, zz, ww);
  42. }
  43. struct PosNormalTexcoordVertex
  44. {
  45. float m_x;
  46. float m_y;
  47. float m_z;
  48. uint32_t m_normal;
  49. float m_u;
  50. float m_v;
  51. };
  52. static const float s_texcoord = 5.0f;
  53. static const uint32_t s_numHPlaneVertices = 4;
  54. static PosNormalTexcoordVertex s_hplaneVertices[s_numHPlaneVertices] =
  55. {
  56. { -1.0f, 0.0f, 1.0f, packF4u(0.0f, 1.0f, 0.0f), s_texcoord, s_texcoord },
  57. { 1.0f, 0.0f, 1.0f, packF4u(0.0f, 1.0f, 0.0f), s_texcoord, 0.0f },
  58. { -1.0f, 0.0f, -1.0f, packF4u(0.0f, 1.0f, 0.0f), 0.0f, s_texcoord },
  59. { 1.0f, 0.0f, -1.0f, packF4u(0.0f, 1.0f, 0.0f), 0.0f, 0.0f },
  60. };
  61. static const uint32_t s_numVPlaneVertices = 4;
  62. static PosNormalTexcoordVertex s_vplaneVertices[s_numVPlaneVertices] =
  63. {
  64. { -1.0f, 1.0f, 0.0f, packF4u(0.0f, 0.0f, -1.0f), 1.0f, 1.0f },
  65. { 1.0f, 1.0f, 0.0f, packF4u(0.0f, 0.0f, -1.0f), 1.0f, 0.0f },
  66. { -1.0f, -1.0f, 0.0f, packF4u(0.0f, 0.0f, -1.0f), 0.0f, 1.0f },
  67. { 1.0f, -1.0f, 0.0f, packF4u(0.0f, 0.0f, -1.0f), 0.0f, 0.0f },
  68. };
  69. static const uint32_t s_numCubeVertices = 24;
  70. static const PosNormalTexcoordVertex s_cubeVertices[s_numCubeVertices] =
  71. {
  72. { -1.0f, 1.0f, 1.0f, packF4u( 0.0f, 1.0f, 0.0f), 1.0f, 1.0f },
  73. { 1.0f, 1.0f, 1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0.0f, 1.0f },
  74. { -1.0f, 1.0f, -1.0f, packF4u( 0.0f, 1.0f, 0.0f), 1.0f, 0.0f },
  75. { 1.0f, 1.0f, -1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0.0f, 0.0f },
  76. { -1.0f, -1.0f, 1.0f, packF4u( 0.0f, -1.0f, 0.0f), 1.0f, 1.0f },
  77. { 1.0f, -1.0f, 1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0.0f, 1.0f },
  78. { -1.0f, -1.0f, -1.0f, packF4u( 0.0f, -1.0f, 0.0f), 1.0f, 0.0f },
  79. { 1.0f, -1.0f, -1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0.0f, 0.0f },
  80. { 1.0f, -1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0.0f, 0.0f },
  81. { 1.0f, 1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0.0f, 1.0f },
  82. { -1.0f, -1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 1.0f, 0.0f },
  83. { -1.0f, 1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 1.0f, 1.0f },
  84. { 1.0f, -1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0.0f, 0.0f },
  85. { 1.0f, 1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0.0f, 1.0f },
  86. { -1.0f, -1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 1.0f, 0.0f },
  87. { -1.0f, 1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 1.0f, 1.0f },
  88. { 1.0f, 1.0f, -1.0f, packF4u( 1.0f, 0.0f, 0.0f), 1.0f, 1.0f },
  89. { 1.0f, 1.0f, 1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0.0f, 1.0f },
  90. { 1.0f, -1.0f, -1.0f, packF4u( 1.0f, 0.0f, 0.0f), 1.0f, 0.0f },
  91. { 1.0f, -1.0f, 1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0.0f, 0.0f },
  92. { -1.0f, 1.0f, -1.0f, packF4u(-1.0f, 0.0f, 0.0f), 1.0f, 1.0f },
  93. { -1.0f, 1.0f, 1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0.0f, 1.0f },
  94. { -1.0f, -1.0f, -1.0f, packF4u(-1.0f, 0.0f, 0.0f), 1.0f, 0.0f },
  95. { -1.0f, -1.0f, 1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0.0f, 0.0f },
  96. };
  97. static const uint32_t s_numCubeIndices = 36;
  98. static const uint16_t s_cubeIndices[s_numCubeIndices] =
  99. {
  100. 0, 1, 2,
  101. 1, 3, 2,
  102. 4, 6, 5,
  103. 5, 6, 7,
  104. 8, 9, 10,
  105. 9, 11, 10,
  106. 12, 14, 13,
  107. 13, 14, 15,
  108. 16, 17, 18,
  109. 17, 19, 18,
  110. 20, 22, 21,
  111. 21, 22, 23,
  112. };
  113. static const uint32_t s_numPlaneIndices = 6;
  114. static const uint16_t s_planeIndices[s_numPlaneIndices] =
  115. {
  116. 0, 1, 2,
  117. 1, 3, 2,
  118. };
  119. //-------------------------------------------------
  120. // Helper functions
  121. //-------------------------------------------------
  122. static const char* s_shaderPath = NULL;
  123. static bool s_flipV = false;
  124. static uint32_t s_viewMask = 0;
  125. static uint32_t s_clearMask = 0;
  126. static bgfx::UniformHandle u_texColor;
  127. static void shaderFilePath(char* _out, const char* _name)
  128. {
  129. strcpy(_out, s_shaderPath);
  130. strcat(_out, _name);
  131. strcat(_out, ".bin");
  132. }
  133. long int fsize(FILE* _file)
  134. {
  135. long int pos = ftell(_file);
  136. fseek(_file, 0L, SEEK_END);
  137. long int size = ftell(_file);
  138. fseek(_file, pos, SEEK_SET);
  139. return size;
  140. }
  141. static const bgfx::Memory* load(const char* _filePath)
  142. {
  143. FILE* file = fopen(_filePath, "rb");
  144. if (NULL != file)
  145. {
  146. uint32_t size = (uint32_t)fsize(file);
  147. const bgfx::Memory* mem = bgfx::alloc(size+1);
  148. size_t ignore = fread(mem->data, 1, size, file);
  149. BX_UNUSED(ignore);
  150. fclose(file);
  151. mem->data[mem->size-1] = '\0';
  152. return mem;
  153. }
  154. return NULL;
  155. }
  156. static const bgfx::Memory* loadShader(const char* _name)
  157. {
  158. char filePath[512];
  159. shaderFilePath(filePath, _name);
  160. return load(filePath);
  161. }
  162. static const bgfx::Memory* loadTexture(const char* _name)
  163. {
  164. char filePath[512];
  165. strcpy(filePath, "textures/");
  166. strcat(filePath, _name);
  167. return load(filePath);
  168. }
  169. static bgfx::ProgramHandle loadProgram(const char* _vsName, const char* _fsName)
  170. {
  171. const bgfx::Memory* mem;
  172. // Load vertex shader.
  173. mem = loadShader(_vsName);
  174. bgfx::VertexShaderHandle vsh = bgfx::createVertexShader(mem);
  175. // Load fragment shader.
  176. mem = loadShader(_fsName);
  177. bgfx::FragmentShaderHandle fsh = bgfx::createFragmentShader(mem);
  178. // Create program from shaders.
  179. bgfx::ProgramHandle program = bgfx::createProgram(vsh, fsh);
  180. // We can destroy vertex and fragment shader here since
  181. // their reference is kept inside bgfx after calling createProgram.
  182. // Vertex and fragment shader will be destroyed once program is
  183. // destroyed.
  184. bgfx::destroyVertexShader(vsh);
  185. bgfx::destroyFragmentShader(fsh);
  186. return program;
  187. }
  188. //-------------------------------------------------
  189. // Math
  190. //-------------------------------------------------
  191. void mtxScaleRotateTranslate(float* _result
  192. , const float _scaleX
  193. , const float _scaleY
  194. , const float _scaleZ
  195. , const float _rotX
  196. , const float _rotY
  197. , const float _rotZ
  198. , const float _translateX
  199. , const float _translateY
  200. , const float _translateZ
  201. )
  202. {
  203. float mtxRotateTranslate[16];
  204. float mtxScale[16];
  205. mtxRotateXYZ(mtxRotateTranslate, _rotX, _rotY, _rotZ);
  206. mtxRotateTranslate[12] = _translateX;
  207. mtxRotateTranslate[13] = _translateY;
  208. mtxRotateTranslate[14] = _translateZ;
  209. memset(mtxScale, 0, sizeof(float)*16);
  210. mtxScale[0] = _scaleX;
  211. mtxScale[5] = _scaleY;
  212. mtxScale[10] = _scaleZ;
  213. mtxScale[15] = 1.0f;
  214. mtxMul(_result, mtxScale, mtxRotateTranslate);
  215. }
  216. void mtxReflected(float*__restrict _result
  217. , const float* __restrict _p /* plane */
  218. , const float* __restrict _n /* normal */
  219. )
  220. {
  221. float dot = vec3Dot(_p, _n);
  222. _result[ 0] = 1.0f - 2.0f * _n[0] * _n[0]; //1-2Nx^2
  223. _result[ 1] = -2.0f * _n[0] * _n[1]; //-2*Nx*Ny
  224. _result[ 2] = -2.0f * _n[0] * _n[2]; //-2*NxNz
  225. _result[ 3] = 0.0f; //0
  226. _result[ 4] = -2.0f * _n[0] * _n[1]; //-2*NxNy
  227. _result[ 5] = 1.0f - 2.0f * _n[1] * _n[1]; //1-2*Ny^2
  228. _result[ 6] = -2.0f * _n[1] * _n[2]; //-2*NyNz
  229. _result[ 7] = 0.0f; //0
  230. _result[ 8] = -2.0f * _n[0] * _n[2]; //-2*NxNz
  231. _result[ 9] = -2.0f * _n[1] * _n[2]; //-2NyNz
  232. _result[10] = 1.0f - 2.0f * _n[2] * _n[2]; //1-2*Nz^2
  233. _result[11] = 0.0f; //0
  234. _result[12] = 2.0f * dot * _n[0]; //2*dot*Nx
  235. _result[13] = 2.0f * dot * _n[1]; //2*dot*Ny
  236. _result[14] = 2.0f * dot * _n[2]; //2*dot*Nz
  237. _result[15] = 1.0f; //1
  238. }
  239. void mtxShadow(float* __restrict _result
  240. , const float* __restrict _ground
  241. , const float* __restrict _light
  242. )
  243. {
  244. float dot = _ground[0] * _light[0]
  245. + _ground[1] * _light[1]
  246. + _ground[2] * _light[2]
  247. + _ground[3] * _light[3]
  248. ;
  249. _result[ 0] = dot - _light[0] * _ground[0];
  250. _result[ 1] = 0.0f - _light[1] * _ground[0];
  251. _result[ 2] = 0.0f - _light[2] * _ground[0];
  252. _result[ 3] = 0.0f - _light[3] * _ground[0];
  253. _result[ 4] = 0.0f - _light[0] * _ground[1];
  254. _result[ 5] = dot - _light[1] * _ground[1];
  255. _result[ 6] = 0.0f - _light[2] * _ground[1];
  256. _result[ 7] = 0.0f - _light[3] * _ground[1];
  257. _result[ 8] = 0.0f - _light[0] * _ground[2];
  258. _result[ 9] = 0.0f - _light[1] * _ground[2];
  259. _result[10] = dot - _light[2] * _ground[2];
  260. _result[11] = 0.0f - _light[3] * _ground[2];
  261. _result[12] = 0.0f - _light[0] * _ground[3];
  262. _result[13] = 0.0f - _light[1] * _ground[3];
  263. _result[14] = 0.0f - _light[2] * _ground[3];
  264. _result[15] = dot - _light[3] * _ground[3];
  265. }
  266. void mtxBillboard(float* __restrict _result
  267. , const float* __restrict _view
  268. , const float* __restrict _pos
  269. , const float* __restrict _scale)
  270. {
  271. _result[ 0] = _view[0] * _scale[0];
  272. _result[ 1] = _view[4] * _scale[0];
  273. _result[ 2] = _view[8] * _scale[0];
  274. _result[ 3] = 0.0f;
  275. _result[ 4] = _view[1] * _scale[1];
  276. _result[ 5] = _view[5] * _scale[1];
  277. _result[ 6] = _view[9] * _scale[1];
  278. _result[ 7] = 0.0f;
  279. _result[ 8] = _view[2] * _scale[2];
  280. _result[ 9] = _view[6] * _scale[2];
  281. _result[10] = _view[10] * _scale[2];
  282. _result[11] = 0.0f;
  283. _result[12] = _pos[0];
  284. _result[13] = _pos[1];
  285. _result[14] = _pos[2];
  286. _result[15] = 1.0f;
  287. }
  288. struct RenderState
  289. {
  290. enum Enum
  291. {
  292. StencilReflection_CraftStencil = 0,
  293. StencilReflection_DrawReflected,
  294. StencilReflection_DarkenReflections,
  295. StencilReflection_BlendPlane,
  296. StencilReflection_DrawScene,
  297. ProjectionShadows_DrawAmbient,
  298. ProjectionShadows_CraftStencil,
  299. ProjectionShadows_DrawDiffuse,
  300. Custom_BlendLightTexture,
  301. Custom_DrawPlaneBottom,
  302. Count
  303. };
  304. uint64_t m_state;
  305. uint32_t m_blendFactorRgba;
  306. uint32_t m_fstencil;
  307. uint32_t m_bstencil;
  308. };
  309. static RenderState s_renderStates[RenderState::Count] =
  310. {
  311. { // StencilReflection_CraftStencil
  312. BGFX_STATE_MSAA
  313. , UINT32_MAX
  314. , BGFX_STENCIL_TEST_ALWAYS // pass always
  315. | BGFX_STENCIL_FUNC_REF(1) // value = 1
  316. | BGFX_STENCIL_FUNC_RMASK(0xff)
  317. | BGFX_STENCIL_OP_FAIL_S_REPLACE
  318. | BGFX_STENCIL_OP_FAIL_Z_REPLACE
  319. | BGFX_STENCIL_OP_PASS_Z_REPLACE // store the value
  320. , BGFX_STENCIL_NONE
  321. },
  322. { // StencilReflection_DrawReflected
  323. BGFX_STATE_RGB_WRITE
  324. | BGFX_STATE_ALPHA_WRITE
  325. | BGFX_STATE_DEPTH_WRITE
  326. | BGFX_STATE_DEPTH_TEST_LESS
  327. | BGFX_STATE_CULL_CW //reflection matrix has inverted normals. using CCW instead of CW.
  328. | BGFX_STATE_MSAA
  329. , UINT32_MAX
  330. , BGFX_STENCIL_TEST_EQUAL
  331. | BGFX_STENCIL_FUNC_REF(1)
  332. | BGFX_STENCIL_FUNC_RMASK(1)
  333. | BGFX_STENCIL_OP_FAIL_S_KEEP
  334. | BGFX_STENCIL_OP_FAIL_Z_KEEP
  335. | BGFX_STENCIL_OP_PASS_Z_KEEP
  336. , BGFX_STENCIL_NONE
  337. },
  338. { // StencilReflection_DarkenReflections
  339. BGFX_STATE_RGB_WRITE
  340. | BGFX_STATE_DEPTH_WRITE
  341. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ZERO, BGFX_STATE_BLEND_FACTOR)
  342. | BGFX_STATE_DEPTH_TEST_LESS
  343. | BGFX_STATE_CULL_CCW
  344. | BGFX_STATE_MSAA
  345. , UINT32_MAX
  346. , BGFX_STENCIL_NONE
  347. , BGFX_STENCIL_NONE
  348. },
  349. { // StencilReflection_BlendPlane
  350. BGFX_STATE_RGB_WRITE
  351. | BGFX_STATE_DEPTH_WRITE
  352. | BGFX_STATE_BLEND_LIGHTEN
  353. | BGFX_STATE_DEPTH_TEST_EQUAL
  354. | BGFX_STATE_CULL_CCW
  355. | BGFX_STATE_MSAA
  356. , UINT32_MAX
  357. , BGFX_STENCIL_NONE
  358. , BGFX_STENCIL_NONE
  359. },
  360. { // StencilReflection_DrawScene
  361. BGFX_STATE_RGB_WRITE
  362. | BGFX_STATE_DEPTH_WRITE
  363. | BGFX_STATE_DEPTH_TEST_LESS
  364. | BGFX_STATE_CULL_CCW
  365. | BGFX_STATE_MSAA
  366. , UINT32_MAX
  367. , BGFX_STENCIL_NONE
  368. , BGFX_STENCIL_NONE
  369. },
  370. { // ProjectionShadows_DrawAmbient
  371. BGFX_STATE_RGB_WRITE
  372. | BGFX_STATE_DEPTH_WRITE // write depth !
  373. | BGFX_STATE_DEPTH_TEST_LESS
  374. | BGFX_STATE_CULL_CCW
  375. | BGFX_STATE_MSAA
  376. , UINT32_MAX
  377. , BGFX_STENCIL_NONE
  378. , BGFX_STENCIL_NONE
  379. },
  380. { // ProjectionShadows_CraftStencil
  381. BGFX_STATE_DEPTH_TEST_LESS
  382. | BGFX_STATE_MSAA
  383. , UINT32_MAX
  384. , BGFX_STENCIL_TEST_ALWAYS // pass always
  385. | BGFX_STENCIL_FUNC_REF(1) // value = 1
  386. | BGFX_STENCIL_FUNC_RMASK(0xff)
  387. | BGFX_STENCIL_OP_FAIL_S_KEEP
  388. | BGFX_STENCIL_OP_FAIL_Z_KEEP
  389. | BGFX_STENCIL_OP_PASS_Z_REPLACE // store the value
  390. , BGFX_STENCIL_NONE
  391. },
  392. { // ProjectionShadows_DrawDiffuse
  393. BGFX_STATE_RGB_WRITE
  394. | BGFX_STATE_DEPTH_WRITE
  395. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_FACTOR, BGFX_STATE_BLEND_ONE)
  396. | BGFX_STATE_DEPTH_TEST_EQUAL
  397. | BGFX_STATE_CULL_CCW
  398. | BGFX_STATE_MSAA
  399. , UINT32_MAX
  400. , BGFX_STENCIL_TEST_NOTEQUAL
  401. | BGFX_STENCIL_FUNC_REF(1)
  402. | BGFX_STENCIL_FUNC_RMASK(1)
  403. | BGFX_STENCIL_OP_FAIL_S_KEEP
  404. | BGFX_STENCIL_OP_FAIL_Z_KEEP
  405. | BGFX_STENCIL_OP_PASS_Z_KEEP
  406. , BGFX_STENCIL_NONE
  407. },
  408. { // Custom_BlendLightTexture
  409. BGFX_STATE_RGB_WRITE
  410. | BGFX_STATE_ALPHA_WRITE
  411. | BGFX_STATE_DEPTH_WRITE
  412. | BGFX_STATE_DEPTH_TEST_LESS
  413. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_COLOR, BGFX_STATE_BLEND_INV_SRC_COLOR)
  414. | BGFX_STATE_CULL_CCW
  415. | BGFX_STATE_MSAA
  416. , UINT32_MAX
  417. , BGFX_STENCIL_NONE
  418. , BGFX_STENCIL_NONE
  419. },
  420. { // Custom_DrawPlaneBottom
  421. BGFX_STATE_RGB_WRITE
  422. | BGFX_STATE_CULL_CW
  423. | BGFX_STATE_MSAA
  424. , UINT32_MAX
  425. , BGFX_STENCIL_NONE
  426. , BGFX_STENCIL_NONE
  427. },
  428. };
  429. struct ViewState
  430. {
  431. ViewState(uint32_t _width = 1280
  432. , uint32_t _height = 720
  433. )
  434. : m_width(_width)
  435. , m_height(_height)
  436. { }
  437. uint32_t m_width;
  438. uint32_t m_height;
  439. float m_view[16], m_proj[16];
  440. };
  441. struct ClearValues
  442. {
  443. ClearValues(uint32_t _clearRgba = 0x30303000
  444. , float _clearDepth = 1.0f
  445. , uint8_t _clearStencil = 0
  446. )
  447. : m_clearRgba(_clearRgba)
  448. , m_clearDepth(_clearDepth)
  449. , m_clearStencil(_clearStencil)
  450. { }
  451. uint32_t m_clearRgba;
  452. float m_clearDepth;
  453. uint8_t m_clearStencil;
  454. };
  455. void setViewRectTransform(uint8_t _view, const ViewState& _viewState)
  456. {
  457. bgfx::setViewRect(_view, 0, 0, _viewState.m_width, _viewState.m_height);
  458. bgfx::setViewTransform(_view, _viewState.m_view, _viewState.m_proj);
  459. }
  460. void setViewRectTransformMask(uint32_t _viewMask, const ViewState& _viewState)
  461. {
  462. bgfx::setViewRectMask(_viewMask, 0, 0, _viewState.m_width, _viewState.m_height);
  463. bgfx::setViewTransformMask(_viewMask, _viewState.m_view, _viewState.m_proj);
  464. }
  465. void clearView(uint8_t _id, uint8_t _flags, const ClearValues& _clearValues)
  466. {
  467. bgfx::setViewClear(_id
  468. , _flags
  469. , _clearValues.m_clearRgba
  470. , _clearValues.m_clearDepth
  471. , _clearValues.m_clearStencil
  472. );
  473. // Keep track of cleared views
  474. s_clearMask |= 1 << _id;
  475. }
  476. void clearViewMask(uint32_t _viewMask, uint8_t _flags, const ClearValues& _clearValues)
  477. {
  478. bgfx::setViewClearMask(_viewMask
  479. , _flags
  480. , _clearValues.m_clearRgba
  481. , _clearValues.m_clearDepth
  482. , _clearValues.m_clearStencil
  483. );
  484. // Keep track of cleared views
  485. s_clearMask |= _viewMask;
  486. }
  487. void submit(uint8_t _id, int32_t _depth = 0)
  488. {
  489. // Submit
  490. bgfx::submit(_id, _depth);
  491. // Keep track of submited view ids
  492. s_viewMask |= 1 << _id;
  493. }
  494. void submitMask(uint32_t _viewMask, int32_t _depth = 0)
  495. {
  496. // Submit
  497. bgfx::submitMask(_viewMask, _depth);
  498. // Keep track of submited view ids
  499. s_viewMask |= _viewMask;
  500. }
  501. //-------------------------------------------------
  502. // Mesh
  503. //-------------------------------------------------
  504. struct Aabb
  505. {
  506. float m_min[3];
  507. float m_max[3];
  508. };
  509. struct Obb
  510. {
  511. float m_mtx[16];
  512. };
  513. struct Sphere
  514. {
  515. float m_center[3];
  516. float m_radius;
  517. };
  518. struct Primitive
  519. {
  520. uint32_t m_startIndex;
  521. uint32_t m_numIndices;
  522. uint32_t m_startVertex;
  523. uint32_t m_numVertices;
  524. Sphere m_sphere;
  525. Aabb m_aabb;
  526. Obb m_obb;
  527. };
  528. typedef std::vector<Primitive> PrimitiveArray;
  529. struct Group
  530. {
  531. Group()
  532. {
  533. reset();
  534. }
  535. void reset()
  536. {
  537. m_vbh.idx = bgfx::invalidHandle;
  538. m_ibh.idx = bgfx::invalidHandle;
  539. m_prims.clear();
  540. }
  541. bgfx::VertexBufferHandle m_vbh;
  542. bgfx::IndexBufferHandle m_ibh;
  543. Sphere m_sphere;
  544. Aabb m_aabb;
  545. Obb m_obb;
  546. PrimitiveArray m_prims;
  547. };
  548. struct Mesh
  549. {
  550. void load(const void* _vertices, uint32_t _numVertices, const bgfx::VertexDecl _decl
  551. , const uint16_t* _indices, uint32_t _numIndices)
  552. {
  553. Group group;
  554. const bgfx::Memory* mem;
  555. uint32_t size;
  556. size = _numVertices*_decl.getStride();
  557. mem = bgfx::makeRef(_vertices, size);
  558. group.m_vbh = bgfx::createVertexBuffer(mem, _decl);
  559. size = _numIndices*2;
  560. mem = bgfx::makeRef(_indices, size);
  561. group.m_ibh = bgfx::createIndexBuffer(mem);
  562. //TODO:
  563. // group.m_sphere = ...
  564. // group.m_aabb = ...
  565. // group.m_obb = ...
  566. // group.m_prims = ...
  567. m_groups.push_back(group);
  568. }
  569. void load(const char* _filePath)
  570. {
  571. #define BGFX_CHUNK_MAGIC_VB BX_MAKEFOURCC('V', 'B', ' ', 0x0)
  572. #define BGFX_CHUNK_MAGIC_IB BX_MAKEFOURCC('I', 'B', ' ', 0x0)
  573. #define BGFX_CHUNK_MAGIC_PRI BX_MAKEFOURCC('P', 'R', 'I', 0x0)
  574. bx::CrtFileReader reader;
  575. reader.open(_filePath);
  576. Group group;
  577. uint32_t chunk;
  578. while (4 == bx::read(&reader, chunk) )
  579. {
  580. switch (chunk)
  581. {
  582. case BGFX_CHUNK_MAGIC_VB:
  583. {
  584. bx::read(&reader, group.m_sphere);
  585. bx::read(&reader, group.m_aabb);
  586. bx::read(&reader, group.m_obb);
  587. bx::read(&reader, m_decl);
  588. uint16_t stride = m_decl.getStride();
  589. uint16_t numVertices;
  590. bx::read(&reader, numVertices);
  591. const bgfx::Memory* mem = bgfx::alloc(numVertices*stride);
  592. bx::read(&reader, mem->data, mem->size);
  593. group.m_vbh = bgfx::createVertexBuffer(mem, m_decl);
  594. }
  595. break;
  596. case BGFX_CHUNK_MAGIC_IB:
  597. {
  598. uint32_t numIndices;
  599. bx::read(&reader, numIndices);
  600. const bgfx::Memory* mem = bgfx::alloc(numIndices*2);
  601. bx::read(&reader, mem->data, mem->size);
  602. group.m_ibh = bgfx::createIndexBuffer(mem);
  603. }
  604. break;
  605. case BGFX_CHUNK_MAGIC_PRI:
  606. {
  607. uint16_t len;
  608. bx::read(&reader, len);
  609. std::string material;
  610. material.resize(len);
  611. bx::read(&reader, const_cast<char*>(material.c_str() ), len);
  612. uint16_t num;
  613. bx::read(&reader, num);
  614. for (uint32_t ii = 0; ii < num; ++ii)
  615. {
  616. bx::read(&reader, len);
  617. std::string name;
  618. name.resize(len);
  619. bx::read(&reader, const_cast<char*>(name.c_str() ), len);
  620. Primitive prim;
  621. bx::read(&reader, prim.m_startIndex);
  622. bx::read(&reader, prim.m_numIndices);
  623. bx::read(&reader, prim.m_startVertex);
  624. bx::read(&reader, prim.m_numVertices);
  625. bx::read(&reader, prim.m_sphere);
  626. bx::read(&reader, prim.m_aabb);
  627. bx::read(&reader, prim.m_obb);
  628. group.m_prims.push_back(prim);
  629. }
  630. m_groups.push_back(group);
  631. group.reset();
  632. }
  633. break;
  634. default:
  635. DBG("%08x at %d", chunk, reader.seek() );
  636. break;
  637. }
  638. }
  639. reader.close();
  640. }
  641. void unload()
  642. {
  643. for (GroupArray::const_iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
  644. {
  645. const Group& group = *it;
  646. bgfx::destroyVertexBuffer(group.m_vbh);
  647. if (bgfx::invalidHandle != group.m_ibh.idx)
  648. {
  649. bgfx::destroyIndexBuffer(group.m_ibh);
  650. }
  651. }
  652. m_groups.clear();
  653. }
  654. void submit(uint8_t _viewId, float* _mtx, bgfx::ProgramHandle _program, const RenderState& _renderState)
  655. {
  656. bgfx::TextureHandle texture = BGFX_INVALID_HANDLE;
  657. submit(_viewId, _mtx, _program, _renderState, texture);
  658. }
  659. void submit(uint8_t _viewId, float* _mtx, bgfx::ProgramHandle _program, const RenderState& _renderState, bgfx::TextureHandle _texture)
  660. {
  661. for (GroupArray::const_iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
  662. {
  663. const Group& group = *it;
  664. // Set model matrix for rendering.
  665. bgfx::setTransform(_mtx);
  666. bgfx::setProgram(_program);
  667. bgfx::setIndexBuffer(group.m_ibh);
  668. bgfx::setVertexBuffer(group.m_vbh);
  669. // Set texture
  670. if (bgfx::invalidHandle != _texture.idx)
  671. {
  672. bgfx::setTexture(0, u_texColor, _texture);
  673. }
  674. // Apply render state
  675. bgfx::setStencil(_renderState.m_fstencil, _renderState.m_bstencil);
  676. bgfx::setState(_renderState.m_state, _renderState.m_blendFactorRgba);
  677. // Submit
  678. ::submit(_viewId);
  679. }
  680. }
  681. bgfx::VertexDecl m_decl;
  682. typedef std::vector<Group> GroupArray;
  683. GroupArray m_groups;
  684. };
  685. int _main_(int /*_argc*/, char** /*_argv*/)
  686. {
  687. ViewState viewState(1280, 720);
  688. ClearValues clearValues(0x30303000, 1.0f, 0);
  689. uint32_t debug = BGFX_DEBUG_TEXT;
  690. uint32_t reset = BGFX_RESET_VSYNC;
  691. bgfx::init();
  692. bgfx::reset(viewState.m_width, viewState.m_height, reset);
  693. // Enable debug text.
  694. bgfx::setDebug(debug);
  695. // Setup root path for binary shaders. Shader binaries are different
  696. // for each renderer.
  697. switch (bgfx::getRendererType() )
  698. {
  699. default:
  700. case bgfx::RendererType::Direct3D9:
  701. s_shaderPath = "shaders/dx9/";
  702. break;
  703. case bgfx::RendererType::Direct3D11:
  704. s_shaderPath = "shaders/dx11/";
  705. break;
  706. case bgfx::RendererType::OpenGL:
  707. s_shaderPath = "shaders/glsl/";
  708. s_flipV = true;
  709. break;
  710. case bgfx::RendererType::OpenGLES2:
  711. case bgfx::RendererType::OpenGLES3:
  712. s_shaderPath = "shaders/gles/";
  713. s_flipV = true;
  714. break;
  715. }
  716. FILE* file = fopen("font/droidsans.ttf", "rb");
  717. uint32_t size = (uint32_t)fsize(file);
  718. void* data = malloc(size);
  719. size_t ignore = fread(data, 1, size, file);
  720. BX_UNUSED(ignore);
  721. fclose(file);
  722. imguiCreate(data, size);
  723. bgfx::VertexDecl PosNormalTexcoordDecl;
  724. PosNormalTexcoordDecl.begin();
  725. PosNormalTexcoordDecl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
  726. PosNormalTexcoordDecl.add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true);
  727. PosNormalTexcoordDecl.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float);
  728. PosNormalTexcoordDecl.end();
  729. const uint8_t MAX_NUM_LIGHTS = 5;
  730. //u_params.x - u_ambientPass
  731. //u_params.y - u_lightningPass
  732. //u_params.z - u_alpha
  733. //u_params.w - u_lightCount
  734. struct UniformParams
  735. {
  736. float m_ambientPass;
  737. float m_lightningPass;
  738. float m_alpha;
  739. float m_lightCount;
  740. } params;
  741. u_texColor = bgfx::createUniform("u_texColor", bgfx::UniformType::Uniform1iv);
  742. bgfx::UniformHandle u_params = bgfx::createUniform("u_params", bgfx::UniformType::Uniform4fv);
  743. bgfx::UniformHandle u_ambient = bgfx::createUniform("u_ambient", bgfx::UniformType::Uniform3fv);
  744. bgfx::UniformHandle u_diffuse = bgfx::createUniform("u_diffuse", bgfx::UniformType::Uniform3fv);
  745. bgfx::UniformHandle u_specular = bgfx::createUniform("u_specular_shininess", bgfx::UniformType::Uniform4fv);
  746. bgfx::UniformHandle u_color = bgfx::createUniform("u_color", bgfx::UniformType::Uniform4fv);
  747. bgfx::UniformHandle u_time = bgfx::createUniform("u_time", bgfx::UniformType::Uniform1f );
  748. bgfx::UniformHandle u_lightPosRadius = bgfx::createUniform("u_lightPosRadius", bgfx::UniformType::Uniform4fv, MAX_NUM_LIGHTS);
  749. bgfx::UniformHandle u_lightRgbInnerR = bgfx::createUniform("u_lightRgbInnerR", bgfx::UniformType::Uniform4fv, MAX_NUM_LIGHTS);
  750. bgfx::ProgramHandle programTextureLightning = loadProgram("vs_stencil_texture_lightning", "fs_stencil_texture_lightning");
  751. bgfx::ProgramHandle programColorLightning = loadProgram("vs_stencil_color_lightning", "fs_stencil_color_lightning" );
  752. bgfx::ProgramHandle programColorTexture = loadProgram("vs_stencil_color_texture", "fs_stencil_color_texture" );
  753. bgfx::ProgramHandle programColorBlack = loadProgram("vs_stencil_color", "fs_stencil_color_black" );
  754. bgfx::ProgramHandle programTexture = loadProgram("vs_stencil_texture", "fs_stencil_texture" );
  755. Mesh bunnyMesh;
  756. Mesh columnMesh;
  757. Mesh cubeMesh;
  758. Mesh hplaneMesh;
  759. Mesh vplaneMesh;
  760. bunnyMesh.load("meshes/bunny.bin");
  761. columnMesh.load("meshes/column.bin");
  762. cubeMesh.load(s_cubeVertices, s_numCubeVertices, PosNormalTexcoordDecl, s_cubeIndices, s_numCubeIndices);
  763. hplaneMesh.load(s_hplaneVertices, s_numHPlaneVertices, PosNormalTexcoordDecl, s_planeIndices, s_numPlaneIndices);
  764. vplaneMesh.load(s_vplaneVertices, s_numVPlaneVertices, PosNormalTexcoordDecl, s_planeIndices, s_numPlaneIndices);
  765. const bgfx::Memory* mem;
  766. mem = loadTexture("figure-rgba.dds");
  767. bgfx::TextureHandle figureTex = bgfx::createTexture(mem);
  768. mem = loadTexture("flare.dds");
  769. bgfx::TextureHandle flareTex = bgfx::createTexture(mem);
  770. mem = loadTexture("fieldstone-rgba.dds");
  771. bgfx::TextureHandle fieldstoneTex = bgfx::createTexture(mem);
  772. //setup lights
  773. uint8_t numLights = 5;
  774. const float rgbInnerR[5][4] =
  775. {
  776. { 1.0f, 0.7f, 0.2f, 0.0f }, //yellow
  777. { 0.7f, 0.2f, 1.0f, 0.0f }, //purple
  778. { 0.2f, 1.0f, 0.7f, 0.0f }, //cyan
  779. { 1.0f, 0.4f, 0.2f, 0.0f }, //orange
  780. { 0.7f, 0.7f, 0.7f, 0.0f }, //white
  781. };
  782. float lightRgbInnerR[MAX_NUM_LIGHTS][4];
  783. for (uint8_t ii = 0, jj = 0; ii < numLights; ++ii, ++jj)
  784. {
  785. const uint8_t index = jj%numLights;
  786. lightRgbInnerR[ii][0] = rgbInnerR[index][0];
  787. lightRgbInnerR[ii][1] = rgbInnerR[index][1];
  788. lightRgbInnerR[ii][2] = rgbInnerR[index][2];
  789. lightRgbInnerR[ii][3] = rgbInnerR[index][3];
  790. }
  791. bgfx::setUniform(u_lightRgbInnerR, lightRgbInnerR, numLights);
  792. //init uniforms
  793. params.m_ambientPass = 1.0f;
  794. params.m_lightningPass = 1.0f;
  795. params.m_lightCount = 4.0f;
  796. params.m_alpha = 1.0f;
  797. bgfx::setUniform(u_params, (const void*)&params);
  798. float color[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
  799. bgfx::setUniform(u_color, color);
  800. float ambient[3] = { 0.02f, 0.02f, 0.02f };
  801. float diffuse[3] = { 0.8f, 0.8f, 0.8f };
  802. float specular[4] = { 1.0f, 1.0f, 1.0f, 5.0f };
  803. bgfx::setUniform(u_ambient, ambient);
  804. bgfx::setUniform(u_diffuse, diffuse);
  805. bgfx::setUniform(u_specular, specular);
  806. int64_t timeOffset = bx::getHPCounter();
  807. enum Scene
  808. {
  809. StencilReflectionScene = 0,
  810. ProjectionShadowsScene,
  811. };
  812. Scene scene = StencilReflectionScene;
  813. float settings_numLights = 4.0f;
  814. float settings_reflectionValue = 0.8f;
  815. bool settings_updateLights = true;
  816. bool settings_updateScene = true;
  817. static const char* titles[3] =
  818. {
  819. "Stencil Reflection Scene",
  820. "Projection Shadows Scene",
  821. };
  822. entry::MouseState mouseState;
  823. while (!entry::processEvents(viewState.m_width, viewState.m_height, debug, reset, &mouseState) )
  824. {
  825. imguiBeginFrame(mouseState.m_mx
  826. , mouseState.m_my
  827. , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  828. | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  829. , 0
  830. , viewState.m_width
  831. , viewState.m_height
  832. );
  833. static int32_t scrollArea = 0;
  834. imguiBeginScrollArea("Settings", viewState.m_width - 256 - 10, 10, 256, 215, &scrollArea);
  835. if (imguiCheck(titles[StencilReflectionScene], StencilReflectionScene == scene) )
  836. {
  837. scene = StencilReflectionScene;
  838. settings_numLights = 4.0f;
  839. }
  840. if (imguiCheck(titles[ProjectionShadowsScene], ProjectionShadowsScene == scene) )
  841. {
  842. scene = ProjectionShadowsScene;
  843. settings_numLights = 1.0f;
  844. }
  845. imguiSeparatorLine();
  846. imguiSlider("Lights", &settings_numLights, 1.0f, 5.0f, 1.0f);
  847. if (scene == StencilReflectionScene)
  848. {
  849. imguiSlider("Reflection value", &settings_reflectionValue, 0.0f, 1.0f, 0.01f);
  850. }
  851. if (imguiCheck("Update lights", settings_updateLights) )
  852. {
  853. settings_updateLights = !settings_updateLights;
  854. }
  855. if (imguiCheck("Update scene", settings_updateScene) )
  856. {
  857. settings_updateScene = !settings_updateScene;
  858. }
  859. imguiEndScrollArea();
  860. imguiEndFrame();
  861. // Update settings.
  862. numLights = (uint8_t)settings_numLights;
  863. params.m_ambientPass = 1.0f;
  864. params.m_lightningPass = 1.0f;
  865. params.m_lightCount = settings_numLights;
  866. bgfx::setUniform(u_params, (const void*)&params);
  867. // Time.
  868. int64_t now = bx::getHPCounter();
  869. static int64_t last = now;
  870. const int64_t frameTime = now - last;
  871. last = now;
  872. const double freq = double(bx::getHPFrequency() );
  873. const double toMs = 1000.0/freq;
  874. float time = (float)( (now - timeOffset)/double(bx::getHPFrequency() ) );
  875. const float deltaTime = float(frameTime/freq);
  876. bgfx::setUniform(u_time, &time);
  877. // Use debug font to print information about this example.
  878. bgfx::dbgTextClear();
  879. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/13-stencil");
  880. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Stencil reflections and shadows.");
  881. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  882. // Set view and projection matrices.
  883. const float aspect = float(viewState.m_width)/float(viewState.m_height);
  884. mtxProj(viewState.m_proj, 60.0f, aspect, 0.1f, 100.0f);
  885. float at[3] = { 0.0f, 5.0f, 0.0f };
  886. float eye[3] = { 0.0f, 18.0f, -40.0f };
  887. mtxLookAt(viewState.m_view, eye, at);
  888. static float lightTimeAccumulator = 0.0f;
  889. if (settings_updateLights)
  890. {
  891. lightTimeAccumulator += deltaTime;
  892. }
  893. static float sceneTimeAccumulator = 0.0f;
  894. if (settings_updateScene)
  895. {
  896. sceneTimeAccumulator += deltaTime;
  897. }
  898. float lightPosRadius[MAX_NUM_LIGHTS][4];
  899. const float radius = (scene == StencilReflectionScene) ? 0.0f : 60.0f;
  900. for (uint8_t ii = 0; ii < numLights; ++ii)
  901. {
  902. lightPosRadius[ii][0] = sin( (lightTimeAccumulator*(1.1f + ii*0.07f) + float(ii*M_PI_2)*1.07f ) )*25.0f;
  903. lightPosRadius[ii][1] = 7.0f + (1.0f - cos( (lightTimeAccumulator*(1.2f + ii*0.29f) + float(ii*M_PI_2)*1.49f ) ))*5.0f;
  904. lightPosRadius[ii][2] = cos( (lightTimeAccumulator*(1.3f + ii*0.09f) + float(ii*M_PI_2)*1.79f ) )*20.0f;
  905. lightPosRadius[ii][3] = radius;
  906. }
  907. bgfx::setUniform(u_lightPosRadius, lightPosRadius, numLights);
  908. // Floor position.
  909. float floorMtx[16];
  910. mtxScaleRotateTranslate(floorMtx
  911. , 20.0f //scaleX
  912. , 20.0f //scaleY
  913. , 20.0f //scaleZ
  914. , 0.0f //rotX
  915. , 0.0f //rotY
  916. , 0.0f //rotZ
  917. , 0.0f //translateX
  918. , 0.0f //translateY
  919. , 0.0f //translateZ
  920. );
  921. // Bunny position.
  922. float bunnyMtx[16];
  923. mtxScaleRotateTranslate(bunnyMtx
  924. , 5.0f
  925. , 5.0f
  926. , 5.0f
  927. , 0.0f
  928. , 1.56f + sceneTimeAccumulator
  929. , 0.0f
  930. , 0.0f
  931. , 2.0f
  932. , 0.0f
  933. );
  934. // Columns position.
  935. const float dist = 14.0f;
  936. const float columnPositions[4][3] =
  937. {
  938. { dist, 0.0f, dist },
  939. { -dist, 0.0f, dist },
  940. { dist, 0.0f, -dist },
  941. { -dist, 0.0f, -dist },
  942. };
  943. float columnMtx[4][16];
  944. for (uint8_t ii = 0; ii < 4; ++ii)
  945. {
  946. mtxScaleRotateTranslate(columnMtx[ii]
  947. , 1.0f
  948. , 1.0f
  949. , 1.0f
  950. , 0.0f
  951. , 0.0f
  952. , 0.0f
  953. , columnPositions[ii][0]
  954. , columnPositions[ii][1]
  955. , columnPositions[ii][2]
  956. );
  957. }
  958. const uint8_t numCubes = 9;
  959. float cubeMtx[numCubes][16];
  960. for (uint16_t ii = 0; ii < numCubes; ++ii)
  961. {
  962. mtxScaleRotateTranslate(cubeMtx[ii]
  963. , 1.0f
  964. , 1.0f
  965. , 1.0f
  966. , 0.0f
  967. , 0.0f
  968. , 0.0f
  969. , sin(ii * 2.0f + 13.0f + sceneTimeAccumulator) * 13.0f
  970. , 4.0f
  971. , cos(ii * 2.0f + 13.0f + sceneTimeAccumulator) * 13.0f
  972. );
  973. }
  974. // Make sure at the beginning everything gets cleared.
  975. clearView(0, BGFX_CLEAR_COLOR_BIT | BGFX_CLEAR_DEPTH_BIT | BGFX_CLEAR_STENCIL_BIT, clearValues);
  976. submit(0);
  977. // White bunny and columns.
  978. color[0] = 1.0f;
  979. color[1] = 1.0f;
  980. color[2] = 1.0f;
  981. bgfx::setUniform(u_color, color);
  982. switch (scene)
  983. {
  984. case StencilReflectionScene:
  985. {
  986. // First pass - Draw plane.
  987. // Floor
  988. hplaneMesh.submit(RENDER_VIEWID_RANGE1_PASS_0
  989. , floorMtx
  990. , programColorBlack
  991. , s_renderStates[RenderState::StencilReflection_CraftStencil]
  992. );
  993. // Second pass - Draw reflected objects.
  994. // Compute reflected matrix.
  995. float reflectMtx[16];
  996. float plane_pos[3] = { 0.0f, 0.01f, 0.0f };
  997. float normal[3] = { 0.0f, 1.0f, 0.0f };
  998. mtxReflected(reflectMtx, plane_pos, normal);
  999. // Reflect lights.
  1000. float reflectedLights[MAX_NUM_LIGHTS][4];
  1001. for (uint8_t ii = 0; ii < numLights; ++ii)
  1002. {
  1003. vec3MulMtx(reflectedLights[ii], lightPosRadius[ii], reflectMtx);
  1004. reflectedLights[ii][3] = lightPosRadius[ii][3];
  1005. }
  1006. bgfx::setUniform(u_lightPosRadius, reflectedLights, numLights);
  1007. // Reflect and submit bunny.
  1008. float mtxReflectedBunny[16];
  1009. mtxMul(mtxReflectedBunny, bunnyMtx, reflectMtx);
  1010. bunnyMesh.submit(RENDER_VIEWID_RANGE1_PASS_1
  1011. , mtxReflectedBunny
  1012. , programColorLightning
  1013. , s_renderStates[RenderState::StencilReflection_DrawReflected]
  1014. );
  1015. // Reflect and submit columns.
  1016. float mtxReflectedColumn[16];
  1017. for (uint8_t ii = 0; ii < 4; ++ii)
  1018. {
  1019. mtxMul(mtxReflectedColumn, columnMtx[ii], reflectMtx);
  1020. columnMesh.submit(RENDER_VIEWID_RANGE1_PASS_1
  1021. , mtxReflectedColumn
  1022. , programColorLightning
  1023. , s_renderStates[RenderState::StencilReflection_DrawReflected]
  1024. );
  1025. }
  1026. // Set lights back.
  1027. bgfx::setUniform(u_lightPosRadius, lightPosRadius, numLights);
  1028. // Third pass - Darken reflected objects.
  1029. uint8_t val = uint8_t(settings_reflectionValue * UINT8_MAX);
  1030. uint32_t factor = (val << 24)
  1031. | (val << 16)
  1032. | (val << 8 )
  1033. | (val << 0 )
  1034. ;
  1035. s_renderStates[RenderState::StencilReflection_DarkenReflections].m_blendFactorRgba = factor;
  1036. // Floor.
  1037. hplaneMesh.submit(RENDER_VIEWID_RANGE1_PASS_2
  1038. , floorMtx
  1039. , programColorBlack
  1040. , s_renderStates[RenderState::StencilReflection_DarkenReflections]
  1041. );
  1042. // Fourth pass - Draw plane. (blend plane with what's behind it)
  1043. // Floor.
  1044. hplaneMesh.submit(RENDER_VIEWID_RANGE1_PASS_3
  1045. , floorMtx
  1046. , programTextureLightning
  1047. , s_renderStates[RenderState::StencilReflection_BlendPlane]
  1048. , fieldstoneTex
  1049. );
  1050. // Fifth pass - Draw everything else but the plane.
  1051. // Bunny.
  1052. bunnyMesh.submit(RENDER_VIEWID_RANGE1_PASS_4
  1053. , bunnyMtx
  1054. , programColorLightning
  1055. , s_renderStates[RenderState::StencilReflection_DrawScene]
  1056. );
  1057. // Columns.
  1058. for (uint8_t ii = 0; ii < 4; ++ii)
  1059. {
  1060. columnMesh.submit(RENDER_VIEWID_RANGE1_PASS_4
  1061. , columnMtx[ii]
  1062. , programColorLightning
  1063. , s_renderStates[RenderState::StencilReflection_DrawScene]
  1064. );
  1065. }
  1066. }
  1067. break;
  1068. case ProjectionShadowsScene:
  1069. {
  1070. // First pass - Draw entire scene. (ambient only).
  1071. params.m_ambientPass = 1.0f;
  1072. params.m_lightningPass = 1.0f;
  1073. bgfx::setUniform(u_params, (const void*)&params);
  1074. // Bunny.
  1075. bunnyMesh.submit(RENDER_VIEWID_RANGE1_PASS_0
  1076. , bunnyMtx
  1077. , programColorLightning
  1078. , s_renderStates[RenderState::ProjectionShadows_DrawAmbient]
  1079. );
  1080. // Floor.
  1081. hplaneMesh.submit(RENDER_VIEWID_RANGE1_PASS_0
  1082. , floorMtx
  1083. , programTextureLightning
  1084. , s_renderStates[RenderState::ProjectionShadows_DrawAmbient]
  1085. , fieldstoneTex
  1086. );
  1087. // Cubes.
  1088. for (uint8_t ii = 0; ii < numCubes; ++ii)
  1089. {
  1090. cubeMesh.submit(RENDER_VIEWID_RANGE1_PASS_0
  1091. , cubeMtx[ii]
  1092. , programTextureLightning
  1093. , s_renderStates[RenderState::ProjectionShadows_DrawAmbient]
  1094. , figureTex
  1095. );
  1096. }
  1097. // Ground plane.
  1098. float ground[4];
  1099. float plane_pos[3] = { 0.0f, 0.0f, 0.0f };
  1100. float normal[3] = { 0.0f, 1.0f, 0.0f };
  1101. memcpy(ground, normal, sizeof(float) * 3);
  1102. ground[3] = -vec3Dot(plane_pos, normal) - 0.01f; // - 0.01 against z-fighting
  1103. for (uint8_t ii = 0; ii < numLights; ++ii)
  1104. {
  1105. uint8_t viewId = RENDER_VIEWID_RANGE5_PASS_6+ii;
  1106. clearView(viewId, BGFX_CLEAR_STENCIL_BIT, clearValues);
  1107. // Draw shadow projection of scene objects.
  1108. // Get homogeneous light pos.
  1109. float* lightPos = lightPosRadius[ii];
  1110. float pos[4];
  1111. memcpy(pos, lightPos, sizeof(float) * 3);
  1112. pos[3] = 1.0f;
  1113. // Calculate shadow mtx for current light.
  1114. float shadowMtx[16];
  1115. mtxShadow(shadowMtx, ground, pos);
  1116. // Submit bunny's shadow.
  1117. float mtxShadowedBunny[16];
  1118. mtxMul(mtxShadowedBunny, bunnyMtx, shadowMtx);
  1119. bunnyMesh.submit(viewId
  1120. , mtxShadowedBunny
  1121. , programColorLightning
  1122. , s_renderStates[RenderState::ProjectionShadows_CraftStencil]
  1123. );
  1124. // Submit cube shadows.
  1125. float mtxShadowedCube[16];
  1126. for (uint8_t ii = 0; ii < numCubes; ++ii)
  1127. {
  1128. mtxMul(mtxShadowedCube, cubeMtx[ii], shadowMtx);
  1129. cubeMesh.submit(viewId
  1130. , mtxShadowedCube
  1131. , programTextureLightning
  1132. , s_renderStates[RenderState::ProjectionShadows_CraftStencil]
  1133. );
  1134. }
  1135. // Draw entire scene. (lightning pass only. blending is on)
  1136. params.m_ambientPass = 0.0f;
  1137. params.m_lightningPass = 1.0f;
  1138. bgfx::setUniform(u_params, (const void*)&params);
  1139. // Set blending factor based on number of lights.
  1140. uint32_t factor = 0xff / (numLights < 1 ? 1 : numLights);
  1141. factor = (factor << 24)
  1142. | (factor << 16)
  1143. | (factor << 8 )
  1144. | (factor << 0 )
  1145. ;
  1146. s_renderStates[RenderState::ProjectionShadows_DrawDiffuse].m_blendFactorRgba = factor;
  1147. // Bunny.
  1148. bunnyMesh.submit(viewId
  1149. , bunnyMtx
  1150. , programColorLightning
  1151. , s_renderStates[RenderState::ProjectionShadows_DrawDiffuse]
  1152. );
  1153. // Floor.
  1154. hplaneMesh.submit(viewId
  1155. , floorMtx
  1156. , programTextureLightning
  1157. , s_renderStates[RenderState::ProjectionShadows_DrawDiffuse]
  1158. , fieldstoneTex
  1159. );
  1160. // Cubes.
  1161. for (uint8_t ii = 0; ii < numCubes; ++ii)
  1162. {
  1163. cubeMesh.submit(viewId
  1164. , cubeMtx[ii]
  1165. , programTextureLightning
  1166. , s_renderStates[RenderState::ProjectionShadows_DrawDiffuse]
  1167. , figureTex
  1168. );
  1169. }
  1170. }
  1171. // Reset these to default..
  1172. params.m_ambientPass = 1.0f;
  1173. params.m_lightningPass = 1.0f;
  1174. bgfx::setUniform(u_params, (const void*)&params);
  1175. }
  1176. break;
  1177. };
  1178. //lights
  1179. const float lightScale[3] = { 1.5f, 1.5f, 1.5f };
  1180. float lightMtx[16];
  1181. for (uint8_t ii = 0; ii < numLights; ++ii)
  1182. {
  1183. color[0] = lightRgbInnerR[ii][0];
  1184. color[1] = lightRgbInnerR[ii][1];
  1185. color[2] = lightRgbInnerR[ii][2];
  1186. bgfx::setUniform(u_color, color);
  1187. mtxBillboard(lightMtx, viewState.m_view, lightPosRadius[ii], lightScale);
  1188. vplaneMesh.submit(RENDER_VIEWID_RANGE1_PASS_7
  1189. , lightMtx
  1190. , programColorTexture
  1191. , s_renderStates[RenderState::Custom_BlendLightTexture]
  1192. , flareTex
  1193. );
  1194. }
  1195. // Draw floor bottom.
  1196. float floorBottomMtx[16];
  1197. mtxScaleRotateTranslate(floorBottomMtx
  1198. , 20.0f //scaleX
  1199. , 20.0f //scaleY
  1200. , 20.0f //scaleZ
  1201. , 0.0f //rotX
  1202. , 0.0f //rotY
  1203. , 0.0f //rotZ
  1204. , 0.0f //translateX
  1205. , -0.1f //translateY
  1206. , 0.0f //translateZ
  1207. );
  1208. hplaneMesh.submit(RENDER_VIEWID_RANGE1_PASS_7
  1209. , floorBottomMtx
  1210. , programTexture
  1211. , s_renderStates[RenderState::Custom_DrawPlaneBottom]
  1212. , figureTex
  1213. );
  1214. // Setup view rect and transform for all used views.
  1215. setViewRectTransformMask(s_viewMask, viewState);
  1216. s_viewMask = 0;
  1217. // Advance to next frame. Rendering thread will be kicked to
  1218. // process submitted rendering primitives.
  1219. bgfx::frame();
  1220. //reset clear values on used views
  1221. clearViewMask(s_clearMask, BGFX_CLEAR_NONE, clearValues);
  1222. s_clearMask = 0;
  1223. }
  1224. // Cleanup.
  1225. bunnyMesh.unload();
  1226. columnMesh.unload();
  1227. cubeMesh.unload();
  1228. hplaneMesh.unload();
  1229. vplaneMesh.unload();
  1230. bgfx::destroyTexture(figureTex);
  1231. bgfx::destroyTexture(fieldstoneTex);
  1232. bgfx::destroyTexture(flareTex);
  1233. bgfx::destroyProgram(programTextureLightning);
  1234. bgfx::destroyProgram(programColorLightning);
  1235. bgfx::destroyProgram(programColorTexture);
  1236. bgfx::destroyProgram(programColorBlack);
  1237. bgfx::destroyProgram(programTexture);
  1238. bgfx::destroyUniform(u_params);
  1239. bgfx::destroyUniform(u_ambient);
  1240. bgfx::destroyUniform(u_diffuse);
  1241. bgfx::destroyUniform(u_specular);
  1242. bgfx::destroyUniform(u_color);
  1243. bgfx::destroyUniform(u_time);
  1244. bgfx::destroyUniform(u_lightPosRadius);
  1245. bgfx::destroyUniform(u_lightRgbInnerR);
  1246. bgfx::destroyUniform(u_texColor);
  1247. imguiDestroy();
  1248. // Shutdown bgfx.
  1249. bgfx::shutdown();
  1250. return 0;
  1251. }