stencil.cpp 38 KB

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