stencil.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  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.02f;
  292. m_ambient[1] = 0.02f;
  293. m_ambient[2] = 0.02f;
  294. m_ambient[3] = 0.0f; //unused
  295. m_diffuse[0] = 0.2f;
  296. m_diffuse[1] = 0.2f;
  297. m_diffuse[2] = 0.2f;
  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] = 10.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.0f;
  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_BlendPlane,
  408. StencilReflection_DrawScene,
  409. ProjectionShadows_DrawAmbient,
  410. ProjectionShadows_CraftStencil,
  411. ProjectionShadows_DrawDiffuse,
  412. Custom_BlendLightTexture,
  413. Custom_DrawPlaneBottom,
  414. Count
  415. };
  416. uint64_t m_state;
  417. uint32_t m_blendFactorRgba;
  418. uint32_t m_fstencil;
  419. uint32_t m_bstencil;
  420. };
  421. static RenderState s_renderStates[RenderState::Count] =
  422. {
  423. { // StencilReflection_CraftStencil
  424. BGFX_STATE_RGB_WRITE
  425. | BGFX_STATE_DEPTH_WRITE
  426. | BGFX_STATE_DEPTH_TEST_LESS
  427. | BGFX_STATE_MSAA
  428. , UINT32_MAX
  429. , BGFX_STENCIL_TEST_ALWAYS // pass always
  430. | BGFX_STENCIL_FUNC_REF(1) // value = 1
  431. | BGFX_STENCIL_FUNC_RMASK(0xff)
  432. | BGFX_STENCIL_OP_FAIL_S_REPLACE
  433. | BGFX_STENCIL_OP_FAIL_Z_REPLACE
  434. | BGFX_STENCIL_OP_PASS_Z_REPLACE // store the value
  435. , BGFX_STENCIL_NONE
  436. },
  437. { // StencilReflection_DrawReflected
  438. BGFX_STATE_RGB_WRITE
  439. | BGFX_STATE_ALPHA_WRITE
  440. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  441. | BGFX_STATE_DEPTH_WRITE
  442. | BGFX_STATE_DEPTH_TEST_LESS
  443. | BGFX_STATE_CULL_CW //reflection matrix has inverted normals. using CCW instead of CW.
  444. | BGFX_STATE_MSAA
  445. , UINT32_MAX
  446. , BGFX_STENCIL_TEST_EQUAL
  447. | BGFX_STENCIL_FUNC_REF(1)
  448. | BGFX_STENCIL_FUNC_RMASK(1)
  449. | BGFX_STENCIL_OP_FAIL_S_KEEP
  450. | BGFX_STENCIL_OP_FAIL_Z_KEEP
  451. | BGFX_STENCIL_OP_PASS_Z_KEEP
  452. , BGFX_STENCIL_NONE
  453. },
  454. { // StencilReflection_BlendPlane
  455. BGFX_STATE_RGB_WRITE
  456. | BGFX_STATE_DEPTH_WRITE
  457. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_SRC_COLOR)
  458. | BGFX_STATE_DEPTH_TEST_LESS
  459. | BGFX_STATE_CULL_CCW
  460. | BGFX_STATE_MSAA
  461. , UINT32_MAX
  462. , BGFX_STENCIL_NONE
  463. , BGFX_STENCIL_NONE
  464. },
  465. { // StencilReflection_DrawScene
  466. BGFX_STATE_RGB_WRITE
  467. | BGFX_STATE_DEPTH_WRITE
  468. | BGFX_STATE_DEPTH_TEST_LESS
  469. | BGFX_STATE_CULL_CCW
  470. | BGFX_STATE_MSAA
  471. , UINT32_MAX
  472. , BGFX_STENCIL_NONE
  473. , BGFX_STENCIL_NONE
  474. },
  475. { // ProjectionShadows_DrawAmbient
  476. BGFX_STATE_RGB_WRITE
  477. | BGFX_STATE_DEPTH_WRITE // write depth !
  478. | BGFX_STATE_DEPTH_TEST_LESS
  479. | BGFX_STATE_CULL_CCW
  480. | BGFX_STATE_MSAA
  481. , UINT32_MAX
  482. , BGFX_STENCIL_NONE
  483. , BGFX_STENCIL_NONE
  484. },
  485. { // ProjectionShadows_CraftStencil
  486. BGFX_STATE_DEPTH_TEST_LESS
  487. | BGFX_STATE_MSAA
  488. , UINT32_MAX
  489. , BGFX_STENCIL_TEST_ALWAYS // pass always
  490. | BGFX_STENCIL_FUNC_REF(1) // value = 1
  491. | BGFX_STENCIL_FUNC_RMASK(0xff)
  492. | BGFX_STENCIL_OP_FAIL_S_KEEP
  493. | BGFX_STENCIL_OP_FAIL_Z_KEEP
  494. | BGFX_STENCIL_OP_PASS_Z_REPLACE // store the value
  495. , BGFX_STENCIL_NONE
  496. },
  497. { // ProjectionShadows_DrawDiffuse
  498. BGFX_STATE_RGB_WRITE
  499. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE)
  500. | BGFX_STATE_DEPTH_TEST_EQUAL
  501. | BGFX_STATE_CULL_CCW
  502. | BGFX_STATE_MSAA
  503. , UINT32_MAX
  504. , BGFX_STENCIL_TEST_NOTEQUAL
  505. | BGFX_STENCIL_FUNC_REF(1)
  506. | BGFX_STENCIL_FUNC_RMASK(1)
  507. | BGFX_STENCIL_OP_FAIL_S_KEEP
  508. | BGFX_STENCIL_OP_FAIL_Z_KEEP
  509. | BGFX_STENCIL_OP_PASS_Z_KEEP
  510. , BGFX_STENCIL_NONE
  511. },
  512. { // Custom_BlendLightTexture
  513. BGFX_STATE_RGB_WRITE
  514. | BGFX_STATE_ALPHA_WRITE
  515. | BGFX_STATE_DEPTH_WRITE
  516. | BGFX_STATE_DEPTH_TEST_LESS
  517. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_COLOR, BGFX_STATE_BLEND_INV_SRC_COLOR)
  518. | BGFX_STATE_CULL_CCW
  519. | BGFX_STATE_MSAA
  520. , UINT32_MAX
  521. , BGFX_STENCIL_NONE
  522. , BGFX_STENCIL_NONE
  523. },
  524. { // Custom_DrawPlaneBottom
  525. BGFX_STATE_RGB_WRITE
  526. | BGFX_STATE_CULL_CW
  527. | BGFX_STATE_MSAA
  528. , UINT32_MAX
  529. , BGFX_STENCIL_NONE
  530. , BGFX_STENCIL_NONE
  531. },
  532. };
  533. struct ViewState
  534. {
  535. ViewState(uint32_t _width = 1280, uint32_t _height = 720)
  536. : m_width(_width)
  537. , m_height(_height)
  538. {
  539. }
  540. uint32_t m_width;
  541. uint32_t m_height;
  542. float m_view[16];
  543. float m_proj[16];
  544. };
  545. struct ClearValues
  546. {
  547. ClearValues(uint32_t _clearRgba = 0x30303000
  548. , float _clearDepth = 1.0f
  549. , uint8_t _clearStencil = 0
  550. )
  551. : m_clearRgba(_clearRgba)
  552. , m_clearDepth(_clearDepth)
  553. , m_clearStencil(_clearStencil)
  554. { }
  555. uint32_t m_clearRgba;
  556. float m_clearDepth;
  557. uint8_t m_clearStencil;
  558. };
  559. void clearView(uint8_t _id, uint8_t _flags, const ClearValues& _clearValues)
  560. {
  561. bgfx::setViewClear(_id
  562. , _flags
  563. , _clearValues.m_clearRgba
  564. , _clearValues.m_clearDepth
  565. , _clearValues.m_clearStencil
  566. );
  567. // Keep track of cleared views
  568. s_clearMask |= 1 << _id;
  569. }
  570. void clearViewMask(uint32_t _viewMask, uint8_t _flags, const ClearValues& _clearValues)
  571. {
  572. bgfx::setViewClearMask(_viewMask
  573. , _flags
  574. , _clearValues.m_clearRgba
  575. , _clearValues.m_clearDepth
  576. , _clearValues.m_clearStencil
  577. );
  578. // Keep track of cleared views
  579. s_clearMask |= _viewMask;
  580. }
  581. void submit(uint8_t _id, int32_t _depth = 0)
  582. {
  583. // Submit
  584. bgfx::submit(_id, _depth);
  585. // Keep track of submited view ids
  586. s_viewMask |= 1 << _id;
  587. }
  588. void submitMask(uint32_t _viewMask, int32_t _depth = 0)
  589. {
  590. // Submit
  591. bgfx::submitMask(_viewMask, _depth);
  592. // Keep track of submited view ids
  593. s_viewMask |= _viewMask;
  594. }
  595. struct Aabb
  596. {
  597. float m_min[3];
  598. float m_max[3];
  599. };
  600. struct Obb
  601. {
  602. float m_mtx[16];
  603. };
  604. struct Sphere
  605. {
  606. float m_center[3];
  607. float m_radius;
  608. };
  609. struct Primitive
  610. {
  611. uint32_t m_startIndex;
  612. uint32_t m_numIndices;
  613. uint32_t m_startVertex;
  614. uint32_t m_numVertices;
  615. Sphere m_sphere;
  616. Aabb m_aabb;
  617. Obb m_obb;
  618. };
  619. typedef std::vector<Primitive> PrimitiveArray;
  620. struct Group
  621. {
  622. Group()
  623. {
  624. reset();
  625. }
  626. void reset()
  627. {
  628. m_vbh.idx = bgfx::invalidHandle;
  629. m_ibh.idx = bgfx::invalidHandle;
  630. m_prims.clear();
  631. }
  632. bgfx::VertexBufferHandle m_vbh;
  633. bgfx::IndexBufferHandle m_ibh;
  634. Sphere m_sphere;
  635. Aabb m_aabb;
  636. Obb m_obb;
  637. PrimitiveArray m_prims;
  638. };
  639. struct Mesh
  640. {
  641. void load(const void* _vertices, uint32_t _numVertices, const bgfx::VertexDecl _decl
  642. , const uint16_t* _indices, uint32_t _numIndices)
  643. {
  644. Group group;
  645. const bgfx::Memory* mem;
  646. uint32_t size;
  647. size = _numVertices*_decl.getStride();
  648. mem = bgfx::makeRef(_vertices, size);
  649. group.m_vbh = bgfx::createVertexBuffer(mem, _decl);
  650. size = _numIndices*2;
  651. mem = bgfx::makeRef(_indices, size);
  652. group.m_ibh = bgfx::createIndexBuffer(mem);
  653. //TODO:
  654. // group.m_sphere = ...
  655. // group.m_aabb = ...
  656. // group.m_obb = ...
  657. // group.m_prims = ...
  658. m_groups.push_back(group);
  659. }
  660. void load(const char* _filePath)
  661. {
  662. #define BGFX_CHUNK_MAGIC_VB BX_MAKEFOURCC('V', 'B', ' ', 0x0)
  663. #define BGFX_CHUNK_MAGIC_IB BX_MAKEFOURCC('I', 'B', ' ', 0x0)
  664. #define BGFX_CHUNK_MAGIC_PRI BX_MAKEFOURCC('P', 'R', 'I', 0x0)
  665. bx::CrtFileReader reader;
  666. reader.open(_filePath);
  667. Group group;
  668. uint32_t chunk;
  669. while (4 == bx::read(&reader, chunk) )
  670. {
  671. switch (chunk)
  672. {
  673. case BGFX_CHUNK_MAGIC_VB:
  674. {
  675. bx::read(&reader, group.m_sphere);
  676. bx::read(&reader, group.m_aabb);
  677. bx::read(&reader, group.m_obb);
  678. bx::read(&reader, m_decl);
  679. uint16_t stride = m_decl.getStride();
  680. uint16_t numVertices;
  681. bx::read(&reader, numVertices);
  682. const bgfx::Memory* mem = bgfx::alloc(numVertices*stride);
  683. bx::read(&reader, mem->data, mem->size);
  684. group.m_vbh = bgfx::createVertexBuffer(mem, m_decl);
  685. }
  686. break;
  687. case BGFX_CHUNK_MAGIC_IB:
  688. {
  689. uint32_t numIndices;
  690. bx::read(&reader, numIndices);
  691. const bgfx::Memory* mem = bgfx::alloc(numIndices*2);
  692. bx::read(&reader, mem->data, mem->size);
  693. group.m_ibh = bgfx::createIndexBuffer(mem);
  694. }
  695. break;
  696. case BGFX_CHUNK_MAGIC_PRI:
  697. {
  698. uint16_t len;
  699. bx::read(&reader, len);
  700. std::string material;
  701. material.resize(len);
  702. bx::read(&reader, const_cast<char*>(material.c_str() ), len);
  703. uint16_t num;
  704. bx::read(&reader, num);
  705. for (uint32_t ii = 0; ii < num; ++ii)
  706. {
  707. bx::read(&reader, len);
  708. std::string name;
  709. name.resize(len);
  710. bx::read(&reader, const_cast<char*>(name.c_str() ), len);
  711. Primitive prim;
  712. bx::read(&reader, prim.m_startIndex);
  713. bx::read(&reader, prim.m_numIndices);
  714. bx::read(&reader, prim.m_startVertex);
  715. bx::read(&reader, prim.m_numVertices);
  716. bx::read(&reader, prim.m_sphere);
  717. bx::read(&reader, prim.m_aabb);
  718. bx::read(&reader, prim.m_obb);
  719. group.m_prims.push_back(prim);
  720. }
  721. m_groups.push_back(group);
  722. group.reset();
  723. }
  724. break;
  725. default:
  726. DBG("%08x at %d", chunk, reader.seek() );
  727. break;
  728. }
  729. }
  730. reader.close();
  731. }
  732. void unload()
  733. {
  734. for (GroupArray::const_iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
  735. {
  736. const Group& group = *it;
  737. bgfx::destroyVertexBuffer(group.m_vbh);
  738. if (bgfx::invalidHandle != group.m_ibh.idx)
  739. {
  740. bgfx::destroyIndexBuffer(group.m_ibh);
  741. }
  742. }
  743. m_groups.clear();
  744. }
  745. void submit(uint8_t _viewId, float* _mtx, bgfx::ProgramHandle _program, const RenderState& _renderState)
  746. {
  747. bgfx::TextureHandle texture = BGFX_INVALID_HANDLE;
  748. submit(_viewId, _mtx, _program, _renderState, texture);
  749. }
  750. void submit(uint8_t _viewId, float* _mtx, bgfx::ProgramHandle _program, const RenderState& _renderState, bgfx::TextureHandle _texture)
  751. {
  752. for (GroupArray::const_iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
  753. {
  754. const Group& group = *it;
  755. // Set uniforms
  756. s_uniforms.submitPerDrawUniforms();
  757. // Set model matrix for rendering.
  758. bgfx::setTransform(_mtx);
  759. bgfx::setProgram(_program);
  760. bgfx::setIndexBuffer(group.m_ibh);
  761. bgfx::setVertexBuffer(group.m_vbh);
  762. // Set texture
  763. if (bgfx::invalidHandle != _texture.idx)
  764. {
  765. bgfx::setTexture(0, u_texColor, _texture);
  766. }
  767. // Apply render state
  768. bgfx::setStencil(_renderState.m_fstencil, _renderState.m_bstencil);
  769. bgfx::setState(_renderState.m_state, _renderState.m_blendFactorRgba);
  770. // Submit
  771. ::submit(_viewId);
  772. }
  773. }
  774. bgfx::VertexDecl m_decl;
  775. typedef std::vector<Group> GroupArray;
  776. GroupArray m_groups;
  777. };
  778. int _main_(int /*_argc*/, char** /*_argv*/)
  779. {
  780. ViewState viewState(1280, 720);
  781. ClearValues clearValues(0x30303000, 1.0f, 0);
  782. uint32_t debug = BGFX_DEBUG_TEXT;
  783. uint32_t reset = BGFX_RESET_VSYNC;
  784. bgfx::init();
  785. bgfx::reset(viewState.m_width, viewState.m_height, reset);
  786. // Enable debug text.
  787. bgfx::setDebug(debug);
  788. // Setup root path for binary shaders. Shader binaries are different
  789. // for each renderer.
  790. switch (bgfx::getRendererType() )
  791. {
  792. default:
  793. case bgfx::RendererType::Direct3D9:
  794. s_shaderPath = "shaders/dx9/";
  795. break;
  796. case bgfx::RendererType::Direct3D11:
  797. s_shaderPath = "shaders/dx11/";
  798. break;
  799. case bgfx::RendererType::OpenGL:
  800. s_shaderPath = "shaders/glsl/";
  801. s_flipV = true;
  802. break;
  803. case bgfx::RendererType::OpenGLES2:
  804. case bgfx::RendererType::OpenGLES3:
  805. s_shaderPath = "shaders/gles/";
  806. s_flipV = true;
  807. break;
  808. }
  809. FILE* file = fopen("font/droidsans.ttf", "rb");
  810. uint32_t size = (uint32_t)fsize(file);
  811. void* data = malloc(size);
  812. size_t ignore = fread(data, 1, size, file);
  813. BX_UNUSED(ignore);
  814. fclose(file);
  815. imguiCreate(data, size);
  816. bgfx::VertexDecl PosNormalTexcoordDecl;
  817. PosNormalTexcoordDecl.begin();
  818. PosNormalTexcoordDecl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
  819. PosNormalTexcoordDecl.add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true);
  820. PosNormalTexcoordDecl.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float);
  821. PosNormalTexcoordDecl.end();
  822. s_uniforms.init();
  823. s_uniforms.submitConstUniforms();
  824. u_texColor = bgfx::createUniform("u_texColor", bgfx::UniformType::Uniform1iv);
  825. bgfx::ProgramHandle programTextureLightning = loadProgram("vs_stencil_texture_lightning", "fs_stencil_texture_lightning");
  826. bgfx::ProgramHandle programColorLightning = loadProgram("vs_stencil_color_lightning", "fs_stencil_color_lightning" );
  827. bgfx::ProgramHandle programColorTexture = loadProgram("vs_stencil_color_texture", "fs_stencil_color_texture" );
  828. bgfx::ProgramHandle programColorBlack = loadProgram("vs_stencil_color", "fs_stencil_color_black" );
  829. bgfx::ProgramHandle programTexture = loadProgram("vs_stencil_texture", "fs_stencil_texture" );
  830. Mesh bunnyMesh;
  831. Mesh columnMesh;
  832. Mesh cubeMesh;
  833. Mesh hplaneMesh;
  834. Mesh vplaneMesh;
  835. bunnyMesh.load("meshes/bunny.bin");
  836. columnMesh.load("meshes/column.bin");
  837. cubeMesh.load(s_cubeVertices, s_numCubeVertices, PosNormalTexcoordDecl, s_cubeIndices, s_numCubeIndices);
  838. hplaneMesh.load(s_hplaneVertices, s_numHPlaneVertices, PosNormalTexcoordDecl, s_planeIndices, s_numPlaneIndices);
  839. vplaneMesh.load(s_vplaneVertices, s_numVPlaneVertices, PosNormalTexcoordDecl, s_planeIndices, s_numPlaneIndices);
  840. const bgfx::Memory* mem;
  841. mem = loadTexture("figure-rgba.dds");
  842. bgfx::TextureHandle figureTex = bgfx::createTexture(mem);
  843. mem = loadTexture("flare.dds");
  844. bgfx::TextureHandle flareTex = bgfx::createTexture(mem);
  845. mem = loadTexture("fieldstone-rgba.dds");
  846. bgfx::TextureHandle fieldstoneTex = bgfx::createTexture(mem);
  847. // Setup lights.
  848. const uint8_t colorCount = 5;
  849. const float rgbInnerR[colorCount][4] =
  850. {
  851. { 1.0f, 0.7f, 0.2f, 0.0f }, //yellow
  852. { 0.7f, 0.2f, 1.0f, 0.0f }, //purple
  853. { 0.2f, 1.0f, 0.7f, 0.0f }, //cyan
  854. { 1.0f, 0.4f, 0.2f, 0.0f }, //orange
  855. { 0.7f, 0.7f, 0.7f, 0.0f }, //white
  856. };
  857. float lightRgbInnerR[MAX_NUM_LIGHTS][4];
  858. for (uint8_t ii = 0, jj = 0; ii < MAX_NUM_LIGHTS; ++ii, ++jj)
  859. {
  860. const uint8_t index = jj%colorCount;
  861. lightRgbInnerR[ii][0] = rgbInnerR[index][0];
  862. lightRgbInnerR[ii][1] = rgbInnerR[index][1];
  863. lightRgbInnerR[ii][2] = rgbInnerR[index][2];
  864. lightRgbInnerR[ii][3] = rgbInnerR[index][3];
  865. }
  866. memcpy(s_uniforms.m_lightRgbInnerR, lightRgbInnerR, MAX_NUM_LIGHTS * 4*sizeof(float));
  867. int64_t timeOffset = bx::getHPCounter();
  868. enum Scene
  869. {
  870. StencilReflectionScene = 0,
  871. ProjectionShadowsScene,
  872. };
  873. Scene scene = StencilReflectionScene;
  874. float settings_numLights = 4.0f;
  875. float settings_reflectionValue = 0.8f;
  876. bool settings_updateLights = true;
  877. bool settings_updateScene = true;
  878. static const char* titles[3] =
  879. {
  880. "Stencil Reflection Scene",
  881. "Projection Shadows Scene",
  882. };
  883. entry::MouseState mouseState;
  884. while (!entry::processEvents(viewState.m_width, viewState.m_height, debug, reset, &mouseState) )
  885. {
  886. imguiBeginFrame(mouseState.m_mx
  887. , mouseState.m_my
  888. , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  889. | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  890. , 0
  891. , viewState.m_width
  892. , viewState.m_height
  893. );
  894. static int32_t scrollArea = 0;
  895. imguiBeginScrollArea("Settings", viewState.m_width - 256 - 10, 10, 256, 215, &scrollArea);
  896. if (imguiCheck(titles[StencilReflectionScene], StencilReflectionScene == scene) )
  897. {
  898. scene = StencilReflectionScene;
  899. settings_numLights = 4.0f;
  900. }
  901. if (imguiCheck(titles[ProjectionShadowsScene], ProjectionShadowsScene == scene) )
  902. {
  903. scene = ProjectionShadowsScene;
  904. settings_numLights = 1.0f;
  905. }
  906. imguiSeparatorLine();
  907. imguiSlider("Lights", &settings_numLights, 1.0f, float(MAX_NUM_LIGHTS), 1.0f);
  908. if (scene == StencilReflectionScene)
  909. {
  910. imguiSlider("Reflection value", &settings_reflectionValue, 0.0f, 1.0f, 0.01f);
  911. }
  912. if (imguiCheck("Update lights", settings_updateLights) )
  913. {
  914. settings_updateLights = !settings_updateLights;
  915. }
  916. if (imguiCheck("Update scene", settings_updateScene) )
  917. {
  918. settings_updateScene = !settings_updateScene;
  919. }
  920. imguiEndScrollArea();
  921. imguiEndFrame();
  922. // Update settings.
  923. uint8_t numLights = (uint8_t)settings_numLights;
  924. s_uniforms.m_params.m_ambientPass = 1.0f;
  925. s_uniforms.m_params.m_lightningPass = 1.0f;
  926. s_uniforms.m_params.m_lightCount = settings_numLights;
  927. s_uniforms.m_params.m_lightIndex = 0.0f;
  928. s_uniforms.m_color[3] = settings_reflectionValue;
  929. s_uniforms.submitPerFrameUniforms();
  930. // Time.
  931. int64_t now = bx::getHPCounter();
  932. static int64_t last = now;
  933. const int64_t frameTime = now - last;
  934. last = now;
  935. const double freq = double(bx::getHPFrequency() );
  936. const double toMs = 1000.0/freq;
  937. float time = (float)( (now - timeOffset)/double(bx::getHPFrequency() ) );
  938. const float deltaTime = float(frameTime/freq);
  939. s_uniforms.m_time = time;
  940. // Use debug font to print information about this example.
  941. bgfx::dbgTextClear();
  942. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/13-stencil");
  943. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Stencil reflections and shadows.");
  944. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  945. // Set view and projection matrices.
  946. const float aspect = float(viewState.m_width)/float(viewState.m_height);
  947. mtxProj(viewState.m_proj, 60.0f, aspect, 0.1f, 100.0f);
  948. float at[3] = { 0.0f, 5.0f, 0.0f };
  949. float eye[3] = { 0.0f, 18.0f, -40.0f };
  950. mtxLookAt(viewState.m_view, eye, at);
  951. static float lightTimeAccumulator = 0.0f;
  952. if (settings_updateLights)
  953. {
  954. lightTimeAccumulator += deltaTime;
  955. }
  956. static float sceneTimeAccumulator = 0.0f;
  957. if (settings_updateScene)
  958. {
  959. sceneTimeAccumulator += deltaTime;
  960. }
  961. float lightPosRadius[MAX_NUM_LIGHTS][4];
  962. const float radius = (scene == StencilReflectionScene) ? 15.0f : 25.0f;
  963. for (uint8_t ii = 0; ii < numLights; ++ii)
  964. {
  965. lightPosRadius[ii][0] = sin( (lightTimeAccumulator*1.1f + ii*0.03f + float(ii*M_PI_2)*1.07f ) )*20.0f;
  966. lightPosRadius[ii][1] = 8.0f + (1.0f - cos( (lightTimeAccumulator*1.5f + ii*0.29f + float(ii*M_PI_2)*1.49f ) ))*4.0f;
  967. lightPosRadius[ii][2] = cos( (lightTimeAccumulator*1.3f + ii*0.13f + float(ii*M_PI_2)*1.79f ) )*20.0f;
  968. lightPosRadius[ii][3] = radius;
  969. }
  970. memcpy(s_uniforms.m_lightPosRadius, lightPosRadius, numLights * 4*sizeof(float));
  971. // Floor position.
  972. float floorMtx[16];
  973. mtxScaleRotateTranslate(floorMtx
  974. , 20.0f //scaleX
  975. , 20.0f //scaleY
  976. , 20.0f //scaleZ
  977. , 0.0f //rotX
  978. , 0.0f //rotY
  979. , 0.0f //rotZ
  980. , 0.0f //translateX
  981. , 0.0f //translateY
  982. , 0.0f //translateZ
  983. );
  984. // Bunny position.
  985. float bunnyMtx[16];
  986. mtxScaleRotateTranslate(bunnyMtx
  987. , 5.0f
  988. , 5.0f
  989. , 5.0f
  990. , 0.0f
  991. , 1.56f - sceneTimeAccumulator
  992. , 0.0f
  993. , 0.0f
  994. , 2.0f
  995. , 0.0f
  996. );
  997. // Columns position.
  998. const float dist = 14.0f;
  999. const float columnPositions[4][3] =
  1000. {
  1001. { dist, 0.0f, dist },
  1002. { -dist, 0.0f, dist },
  1003. { dist, 0.0f, -dist },
  1004. { -dist, 0.0f, -dist },
  1005. };
  1006. float columnMtx[4][16];
  1007. for (uint8_t ii = 0; ii < 4; ++ii)
  1008. {
  1009. mtxScaleRotateTranslate(columnMtx[ii]
  1010. , 1.0f
  1011. , 1.0f
  1012. , 1.0f
  1013. , 0.0f
  1014. , 0.0f
  1015. , 0.0f
  1016. , columnPositions[ii][0]
  1017. , columnPositions[ii][1]
  1018. , columnPositions[ii][2]
  1019. );
  1020. }
  1021. const uint8_t numCubes = 9;
  1022. float cubeMtx[numCubes][16];
  1023. for (uint16_t ii = 0; ii < numCubes; ++ii)
  1024. {
  1025. mtxScaleRotateTranslate(cubeMtx[ii]
  1026. , 1.0f
  1027. , 1.0f
  1028. , 1.0f
  1029. , 0.0f
  1030. , 0.0f
  1031. , 0.0f
  1032. , sin(ii * 2.0f + 13.0f - sceneTimeAccumulator) * 13.0f
  1033. , 4.0f
  1034. , cos(ii * 2.0f + 13.0f - sceneTimeAccumulator) * 13.0f
  1035. );
  1036. }
  1037. // Make sure at the beginning everything gets cleared.
  1038. clearView(0, BGFX_CLEAR_COLOR_BIT | BGFX_CLEAR_DEPTH_BIT | BGFX_CLEAR_STENCIL_BIT, clearValues);
  1039. submit(0);
  1040. // Bunny and columns color.
  1041. s_uniforms.m_color[0] = 0.70f;
  1042. s_uniforms.m_color[1] = 0.65f;
  1043. s_uniforms.m_color[2] = 0.60f;
  1044. switch (scene)
  1045. {
  1046. case StencilReflectionScene:
  1047. {
  1048. // First pass - Draw plane.
  1049. // Setup params for this scene.
  1050. s_uniforms.m_params.m_ambientPass = 1.0f;
  1051. s_uniforms.m_params.m_lightningPass = 1.0f;
  1052. // Floor.
  1053. hplaneMesh.submit(RENDER_VIEWID_RANGE1_PASS_0
  1054. , floorMtx
  1055. , programColorBlack
  1056. , s_renderStates[RenderState::StencilReflection_CraftStencil]
  1057. );
  1058. // Second pass - Draw reflected objects.
  1059. // Clear depth from previous pass.
  1060. clearView(RENDER_VIEWID_RANGE1_PASS_1, BGFX_CLEAR_DEPTH_BIT, clearValues);
  1061. // Compute reflected matrix.
  1062. float reflectMtx[16];
  1063. float plane_pos[3] = { 0.0f, 0.01f, 0.0f };
  1064. float normal[3] = { 0.0f, 1.0f, 0.0f };
  1065. mtxReflected(reflectMtx, plane_pos, normal);
  1066. // Reflect lights.
  1067. float reflectedLights[MAX_NUM_LIGHTS][4];
  1068. for (uint8_t ii = 0; ii < numLights; ++ii)
  1069. {
  1070. vec3MulMtx(reflectedLights[ii], lightPosRadius[ii], reflectMtx);
  1071. reflectedLights[ii][3] = lightPosRadius[ii][3];
  1072. }
  1073. memcpy(s_uniforms.m_lightPosRadius, reflectedLights, numLights * 4*sizeof(float));
  1074. // Reflect and submit bunny.
  1075. float mtxReflectedBunny[16];
  1076. mtxMul(mtxReflectedBunny, bunnyMtx, reflectMtx);
  1077. bunnyMesh.submit(RENDER_VIEWID_RANGE1_PASS_1
  1078. , mtxReflectedBunny
  1079. , programColorLightning
  1080. , s_renderStates[RenderState::StencilReflection_DrawReflected]
  1081. );
  1082. // Reflect and submit columns.
  1083. float mtxReflectedColumn[16];
  1084. for (uint8_t ii = 0; ii < 4; ++ii)
  1085. {
  1086. mtxMul(mtxReflectedColumn, columnMtx[ii], reflectMtx);
  1087. columnMesh.submit(RENDER_VIEWID_RANGE1_PASS_1
  1088. , mtxReflectedColumn
  1089. , programColorLightning
  1090. , s_renderStates[RenderState::StencilReflection_DrawReflected]
  1091. );
  1092. }
  1093. // Set lights back.
  1094. memcpy(s_uniforms.m_lightPosRadius, lightPosRadius, numLights * 4*sizeof(float));
  1095. // Third pass - Blend plane.
  1096. // Floor.
  1097. hplaneMesh.submit(RENDER_VIEWID_RANGE1_PASS_2
  1098. , floorMtx
  1099. , programTextureLightning
  1100. , s_renderStates[RenderState::StencilReflection_BlendPlane]
  1101. , fieldstoneTex
  1102. );
  1103. // Fourth pass - Draw everything else but the plane.
  1104. // Bunny.
  1105. bunnyMesh.submit(RENDER_VIEWID_RANGE1_PASS_3
  1106. , bunnyMtx
  1107. , programColorLightning
  1108. , s_renderStates[RenderState::StencilReflection_DrawScene]
  1109. );
  1110. // Columns.
  1111. for (uint8_t ii = 0; ii < 4; ++ii)
  1112. {
  1113. columnMesh.submit(RENDER_VIEWID_RANGE1_PASS_3
  1114. , columnMtx[ii]
  1115. , programColorLightning
  1116. , s_renderStates[RenderState::StencilReflection_DrawScene]
  1117. );
  1118. }
  1119. }
  1120. break;
  1121. case ProjectionShadowsScene:
  1122. {
  1123. // First pass - Draw entire scene. (ambient only).
  1124. s_uniforms.m_params.m_ambientPass = 1.0f;
  1125. s_uniforms.m_params.m_lightningPass = 0.0f;
  1126. // Bunny.
  1127. bunnyMesh.submit(RENDER_VIEWID_RANGE1_PASS_0
  1128. , bunnyMtx
  1129. , programColorLightning
  1130. , s_renderStates[RenderState::ProjectionShadows_DrawAmbient]
  1131. );
  1132. // Floor.
  1133. hplaneMesh.submit(RENDER_VIEWID_RANGE1_PASS_0
  1134. , floorMtx
  1135. , programTextureLightning
  1136. , s_renderStates[RenderState::ProjectionShadows_DrawAmbient]
  1137. , fieldstoneTex
  1138. );
  1139. // Cubes.
  1140. for (uint8_t ii = 0; ii < numCubes; ++ii)
  1141. {
  1142. cubeMesh.submit(RENDER_VIEWID_RANGE1_PASS_0
  1143. , cubeMtx[ii]
  1144. , programTextureLightning
  1145. , s_renderStates[RenderState::ProjectionShadows_DrawAmbient]
  1146. , figureTex
  1147. );
  1148. }
  1149. // Ground plane.
  1150. float ground[4];
  1151. float plane_pos[3] = { 0.0f, 0.0f, 0.0f };
  1152. float normal[3] = { 0.0f, 1.0f, 0.0f };
  1153. memcpy(ground, normal, sizeof(float) * 3);
  1154. ground[3] = -vec3Dot(plane_pos, normal) - 0.01f; // - 0.01 against z-fighting
  1155. for (uint8_t ii = 0, viewId = RENDER_VIEWID_RANGE5_PASS_6; ii < numLights; ++ii, ++viewId)
  1156. {
  1157. // Clear stencil for this light source.
  1158. clearView(viewId, BGFX_CLEAR_STENCIL_BIT, clearValues);
  1159. // Draw shadow projection of scene objects.
  1160. // Get homogeneous light pos.
  1161. float* lightPos = lightPosRadius[ii];
  1162. float pos[4];
  1163. memcpy(pos, lightPos, sizeof(float) * 3);
  1164. pos[3] = 1.0f;
  1165. // Calculate shadow mtx for current light.
  1166. float shadowMtx[16];
  1167. mtxShadow(shadowMtx, ground, pos);
  1168. // Submit bunny's shadow.
  1169. float mtxShadowedBunny[16];
  1170. mtxMul(mtxShadowedBunny, bunnyMtx, shadowMtx);
  1171. bunnyMesh.submit(viewId
  1172. , mtxShadowedBunny
  1173. , programColorBlack
  1174. , s_renderStates[RenderState::ProjectionShadows_CraftStencil]
  1175. );
  1176. // Submit cube shadows.
  1177. float mtxShadowedCube[16];
  1178. for (uint8_t jj = 0; jj < numCubes; ++jj)
  1179. {
  1180. mtxMul(mtxShadowedCube, cubeMtx[jj], shadowMtx);
  1181. cubeMesh.submit(viewId
  1182. , mtxShadowedCube
  1183. , programColorBlack
  1184. , s_renderStates[RenderState::ProjectionShadows_CraftStencil]
  1185. );
  1186. }
  1187. // Draw entire scene. (lightning pass only. blending is on)
  1188. s_uniforms.m_params.m_ambientPass = 0.0f;
  1189. s_uniforms.m_params.m_lightningPass = 1.0f;
  1190. s_uniforms.m_params.m_lightCount = 1.0f;
  1191. s_uniforms.m_params.m_lightIndex = float(ii);
  1192. // Bunny.
  1193. bunnyMesh.submit(viewId
  1194. , bunnyMtx
  1195. , programColorLightning
  1196. , s_renderStates[RenderState::ProjectionShadows_DrawDiffuse]
  1197. );
  1198. // Floor.
  1199. hplaneMesh.submit(viewId
  1200. , floorMtx
  1201. , programTextureLightning
  1202. , s_renderStates[RenderState::ProjectionShadows_DrawDiffuse]
  1203. , fieldstoneTex
  1204. );
  1205. // Cubes.
  1206. for (uint8_t ii = 0; ii < numCubes; ++ii)
  1207. {
  1208. cubeMesh.submit(viewId
  1209. , cubeMtx[ii]
  1210. , programTextureLightning
  1211. , s_renderStates[RenderState::ProjectionShadows_DrawDiffuse]
  1212. , figureTex
  1213. );
  1214. }
  1215. }
  1216. // Reset these to default..
  1217. s_uniforms.m_params.m_ambientPass = 1.0f;
  1218. s_uniforms.m_params.m_lightningPass = 1.0f;
  1219. }
  1220. break;
  1221. };
  1222. //lights
  1223. const float lightScale[3] = { 1.5f, 1.5f, 1.5f };
  1224. float lightMtx[16];
  1225. for (uint8_t ii = 0; ii < numLights; ++ii)
  1226. {
  1227. s_uniforms.m_color[0] = lightRgbInnerR[ii][0];
  1228. s_uniforms.m_color[1] = lightRgbInnerR[ii][1];
  1229. s_uniforms.m_color[2] = lightRgbInnerR[ii][2];
  1230. mtxBillboard(lightMtx, viewState.m_view, lightPosRadius[ii], lightScale);
  1231. vplaneMesh.submit(RENDER_VIEWID_RANGE1_PASS_7
  1232. , lightMtx
  1233. , programColorTexture
  1234. , s_renderStates[RenderState::Custom_BlendLightTexture]
  1235. , flareTex
  1236. );
  1237. }
  1238. // Draw floor bottom.
  1239. float floorBottomMtx[16];
  1240. mtxScaleRotateTranslate(floorBottomMtx
  1241. , 20.0f //scaleX
  1242. , 20.0f //scaleY
  1243. , 20.0f //scaleZ
  1244. , 0.0f //rotX
  1245. , 0.0f //rotY
  1246. , 0.0f //rotZ
  1247. , 0.0f //translateX
  1248. , -0.1f //translateY
  1249. , 0.0f //translateZ
  1250. );
  1251. hplaneMesh.submit(RENDER_VIEWID_RANGE1_PASS_7
  1252. , floorBottomMtx
  1253. , programTexture
  1254. , s_renderStates[RenderState::Custom_DrawPlaneBottom]
  1255. , figureTex
  1256. );
  1257. // Setup view rect and transform for all used views.
  1258. bgfx::setViewRectMask(s_viewMask, 0, 0, viewState.m_width, viewState.m_height);
  1259. bgfx::setViewTransformMask(s_viewMask, viewState.m_view, viewState.m_proj);
  1260. s_viewMask = 0;
  1261. // Advance to next frame. Rendering thread will be kicked to
  1262. // process submitted rendering primitives.
  1263. bgfx::frame();
  1264. //reset clear values on used views
  1265. clearViewMask(s_clearMask, BGFX_CLEAR_NONE, clearValues);
  1266. s_clearMask = 0;
  1267. }
  1268. // Cleanup.
  1269. bunnyMesh.unload();
  1270. columnMesh.unload();
  1271. cubeMesh.unload();
  1272. hplaneMesh.unload();
  1273. vplaneMesh.unload();
  1274. bgfx::destroyTexture(figureTex);
  1275. bgfx::destroyTexture(fieldstoneTex);
  1276. bgfx::destroyTexture(flareTex);
  1277. bgfx::destroyProgram(programTextureLightning);
  1278. bgfx::destroyProgram(programColorLightning);
  1279. bgfx::destroyProgram(programColorTexture);
  1280. bgfx::destroyProgram(programColorBlack);
  1281. bgfx::destroyProgram(programTexture);
  1282. bgfx::destroyUniform(u_texColor);
  1283. s_uniforms.destroy();
  1284. imguiDestroy();
  1285. // Shutdown bgfx.
  1286. bgfx::shutdown();
  1287. return 0;
  1288. }