stencil.cpp 40 KB

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