stencil.cpp 40 KB

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