ibl.cpp 23 KB

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