stencil.cpp 38 KB

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