stencil.cpp 38 KB

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