ibl.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. /*
  2. * Copyright 2014-2016 Dario Manesku. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include <vector>
  6. #include <string>
  7. #include "common.h"
  8. #include "bgfx_utils.h"
  9. #include "imgui/imgui.h"
  10. #include "nanovg/nanovg.h"
  11. #include <bx/readerwriter.h>
  12. #include <bx/string.h>
  13. namespace
  14. {
  15. static float s_texelHalf = 0.0f;
  16. struct Uniforms
  17. {
  18. enum { NumVec4 = 12 };
  19. void init()
  20. {
  21. u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4, NumVec4);
  22. }
  23. void submit()
  24. {
  25. bgfx::setUniform(u_params, m_params, NumVec4);
  26. }
  27. void destroy()
  28. {
  29. bgfx::destroy(u_params);
  30. }
  31. union
  32. {
  33. struct
  34. {
  35. union
  36. {
  37. float m_mtx[16];
  38. /* 0*/ struct { float m_mtx0[4]; };
  39. /* 1*/ struct { float m_mtx1[4]; };
  40. /* 2*/ struct { float m_mtx2[4]; };
  41. /* 3*/ struct { float m_mtx3[4]; };
  42. };
  43. /* 4*/ struct { float m_glossiness, m_reflectivity, m_exposure, m_bgType; };
  44. /* 5*/ struct { float m_metalOrSpec, m_unused5[3]; };
  45. /* 6*/ struct { float m_doDiffuse, m_doSpecular, m_doDiffuseIbl, m_doSpecularIbl; };
  46. /* 7*/ struct { float m_cameraPos[3], m_unused7[1]; };
  47. /* 8*/ struct { float m_rgbDiff[4]; };
  48. /* 9*/ struct { float m_rgbSpec[4]; };
  49. /*10*/ struct { float m_lightDir[3], m_unused10[1]; };
  50. /*11*/ struct { float m_lightCol[3], m_unused11[1]; };
  51. };
  52. float m_params[NumVec4*4];
  53. };
  54. bgfx::UniformHandle u_params;
  55. };
  56. struct PosColorTexCoord0Vertex
  57. {
  58. float m_x;
  59. float m_y;
  60. float m_z;
  61. uint32_t m_rgba;
  62. float m_u;
  63. float m_v;
  64. static void init()
  65. {
  66. ms_decl
  67. .begin()
  68. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  69. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  70. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  71. .end();
  72. }
  73. static bgfx::VertexDecl ms_decl;
  74. };
  75. bgfx::VertexDecl PosColorTexCoord0Vertex::ms_decl;
  76. void screenSpaceQuad(float _textureWidth, float _textureHeight, bool _originBottomLeft = false, float _width = 1.0f, float _height = 1.0f)
  77. {
  78. if (3 == bgfx::getAvailTransientVertexBuffer(3, PosColorTexCoord0Vertex::ms_decl) )
  79. {
  80. bgfx::TransientVertexBuffer vb;
  81. bgfx::allocTransientVertexBuffer(&vb, 3, PosColorTexCoord0Vertex::ms_decl);
  82. PosColorTexCoord0Vertex* vertex = (PosColorTexCoord0Vertex*)vb.data;
  83. const float zz = 0.0f;
  84. const float minx = -_width;
  85. const float maxx = _width;
  86. const float miny = 0.0f;
  87. const float maxy = _height*2.0f;
  88. const float texelHalfW = s_texelHalf/_textureWidth;
  89. const float texelHalfH = s_texelHalf/_textureHeight;
  90. const float minu = -1.0f + texelHalfW;
  91. const float maxu = 1.0f + texelHalfW;
  92. float minv = texelHalfH;
  93. float maxv = 2.0f + texelHalfH;
  94. if (_originBottomLeft)
  95. {
  96. std::swap(minv, maxv);
  97. minv -= 1.0f;
  98. maxv -= 1.0f;
  99. }
  100. vertex[0].m_x = minx;
  101. vertex[0].m_y = miny;
  102. vertex[0].m_z = zz;
  103. vertex[0].m_rgba = 0xffffffff;
  104. vertex[0].m_u = minu;
  105. vertex[0].m_v = minv;
  106. vertex[1].m_x = maxx;
  107. vertex[1].m_y = miny;
  108. vertex[1].m_z = zz;
  109. vertex[1].m_rgba = 0xffffffff;
  110. vertex[1].m_u = maxu;
  111. vertex[1].m_v = minv;
  112. vertex[2].m_x = maxx;
  113. vertex[2].m_y = maxy;
  114. vertex[2].m_z = zz;
  115. vertex[2].m_rgba = 0xffffffff;
  116. vertex[2].m_u = maxu;
  117. vertex[2].m_v = maxv;
  118. bgfx::setVertexBuffer(0, &vb);
  119. }
  120. }
  121. struct LightProbe
  122. {
  123. enum Enum
  124. {
  125. Bolonga,
  126. Kyoto,
  127. Count
  128. };
  129. void load(const char* _name)
  130. {
  131. char filePath[512];
  132. bx::snprintf(filePath, BX_COUNTOF(filePath), "textures/%s_lod.dds", _name);
  133. m_tex = loadTexture(filePath, BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP);
  134. bx::snprintf(filePath, BX_COUNTOF(filePath), "textures/%s_irr.dds", _name);
  135. m_texIrr = loadTexture(filePath, BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP);
  136. }
  137. void destroy()
  138. {
  139. bgfx::destroy(m_tex);
  140. bgfx::destroy(m_texIrr);
  141. }
  142. bgfx::TextureHandle m_tex;
  143. bgfx::TextureHandle m_texIrr;
  144. };
  145. struct Camera
  146. {
  147. Camera()
  148. {
  149. reset();
  150. }
  151. void reset()
  152. {
  153. m_target.curr[0] = 0.0f;
  154. m_target.curr[1] = 0.0f;
  155. m_target.curr[2] = 0.0f;
  156. m_target.dest[0] = 0.0f;
  157. m_target.dest[1] = 0.0f;
  158. m_target.dest[2] = 0.0f;
  159. m_pos.curr[0] = 0.0f;
  160. m_pos.curr[1] = 0.0f;
  161. m_pos.curr[2] = -3.0f;
  162. m_pos.dest[0] = 0.0f;
  163. m_pos.dest[1] = 0.0f;
  164. m_pos.dest[2] = -3.0f;
  165. m_orbit[0] = 0.0f;
  166. m_orbit[1] = 0.0f;
  167. }
  168. void mtxLookAt(float* _outViewMtx)
  169. {
  170. bx::mtxLookAt(_outViewMtx, m_pos.curr, m_target.curr);
  171. }
  172. void orbit(float _dx, float _dy)
  173. {
  174. m_orbit[0] += _dx;
  175. m_orbit[1] += _dy;
  176. }
  177. void dolly(float _dz)
  178. {
  179. const float cnear = 1.0f;
  180. const float cfar = 10.0f;
  181. const float toTarget[3] =
  182. {
  183. m_target.dest[0] - m_pos.dest[0],
  184. m_target.dest[1] - m_pos.dest[1],
  185. m_target.dest[2] - m_pos.dest[2],
  186. };
  187. const float toTargetLen = bx::vec3Length(toTarget);
  188. const float invToTargetLen = 1.0f/(toTargetLen+FLT_MIN);
  189. const float toTargetNorm[3] =
  190. {
  191. toTarget[0]*invToTargetLen,
  192. toTarget[1]*invToTargetLen,
  193. toTarget[2]*invToTargetLen,
  194. };
  195. float delta = toTargetLen*_dz;
  196. float newLen = toTargetLen + delta;
  197. if ( (cnear < newLen || _dz < 0.0f)
  198. && (newLen < cfar || _dz > 0.0f) )
  199. {
  200. m_pos.dest[0] += toTargetNorm[0]*delta;
  201. m_pos.dest[1] += toTargetNorm[1]*delta;
  202. m_pos.dest[2] += toTargetNorm[2]*delta;
  203. }
  204. }
  205. void consumeOrbit(float _amount)
  206. {
  207. float consume[2];
  208. consume[0] = m_orbit[0]*_amount;
  209. consume[1] = m_orbit[1]*_amount;
  210. m_orbit[0] -= consume[0];
  211. m_orbit[1] -= consume[1];
  212. const float toPos[3] =
  213. {
  214. m_pos.curr[0] - m_target.curr[0],
  215. m_pos.curr[1] - m_target.curr[1],
  216. m_pos.curr[2] - m_target.curr[2],
  217. };
  218. const float toPosLen = bx::vec3Length(toPos);
  219. const float invToPosLen = 1.0f/(toPosLen+FLT_MIN);
  220. const float toPosNorm[3] =
  221. {
  222. toPos[0]*invToPosLen,
  223. toPos[1]*invToPosLen,
  224. toPos[2]*invToPosLen,
  225. };
  226. float ll[2];
  227. latLongFromVec(ll[0], ll[1], toPosNorm);
  228. ll[0] += consume[0];
  229. ll[1] -= consume[1];
  230. ll[1] = bx::clamp(ll[1], 0.02f, 0.98f);
  231. float tmp[3];
  232. vecFromLatLong(tmp, ll[0], ll[1]);
  233. float diff[3];
  234. diff[0] = (tmp[0]-toPosNorm[0])*toPosLen;
  235. diff[1] = (tmp[1]-toPosNorm[1])*toPosLen;
  236. diff[2] = (tmp[2]-toPosNorm[2])*toPosLen;
  237. m_pos.curr[0] += diff[0];
  238. m_pos.curr[1] += diff[1];
  239. m_pos.curr[2] += diff[2];
  240. m_pos.dest[0] += diff[0];
  241. m_pos.dest[1] += diff[1];
  242. m_pos.dest[2] += diff[2];
  243. }
  244. void update(float _dt)
  245. {
  246. const float amount = bx::min(_dt/0.12f, 1.0f);
  247. consumeOrbit(amount);
  248. m_target.curr[0] = bx::lerp(m_target.curr[0], m_target.dest[0], amount);
  249. m_target.curr[1] = bx::lerp(m_target.curr[1], m_target.dest[1], amount);
  250. m_target.curr[2] = bx::lerp(m_target.curr[2], m_target.dest[2], amount);
  251. m_pos.curr[0] = bx::lerp(m_pos.curr[0], m_pos.dest[0], amount);
  252. m_pos.curr[1] = bx::lerp(m_pos.curr[1], m_pos.dest[1], amount);
  253. m_pos.curr[2] = bx::lerp(m_pos.curr[2], m_pos.dest[2], amount);
  254. }
  255. void envViewMtx(float* _mtx)
  256. {
  257. const float toTarget[3] =
  258. {
  259. m_target.curr[0] - m_pos.curr[0],
  260. m_target.curr[1] - m_pos.curr[1],
  261. m_target.curr[2] - m_pos.curr[2],
  262. };
  263. const float toTargetLen = bx::vec3Length(toTarget);
  264. const float invToTargetLen = 1.0f/(toTargetLen+FLT_MIN);
  265. const float toTargetNorm[3] =
  266. {
  267. toTarget[0]*invToTargetLen,
  268. toTarget[1]*invToTargetLen,
  269. toTarget[2]*invToTargetLen,
  270. };
  271. float tmp[3];
  272. const float fakeUp[3] = { 0.0f, 1.0f, 0.0f };
  273. float right[3];
  274. bx::vec3Cross(tmp, fakeUp, toTargetNorm);
  275. bx::vec3Norm(right, tmp);
  276. float up[3];
  277. bx::vec3Cross(tmp, toTargetNorm, right);
  278. bx::vec3Norm(up, tmp);
  279. _mtx[ 0] = right[0];
  280. _mtx[ 1] = right[1];
  281. _mtx[ 2] = right[2];
  282. _mtx[ 3] = 0.0f;
  283. _mtx[ 4] = up[0];
  284. _mtx[ 5] = up[1];
  285. _mtx[ 6] = up[2];
  286. _mtx[ 7] = 0.0f;
  287. _mtx[ 8] = toTargetNorm[0];
  288. _mtx[ 9] = toTargetNorm[1];
  289. _mtx[10] = toTargetNorm[2];
  290. _mtx[11] = 0.0f;
  291. _mtx[12] = 0.0f;
  292. _mtx[13] = 0.0f;
  293. _mtx[14] = 0.0f;
  294. _mtx[15] = 1.0f;
  295. }
  296. static inline void vecFromLatLong(float _vec[3], float _u, float _v)
  297. {
  298. const float phi = _u * 2.0f*bx::kPi;
  299. const float theta = _v * bx::kPi;
  300. const float st = bx::sin(theta);
  301. const float sp = bx::sin(phi);
  302. const float ct = bx::cos(theta);
  303. const float cp = bx::cos(phi);
  304. _vec[0] = -st*sp;
  305. _vec[1] = ct;
  306. _vec[2] = -st*cp;
  307. }
  308. static inline void latLongFromVec(float& _u, float& _v, const float _vec[3])
  309. {
  310. const float phi = bx::atan2(_vec[0], _vec[2]);
  311. const float theta = bx::acos(_vec[1]);
  312. _u = (bx::kPi + phi)*bx::kInvPi*0.5f;
  313. _v = theta*bx::kInvPi;
  314. }
  315. struct Interp3f
  316. {
  317. float curr[3];
  318. float dest[3];
  319. };
  320. Interp3f m_target;
  321. Interp3f m_pos;
  322. float m_orbit[2];
  323. };
  324. struct Mouse
  325. {
  326. Mouse()
  327. : m_dx(0.0f)
  328. , m_dy(0.0f)
  329. , m_prevMx(0.0f)
  330. , m_prevMy(0.0f)
  331. , m_scroll(0)
  332. , m_scrollPrev(0)
  333. {
  334. }
  335. void update(float _mx, float _my, int32_t _mz, uint32_t _width, uint32_t _height)
  336. {
  337. const float widthf = float(int32_t(_width));
  338. const float heightf = float(int32_t(_height));
  339. // Delta movement.
  340. m_dx = float(_mx - m_prevMx)/widthf;
  341. m_dy = float(_my - m_prevMy)/heightf;
  342. m_prevMx = _mx;
  343. m_prevMy = _my;
  344. // Scroll.
  345. m_scroll = _mz - m_scrollPrev;
  346. m_scrollPrev = _mz;
  347. }
  348. float m_dx; // Screen space.
  349. float m_dy;
  350. float m_prevMx;
  351. float m_prevMy;
  352. int32_t m_scroll;
  353. int32_t m_scrollPrev;
  354. };
  355. struct Settings
  356. {
  357. Settings()
  358. {
  359. m_envRotCurr = 0.0f;
  360. m_envRotDest = 0.0f;
  361. m_lightDir[0] = -0.8f;
  362. m_lightDir[1] = 0.2f;
  363. m_lightDir[2] = -0.5f;
  364. m_lightCol[0] = 1.0f;
  365. m_lightCol[1] = 1.0f;
  366. m_lightCol[2] = 1.0f;
  367. m_glossiness = 0.7f;
  368. m_exposure = 0.0f;
  369. m_bgType = 3.0f;
  370. m_radianceSlider = 2.0f;
  371. m_reflectivity = 0.85f;
  372. m_rgbDiff[0] = 1.0f;
  373. m_rgbDiff[1] = 1.0f;
  374. m_rgbDiff[2] = 1.0f;
  375. m_rgbSpec[0] = 1.0f;
  376. m_rgbSpec[1] = 1.0f;
  377. m_rgbSpec[2] = 1.0f;
  378. m_lod = 0.0f;
  379. m_doDiffuse = false;
  380. m_doSpecular = false;
  381. m_doDiffuseIbl = true;
  382. m_doSpecularIbl = true;
  383. m_showLightColorWheel = true;
  384. m_showDiffColorWheel = true;
  385. m_showSpecColorWheel = true;
  386. m_metalOrSpec = 0;
  387. m_meshSelection = 0;
  388. }
  389. float m_envRotCurr;
  390. float m_envRotDest;
  391. float m_lightDir[3];
  392. float m_lightCol[3];
  393. float m_glossiness;
  394. float m_exposure;
  395. float m_radianceSlider;
  396. float m_bgType;
  397. float m_reflectivity;
  398. float m_rgbDiff[3];
  399. float m_rgbSpec[3];
  400. float m_lod;
  401. bool m_doDiffuse;
  402. bool m_doSpecular;
  403. bool m_doDiffuseIbl;
  404. bool m_doSpecularIbl;
  405. bool m_showLightColorWheel;
  406. bool m_showDiffColorWheel;
  407. bool m_showSpecColorWheel;
  408. int32_t m_metalOrSpec;
  409. int32_t m_meshSelection;
  410. };
  411. class ExampleIbl : public entry::AppI
  412. {
  413. public:
  414. ExampleIbl(const char* _name, const char* _description)
  415. : entry::AppI(_name, _description)
  416. {
  417. }
  418. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  419. {
  420. Args args(_argc, _argv);
  421. m_width = _width;
  422. m_height = _height;
  423. m_debug = BGFX_DEBUG_NONE;
  424. m_reset = 0
  425. | BGFX_RESET_VSYNC
  426. | BGFX_RESET_MSAA_X16
  427. ;
  428. bgfx::Init init;
  429. init.type = args.m_type;
  430. init.vendorId = args.m_pciId;
  431. init.resolution.width = m_width;
  432. init.resolution.height = m_height;
  433. init.resolution.reset = m_reset;
  434. bgfx::init(init);
  435. // Enable debug text.
  436. bgfx::setDebug(m_debug);
  437. // Set views clear state.
  438. bgfx::setViewClear(0
  439. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  440. , 0x303030ff
  441. , 1.0f
  442. , 0
  443. );
  444. // Imgui.
  445. imguiCreate();
  446. // Uniforms.
  447. m_uniforms.init();
  448. // Vertex declarations.
  449. PosColorTexCoord0Vertex::init();
  450. m_lightProbes[LightProbe::Bolonga].load("bolonga");
  451. m_lightProbes[LightProbe::Kyoto ].load("kyoto");
  452. m_currentLightProbe = LightProbe::Bolonga;
  453. u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
  454. u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4);
  455. u_flags = bgfx::createUniform("u_flags", bgfx::UniformType::Vec4);
  456. u_camPos = bgfx::createUniform("u_camPos", bgfx::UniformType::Vec4);
  457. s_texCube = bgfx::createUniform("s_texCube", bgfx::UniformType::Int1);
  458. s_texCubeIrr = bgfx::createUniform("s_texCubeIrr", bgfx::UniformType::Int1);
  459. m_programMesh = loadProgram("vs_ibl_mesh", "fs_ibl_mesh");
  460. m_programSky = loadProgram("vs_ibl_skybox", "fs_ibl_skybox");
  461. m_meshBunny = meshLoad("meshes/bunny.bin");
  462. m_meshOrb = meshLoad("meshes/orb.bin");
  463. }
  464. virtual int shutdown() override
  465. {
  466. meshUnload(m_meshBunny);
  467. meshUnload(m_meshOrb);
  468. // Cleanup.
  469. bgfx::destroy(m_programMesh);
  470. bgfx::destroy(m_programSky);
  471. bgfx::destroy(u_camPos);
  472. bgfx::destroy(u_flags);
  473. bgfx::destroy(u_params);
  474. bgfx::destroy(u_mtx);
  475. bgfx::destroy(s_texCube);
  476. bgfx::destroy(s_texCubeIrr);
  477. for (uint8_t ii = 0; ii < LightProbe::Count; ++ii)
  478. {
  479. m_lightProbes[ii].destroy();
  480. }
  481. m_uniforms.destroy();
  482. imguiDestroy();
  483. // Shutdown bgfx.
  484. bgfx::shutdown();
  485. return 0;
  486. }
  487. bool update() override
  488. {
  489. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  490. {
  491. imguiBeginFrame(m_mouseState.m_mx
  492. , m_mouseState.m_my
  493. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  494. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  495. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  496. , m_mouseState.m_mz
  497. , uint16_t(m_width)
  498. , uint16_t(m_height)
  499. );
  500. showExampleDialog(this);
  501. ImGui::SetNextWindowPos(
  502. ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
  503. , ImGuiCond_FirstUseEver
  504. );
  505. ImGui::SetNextWindowSize(
  506. ImVec2(m_width / 5.0f, m_height - 20.0f)
  507. , ImGuiCond_FirstUseEver
  508. );
  509. ImGui::Begin("Settings"
  510. , NULL
  511. , 0
  512. );
  513. ImGui::PushItemWidth(180.0f);
  514. ImGui::Text("Environment light:");
  515. ImGui::Indent();
  516. ImGui::Checkbox("IBL Diffuse", &m_settings.m_doDiffuseIbl);
  517. ImGui::Checkbox("IBL Specular", &m_settings.m_doSpecularIbl);
  518. {
  519. float tabWidth = ImGui::GetContentRegionAvailWidth() / 2.0f;
  520. if (ImGui::TabButton("Bolonga", tabWidth, m_currentLightProbe == LightProbe::Bolonga) )
  521. {
  522. m_currentLightProbe = LightProbe::Bolonga;
  523. }
  524. ImGui::SameLine(0.0f,0.0f);
  525. if (ImGui::TabButton("Kyoto", tabWidth, m_currentLightProbe == LightProbe::Kyoto) )
  526. {
  527. m_currentLightProbe = LightProbe::Kyoto;
  528. }
  529. }
  530. ImGui::SliderFloat("Texture LOD", &m_settings.m_lod, 0.0f, 10.1f);
  531. ImGui::Unindent();
  532. ImGui::Separator();
  533. ImGui::Text("Directional light:");
  534. ImGui::Indent();
  535. ImGui::Checkbox("Diffuse", &m_settings.m_doDiffuse);
  536. ImGui::Checkbox("Specular", &m_settings.m_doSpecular);
  537. const bool doDirectLighting = m_settings.m_doDiffuse || m_settings.m_doSpecular;
  538. if (doDirectLighting)
  539. {
  540. ImGui::SliderFloat("Light direction X", &m_settings.m_lightDir[0], -1.0f, 1.0f);
  541. ImGui::SliderFloat("Light direction Y", &m_settings.m_lightDir[1], -1.0f, 1.0f);
  542. ImGui::SliderFloat("Light direction Z", &m_settings.m_lightDir[2], -1.0f, 1.0f);
  543. ImGui::ColorWheel("Color:", m_settings.m_lightCol, 0.6f);
  544. }
  545. ImGui::Unindent();
  546. ImGui::Separator();
  547. ImGui::Text("Background:");
  548. ImGui::Indent();
  549. {
  550. int32_t selection;
  551. if (0.0f == m_settings.m_bgType)
  552. {
  553. selection = UINT8_C(0);
  554. }
  555. else if (7.0f == m_settings.m_bgType)
  556. {
  557. selection = UINT8_C(2);
  558. }
  559. else
  560. {
  561. selection = UINT8_C(1);
  562. }
  563. float tabWidth = ImGui::GetContentRegionAvailWidth() / 3.0f;
  564. if (ImGui::TabButton("Skybox", tabWidth, selection == 0) )
  565. {
  566. selection = 0;
  567. }
  568. ImGui::SameLine(0.0f,0.0f);
  569. if (ImGui::TabButton("Radiance", tabWidth, selection == 1) )
  570. {
  571. selection = 1;
  572. }
  573. ImGui::SameLine(0.0f,0.0f);
  574. if (ImGui::TabButton("Irradiance", tabWidth, selection == 2) )
  575. {
  576. selection = 2;
  577. }
  578. if (0 == selection)
  579. {
  580. m_settings.m_bgType = 0.0f;
  581. }
  582. else if (2 == selection)
  583. {
  584. m_settings.m_bgType = 7.0f;
  585. }
  586. else
  587. {
  588. m_settings.m_bgType = m_settings.m_radianceSlider;
  589. }
  590. const bool isRadiance = (selection == 1);
  591. if (isRadiance)
  592. {
  593. ImGui::SliderFloat("Mip level", &m_settings.m_radianceSlider, 1.0f, 6.0f);
  594. }
  595. }
  596. ImGui::Unindent();
  597. ImGui::Separator();
  598. ImGui::Text("Post processing:");
  599. ImGui::Indent();
  600. ImGui::SliderFloat("Exposure",& m_settings.m_exposure, -4.0f, 4.0f);
  601. ImGui::Unindent();
  602. ImGui::PopItemWidth();
  603. ImGui::End();
  604. ImGui::SetNextWindowPos(
  605. ImVec2(10.0f, 260.0f)
  606. , ImGuiCond_FirstUseEver
  607. );
  608. ImGui::SetNextWindowSize(
  609. ImVec2(m_width / 5.0f, 450.0f)
  610. , ImGuiCond_FirstUseEver
  611. );
  612. ImGui::Begin("Mesh"
  613. , NULL
  614. , 0
  615. );
  616. ImGui::Text("Mesh:");
  617. ImGui::Indent();
  618. ImGui::RadioButton("Bunny", &m_settings.m_meshSelection, 0);
  619. ImGui::RadioButton("Orbs", &m_settings.m_meshSelection, 1);
  620. ImGui::Unindent();
  621. const bool isBunny = (0 == m_settings.m_meshSelection);
  622. if (!isBunny)
  623. {
  624. m_settings.m_metalOrSpec = 0;
  625. }
  626. else
  627. {
  628. ImGui::Separator();
  629. ImGui::Text("Workflow:");
  630. ImGui::Indent();
  631. ImGui::RadioButton("Metalness", &m_settings.m_metalOrSpec, 0);
  632. ImGui::RadioButton("Specular", &m_settings.m_metalOrSpec, 1);
  633. ImGui::Unindent();
  634. ImGui::Separator();
  635. ImGui::Text("Material:");
  636. ImGui::Indent();
  637. ImGui::PushItemWidth(130.0f);
  638. ImGui::SliderFloat("Glossiness", &m_settings.m_glossiness, 0.0f, 1.0f);
  639. ImGui::SliderFloat(0 == m_settings.m_metalOrSpec ? "Metalness" : "Diffuse - Specular", &m_settings.m_reflectivity, 0.0f, 1.0f);
  640. ImGui::PopItemWidth();
  641. ImGui::Unindent();
  642. }
  643. ImGui::ColorWheel("Diffuse:", &m_settings.m_rgbDiff[0], 0.7f);
  644. ImGui::Separator();
  645. if ( (1 == m_settings.m_metalOrSpec) && isBunny )
  646. {
  647. ImGui::ColorWheel("Specular:", &m_settings.m_rgbSpec[0], 0.7f);
  648. }
  649. ImGui::End();
  650. imguiEndFrame();
  651. m_uniforms.m_glossiness = m_settings.m_glossiness;
  652. m_uniforms.m_reflectivity = m_settings.m_reflectivity;
  653. m_uniforms.m_exposure = m_settings.m_exposure;
  654. m_uniforms.m_bgType = m_settings.m_bgType;
  655. m_uniforms.m_metalOrSpec = float(m_settings.m_metalOrSpec);
  656. m_uniforms.m_doDiffuse = float(m_settings.m_doDiffuse);
  657. m_uniforms.m_doSpecular = float(m_settings.m_doSpecular);
  658. m_uniforms.m_doDiffuseIbl = float(m_settings.m_doDiffuseIbl);
  659. m_uniforms.m_doSpecularIbl = float(m_settings.m_doSpecularIbl);
  660. bx::memCopy(m_uniforms.m_rgbDiff, m_settings.m_rgbDiff, 3*sizeof(float) );
  661. bx::memCopy(m_uniforms.m_rgbSpec, m_settings.m_rgbSpec, 3*sizeof(float) );
  662. bx::memCopy(m_uniforms.m_lightDir, m_settings.m_lightDir, 3*sizeof(float) );
  663. bx::memCopy(m_uniforms.m_lightCol, m_settings.m_lightCol, 3*sizeof(float) );
  664. int64_t now = bx::getHPCounter();
  665. static int64_t last = now;
  666. const int64_t frameTime = now - last;
  667. last = now;
  668. const double freq = double(bx::getHPFrequency() );
  669. const float deltaTimeSec = float(double(frameTime)/freq);
  670. // Camera.
  671. const bool mouseOverGui = ImGui::MouseOverArea();
  672. m_mouse.update(float(m_mouseState.m_mx), float(m_mouseState.m_my), m_mouseState.m_mz, m_width, m_height);
  673. if (!mouseOverGui)
  674. {
  675. if (m_mouseState.m_buttons[entry::MouseButton::Left])
  676. {
  677. m_camera.orbit(m_mouse.m_dx, m_mouse.m_dy);
  678. }
  679. else if (m_mouseState.m_buttons[entry::MouseButton::Right])
  680. {
  681. m_camera.dolly(m_mouse.m_dx + m_mouse.m_dy);
  682. }
  683. else if (m_mouseState.m_buttons[entry::MouseButton::Middle])
  684. {
  685. m_settings.m_envRotDest += m_mouse.m_dx*2.0f;
  686. }
  687. else if (0 != m_mouse.m_scroll)
  688. {
  689. m_camera.dolly(float(m_mouse.m_scroll)*0.05f);
  690. }
  691. }
  692. m_camera.update(deltaTimeSec);
  693. bx::memCopy(m_uniforms.m_cameraPos, m_camera.m_pos.curr, 3*sizeof(float) );
  694. // View Transform 0.
  695. float view[16];
  696. bx::mtxIdentity(view);
  697. const bgfx::Caps* caps = bgfx::getCaps();
  698. float proj[16];
  699. bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f, 0.0, caps->homogeneousDepth);
  700. bgfx::setViewTransform(0, view, proj);
  701. // View Transform 1.
  702. m_camera.mtxLookAt(view);
  703. bx::mtxProj(proj, 45.0f, float(m_width)/float(m_height), 0.1f, 100.0f, caps->homogeneousDepth);
  704. bgfx::setViewTransform(1, view, proj);
  705. // View rect.
  706. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  707. bgfx::setViewRect(1, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  708. // Env rotation.
  709. const float amount = bx::min(deltaTimeSec/0.12f, 1.0f);
  710. m_settings.m_envRotCurr = bx::lerp(m_settings.m_envRotCurr, m_settings.m_envRotDest, amount);
  711. // Env mtx.
  712. float mtxEnvView[16];
  713. m_camera.envViewMtx(mtxEnvView);
  714. float mtxEnvRot[16];
  715. bx::mtxRotateY(mtxEnvRot, m_settings.m_envRotCurr);
  716. bx::mtxMul(m_uniforms.m_mtx, mtxEnvView, mtxEnvRot); // Used for Skybox.
  717. // Submit view 0.
  718. bgfx::setTexture(0, s_texCube, m_lightProbes[m_currentLightProbe].m_tex);
  719. bgfx::setTexture(1, s_texCubeIrr, m_lightProbes[m_currentLightProbe].m_texIrr);
  720. bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
  721. screenSpaceQuad( (float)m_width, (float)m_height, true);
  722. m_uniforms.submit();
  723. bgfx::submit(0, m_programSky);
  724. // Submit view 1.
  725. bx::memCopy(m_uniforms.m_mtx, mtxEnvRot, 16*sizeof(float)); // Used for IBL.
  726. if (0 == m_settings.m_meshSelection)
  727. {
  728. // Submit bunny.
  729. float mtx[16];
  730. bx::mtxSRT(mtx, 1.0f, 1.0f, 1.0f, 0.0f, bx::kPi, 0.0f, 0.0f, -0.80f, 0.0f);
  731. bgfx::setTexture(0, s_texCube, m_lightProbes[m_currentLightProbe].m_tex);
  732. bgfx::setTexture(1, s_texCubeIrr, m_lightProbes[m_currentLightProbe].m_texIrr);
  733. m_uniforms.submit();
  734. meshSubmit(m_meshBunny, 1, m_programMesh, mtx);
  735. }
  736. else
  737. {
  738. // Submit orbs.
  739. for (float yy = 0, yend = 5.0f; yy < yend; yy+=1.0f)
  740. {
  741. for (float xx = 0, xend = 5.0f; xx < xend; xx+=1.0f)
  742. {
  743. const float scale = 1.2f;
  744. const float spacing = 2.2f;
  745. const float yAdj = -0.8f;
  746. float mtx[16];
  747. bx::mtxSRT(mtx
  748. , scale/xend
  749. , scale/xend
  750. , scale/xend
  751. , 0.0f
  752. , 0.0f
  753. , 0.0f
  754. , 0.0f + (xx/xend)*spacing - (1.0f + (scale-1.0f)*0.5f - 1.0f/xend)
  755. , yAdj/yend + (yy/yend)*spacing - (1.0f + (scale-1.0f)*0.5f - 1.0f/yend)
  756. , 0.0f
  757. );
  758. m_uniforms.m_glossiness = xx*(1.0f/xend);
  759. m_uniforms.m_reflectivity = (yend-yy)*(1.0f/yend);
  760. m_uniforms.m_metalOrSpec = 0.0f;
  761. m_uniforms.submit();
  762. bgfx::setTexture(0, s_texCube, m_lightProbes[m_currentLightProbe].m_tex);
  763. bgfx::setTexture(1, s_texCubeIrr, m_lightProbes[m_currentLightProbe].m_texIrr);
  764. meshSubmit(m_meshOrb, 1, m_programMesh, mtx);
  765. }
  766. }
  767. }
  768. // Advance to next frame. Rendering thread will be kicked to
  769. // process submitted rendering primitives.
  770. bgfx::frame();
  771. return true;
  772. }
  773. return false;
  774. }
  775. uint32_t m_width;
  776. uint32_t m_height;
  777. uint32_t m_debug;
  778. uint32_t m_reset;
  779. entry::MouseState m_mouseState;
  780. Uniforms m_uniforms;
  781. LightProbe m_lightProbes[LightProbe::Count];
  782. LightProbe::Enum m_currentLightProbe;
  783. bgfx::UniformHandle u_mtx;
  784. bgfx::UniformHandle u_params;
  785. bgfx::UniformHandle u_flags;
  786. bgfx::UniformHandle u_camPos;
  787. bgfx::UniformHandle s_texCube;
  788. bgfx::UniformHandle s_texCubeIrr;
  789. bgfx::ProgramHandle m_programMesh;
  790. bgfx::ProgramHandle m_programSky;
  791. Mesh* m_meshBunny;
  792. Mesh* m_meshOrb;
  793. Camera m_camera;
  794. Mouse m_mouse;
  795. Settings m_settings;
  796. };
  797. } // namespace
  798. ENTRY_IMPLEMENT_MAIN(ExampleIbl, "18-ibl", "Image-based lighting.");