stencil.cpp 37 KB

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