stencil.cpp 37 KB

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