stencil.cpp 39 KB

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