stencil.cpp 38 KB

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