stencil.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. /*
  2. * Copyright 2013-2014 Dario Manesku. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include <string>
  6. #include <vector>
  7. #include "common.h"
  8. #include "bgfx_utils.h"
  9. #include <bx/file.h>
  10. #include "camera.h"
  11. #include "imgui/imgui.h"
  12. namespace bgfx
  13. {
  14. int32_t read(bx::ReaderI* _reader, bgfx::VertexDecl& _decl, bx::Error* _err = NULL);
  15. }
  16. namespace
  17. {
  18. #define RENDER_VIEWID_RANGE1_PASS_0 1
  19. #define RENDER_VIEWID_RANGE1_PASS_1 2
  20. #define RENDER_VIEWID_RANGE1_PASS_2 3
  21. #define RENDER_VIEWID_RANGE1_PASS_3 4
  22. #define RENDER_VIEWID_RANGE1_PASS_4 5
  23. #define RENDER_VIEWID_RANGE1_PASS_5 6
  24. #define RENDER_VIEWID_RANGE5_PASS_6 7
  25. #define RENDER_VIEWID_RANGE1_PASS_7 13
  26. #define MAX_NUM_LIGHTS 5
  27. struct PosNormalTexcoordVertex
  28. {
  29. float m_x;
  30. float m_y;
  31. float m_z;
  32. uint32_t m_normal;
  33. float m_u;
  34. float m_v;
  35. static void init()
  36. {
  37. ms_decl
  38. .begin()
  39. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  40. .add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true)
  41. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  42. .end();
  43. }
  44. static bgfx::VertexDecl ms_decl;
  45. };
  46. bgfx::VertexDecl PosNormalTexcoordVertex::ms_decl;
  47. static const float s_texcoord = 5.0f;
  48. static PosNormalTexcoordVertex s_hplaneVertices[] =
  49. {
  50. { -1.0f, 0.0f, 1.0f, encodeNormalRgba8(0.0f, 1.0f, 0.0f), s_texcoord, s_texcoord },
  51. { 1.0f, 0.0f, 1.0f, encodeNormalRgba8(0.0f, 1.0f, 0.0f), s_texcoord, 0.0f },
  52. { -1.0f, 0.0f, -1.0f, encodeNormalRgba8(0.0f, 1.0f, 0.0f), 0.0f, s_texcoord },
  53. { 1.0f, 0.0f, -1.0f, encodeNormalRgba8(0.0f, 1.0f, 0.0f), 0.0f, 0.0f },
  54. };
  55. static PosNormalTexcoordVertex s_vplaneVertices[] =
  56. {
  57. { -1.0f, 1.0f, 0.0f, encodeNormalRgba8(0.0f, 0.0f, -1.0f), 1.0f, 1.0f },
  58. { 1.0f, 1.0f, 0.0f, encodeNormalRgba8(0.0f, 0.0f, -1.0f), 1.0f, 0.0f },
  59. { -1.0f, -1.0f, 0.0f, encodeNormalRgba8(0.0f, 0.0f, -1.0f), 0.0f, 1.0f },
  60. { 1.0f, -1.0f, 0.0f, encodeNormalRgba8(0.0f, 0.0f, -1.0f), 0.0f, 0.0f },
  61. };
  62. static const PosNormalTexcoordVertex s_cubeVertices[] =
  63. {
  64. { -1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 1.0f, 1.0f },
  65. { 1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0.0f, 1.0f },
  66. { -1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 1.0f, 0.0f },
  67. { 1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 1.0f, 0.0f), 0.0f, 0.0f },
  68. { -1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 1.0f, 1.0f },
  69. { 1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0.0f, 1.0f },
  70. { -1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 1.0f, 0.0f },
  71. { 1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, -1.0f, 0.0f), 0.0f, 0.0f },
  72. { 1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0.0f, 0.0f },
  73. { 1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 0.0f, 1.0f },
  74. { -1.0f, -1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 1.0f, 0.0f },
  75. { -1.0f, 1.0f, 1.0f, encodeNormalRgba8( 0.0f, 0.0f, 1.0f), 1.0f, 1.0f },
  76. { 1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0.0f, 0.0f },
  77. { 1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 0.0f, 1.0f },
  78. { -1.0f, -1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 1.0f, 0.0f },
  79. { -1.0f, 1.0f, -1.0f, encodeNormalRgba8( 0.0f, 0.0f, -1.0f), 1.0f, 1.0f },
  80. { 1.0f, 1.0f, -1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 1.0f, 1.0f },
  81. { 1.0f, 1.0f, 1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0.0f, 1.0f },
  82. { 1.0f, -1.0f, -1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 1.0f, 0.0f },
  83. { 1.0f, -1.0f, 1.0f, encodeNormalRgba8( 1.0f, 0.0f, 0.0f), 0.0f, 0.0f },
  84. { -1.0f, 1.0f, -1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 1.0f, 1.0f },
  85. { -1.0f, 1.0f, 1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0.0f, 1.0f },
  86. { -1.0f, -1.0f, -1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 1.0f, 0.0f },
  87. { -1.0f, -1.0f, 1.0f, encodeNormalRgba8(-1.0f, 0.0f, 0.0f), 0.0f, 0.0f },
  88. };
  89. static const uint16_t s_cubeIndices[] =
  90. {
  91. 0, 1, 2,
  92. 1, 3, 2,
  93. 4, 6, 5,
  94. 5, 6, 7,
  95. 8, 9, 10,
  96. 9, 11, 10,
  97. 12, 14, 13,
  98. 13, 14, 15,
  99. 16, 17, 18,
  100. 17, 19, 18,
  101. 20, 22, 21,
  102. 21, 22, 23,
  103. };
  104. static const uint16_t s_planeIndices[] =
  105. {
  106. 0, 1, 2,
  107. 1, 3, 2,
  108. };
  109. static uint32_t s_viewMask = 0;
  110. static uint32_t s_clearMask = 0;
  111. static bgfx::UniformHandle s_texColor;
  112. void setViewClearMask(uint32_t _viewMask, uint8_t _flags, uint32_t _rgba, float _depth, uint8_t _stencil)
  113. {
  114. for (uint32_t view = 0, viewMask = _viewMask, ntz = bx::uint32_cnttz(_viewMask); 0 != viewMask; viewMask >>= 1, view += 1, ntz = bx::uint32_cnttz(viewMask) )
  115. {
  116. viewMask >>= ntz;
  117. view += ntz;
  118. bgfx::setViewClear( (uint8_t)view, _flags, _rgba, _depth, _stencil);
  119. }
  120. }
  121. void setViewTransformMask(uint32_t _viewMask, const void* _view, const void* _proj)
  122. {
  123. for (uint32_t view = 0, viewMask = _viewMask, ntz = bx::uint32_cnttz(_viewMask); 0 != viewMask; viewMask >>= 1, view += 1, ntz = bx::uint32_cnttz(viewMask) )
  124. {
  125. viewMask >>= ntz;
  126. view += ntz;
  127. bgfx::setViewTransform( (uint8_t)view, _view, _proj);
  128. }
  129. }
  130. void setViewRectMask(uint32_t _viewMask, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height)
  131. {
  132. for (uint32_t view = 0, viewMask = _viewMask, ntz = bx::uint32_cnttz(_viewMask); 0 != viewMask; viewMask >>= 1, view += 1, ntz = bx::uint32_cnttz(viewMask) )
  133. {
  134. viewMask >>= ntz;
  135. view += ntz;
  136. bgfx::setViewRect( (uint8_t)view, _x, _y, _width, _height);
  137. }
  138. }
  139. void mtxReflected(float*__restrict _result
  140. , const float* __restrict _p /* plane */
  141. , const float* __restrict _n /* normal */
  142. )
  143. {
  144. float dot = bx::vec3Dot(_p, _n);
  145. _result[ 0] = 1.0f - 2.0f * _n[0] * _n[0]; //1-2Nx^2
  146. _result[ 1] = -2.0f * _n[0] * _n[1]; //-2*Nx*Ny
  147. _result[ 2] = -2.0f * _n[0] * _n[2]; //-2*NxNz
  148. _result[ 3] = 0.0f; //0
  149. _result[ 4] = -2.0f * _n[0] * _n[1]; //-2*NxNy
  150. _result[ 5] = 1.0f - 2.0f * _n[1] * _n[1]; //1-2*Ny^2
  151. _result[ 6] = -2.0f * _n[1] * _n[2]; //-2*NyNz
  152. _result[ 7] = 0.0f; //0
  153. _result[ 8] = -2.0f * _n[0] * _n[2]; //-2*NxNz
  154. _result[ 9] = -2.0f * _n[1] * _n[2]; //-2NyNz
  155. _result[10] = 1.0f - 2.0f * _n[2] * _n[2]; //1-2*Nz^2
  156. _result[11] = 0.0f; //0
  157. _result[12] = 2.0f * dot * _n[0]; //2*dot*Nx
  158. _result[13] = 2.0f * dot * _n[1]; //2*dot*Ny
  159. _result[14] = 2.0f * dot * _n[2]; //2*dot*Nz
  160. _result[15] = 1.0f; //1
  161. }
  162. void mtxShadow(float* __restrict _result
  163. , const float* __restrict _ground
  164. , const float* __restrict _light
  165. )
  166. {
  167. float dot = _ground[0] * _light[0]
  168. + _ground[1] * _light[1]
  169. + _ground[2] * _light[2]
  170. + _ground[3] * _light[3]
  171. ;
  172. _result[ 0] = dot - _light[0] * _ground[0];
  173. _result[ 1] = 0.0f - _light[1] * _ground[0];
  174. _result[ 2] = 0.0f - _light[2] * _ground[0];
  175. _result[ 3] = 0.0f - _light[3] * _ground[0];
  176. _result[ 4] = 0.0f - _light[0] * _ground[1];
  177. _result[ 5] = dot - _light[1] * _ground[1];
  178. _result[ 6] = 0.0f - _light[2] * _ground[1];
  179. _result[ 7] = 0.0f - _light[3] * _ground[1];
  180. _result[ 8] = 0.0f - _light[0] * _ground[2];
  181. _result[ 9] = 0.0f - _light[1] * _ground[2];
  182. _result[10] = dot - _light[2] * _ground[2];
  183. _result[11] = 0.0f - _light[3] * _ground[2];
  184. _result[12] = 0.0f - _light[0] * _ground[3];
  185. _result[13] = 0.0f - _light[1] * _ground[3];
  186. _result[14] = 0.0f - _light[2] * _ground[3];
  187. _result[15] = dot - _light[3] * _ground[3];
  188. }
  189. void mtxBillboard(float* __restrict _result
  190. , const float* __restrict _view
  191. , const float* __restrict _pos
  192. , const float* __restrict _scale)
  193. {
  194. _result[ 0] = _view[0] * _scale[0];
  195. _result[ 1] = _view[4] * _scale[0];
  196. _result[ 2] = _view[8] * _scale[0];
  197. _result[ 3] = 0.0f;
  198. _result[ 4] = _view[1] * _scale[1];
  199. _result[ 5] = _view[5] * _scale[1];
  200. _result[ 6] = _view[9] * _scale[1];
  201. _result[ 7] = 0.0f;
  202. _result[ 8] = _view[2] * _scale[2];
  203. _result[ 9] = _view[6] * _scale[2];
  204. _result[10] = _view[10] * _scale[2];
  205. _result[11] = 0.0f;
  206. _result[12] = _pos[0];
  207. _result[13] = _pos[1];
  208. _result[14] = _pos[2];
  209. _result[15] = 1.0f;
  210. }
  211. struct Uniforms
  212. {
  213. void init()
  214. {
  215. m_params.m_ambientPass = 1.0f;
  216. m_params.m_lightingPass = 1.0f;
  217. m_params.m_lightCount = 4.0f;
  218. m_params.m_lightIndex = 4.0f;
  219. m_ambient[0] = 0.02f;
  220. m_ambient[1] = 0.02f;
  221. m_ambient[2] = 0.02f;
  222. m_ambient[3] = 0.0f; //unused
  223. m_diffuse[0] = 0.2f;
  224. m_diffuse[1] = 0.2f;
  225. m_diffuse[2] = 0.2f;
  226. m_diffuse[3] = 0.0f; //unused
  227. m_specular_shininess[0] = 1.0f;
  228. m_specular_shininess[1] = 1.0f;
  229. m_specular_shininess[2] = 1.0f;
  230. m_specular_shininess[3] = 10.0f; //shininess
  231. m_color[0] = 1.0f;
  232. m_color[1] = 1.0f;
  233. m_color[2] = 1.0f;
  234. m_color[3] = 1.0f;
  235. m_time = 0.0f;
  236. for (uint8_t ii = 0; ii < MAX_NUM_LIGHTS; ++ii)
  237. {
  238. m_lightPosRadius[ii][0] = 0.0f;
  239. m_lightPosRadius[ii][1] = 0.0f;
  240. m_lightPosRadius[ii][2] = 0.0f;
  241. m_lightPosRadius[ii][3] = 1.0f;
  242. m_lightRgbInnerR[ii][0] = 1.0f;
  243. m_lightRgbInnerR[ii][1] = 1.0f;
  244. m_lightRgbInnerR[ii][2] = 1.0f;
  245. m_lightRgbInnerR[ii][3] = 1.0f;
  246. }
  247. u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4);
  248. u_ambient = bgfx::createUniform("u_ambient", bgfx::UniformType::Vec4);
  249. u_diffuse = bgfx::createUniform("u_diffuse", bgfx::UniformType::Vec4);
  250. u_specular_shininess = bgfx::createUniform("u_specular_shininess", bgfx::UniformType::Vec4);
  251. u_color = bgfx::createUniform("u_color", bgfx::UniformType::Vec4);
  252. u_lightPosRadius = bgfx::createUniform("u_lightPosRadius", bgfx::UniformType::Vec4, MAX_NUM_LIGHTS);
  253. u_lightRgbInnerR = bgfx::createUniform("u_lightRgbInnerR", bgfx::UniformType::Vec4, MAX_NUM_LIGHTS);
  254. }
  255. //call this once at initialization
  256. void submitConstUniforms()
  257. {
  258. bgfx::setUniform(u_ambient, &m_ambient);
  259. bgfx::setUniform(u_diffuse, &m_diffuse);
  260. bgfx::setUniform(u_specular_shininess, &m_specular_shininess);
  261. }
  262. //call this before each draw call
  263. void submitPerDrawUniforms()
  264. {
  265. bgfx::setUniform(u_params, &m_params);
  266. bgfx::setUniform(u_color, &m_color);
  267. bgfx::setUniform(u_lightPosRadius, &m_lightPosRadius, MAX_NUM_LIGHTS);
  268. bgfx::setUniform(u_lightRgbInnerR, &m_lightRgbInnerR, MAX_NUM_LIGHTS);
  269. }
  270. void destroy()
  271. {
  272. bgfx::destroy(u_params);
  273. bgfx::destroy(u_ambient);
  274. bgfx::destroy(u_diffuse);
  275. bgfx::destroy(u_specular_shininess);
  276. bgfx::destroy(u_color);
  277. bgfx::destroy(u_lightPosRadius);
  278. bgfx::destroy(u_lightRgbInnerR);
  279. }
  280. struct Params
  281. {
  282. float m_ambientPass;
  283. float m_lightingPass;
  284. float m_lightCount;
  285. float m_lightIndex;
  286. };
  287. struct SvParams
  288. {
  289. float m_useStencilTex;
  290. float m_dfail;
  291. float m_unused0;
  292. float m_unused1;
  293. };
  294. Params m_params;
  295. SvParams m_svparams;
  296. float m_ambient[4];
  297. float m_diffuse[4];
  298. float m_specular_shininess[4];
  299. float m_color[4];
  300. float m_time;
  301. float m_lightPosRadius[MAX_NUM_LIGHTS][4];
  302. float m_lightRgbInnerR[MAX_NUM_LIGHTS][4];
  303. /**
  304. * u_params.x - u_ambientPass
  305. * u_params.y - u_lightingPass
  306. * u_params.z - u_lightCount
  307. * u_params.w - u_lightIndex
  308. */
  309. bgfx::UniformHandle u_params;
  310. bgfx::UniformHandle u_ambient;
  311. bgfx::UniformHandle u_diffuse;
  312. bgfx::UniformHandle u_specular_shininess;
  313. bgfx::UniformHandle u_color;
  314. bgfx::UniformHandle u_lightPosRadius;
  315. bgfx::UniformHandle u_lightRgbInnerR;
  316. };
  317. static Uniforms s_uniforms;
  318. //-------------------------------------------------
  319. // Render state
  320. //-------------------------------------------------
  321. struct RenderState
  322. {
  323. enum Enum
  324. {
  325. StencilReflection_CraftStencil = 0,
  326. StencilReflection_DrawReflected,
  327. StencilReflection_BlendPlane,
  328. StencilReflection_DrawScene,
  329. ProjectionShadows_DrawAmbient,
  330. ProjectionShadows_CraftStencil,
  331. ProjectionShadows_DrawDiffuse,
  332. Custom_BlendLightTexture,
  333. Custom_DrawPlaneBottom,
  334. Count
  335. };
  336. uint64_t m_state;
  337. uint32_t m_blendFactorRgba;
  338. uint32_t m_fstencil;
  339. uint32_t m_bstencil;
  340. };
  341. static RenderState s_renderStates[RenderState::Count] =
  342. {
  343. { // StencilReflection_CraftStencil
  344. BGFX_STATE_WRITE_RGB
  345. | BGFX_STATE_WRITE_Z
  346. | BGFX_STATE_DEPTH_TEST_LESS
  347. | BGFX_STATE_MSAA
  348. , UINT32_MAX
  349. , BGFX_STENCIL_TEST_ALWAYS // pass always
  350. | BGFX_STENCIL_FUNC_REF(1) // value = 1
  351. | BGFX_STENCIL_FUNC_RMASK(0xff)
  352. | BGFX_STENCIL_OP_FAIL_S_REPLACE
  353. | BGFX_STENCIL_OP_FAIL_Z_REPLACE
  354. | BGFX_STENCIL_OP_PASS_Z_REPLACE // store the value
  355. , BGFX_STENCIL_NONE
  356. },
  357. { // StencilReflection_DrawReflected
  358. BGFX_STATE_WRITE_RGB
  359. | BGFX_STATE_WRITE_A
  360. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  361. | BGFX_STATE_WRITE_Z
  362. | BGFX_STATE_DEPTH_TEST_LESS
  363. | BGFX_STATE_CULL_CW //reflection matrix has inverted normals. using CCW instead of CW.
  364. | BGFX_STATE_MSAA
  365. , UINT32_MAX
  366. , BGFX_STENCIL_TEST_EQUAL
  367. | BGFX_STENCIL_FUNC_REF(1)
  368. | BGFX_STENCIL_FUNC_RMASK(1)
  369. | BGFX_STENCIL_OP_FAIL_S_KEEP
  370. | BGFX_STENCIL_OP_FAIL_Z_KEEP
  371. | BGFX_STENCIL_OP_PASS_Z_KEEP
  372. , BGFX_STENCIL_NONE
  373. },
  374. { // StencilReflection_BlendPlane
  375. BGFX_STATE_WRITE_RGB
  376. | BGFX_STATE_WRITE_Z
  377. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_SRC_COLOR)
  378. | BGFX_STATE_DEPTH_TEST_LESS
  379. | BGFX_STATE_CULL_CCW
  380. | BGFX_STATE_MSAA
  381. , UINT32_MAX
  382. , BGFX_STENCIL_NONE
  383. , BGFX_STENCIL_NONE
  384. },
  385. { // StencilReflection_DrawScene
  386. BGFX_STATE_WRITE_RGB
  387. | BGFX_STATE_WRITE_Z
  388. | BGFX_STATE_DEPTH_TEST_LESS
  389. | BGFX_STATE_CULL_CCW
  390. | BGFX_STATE_MSAA
  391. , UINT32_MAX
  392. , BGFX_STENCIL_NONE
  393. , BGFX_STENCIL_NONE
  394. },
  395. { // ProjectionShadows_DrawAmbient
  396. BGFX_STATE_WRITE_RGB
  397. | BGFX_STATE_WRITE_Z // write depth !
  398. | BGFX_STATE_DEPTH_TEST_LESS
  399. | BGFX_STATE_CULL_CCW
  400. | BGFX_STATE_MSAA
  401. , UINT32_MAX
  402. , BGFX_STENCIL_NONE
  403. , BGFX_STENCIL_NONE
  404. },
  405. { // ProjectionShadows_CraftStencil
  406. BGFX_STATE_DEPTH_TEST_LESS
  407. | BGFX_STATE_MSAA
  408. , UINT32_MAX
  409. , BGFX_STENCIL_TEST_ALWAYS // pass always
  410. | BGFX_STENCIL_FUNC_REF(1) // value = 1
  411. | BGFX_STENCIL_FUNC_RMASK(0xff)
  412. | BGFX_STENCIL_OP_FAIL_S_KEEP
  413. | BGFX_STENCIL_OP_FAIL_Z_KEEP
  414. | BGFX_STENCIL_OP_PASS_Z_REPLACE // store the value
  415. , BGFX_STENCIL_NONE
  416. },
  417. { // ProjectionShadows_DrawDiffuse
  418. BGFX_STATE_WRITE_RGB
  419. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE)
  420. | BGFX_STATE_DEPTH_TEST_EQUAL
  421. | BGFX_STATE_CULL_CCW
  422. | BGFX_STATE_MSAA
  423. , UINT32_MAX
  424. , BGFX_STENCIL_TEST_NOTEQUAL
  425. | BGFX_STENCIL_FUNC_REF(1)
  426. | BGFX_STENCIL_FUNC_RMASK(1)
  427. | BGFX_STENCIL_OP_FAIL_S_KEEP
  428. | BGFX_STENCIL_OP_FAIL_Z_KEEP
  429. | BGFX_STENCIL_OP_PASS_Z_KEEP
  430. , BGFX_STENCIL_NONE
  431. },
  432. { // Custom_BlendLightTexture
  433. BGFX_STATE_WRITE_RGB
  434. | BGFX_STATE_WRITE_A
  435. | BGFX_STATE_WRITE_Z
  436. | BGFX_STATE_DEPTH_TEST_LESS
  437. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_COLOR, BGFX_STATE_BLEND_INV_SRC_COLOR)
  438. | BGFX_STATE_CULL_CCW
  439. | BGFX_STATE_MSAA
  440. , UINT32_MAX
  441. , BGFX_STENCIL_NONE
  442. , BGFX_STENCIL_NONE
  443. },
  444. { // Custom_DrawPlaneBottom
  445. BGFX_STATE_WRITE_RGB
  446. | BGFX_STATE_CULL_CW
  447. | BGFX_STATE_MSAA
  448. , UINT32_MAX
  449. , BGFX_STENCIL_NONE
  450. , BGFX_STENCIL_NONE
  451. },
  452. };
  453. struct ViewState
  454. {
  455. ViewState(uint32_t _width = 0, uint32_t _height = 0)
  456. : m_width(_width)
  457. , m_height(_height)
  458. {
  459. }
  460. uint32_t m_width;
  461. uint32_t m_height;
  462. float m_view[16];
  463. float m_proj[16];
  464. };
  465. struct ClearValues
  466. {
  467. ClearValues(uint32_t _clearRgba = 0x30303000
  468. , float _clearDepth = 1.0f
  469. , uint8_t _clearStencil = 0
  470. )
  471. : m_clearRgba(_clearRgba)
  472. , m_clearDepth(_clearDepth)
  473. , m_clearStencil(_clearStencil)
  474. { }
  475. uint32_t m_clearRgba;
  476. float m_clearDepth;
  477. uint8_t m_clearStencil;
  478. };
  479. void clearView(bgfx::ViewId _id, uint8_t _flags, const ClearValues& _clearValues)
  480. {
  481. bgfx::setViewClear(_id
  482. , _flags
  483. , _clearValues.m_clearRgba
  484. , _clearValues.m_clearDepth
  485. , _clearValues.m_clearStencil
  486. );
  487. // Keep track of cleared views
  488. s_clearMask |= 1 << _id;
  489. }
  490. void clearViewMask(uint32_t _viewMask, uint8_t _flags, const ClearValues& _clearValues)
  491. {
  492. setViewClearMask(_viewMask
  493. , _flags
  494. , _clearValues.m_clearRgba
  495. , _clearValues.m_clearDepth
  496. , _clearValues.m_clearStencil
  497. );
  498. // Keep track of cleared views
  499. s_clearMask |= _viewMask;
  500. }
  501. struct Aabb
  502. {
  503. float m_min[3];
  504. float m_max[3];
  505. };
  506. struct Obb
  507. {
  508. float m_mtx[16];
  509. };
  510. struct Sphere
  511. {
  512. float m_center[3];
  513. float m_radius;
  514. };
  515. struct Primitive
  516. {
  517. uint32_t m_startIndex;
  518. uint32_t m_numIndices;
  519. uint32_t m_startVertex;
  520. uint32_t m_numVertices;
  521. Sphere m_sphere;
  522. Aabb m_aabb;
  523. Obb m_obb;
  524. };
  525. typedef std::vector<Primitive> PrimitiveArray;
  526. struct Group
  527. {
  528. Group()
  529. {
  530. reset();
  531. }
  532. void reset()
  533. {
  534. m_vbh.idx = bgfx::kInvalidHandle;
  535. m_ibh.idx = bgfx::kInvalidHandle;
  536. m_prims.clear();
  537. }
  538. bgfx::VertexBufferHandle m_vbh;
  539. bgfx::IndexBufferHandle m_ibh;
  540. Sphere m_sphere;
  541. Aabb m_aabb;
  542. Obb m_obb;
  543. PrimitiveArray m_prims;
  544. };
  545. struct Mesh
  546. {
  547. void load(const void* _vertices, uint32_t _numVertices, const bgfx::VertexDecl _decl, const uint16_t* _indices, uint32_t _numIndices)
  548. {
  549. Group group;
  550. const bgfx::Memory* mem;
  551. uint32_t size;
  552. size = _numVertices*_decl.getStride();
  553. mem = bgfx::makeRef(_vertices, size);
  554. group.m_vbh = bgfx::createVertexBuffer(mem, _decl);
  555. size = _numIndices*2;
  556. mem = bgfx::makeRef(_indices, size);
  557. group.m_ibh = bgfx::createIndexBuffer(mem);
  558. m_groups.push_back(group);
  559. }
  560. void load(const char* _filePath)
  561. {
  562. #define BGFX_CHUNK_MAGIC_VB BX_MAKEFOURCC('V', 'B', ' ', 0x1)
  563. #define BGFX_CHUNK_MAGIC_IB BX_MAKEFOURCC('I', 'B', ' ', 0x0)
  564. #define BGFX_CHUNK_MAGIC_PRI BX_MAKEFOURCC('P', 'R', 'I', 0x0)
  565. bx::FileReaderI* reader = entry::getFileReader();
  566. bx::open(reader, _filePath);
  567. Group group;
  568. uint32_t chunk;
  569. while (4 == bx::read(reader, chunk) )
  570. {
  571. switch (chunk)
  572. {
  573. case BGFX_CHUNK_MAGIC_VB:
  574. {
  575. bx::read(reader, group.m_sphere);
  576. bx::read(reader, group.m_aabb);
  577. bx::read(reader, group.m_obb);
  578. bgfx::read(reader, m_decl);
  579. uint16_t stride = m_decl.getStride();
  580. uint16_t numVertices;
  581. bx::read(reader, numVertices);
  582. const bgfx::Memory* mem = bgfx::alloc(numVertices*stride);
  583. bx::read(reader, mem->data, mem->size);
  584. group.m_vbh = bgfx::createVertexBuffer(mem, m_decl);
  585. }
  586. break;
  587. case BGFX_CHUNK_MAGIC_IB:
  588. {
  589. uint32_t numIndices;
  590. bx::read(reader, numIndices);
  591. const bgfx::Memory* mem = bgfx::alloc(numIndices*2);
  592. bx::read(reader, mem->data, mem->size);
  593. group.m_ibh = bgfx::createIndexBuffer(mem);
  594. }
  595. break;
  596. case BGFX_CHUNK_MAGIC_PRI:
  597. {
  598. uint16_t len;
  599. bx::read(reader, len);
  600. std::string material;
  601. material.resize(len);
  602. bx::read(reader, const_cast<char*>(material.c_str() ), len);
  603. uint16_t num;
  604. bx::read(reader, num);
  605. for (uint32_t ii = 0; ii < num; ++ii)
  606. {
  607. bx::read(reader, len);
  608. std::string name;
  609. name.resize(len);
  610. bx::read(reader, const_cast<char*>(name.c_str() ), len);
  611. Primitive prim;
  612. bx::read(reader, prim.m_startIndex);
  613. bx::read(reader, prim.m_numIndices);
  614. bx::read(reader, prim.m_startVertex);
  615. bx::read(reader, prim.m_numVertices);
  616. bx::read(reader, prim.m_sphere);
  617. bx::read(reader, prim.m_aabb);
  618. bx::read(reader, prim.m_obb);
  619. group.m_prims.push_back(prim);
  620. }
  621. m_groups.push_back(group);
  622. group.reset();
  623. }
  624. break;
  625. default:
  626. DBG("%08x at %d", chunk, bx::seek(reader) );
  627. abort();
  628. break;
  629. }
  630. }
  631. bx::close(reader);
  632. }
  633. void unload()
  634. {
  635. for (GroupArray::const_iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
  636. {
  637. const Group& group = *it;
  638. bgfx::destroy(group.m_vbh);
  639. if (bgfx::isValid(group.m_ibh) )
  640. {
  641. bgfx::destroy(group.m_ibh);
  642. }
  643. }
  644. m_groups.clear();
  645. }
  646. void submit(bgfx::ViewId _id, float* _mtx, bgfx::ProgramHandle _program, const RenderState& _renderState)
  647. {
  648. bgfx::TextureHandle texture = BGFX_INVALID_HANDLE;
  649. submit(_id, _mtx, _program, _renderState, texture);
  650. }
  651. void submit(bgfx::ViewId _id, float* _mtx, bgfx::ProgramHandle _program, const RenderState& _renderState, bgfx::TextureHandle _texture)
  652. {
  653. for (GroupArray::const_iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
  654. {
  655. const Group& group = *it;
  656. // Set uniforms
  657. s_uniforms.submitPerDrawUniforms();
  658. // Set model matrix for rendering.
  659. bgfx::setTransform(_mtx);
  660. bgfx::setIndexBuffer(group.m_ibh);
  661. bgfx::setVertexBuffer(0, group.m_vbh);
  662. // Set texture
  663. bgfx::setTexture(0, s_texColor, _texture);
  664. // Apply render state
  665. bgfx::setStencil(_renderState.m_fstencil, _renderState.m_bstencil);
  666. bgfx::setState(_renderState.m_state, _renderState.m_blendFactorRgba);
  667. // Submit
  668. bgfx::submit(_id, _program);
  669. // Keep track of submited view ids
  670. s_viewMask |= 1 << _id;
  671. }
  672. }
  673. bgfx::VertexDecl m_decl;
  674. typedef std::vector<Group> GroupArray;
  675. GroupArray m_groups;
  676. };
  677. class ExampleStencil : public entry::AppI
  678. {
  679. public:
  680. ExampleStencil(const char* _name, const char* _description)
  681. : entry::AppI(_name, _description)
  682. {
  683. }
  684. virtual void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  685. {
  686. Args args(_argc, _argv);
  687. m_viewState = ViewState(_width, _height);
  688. m_clearValues = ClearValues(0x30303000, 1.0f, 0);
  689. m_debug = BGFX_DEBUG_NONE;
  690. m_reset = BGFX_RESET_VSYNC;
  691. bgfx::Init init;
  692. init.type = args.m_type;
  693. init.vendorId = args.m_pciId;
  694. init.resolution.width = m_viewState.m_width;
  695. init.resolution.height = m_viewState.m_height;
  696. init.resolution.reset = m_reset;
  697. bgfx::init(init);
  698. // Enable debug text.
  699. bgfx::setDebug(m_debug);
  700. // Imgui.
  701. imguiCreate();
  702. PosNormalTexcoordVertex::init();
  703. s_uniforms.init();
  704. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  705. m_programTextureLighting = loadProgram("vs_stencil_texture_lighting", "fs_stencil_texture_lighting");
  706. m_programColorLighting = loadProgram("vs_stencil_color_lighting", "fs_stencil_color_lighting" );
  707. m_programColorTexture = loadProgram("vs_stencil_color_texture", "fs_stencil_color_texture" );
  708. m_programColorBlack = loadProgram("vs_stencil_color", "fs_stencil_color_black" );
  709. m_programTexture = loadProgram("vs_stencil_texture", "fs_stencil_texture" );
  710. m_bunnyMesh.load("meshes/bunny.bin");
  711. m_columnMesh.load("meshes/column.bin");
  712. m_cubeMesh.load(s_cubeVertices, BX_COUNTOF(s_cubeVertices), PosNormalTexcoordVertex::ms_decl, s_cubeIndices, BX_COUNTOF(s_cubeIndices) );
  713. m_hplaneMesh.load(s_hplaneVertices, BX_COUNTOF(s_hplaneVertices), PosNormalTexcoordVertex::ms_decl, s_planeIndices, BX_COUNTOF(s_planeIndices) );
  714. m_vplaneMesh.load(s_vplaneVertices, BX_COUNTOF(s_vplaneVertices), PosNormalTexcoordVertex::ms_decl, s_planeIndices, BX_COUNTOF(s_planeIndices) );
  715. m_figureTex = loadTexture("textures/figure-rgba.dds");
  716. m_flareTex = loadTexture("textures/flare.dds");
  717. m_fieldstoneTex = loadTexture("textures/fieldstone-rgba.dds");
  718. // Setup lights.
  719. const float rgbInnerR[][4] =
  720. {
  721. { 1.0f, 0.7f, 0.2f, 0.0f }, //yellow
  722. { 0.7f, 0.2f, 1.0f, 0.0f }, //purple
  723. { 0.2f, 1.0f, 0.7f, 0.0f }, //cyan
  724. { 1.0f, 0.4f, 0.2f, 0.0f }, //orange
  725. { 0.7f, 0.7f, 0.7f, 0.0f }, //white
  726. };
  727. for (uint8_t ii = 0, jj = 0; ii < MAX_NUM_LIGHTS; ++ii, ++jj)
  728. {
  729. const uint8_t index = jj%BX_COUNTOF(rgbInnerR);
  730. m_lightRgbInnerR[ii][0] = rgbInnerR[index][0];
  731. m_lightRgbInnerR[ii][1] = rgbInnerR[index][1];
  732. m_lightRgbInnerR[ii][2] = rgbInnerR[index][2];
  733. m_lightRgbInnerR[ii][3] = rgbInnerR[index][3];
  734. }
  735. bx::memCopy(s_uniforms.m_lightRgbInnerR, m_lightRgbInnerR, MAX_NUM_LIGHTS * 4*sizeof(float) );
  736. // Set view and projection matrices.
  737. const float aspect = float(m_viewState.m_width)/float(m_viewState.m_height);
  738. const bgfx::Caps* caps = bgfx::getCaps();
  739. bx::mtxProj(m_viewState.m_proj, 60.0f, aspect, 0.1f, 100.0f, caps->homogeneousDepth);
  740. float initialPos[3] = { 0.0f, 18.0f, -40.0f };
  741. cameraCreate();
  742. cameraSetPosition(initialPos);
  743. cameraSetVerticalAngle(-0.35f);
  744. cameraGetViewMtx(m_viewState.m_view);
  745. m_timeOffset = bx::getHPCounter();
  746. m_scene = StencilReflectionScene;
  747. m_numLights = 4;
  748. m_reflectionValue = 0.8f;
  749. m_updateLights = true;
  750. m_updateScene = true;
  751. }
  752. virtual int shutdown() override
  753. {
  754. // Cleanup.
  755. m_bunnyMesh.unload();
  756. m_columnMesh.unload();
  757. m_cubeMesh.unload();
  758. m_hplaneMesh.unload();
  759. m_vplaneMesh.unload();
  760. bgfx::destroy(m_figureTex);
  761. bgfx::destroy(m_fieldstoneTex);
  762. bgfx::destroy(m_flareTex);
  763. bgfx::destroy(m_programTextureLighting);
  764. bgfx::destroy(m_programColorLighting);
  765. bgfx::destroy(m_programColorTexture);
  766. bgfx::destroy(m_programColorBlack);
  767. bgfx::destroy(m_programTexture);
  768. bgfx::destroy(s_texColor);
  769. s_uniforms.destroy();
  770. cameraDestroy();
  771. imguiDestroy();
  772. // Shutdown bgfx.
  773. bgfx::shutdown();
  774. return 0;
  775. }
  776. virtual bool update() override
  777. {
  778. if (!entry::processEvents(m_viewState.m_width, m_viewState.m_height, m_debug, m_reset, &m_mouseState) )
  779. {
  780. imguiBeginFrame(m_mouseState.m_mx
  781. , m_mouseState.m_my
  782. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  783. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  784. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  785. , m_mouseState.m_mz
  786. , uint16_t(m_viewState.m_width)
  787. , uint16_t(m_viewState.m_height)
  788. );
  789. showExampleDialog(this);
  790. ImGui::SetNextWindowPos(
  791. ImVec2(m_viewState.m_width - m_viewState.m_width / 5.0f - 10.0f, 10.0f)
  792. , ImGuiCond_FirstUseEver
  793. );
  794. ImGui::SetNextWindowSize(
  795. ImVec2(m_viewState.m_width / 5.0f, m_viewState.m_height / 2.0f)
  796. , ImGuiCond_FirstUseEver
  797. );
  798. ImGui::Begin("Settings"
  799. , NULL
  800. , 0
  801. );
  802. {
  803. bool check = StencilReflectionScene == m_scene;
  804. if (ImGui::Checkbox("Stencil Reflection Scene", &check) )
  805. {
  806. m_scene = StencilReflectionScene;
  807. m_numLights = 4;
  808. }
  809. }
  810. {
  811. bool check = ProjectionShadowsScene == m_scene;
  812. if (ImGui::Checkbox("Projection Shadows Scene", &check) )
  813. {
  814. m_scene = ProjectionShadowsScene;
  815. m_numLights = 1;
  816. }
  817. }
  818. ImGui::SliderInt("Lights", &m_numLights, 1, MAX_NUM_LIGHTS);
  819. if (m_scene == StencilReflectionScene)
  820. {
  821. ImGui::SliderFloat("Reflection value", &m_reflectionValue, 0.0f, 1.0f);
  822. }
  823. ImGui::Checkbox("Update lights", &m_updateLights);
  824. ImGui::Checkbox("Update scene", &m_updateScene);
  825. ImGui::End();
  826. imguiEndFrame();
  827. s_uniforms.submitConstUniforms();
  828. // Update settings.
  829. uint8_t numLights = (uint8_t)m_numLights;
  830. s_uniforms.m_params.m_ambientPass = 1.0f;
  831. s_uniforms.m_params.m_lightingPass = 1.0f;
  832. s_uniforms.m_params.m_lightCount = float(m_numLights);
  833. s_uniforms.m_params.m_lightIndex = 0.0f;
  834. s_uniforms.m_color[3] = m_reflectionValue;
  835. // Time.
  836. int64_t now = bx::getHPCounter();
  837. static int64_t last = now;
  838. const int64_t frameTime = now - last;
  839. last = now;
  840. const double freq = double(bx::getHPFrequency() );
  841. const float time = (float)( (now - m_timeOffset)/double(bx::getHPFrequency() ) );
  842. const float deltaTime = float(frameTime/freq);
  843. s_uniforms.m_time = time;
  844. // Update camera.
  845. cameraUpdate(deltaTime, m_mouseState);
  846. cameraGetViewMtx(m_viewState.m_view);
  847. static float lightTimeAccumulator = 0.0f;
  848. if (m_updateLights)
  849. {
  850. lightTimeAccumulator += deltaTime;
  851. }
  852. static float sceneTimeAccumulator = 0.0f;
  853. if (m_updateScene)
  854. {
  855. sceneTimeAccumulator += deltaTime;
  856. }
  857. float lightPosRadius[MAX_NUM_LIGHTS][4];
  858. const float radius = (m_scene == StencilReflectionScene) ? 15.0f : 25.0f;
  859. for (uint8_t ii = 0; ii < numLights; ++ii)
  860. {
  861. lightPosRadius[ii][0] = bx::sin( (lightTimeAccumulator*1.1f + ii*0.03f + ii*bx::kPiHalf*1.07f ) )*20.0f;
  862. lightPosRadius[ii][1] = 8.0f + (1.0f - bx::cos( (lightTimeAccumulator*1.5f + ii*0.29f + bx::kPiHalf*1.49f ) ) )*4.0f;
  863. lightPosRadius[ii][2] = bx::cos( (lightTimeAccumulator*1.3f + ii*0.13f + ii*bx::kPiHalf*1.79f ) )*20.0f;
  864. lightPosRadius[ii][3] = radius;
  865. }
  866. bx::memCopy(s_uniforms.m_lightPosRadius, lightPosRadius, numLights * 4*sizeof(float) );
  867. // Floor position.
  868. float floorMtx[16];
  869. bx::mtxSRT(floorMtx
  870. , 20.0f //scaleX
  871. , 20.0f //scaleY
  872. , 20.0f //scaleZ
  873. , 0.0f //rotX
  874. , 0.0f //rotY
  875. , 0.0f //rotZ
  876. , 0.0f //translateX
  877. , 0.0f //translateY
  878. , 0.0f //translateZ
  879. );
  880. // Bunny position.
  881. float bunnyMtx[16];
  882. bx::mtxSRT(bunnyMtx
  883. , 5.0f
  884. , 5.0f
  885. , 5.0f
  886. , 0.0f
  887. , 1.56f - sceneTimeAccumulator
  888. , 0.0f
  889. , 0.0f
  890. , 2.0f
  891. , 0.0f
  892. );
  893. // Columns position.
  894. const float dist = 14.0f;
  895. const float columnPositions[4][3] =
  896. {
  897. { dist, 0.0f, dist },
  898. { -dist, 0.0f, dist },
  899. { dist, 0.0f, -dist },
  900. { -dist, 0.0f, -dist },
  901. };
  902. float columnMtx[4][16];
  903. for (uint8_t ii = 0; ii < 4; ++ii)
  904. {
  905. bx::mtxSRT(columnMtx[ii]
  906. , 1.0f
  907. , 1.0f
  908. , 1.0f
  909. , 0.0f
  910. , 0.0f
  911. , 0.0f
  912. , columnPositions[ii][0]
  913. , columnPositions[ii][1]
  914. , columnPositions[ii][2]
  915. );
  916. }
  917. const uint8_t numCubes = 9;
  918. float cubeMtx[numCubes][16];
  919. for (uint16_t ii = 0; ii < numCubes; ++ii)
  920. {
  921. bx::mtxSRT(cubeMtx[ii]
  922. , 1.0f
  923. , 1.0f
  924. , 1.0f
  925. , 0.0f
  926. , 0.0f
  927. , 0.0f
  928. , bx::sin(ii * 2.0f + 13.0f - sceneTimeAccumulator) * 13.0f
  929. , 4.0f
  930. , bx::cos(ii * 2.0f + 13.0f - sceneTimeAccumulator) * 13.0f
  931. );
  932. }
  933. // Make sure at the beginning everything gets cleared.
  934. clearView(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH | BGFX_CLEAR_STENCIL, m_clearValues);
  935. bgfx::touch(0);
  936. s_viewMask |= 1;
  937. // Bunny and columns color.
  938. s_uniforms.m_color[0] = 0.70f;
  939. s_uniforms.m_color[1] = 0.65f;
  940. s_uniforms.m_color[2] = 0.60f;
  941. switch (m_scene)
  942. {
  943. case StencilReflectionScene:
  944. {
  945. // First pass - Draw plane.
  946. // Setup params for this scene.
  947. s_uniforms.m_params.m_ambientPass = 1.0f;
  948. s_uniforms.m_params.m_lightingPass = 1.0f;
  949. // Floor.
  950. m_hplaneMesh.submit(RENDER_VIEWID_RANGE1_PASS_0
  951. , floorMtx
  952. , m_programColorBlack
  953. , s_renderStates[RenderState::StencilReflection_CraftStencil]
  954. );
  955. // Second pass - Draw reflected objects.
  956. // Clear depth from previous pass.
  957. clearView(RENDER_VIEWID_RANGE1_PASS_1, BGFX_CLEAR_DEPTH, m_clearValues);
  958. // Compute reflected matrix.
  959. float reflectMtx[16];
  960. float plane_pos[3] = { 0.0f, 0.01f, 0.0f };
  961. float normal[3] = { 0.0f, 1.0f, 0.0f };
  962. mtxReflected(reflectMtx, plane_pos, normal);
  963. // Reflect lights.
  964. float reflectedLights[MAX_NUM_LIGHTS][4];
  965. for (uint8_t ii = 0; ii < numLights; ++ii)
  966. {
  967. bx::vec3MulMtx(reflectedLights[ii], lightPosRadius[ii], reflectMtx);
  968. reflectedLights[ii][3] = lightPosRadius[ii][3];
  969. }
  970. bx::memCopy(s_uniforms.m_lightPosRadius, reflectedLights, numLights * 4*sizeof(float) );
  971. // Reflect and submit bunny.
  972. float mtxReflectedBunny[16];
  973. bx::mtxMul(mtxReflectedBunny, bunnyMtx, reflectMtx);
  974. m_bunnyMesh.submit(RENDER_VIEWID_RANGE1_PASS_1
  975. , mtxReflectedBunny
  976. , m_programColorLighting
  977. , s_renderStates[RenderState::StencilReflection_DrawReflected]
  978. );
  979. // Reflect and submit columns.
  980. float mtxReflectedColumn[16];
  981. for (uint8_t ii = 0; ii < 4; ++ii)
  982. {
  983. bx::mtxMul(mtxReflectedColumn, columnMtx[ii], reflectMtx);
  984. m_columnMesh.submit(RENDER_VIEWID_RANGE1_PASS_1
  985. , mtxReflectedColumn
  986. , m_programColorLighting
  987. , s_renderStates[RenderState::StencilReflection_DrawReflected]
  988. );
  989. }
  990. // Set lights back.
  991. bx::memCopy(s_uniforms.m_lightPosRadius, lightPosRadius, numLights * 4*sizeof(float) );
  992. // Third pass - Blend plane.
  993. // Floor.
  994. m_hplaneMesh.submit(RENDER_VIEWID_RANGE1_PASS_2
  995. , floorMtx
  996. , m_programTextureLighting
  997. , s_renderStates[RenderState::StencilReflection_BlendPlane]
  998. , m_fieldstoneTex
  999. );
  1000. // Fourth pass - Draw everything else but the plane.
  1001. // Bunny.
  1002. m_bunnyMesh.submit(RENDER_VIEWID_RANGE1_PASS_3
  1003. , bunnyMtx
  1004. , m_programColorLighting
  1005. , s_renderStates[RenderState::StencilReflection_DrawScene]
  1006. );
  1007. // Columns.
  1008. for (uint8_t ii = 0; ii < 4; ++ii)
  1009. {
  1010. m_columnMesh.submit(RENDER_VIEWID_RANGE1_PASS_3
  1011. , columnMtx[ii]
  1012. , m_programColorLighting
  1013. , s_renderStates[RenderState::StencilReflection_DrawScene]
  1014. );
  1015. }
  1016. }
  1017. break;
  1018. case ProjectionShadowsScene:
  1019. {
  1020. // First pass - Draw entire scene. (ambient only).
  1021. s_uniforms.m_params.m_ambientPass = 1.0f;
  1022. s_uniforms.m_params.m_lightingPass = 0.0f;
  1023. // Bunny.
  1024. m_bunnyMesh.submit(RENDER_VIEWID_RANGE1_PASS_0
  1025. , bunnyMtx
  1026. , m_programColorLighting
  1027. , s_renderStates[RenderState::ProjectionShadows_DrawAmbient]
  1028. );
  1029. // Floor.
  1030. m_hplaneMesh.submit(RENDER_VIEWID_RANGE1_PASS_0
  1031. , floorMtx
  1032. , m_programTextureLighting
  1033. , s_renderStates[RenderState::ProjectionShadows_DrawAmbient]
  1034. , m_fieldstoneTex
  1035. );
  1036. // Cubes.
  1037. for (uint8_t ii = 0; ii < numCubes; ++ii)
  1038. {
  1039. m_cubeMesh.submit(RENDER_VIEWID_RANGE1_PASS_0
  1040. , cubeMtx[ii]
  1041. , m_programTextureLighting
  1042. , s_renderStates[RenderState::ProjectionShadows_DrawAmbient]
  1043. , m_figureTex
  1044. );
  1045. }
  1046. // Ground plane.
  1047. float ground[4];
  1048. float plane_pos[3] = { 0.0f, 0.0f, 0.0f };
  1049. float normal[3] = { 0.0f, 1.0f, 0.0f };
  1050. bx::memCopy(ground, normal, sizeof(float) * 3);
  1051. ground[3] = -bx::vec3Dot(plane_pos, normal) - 0.01f; // - 0.01 against z-fighting
  1052. for (uint8_t ii = 0, viewId = RENDER_VIEWID_RANGE5_PASS_6; ii < numLights; ++ii, ++viewId)
  1053. {
  1054. // Clear stencil for this light source.
  1055. clearView(viewId, BGFX_CLEAR_STENCIL, m_clearValues);
  1056. // Draw shadow projection of scene objects.
  1057. // Get homogeneous light pos.
  1058. float* lightPos = lightPosRadius[ii];
  1059. float pos[4];
  1060. bx::memCopy(pos, lightPos, sizeof(float) * 3);
  1061. pos[3] = 1.0f;
  1062. // Calculate shadow mtx for current light.
  1063. float shadowMtx[16];
  1064. mtxShadow(shadowMtx, ground, pos);
  1065. // Submit bunny's shadow.
  1066. float mtxShadowedBunny[16];
  1067. bx::mtxMul(mtxShadowedBunny, bunnyMtx, shadowMtx);
  1068. m_bunnyMesh.submit(viewId
  1069. , mtxShadowedBunny
  1070. , m_programColorBlack
  1071. , s_renderStates[RenderState::ProjectionShadows_CraftStencil]
  1072. );
  1073. // Submit cube shadows.
  1074. float mtxShadowedCube[16];
  1075. for (uint8_t jj = 0; jj < numCubes; ++jj)
  1076. {
  1077. bx::mtxMul(mtxShadowedCube, cubeMtx[jj], shadowMtx);
  1078. m_cubeMesh.submit(viewId
  1079. , mtxShadowedCube
  1080. , m_programColorBlack
  1081. , s_renderStates[RenderState::ProjectionShadows_CraftStencil]
  1082. );
  1083. }
  1084. // Draw entire scene. (lighting pass only. blending is on)
  1085. s_uniforms.m_params.m_ambientPass = 0.0f;
  1086. s_uniforms.m_params.m_lightingPass = 1.0f;
  1087. s_uniforms.m_params.m_lightCount = 1.0f;
  1088. s_uniforms.m_params.m_lightIndex = float(ii);
  1089. // Bunny.
  1090. m_bunnyMesh.submit(viewId
  1091. , bunnyMtx
  1092. , m_programColorLighting
  1093. , s_renderStates[RenderState::ProjectionShadows_DrawDiffuse]
  1094. );
  1095. // Floor.
  1096. m_hplaneMesh.submit(viewId
  1097. , floorMtx
  1098. , m_programTextureLighting
  1099. , s_renderStates[RenderState::ProjectionShadows_DrawDiffuse]
  1100. , m_fieldstoneTex
  1101. );
  1102. // Cubes.
  1103. for (uint8_t jj = 0; jj < numCubes; ++jj)
  1104. {
  1105. m_cubeMesh.submit(viewId
  1106. , cubeMtx[jj]
  1107. , m_programTextureLighting
  1108. , s_renderStates[RenderState::ProjectionShadows_DrawDiffuse]
  1109. , m_figureTex
  1110. );
  1111. }
  1112. }
  1113. // Reset these to default..
  1114. s_uniforms.m_params.m_ambientPass = 1.0f;
  1115. s_uniforms.m_params.m_lightingPass = 1.0f;
  1116. }
  1117. break;
  1118. };
  1119. //lights
  1120. const float lightScale[3] = { 1.5f, 1.5f, 1.5f };
  1121. float lightMtx[16];
  1122. for (uint8_t ii = 0; ii < numLights; ++ii)
  1123. {
  1124. s_uniforms.m_color[0] = m_lightRgbInnerR[ii][0];
  1125. s_uniforms.m_color[1] = m_lightRgbInnerR[ii][1];
  1126. s_uniforms.m_color[2] = m_lightRgbInnerR[ii][2];
  1127. mtxBillboard(lightMtx, m_viewState.m_view, lightPosRadius[ii], lightScale);
  1128. m_vplaneMesh.submit(RENDER_VIEWID_RANGE1_PASS_7
  1129. , lightMtx
  1130. , m_programColorTexture
  1131. , s_renderStates[RenderState::Custom_BlendLightTexture]
  1132. , m_flareTex
  1133. );
  1134. }
  1135. // Draw floor bottom.
  1136. float floorBottomMtx[16];
  1137. bx::mtxSRT(floorBottomMtx
  1138. , 20.0f //scaleX
  1139. , 20.0f //scaleY
  1140. , 20.0f //scaleZ
  1141. , 0.0f //rotX
  1142. , 0.0f //rotY
  1143. , 0.0f //rotZ
  1144. , 0.0f //translateX
  1145. , -0.1f //translateY
  1146. , 0.0f //translateZ
  1147. );
  1148. m_hplaneMesh.submit(RENDER_VIEWID_RANGE1_PASS_7
  1149. , floorBottomMtx
  1150. , m_programTexture
  1151. , s_renderStates[RenderState::Custom_DrawPlaneBottom]
  1152. , m_figureTex
  1153. );
  1154. // Setup view rect and transform for all used views.
  1155. setViewRectMask(s_viewMask, 0, 0, uint16_t(m_viewState.m_width), uint16_t(m_viewState.m_height) );
  1156. setViewTransformMask(s_viewMask, m_viewState.m_view, m_viewState.m_proj);
  1157. s_viewMask = 0;
  1158. // Advance to next frame. Rendering thread will be kicked to
  1159. // process submitted rendering primitives.
  1160. bgfx::frame();
  1161. //reset clear values on used views
  1162. clearViewMask(s_clearMask, BGFX_CLEAR_NONE, m_clearValues);
  1163. s_clearMask = 0;
  1164. return true;
  1165. }
  1166. return false;
  1167. }
  1168. ViewState m_viewState;
  1169. entry::MouseState m_mouseState;
  1170. ClearValues m_clearValues;
  1171. uint32_t m_debug;
  1172. uint32_t m_reset;
  1173. bgfx::ProgramHandle m_programTextureLighting;
  1174. bgfx::ProgramHandle m_programColorLighting;
  1175. bgfx::ProgramHandle m_programColorTexture;
  1176. bgfx::ProgramHandle m_programColorBlack;
  1177. bgfx::ProgramHandle m_programTexture;
  1178. Mesh m_bunnyMesh;
  1179. Mesh m_columnMesh;
  1180. Mesh m_cubeMesh;
  1181. Mesh m_hplaneMesh;
  1182. Mesh m_vplaneMesh;
  1183. bgfx::TextureHandle m_figureTex;
  1184. bgfx::TextureHandle m_flareTex;
  1185. bgfx::TextureHandle m_fieldstoneTex;
  1186. float m_lightRgbInnerR[MAX_NUM_LIGHTS][4];
  1187. int64_t m_timeOffset;
  1188. enum Scene
  1189. {
  1190. StencilReflectionScene = 0,
  1191. ProjectionShadowsScene,
  1192. };
  1193. Scene m_scene;
  1194. int32_t m_numLights;
  1195. float m_reflectionValue;
  1196. bool m_updateLights;
  1197. bool m_updateScene;
  1198. };
  1199. } // namespace
  1200. ENTRY_IMPLEMENT_MAIN(ExampleStencil, "13-stencil", "Stencil reflections and shadows.");