stencil.cpp 40 KB

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