stencil.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  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::invalidHandle;
  534. m_ibh.idx = bgfx::invalidHandle;
  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::invalidHandle != group.m_ibh.idx)
  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(group.m_vbh);
  665. // Set texture
  666. if (bgfx::invalidHandle != _texture.idx)
  667. {
  668. bgfx::setTexture(0, s_texColor, _texture);
  669. }
  670. // Apply render state
  671. bgfx::setStencil(_renderState.m_fstencil, _renderState.m_bstencil);
  672. bgfx::setState(_renderState.m_state, _renderState.m_blendFactorRgba);
  673. // Submit
  674. bgfx::submit(_viewId, _program);
  675. // Keep track of submited view ids
  676. s_viewMask |= 1 << _viewId;
  677. }
  678. }
  679. bgfx::VertexDecl m_decl;
  680. typedef std::vector<Group> GroupArray;
  681. GroupArray m_groups;
  682. };
  683. int _main_(int _argc, char** _argv)
  684. {
  685. Args args(_argc, _argv);
  686. ViewState viewState(1280, 720);
  687. ClearValues clearValues(0x30303000, 1.0f, 0);
  688. uint32_t debug = BGFX_DEBUG_TEXT;
  689. uint32_t reset = BGFX_RESET_VSYNC;
  690. bgfx::init(args.m_type, args.m_pciId);
  691. bgfx::reset(viewState.m_width, viewState.m_height, reset);
  692. // Enable debug text.
  693. bgfx::setDebug(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_uniforms.submitConstUniforms();
  710. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  711. bgfx::ProgramHandle programTextureLighting = loadProgram("vs_stencil_texture_lighting", "fs_stencil_texture_lighting");
  712. bgfx::ProgramHandle programColorLighting = loadProgram("vs_stencil_color_lighting", "fs_stencil_color_lighting" );
  713. bgfx::ProgramHandle programColorTexture = loadProgram("vs_stencil_color_texture", "fs_stencil_color_texture" );
  714. bgfx::ProgramHandle programColorBlack = loadProgram("vs_stencil_color", "fs_stencil_color_black" );
  715. bgfx::ProgramHandle programTexture = loadProgram("vs_stencil_texture", "fs_stencil_texture" );
  716. Mesh bunnyMesh;
  717. Mesh columnMesh;
  718. Mesh cubeMesh;
  719. Mesh hplaneMesh;
  720. Mesh vplaneMesh;
  721. bunnyMesh.load("meshes/bunny.bin");
  722. columnMesh.load("meshes/column.bin");
  723. cubeMesh.load(s_cubeVertices, BX_COUNTOF(s_cubeVertices), PosNormalTexcoordVertex::ms_decl, s_cubeIndices, BX_COUNTOF(s_cubeIndices) );
  724. hplaneMesh.load(s_hplaneVertices, BX_COUNTOF(s_hplaneVertices), PosNormalTexcoordVertex::ms_decl, s_planeIndices, BX_COUNTOF(s_planeIndices) );
  725. vplaneMesh.load(s_vplaneVertices, BX_COUNTOF(s_vplaneVertices), PosNormalTexcoordVertex::ms_decl, s_planeIndices, BX_COUNTOF(s_planeIndices) );
  726. bgfx::TextureHandle figureTex = loadTexture("textures/figure-rgba.dds");
  727. bgfx::TextureHandle flareTex = loadTexture("textures/flare.dds");
  728. bgfx::TextureHandle fieldstoneTex = loadTexture("textures/fieldstone-rgba.dds");
  729. // Setup lights.
  730. const float rgbInnerR[][4] =
  731. {
  732. { 1.0f, 0.7f, 0.2f, 0.0f }, //yellow
  733. { 0.7f, 0.2f, 1.0f, 0.0f }, //purple
  734. { 0.2f, 1.0f, 0.7f, 0.0f }, //cyan
  735. { 1.0f, 0.4f, 0.2f, 0.0f }, //orange
  736. { 0.7f, 0.7f, 0.7f, 0.0f }, //white
  737. };
  738. float lightRgbInnerR[MAX_NUM_LIGHTS][4];
  739. for (uint8_t ii = 0, jj = 0; ii < MAX_NUM_LIGHTS; ++ii, ++jj)
  740. {
  741. const uint8_t index = jj%BX_COUNTOF(rgbInnerR);
  742. lightRgbInnerR[ii][0] = rgbInnerR[index][0];
  743. lightRgbInnerR[ii][1] = rgbInnerR[index][1];
  744. lightRgbInnerR[ii][2] = rgbInnerR[index][2];
  745. lightRgbInnerR[ii][3] = rgbInnerR[index][3];
  746. }
  747. bx::memCopy(s_uniforms.m_lightRgbInnerR, lightRgbInnerR, MAX_NUM_LIGHTS * 4*sizeof(float) );
  748. // Set view and projection matrices.
  749. const float aspect = float(viewState.m_width)/float(viewState.m_height);
  750. mtxProj(viewState.m_proj, 60.0f, aspect, 0.1f, 100.0f);
  751. float initialPos[3] = { 0.0f, 18.0f, -40.0f };
  752. cameraCreate();
  753. cameraSetPosition(initialPos);
  754. cameraSetVerticalAngle(-0.35f);
  755. cameraGetViewMtx(viewState.m_view);
  756. int64_t timeOffset = bx::getHPCounter();
  757. enum Scene
  758. {
  759. StencilReflectionScene = 0,
  760. ProjectionShadowsScene,
  761. };
  762. Scene scene = StencilReflectionScene;
  763. float settings_numLights = 4.0f;
  764. float settings_reflectionValue = 0.8f;
  765. bool settings_updateLights = true;
  766. bool settings_updateScene = true;
  767. static const char* titles[3] =
  768. {
  769. "Stencil Reflection Scene",
  770. "Projection Shadows Scene",
  771. };
  772. entry::MouseState mouseState;
  773. while (!entry::processEvents(viewState.m_width, viewState.m_height, debug, reset, &mouseState) )
  774. {
  775. imguiBeginFrame(mouseState.m_mx
  776. , mouseState.m_my
  777. , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  778. | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  779. | (mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  780. , mouseState.m_mz
  781. , uint16_t(viewState.m_width)
  782. , uint16_t(viewState.m_height)
  783. );
  784. static int32_t scrollArea = 0;
  785. imguiBeginScrollArea("Settings", viewState.m_width - 256 - 10, 10, 256, 215, &scrollArea);
  786. if (imguiCheck(titles[StencilReflectionScene], StencilReflectionScene == scene) )
  787. {
  788. scene = StencilReflectionScene;
  789. settings_numLights = 4.0f;
  790. }
  791. if (imguiCheck(titles[ProjectionShadowsScene], ProjectionShadowsScene == scene) )
  792. {
  793. scene = ProjectionShadowsScene;
  794. settings_numLights = 1.0f;
  795. }
  796. imguiSeparatorLine();
  797. imguiSlider("Lights", settings_numLights, 1.0f, float(MAX_NUM_LIGHTS), 1.0f);
  798. if (scene == StencilReflectionScene)
  799. {
  800. imguiSlider("Reflection value", settings_reflectionValue, 0.0f, 1.0f, 0.01f);
  801. }
  802. if (imguiCheck("Update lights", settings_updateLights) )
  803. {
  804. settings_updateLights = !settings_updateLights;
  805. }
  806. if (imguiCheck("Update scene", settings_updateScene) )
  807. {
  808. settings_updateScene = !settings_updateScene;
  809. }
  810. imguiEndScrollArea();
  811. imguiEndFrame();
  812. // Update settings.
  813. uint8_t numLights = (uint8_t)settings_numLights;
  814. s_uniforms.m_params.m_ambientPass = 1.0f;
  815. s_uniforms.m_params.m_lightingPass = 1.0f;
  816. s_uniforms.m_params.m_lightCount = settings_numLights;
  817. s_uniforms.m_params.m_lightIndex = 0.0f;
  818. s_uniforms.m_color[3] = settings_reflectionValue;
  819. // Time.
  820. int64_t now = bx::getHPCounter();
  821. static int64_t last = now;
  822. const int64_t frameTime = now - last;
  823. last = now;
  824. const double freq = double(bx::getHPFrequency() );
  825. const double toMs = 1000.0/freq;
  826. const float time = (float)( (now - timeOffset)/double(bx::getHPFrequency() ) );
  827. const float deltaTime = float(frameTime/freq);
  828. s_uniforms.m_time = time;
  829. // Use debug font to print information about this example.
  830. bgfx::dbgTextClear();
  831. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/13-stencil");
  832. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Stencil reflections and shadows.");
  833. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  834. // Update camera.
  835. cameraUpdate(deltaTime, mouseState);
  836. cameraGetViewMtx(viewState.m_view);
  837. static float lightTimeAccumulator = 0.0f;
  838. if (settings_updateLights)
  839. {
  840. lightTimeAccumulator += deltaTime;
  841. }
  842. static float sceneTimeAccumulator = 0.0f;
  843. if (settings_updateScene)
  844. {
  845. sceneTimeAccumulator += deltaTime;
  846. }
  847. float lightPosRadius[MAX_NUM_LIGHTS][4];
  848. const float radius = (scene == StencilReflectionScene) ? 15.0f : 25.0f;
  849. for (uint8_t ii = 0; ii < numLights; ++ii)
  850. {
  851. lightPosRadius[ii][0] = bx::fsin( (lightTimeAccumulator*1.1f + ii*0.03f + ii*bx::piHalf*1.07f ) )*20.0f;
  852. lightPosRadius[ii][1] = 8.0f + (1.0f - bx::fcos( (lightTimeAccumulator*1.5f + ii*0.29f + bx::piHalf*1.49f ) ) )*4.0f;
  853. lightPosRadius[ii][2] = bx::fcos( (lightTimeAccumulator*1.3f + ii*0.13f + ii*bx::piHalf*1.79f ) )*20.0f;
  854. lightPosRadius[ii][3] = radius;
  855. }
  856. bx::memCopy(s_uniforms.m_lightPosRadius, lightPosRadius, numLights * 4*sizeof(float) );
  857. // Floor position.
  858. float floorMtx[16];
  859. bx::mtxSRT(floorMtx
  860. , 20.0f //scaleX
  861. , 20.0f //scaleY
  862. , 20.0f //scaleZ
  863. , 0.0f //rotX
  864. , 0.0f //rotY
  865. , 0.0f //rotZ
  866. , 0.0f //translateX
  867. , 0.0f //translateY
  868. , 0.0f //translateZ
  869. );
  870. // Bunny position.
  871. float bunnyMtx[16];
  872. bx::mtxSRT(bunnyMtx
  873. , 5.0f
  874. , 5.0f
  875. , 5.0f
  876. , 0.0f
  877. , 1.56f - sceneTimeAccumulator
  878. , 0.0f
  879. , 0.0f
  880. , 2.0f
  881. , 0.0f
  882. );
  883. // Columns position.
  884. const float dist = 14.0f;
  885. const float columnPositions[4][3] =
  886. {
  887. { dist, 0.0f, dist },
  888. { -dist, 0.0f, dist },
  889. { dist, 0.0f, -dist },
  890. { -dist, 0.0f, -dist },
  891. };
  892. float columnMtx[4][16];
  893. for (uint8_t ii = 0; ii < 4; ++ii)
  894. {
  895. bx::mtxSRT(columnMtx[ii]
  896. , 1.0f
  897. , 1.0f
  898. , 1.0f
  899. , 0.0f
  900. , 0.0f
  901. , 0.0f
  902. , columnPositions[ii][0]
  903. , columnPositions[ii][1]
  904. , columnPositions[ii][2]
  905. );
  906. }
  907. const uint8_t numCubes = 9;
  908. float cubeMtx[numCubes][16];
  909. for (uint16_t ii = 0; ii < numCubes; ++ii)
  910. {
  911. bx::mtxSRT(cubeMtx[ii]
  912. , 1.0f
  913. , 1.0f
  914. , 1.0f
  915. , 0.0f
  916. , 0.0f
  917. , 0.0f
  918. , bx::fsin(ii * 2.0f + 13.0f - sceneTimeAccumulator) * 13.0f
  919. , 4.0f
  920. , bx::fcos(ii * 2.0f + 13.0f - sceneTimeAccumulator) * 13.0f
  921. );
  922. }
  923. // Make sure at the beginning everything gets cleared.
  924. clearView(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH | BGFX_CLEAR_STENCIL, clearValues);
  925. bgfx::touch(0);
  926. s_viewMask |= 1;
  927. // Bunny and columns color.
  928. s_uniforms.m_color[0] = 0.70f;
  929. s_uniforms.m_color[1] = 0.65f;
  930. s_uniforms.m_color[2] = 0.60f;
  931. switch (scene)
  932. {
  933. case StencilReflectionScene:
  934. {
  935. // First pass - Draw plane.
  936. // Setup params for this scene.
  937. s_uniforms.m_params.m_ambientPass = 1.0f;
  938. s_uniforms.m_params.m_lightingPass = 1.0f;
  939. // Floor.
  940. hplaneMesh.submit(RENDER_VIEWID_RANGE1_PASS_0
  941. , floorMtx
  942. , programColorBlack
  943. , s_renderStates[RenderState::StencilReflection_CraftStencil]
  944. );
  945. // Second pass - Draw reflected objects.
  946. // Clear depth from previous pass.
  947. clearView(RENDER_VIEWID_RANGE1_PASS_1, BGFX_CLEAR_DEPTH, clearValues);
  948. // Compute reflected matrix.
  949. float reflectMtx[16];
  950. float plane_pos[3] = { 0.0f, 0.01f, 0.0f };
  951. float normal[3] = { 0.0f, 1.0f, 0.0f };
  952. mtxReflected(reflectMtx, plane_pos, normal);
  953. // Reflect lights.
  954. float reflectedLights[MAX_NUM_LIGHTS][4];
  955. for (uint8_t ii = 0; ii < numLights; ++ii)
  956. {
  957. bx::vec3MulMtx(reflectedLights[ii], lightPosRadius[ii], reflectMtx);
  958. reflectedLights[ii][3] = lightPosRadius[ii][3];
  959. }
  960. bx::memCopy(s_uniforms.m_lightPosRadius, reflectedLights, numLights * 4*sizeof(float) );
  961. // Reflect and submit bunny.
  962. float mtxReflectedBunny[16];
  963. bx::mtxMul(mtxReflectedBunny, bunnyMtx, reflectMtx);
  964. bunnyMesh.submit(RENDER_VIEWID_RANGE1_PASS_1
  965. , mtxReflectedBunny
  966. , programColorLighting
  967. , s_renderStates[RenderState::StencilReflection_DrawReflected]
  968. );
  969. // Reflect and submit columns.
  970. float mtxReflectedColumn[16];
  971. for (uint8_t ii = 0; ii < 4; ++ii)
  972. {
  973. bx::mtxMul(mtxReflectedColumn, columnMtx[ii], reflectMtx);
  974. columnMesh.submit(RENDER_VIEWID_RANGE1_PASS_1
  975. , mtxReflectedColumn
  976. , programColorLighting
  977. , s_renderStates[RenderState::StencilReflection_DrawReflected]
  978. );
  979. }
  980. // Set lights back.
  981. bx::memCopy(s_uniforms.m_lightPosRadius, lightPosRadius, numLights * 4*sizeof(float) );
  982. // Third pass - Blend plane.
  983. // Floor.
  984. hplaneMesh.submit(RENDER_VIEWID_RANGE1_PASS_2
  985. , floorMtx
  986. , programTextureLighting
  987. , s_renderStates[RenderState::StencilReflection_BlendPlane]
  988. , fieldstoneTex
  989. );
  990. // Fourth pass - Draw everything else but the plane.
  991. // Bunny.
  992. bunnyMesh.submit(RENDER_VIEWID_RANGE1_PASS_3
  993. , bunnyMtx
  994. , programColorLighting
  995. , s_renderStates[RenderState::StencilReflection_DrawScene]
  996. );
  997. // Columns.
  998. for (uint8_t ii = 0; ii < 4; ++ii)
  999. {
  1000. columnMesh.submit(RENDER_VIEWID_RANGE1_PASS_3
  1001. , columnMtx[ii]
  1002. , programColorLighting
  1003. , s_renderStates[RenderState::StencilReflection_DrawScene]
  1004. );
  1005. }
  1006. }
  1007. break;
  1008. case ProjectionShadowsScene:
  1009. {
  1010. // First pass - Draw entire scene. (ambient only).
  1011. s_uniforms.m_params.m_ambientPass = 1.0f;
  1012. s_uniforms.m_params.m_lightingPass = 0.0f;
  1013. // Bunny.
  1014. bunnyMesh.submit(RENDER_VIEWID_RANGE1_PASS_0
  1015. , bunnyMtx
  1016. , programColorLighting
  1017. , s_renderStates[RenderState::ProjectionShadows_DrawAmbient]
  1018. );
  1019. // Floor.
  1020. hplaneMesh.submit(RENDER_VIEWID_RANGE1_PASS_0
  1021. , floorMtx
  1022. , programTextureLighting
  1023. , s_renderStates[RenderState::ProjectionShadows_DrawAmbient]
  1024. , fieldstoneTex
  1025. );
  1026. // Cubes.
  1027. for (uint8_t ii = 0; ii < numCubes; ++ii)
  1028. {
  1029. cubeMesh.submit(RENDER_VIEWID_RANGE1_PASS_0
  1030. , cubeMtx[ii]
  1031. , programTextureLighting
  1032. , s_renderStates[RenderState::ProjectionShadows_DrawAmbient]
  1033. , figureTex
  1034. );
  1035. }
  1036. // Ground plane.
  1037. float ground[4];
  1038. float plane_pos[3] = { 0.0f, 0.0f, 0.0f };
  1039. float normal[3] = { 0.0f, 1.0f, 0.0f };
  1040. bx::memCopy(ground, normal, sizeof(float) * 3);
  1041. ground[3] = -bx::vec3Dot(plane_pos, normal) - 0.01f; // - 0.01 against z-fighting
  1042. for (uint8_t ii = 0, viewId = RENDER_VIEWID_RANGE5_PASS_6; ii < numLights; ++ii, ++viewId)
  1043. {
  1044. // Clear stencil for this light source.
  1045. clearView(viewId, BGFX_CLEAR_STENCIL, clearValues);
  1046. // Draw shadow projection of scene objects.
  1047. // Get homogeneous light pos.
  1048. float* lightPos = lightPosRadius[ii];
  1049. float pos[4];
  1050. bx::memCopy(pos, lightPos, sizeof(float) * 3);
  1051. pos[3] = 1.0f;
  1052. // Calculate shadow mtx for current light.
  1053. float shadowMtx[16];
  1054. mtxShadow(shadowMtx, ground, pos);
  1055. // Submit bunny's shadow.
  1056. float mtxShadowedBunny[16];
  1057. bx::mtxMul(mtxShadowedBunny, bunnyMtx, shadowMtx);
  1058. bunnyMesh.submit(viewId
  1059. , mtxShadowedBunny
  1060. , programColorBlack
  1061. , s_renderStates[RenderState::ProjectionShadows_CraftStencil]
  1062. );
  1063. // Submit cube shadows.
  1064. float mtxShadowedCube[16];
  1065. for (uint8_t jj = 0; jj < numCubes; ++jj)
  1066. {
  1067. bx::mtxMul(mtxShadowedCube, cubeMtx[jj], shadowMtx);
  1068. cubeMesh.submit(viewId
  1069. , mtxShadowedCube
  1070. , programColorBlack
  1071. , s_renderStates[RenderState::ProjectionShadows_CraftStencil]
  1072. );
  1073. }
  1074. // Draw entire scene. (lighting pass only. blending is on)
  1075. s_uniforms.m_params.m_ambientPass = 0.0f;
  1076. s_uniforms.m_params.m_lightingPass = 1.0f;
  1077. s_uniforms.m_params.m_lightCount = 1.0f;
  1078. s_uniforms.m_params.m_lightIndex = float(ii);
  1079. // Bunny.
  1080. bunnyMesh.submit(viewId
  1081. , bunnyMtx
  1082. , programColorLighting
  1083. , s_renderStates[RenderState::ProjectionShadows_DrawDiffuse]
  1084. );
  1085. // Floor.
  1086. hplaneMesh.submit(viewId
  1087. , floorMtx
  1088. , programTextureLighting
  1089. , s_renderStates[RenderState::ProjectionShadows_DrawDiffuse]
  1090. , fieldstoneTex
  1091. );
  1092. // Cubes.
  1093. for (uint8_t jj = 0; jj < numCubes; ++jj)
  1094. {
  1095. cubeMesh.submit(viewId
  1096. , cubeMtx[jj]
  1097. , programTextureLighting
  1098. , s_renderStates[RenderState::ProjectionShadows_DrawDiffuse]
  1099. , figureTex
  1100. );
  1101. }
  1102. }
  1103. // Reset these to default..
  1104. s_uniforms.m_params.m_ambientPass = 1.0f;
  1105. s_uniforms.m_params.m_lightingPass = 1.0f;
  1106. }
  1107. break;
  1108. };
  1109. //lights
  1110. const float lightScale[3] = { 1.5f, 1.5f, 1.5f };
  1111. float lightMtx[16];
  1112. for (uint8_t ii = 0; ii < numLights; ++ii)
  1113. {
  1114. s_uniforms.m_color[0] = lightRgbInnerR[ii][0];
  1115. s_uniforms.m_color[1] = lightRgbInnerR[ii][1];
  1116. s_uniforms.m_color[2] = lightRgbInnerR[ii][2];
  1117. mtxBillboard(lightMtx, viewState.m_view, lightPosRadius[ii], lightScale);
  1118. vplaneMesh.submit(RENDER_VIEWID_RANGE1_PASS_7
  1119. , lightMtx
  1120. , programColorTexture
  1121. , s_renderStates[RenderState::Custom_BlendLightTexture]
  1122. , flareTex
  1123. );
  1124. }
  1125. // Draw floor bottom.
  1126. float floorBottomMtx[16];
  1127. bx::mtxSRT(floorBottomMtx
  1128. , 20.0f //scaleX
  1129. , 20.0f //scaleY
  1130. , 20.0f //scaleZ
  1131. , 0.0f //rotX
  1132. , 0.0f //rotY
  1133. , 0.0f //rotZ
  1134. , 0.0f //translateX
  1135. , -0.1f //translateY
  1136. , 0.0f //translateZ
  1137. );
  1138. hplaneMesh.submit(RENDER_VIEWID_RANGE1_PASS_7
  1139. , floorBottomMtx
  1140. , programTexture
  1141. , s_renderStates[RenderState::Custom_DrawPlaneBottom]
  1142. , figureTex
  1143. );
  1144. // Setup view rect and transform for all used views.
  1145. setViewRectMask(s_viewMask, 0, 0, uint16_t(viewState.m_width), uint16_t(viewState.m_height) );
  1146. setViewTransformMask(s_viewMask, viewState.m_view, viewState.m_proj);
  1147. s_viewMask = 0;
  1148. // Advance to next frame. Rendering thread will be kicked to
  1149. // process submitted rendering primitives.
  1150. bgfx::frame();
  1151. //reset clear values on used views
  1152. clearViewMask(s_clearMask, BGFX_CLEAR_NONE, clearValues);
  1153. s_clearMask = 0;
  1154. }
  1155. // Cleanup.
  1156. bunnyMesh.unload();
  1157. columnMesh.unload();
  1158. cubeMesh.unload();
  1159. hplaneMesh.unload();
  1160. vplaneMesh.unload();
  1161. bgfx::destroyTexture(figureTex);
  1162. bgfx::destroyTexture(fieldstoneTex);
  1163. bgfx::destroyTexture(flareTex);
  1164. bgfx::destroyProgram(programTextureLighting);
  1165. bgfx::destroyProgram(programColorLighting);
  1166. bgfx::destroyProgram(programColorTexture);
  1167. bgfx::destroyProgram(programColorBlack);
  1168. bgfx::destroyProgram(programTexture);
  1169. bgfx::destroyUniform(s_texColor);
  1170. s_uniforms.destroy();
  1171. cameraDestroy();
  1172. imguiDestroy();
  1173. // Shutdown bgfx.
  1174. bgfx::shutdown();
  1175. return 0;
  1176. }