stencil.cpp 39 KB

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