stencil.cpp 37 KB

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