ibl.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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 (bgfx::checkAvailTransientVertexBuffer(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(&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. strcpy(filePath, _name);
  131. strcat(filePath, "_lod.dds");
  132. m_tex = loadTexture(filePath, BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP);
  133. strcpy(filePath, _name);
  134. strcat(filePath, "_irr.dds");
  135. m_texIrr = loadTexture(filePath, BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP);
  136. }
  137. void destroy()
  138. {
  139. bgfx::destroyTexture(m_tex);
  140. bgfx::destroyTexture(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::fclamp(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::fmin(_dt/0.12f, 1.0f);
  247. consumeOrbit(amount);
  248. m_target.curr[0] = bx::flerp(m_target.curr[0], m_target.dest[0], amount);
  249. m_target.curr[1] = bx::flerp(m_target.curr[1], m_target.dest[1], amount);
  250. m_target.curr[2] = bx::flerp(m_target.curr[2], m_target.dest[2], amount);
  251. m_pos.curr[0] = bx::flerp(m_pos.curr[0], m_pos.dest[0], amount);
  252. m_pos.curr[1] = bx::flerp(m_pos.curr[1], m_pos.dest[1], amount);
  253. m_pos.curr[2] = bx::flerp(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::pi;
  299. const float theta = _v * bx::pi;
  300. const float st = bx::fsin(theta);
  301. const float sp = bx::fsin(phi);
  302. const float ct = bx::fcos(theta);
  303. const float cp = bx::fcos(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 = atan2f(_vec[0], _vec[2]);
  311. const float theta = acosf(_vec[1]);
  312. _u = (bx::pi + phi)*bx::invPi*0.5f;
  313. _v = theta*bx::invPi;
  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. {
  328. m_dx = 0.0f;
  329. m_dy = 0.0f;
  330. m_prevMx = 0.0f;
  331. m_prevMx = 0.0f;
  332. m_scroll = 0;
  333. m_scrollPrev = 0;
  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. int _main_(int _argc, char** _argv)
  356. {
  357. Args args(_argc, _argv);
  358. uint32_t width = 1280;
  359. uint32_t height = 720;
  360. uint32_t debug = BGFX_DEBUG_TEXT;
  361. uint32_t reset = BGFX_RESET_VSYNC;
  362. bgfx::init(args.m_type, args.m_pciId);
  363. bgfx::reset(width, height, reset);
  364. // Enable debug text.
  365. bgfx::setDebug(debug);
  366. // Set views clear state.
  367. bgfx::setViewClear(0
  368. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  369. , 0x303030ff
  370. , 1.0f
  371. , 0
  372. );
  373. // Imgui.
  374. imguiCreate();
  375. // Uniforms.
  376. Uniforms uniforms;
  377. uniforms.init();
  378. // Vertex declarations.
  379. PosColorTexCoord0Vertex::init();
  380. LightProbe lightProbes[LightProbe::Count];
  381. lightProbes[LightProbe::Bolonga].load("bolonga");
  382. lightProbes[LightProbe::Kyoto ].load("kyoto");
  383. LightProbe::Enum currentLightProbe = LightProbe::Bolonga;
  384. bgfx::UniformHandle u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
  385. bgfx::UniformHandle u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4);
  386. bgfx::UniformHandle u_flags = bgfx::createUniform("u_flags", bgfx::UniformType::Vec4);
  387. bgfx::UniformHandle u_camPos = bgfx::createUniform("u_camPos", bgfx::UniformType::Vec4);
  388. bgfx::UniformHandle s_texCube = bgfx::createUniform("s_texCube", bgfx::UniformType::Int1);
  389. bgfx::UniformHandle s_texCubeIrr = bgfx::createUniform("s_texCubeIrr", bgfx::UniformType::Int1);
  390. bgfx::ProgramHandle programMesh = loadProgram("vs_ibl_mesh", "fs_ibl_mesh");
  391. bgfx::ProgramHandle programSky = loadProgram("vs_ibl_skybox", "fs_ibl_skybox");
  392. Mesh* meshBunny;
  393. meshBunny = meshLoad("meshes/bunny.bin");
  394. Mesh* meshOrb;
  395. meshOrb = meshLoad("meshes/orb.bin");
  396. Camera camera;
  397. Mouse mouse;
  398. struct Settings
  399. {
  400. Settings()
  401. {
  402. m_envRotCurr = 0.0f;
  403. m_envRotDest = 0.0f;
  404. m_lightDir[0] = -0.8f;
  405. m_lightDir[1] = 0.2f;
  406. m_lightDir[2] = -0.5f;
  407. m_lightCol[0] = 1.0f;
  408. m_lightCol[1] = 1.0f;
  409. m_lightCol[2] = 1.0f;
  410. m_glossiness = 0.7f;
  411. m_exposure = 0.0f;
  412. m_bgType = 3.0f;
  413. m_radianceSlider = 2.0f;
  414. m_reflectivity = 0.85f;
  415. m_rgbDiff[0] = 1.0f;
  416. m_rgbDiff[1] = 1.0f;
  417. m_rgbDiff[2] = 1.0f;
  418. m_rgbSpec[0] = 1.0f;
  419. m_rgbSpec[1] = 1.0f;
  420. m_rgbSpec[2] = 1.0f;
  421. m_lod = 0.0f;
  422. m_doDiffuse = false;
  423. m_doSpecular = false;
  424. m_doDiffuseIbl = true;
  425. m_doSpecularIbl = true;
  426. m_showLightColorWheel = true;
  427. m_showDiffColorWheel = true;
  428. m_showSpecColorWheel = true;
  429. m_metalOrSpec = 0;
  430. m_meshSelection = 0;
  431. m_crossCubemapPreview = ImguiCubemap::Latlong;
  432. }
  433. float m_envRotCurr;
  434. float m_envRotDest;
  435. float m_lightDir[3];
  436. float m_lightCol[3];
  437. float m_glossiness;
  438. float m_exposure;
  439. float m_radianceSlider;
  440. float m_bgType;
  441. float m_reflectivity;
  442. float m_rgbDiff[3];
  443. float m_rgbSpec[3];
  444. float m_lod;
  445. bool m_doDiffuse;
  446. bool m_doSpecular;
  447. bool m_doDiffuseIbl;
  448. bool m_doSpecularIbl;
  449. bool m_showLightColorWheel;
  450. bool m_showDiffColorWheel;
  451. bool m_showSpecColorWheel;
  452. uint8_t m_metalOrSpec;
  453. uint8_t m_meshSelection;
  454. ImguiCubemap::Enum m_crossCubemapPreview;
  455. };
  456. Settings settings;
  457. int32_t leftScrollArea = 0;
  458. entry::MouseState mouseState;
  459. while (!entry::processEvents(width, height, debug, reset, &mouseState) )
  460. {
  461. imguiBeginFrame(mouseState.m_mx
  462. , mouseState.m_my
  463. , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  464. | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  465. | (mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  466. , mouseState.m_mz
  467. , width
  468. , height
  469. );
  470. static int32_t rightScrollArea = 0;
  471. imguiBeginScrollArea("", width - 256 - 10, 10, 256, 700, &rightScrollArea);
  472. imguiLabel("Environment light:");
  473. imguiIndent();
  474. imguiBool("IBL Diffuse", settings.m_doDiffuseIbl);
  475. imguiBool("IBL Specular", settings.m_doSpecularIbl);
  476. currentLightProbe = LightProbe::Enum(imguiTabs(currentLightProbe, true, ImguiAlign::LeftIndented, 16, 2, 2
  477. , "Bolonga"
  478. , "Kyoto"
  479. ) );
  480. if (imguiCube(lightProbes[currentLightProbe].m_tex, settings.m_lod, settings.m_crossCubemapPreview, true) )
  481. {
  482. settings.m_crossCubemapPreview = ImguiCubemap::Enum( (settings.m_crossCubemapPreview+1) % ImguiCubemap::Count);
  483. }
  484. imguiSlider("Texture LOD", settings.m_lod, 0.0f, 10.1f, 0.1f);
  485. imguiUnindent();
  486. imguiSeparator(8);
  487. imguiLabel("Directional light:");
  488. imguiIndent();
  489. imguiBool("Diffuse", settings.m_doDiffuse);
  490. imguiBool("Specular", settings.m_doSpecular);
  491. const bool doDirectLighting = settings.m_doDiffuse || settings.m_doSpecular;
  492. imguiSlider("Light direction X", settings.m_lightDir[0], -1.0f, 1.0f, 0.1f, doDirectLighting);
  493. imguiSlider("Light direction Y", settings.m_lightDir[1], -1.0f, 1.0f, 0.1f, doDirectLighting);
  494. imguiSlider("Light direction Z", settings.m_lightDir[2], -1.0f, 1.0f, 0.1f, doDirectLighting);
  495. imguiColorWheel("Color:", settings.m_lightCol, settings.m_showLightColorWheel, 0.6f, doDirectLighting);
  496. imguiUnindent();
  497. imguiSeparator(8);
  498. imguiLabel("Background:");
  499. imguiIndent();
  500. {
  501. int32_t selection;
  502. if (0.0f == settings.m_bgType) { selection = 0; }
  503. else if (7.0f == settings.m_bgType) { selection = 2; }
  504. else { selection = 1; }
  505. selection = imguiTabs(selection, true, ImguiAlign::LeftIndented, 16, 2, 3
  506. , "Skybox"
  507. , "Radiance"
  508. , "Irradiance"
  509. );
  510. if (0 == selection) { settings.m_bgType = 0.0f; }
  511. else if (2 == selection) { settings.m_bgType = 7.0f; }
  512. else { settings.m_bgType = settings.m_radianceSlider; }
  513. const bool isRadiance = (selection == 1);
  514. imguiSlider("Mip level", settings.m_radianceSlider, 1.0f, 6.0f, 0.1f, isRadiance);
  515. }
  516. imguiUnindent();
  517. imguiSeparator(8);
  518. imguiLabel("Post processing:");
  519. imguiIndent();
  520. imguiSlider("Exposure", settings.m_exposure, -4.0f, 4.0f, 0.1f);
  521. imguiUnindent();
  522. imguiSeparator();
  523. imguiEndScrollArea();
  524. imguiBeginScrollArea("", 10, 70, 256, 636, &leftScrollArea);
  525. imguiLabel("Mesh:");
  526. imguiIndent();
  527. settings.m_meshSelection = imguiChoose(settings.m_meshSelection, "Bunny", "Orbs");
  528. imguiUnindent();
  529. const bool isBunny = (0 == settings.m_meshSelection);
  530. if (!isBunny)
  531. {
  532. settings.m_metalOrSpec = 0;
  533. }
  534. imguiSeparator(4);
  535. imguiLabel("Workflow:");
  536. imguiIndent();
  537. if (imguiCheck("Metalness", 0 == settings.m_metalOrSpec, isBunny) ) { settings.m_metalOrSpec = 0; }
  538. if (imguiCheck("Specular", 1 == settings.m_metalOrSpec, isBunny) ) { settings.m_metalOrSpec = 1; }
  539. imguiUnindent();
  540. imguiSeparator(4);
  541. imguiLabel("Material:");
  542. imguiIndent();
  543. imguiSlider("Glossiness", settings.m_glossiness, 0.0f, 1.0f, 0.01f, isBunny);
  544. imguiSlider(0 == settings.m_metalOrSpec ? "Metalness" : "Diffuse - Specular", settings.m_reflectivity, 0.0f, 1.0f, 0.01f, isBunny);
  545. imguiUnindent();
  546. imguiColorWheel("Diffuse:", &settings.m_rgbDiff[0], settings.m_showDiffColorWheel, 0.7f);
  547. imguiSeparator();
  548. imguiColorWheel("Specular:", &settings.m_rgbSpec[0], settings.m_showSpecColorWheel, 0.7f, (1 == settings.m_metalOrSpec) && isBunny);
  549. imguiEndScrollArea();
  550. imguiEndFrame();
  551. uniforms.m_glossiness = settings.m_glossiness;
  552. uniforms.m_reflectivity = settings.m_reflectivity;
  553. uniforms.m_exposure = settings.m_exposure;
  554. uniforms.m_bgType = settings.m_bgType;
  555. uniforms.m_metalOrSpec = float(settings.m_metalOrSpec);
  556. uniforms.m_doDiffuse = float(settings.m_doDiffuse);
  557. uniforms.m_doSpecular = float(settings.m_doSpecular);
  558. uniforms.m_doDiffuseIbl = float(settings.m_doDiffuseIbl);
  559. uniforms.m_doSpecularIbl = float(settings.m_doSpecularIbl);
  560. memcpy(uniforms.m_rgbDiff, settings.m_rgbDiff, 3*sizeof(float) );
  561. memcpy(uniforms.m_rgbSpec, settings.m_rgbSpec, 3*sizeof(float) );
  562. memcpy(uniforms.m_lightDir, settings.m_lightDir, 3*sizeof(float) );
  563. memcpy(uniforms.m_lightCol, settings.m_lightCol, 3*sizeof(float) );
  564. int64_t now = bx::getHPCounter();
  565. static int64_t last = now;
  566. const int64_t frameTime = now - last;
  567. last = now;
  568. const double freq = double(bx::getHPFrequency() );
  569. const double toMs = 1000.0/freq;
  570. const float deltaTimeSec = float(double(frameTime)/freq);
  571. // Use debug font to print information about this example.
  572. bgfx::dbgTextClear();
  573. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/18-ibl");
  574. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Image-based lighting.");
  575. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  576. // Camera.
  577. const bool mouseOverGui = imguiMouseOverArea();
  578. mouse.update(float(mouseState.m_mx), float(mouseState.m_my), mouseState.m_mz, width, height);
  579. if (!mouseOverGui)
  580. {
  581. if (mouseState.m_buttons[entry::MouseButton::Left])
  582. {
  583. camera.orbit(mouse.m_dx, mouse.m_dy);
  584. }
  585. else if (mouseState.m_buttons[entry::MouseButton::Right])
  586. {
  587. camera.dolly(mouse.m_dx + mouse.m_dy);
  588. }
  589. else if (mouseState.m_buttons[entry::MouseButton::Middle])
  590. {
  591. settings.m_envRotDest += mouse.m_dx*2.0f;
  592. }
  593. else if (0 != mouse.m_scroll)
  594. {
  595. camera.dolly(float(mouse.m_scroll)*0.05f);
  596. }
  597. }
  598. camera.update(deltaTimeSec);
  599. memcpy(uniforms.m_cameraPos, camera.m_pos.curr, 3*sizeof(float) );
  600. // View Transform 0.
  601. float view[16];
  602. float proj[16];
  603. bx::mtxIdentity(view);
  604. bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f);
  605. bgfx::setViewTransform(0, view, proj);
  606. // View Transform 1.
  607. camera.mtxLookAt(view);
  608. bx::mtxProj(proj, 45.0f, float(width)/float(height), 0.1f, 100.0f);
  609. bgfx::setViewTransform(1, view, proj);
  610. // View rect.
  611. bgfx::setViewRect(0, 0, 0, width, height);
  612. bgfx::setViewRect(1, 0, 0, width, height);
  613. // Env rotation.
  614. const float amount = bx::fmin(deltaTimeSec/0.12f, 1.0f);
  615. settings.m_envRotCurr = bx::flerp(settings.m_envRotCurr, settings.m_envRotDest, amount);
  616. // Env mtx.
  617. float mtxEnvView[16];
  618. camera.envViewMtx(mtxEnvView);
  619. float mtxEnvRot[16];
  620. bx::mtxRotateY(mtxEnvRot, settings.m_envRotCurr);
  621. bx::mtxMul(uniforms.m_mtx, mtxEnvView, mtxEnvRot);
  622. // Submit view 0.
  623. bgfx::setTexture(0, s_texCube, lightProbes[currentLightProbe].m_tex);
  624. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  625. screenSpaceQuad( (float)width, (float)height, true);
  626. uniforms.submit();
  627. bgfx::submit(0, programSky);
  628. // Submit view 1.
  629. if (0 == settings.m_meshSelection)
  630. {
  631. // Submit bunny.
  632. float mtx[16];
  633. bx::mtxSRT(mtx, 1.0f, 1.0f, 1.0f, 0.0f, bx::pi, 0.0f, 0.0f, -0.80f, 0.0f);
  634. bgfx::setTexture(0, s_texCube, lightProbes[currentLightProbe].m_tex);
  635. bgfx::setTexture(1, s_texCubeIrr, lightProbes[currentLightProbe].m_texIrr);
  636. meshSubmit(meshBunny, 1, programMesh, mtx);
  637. }
  638. else
  639. {
  640. // Submit orbs.
  641. for (float yy = 0, yend = 5.0f; yy < yend; yy+=1.0f)
  642. {
  643. for (float xx = 0, xend = 5.0f; xx < xend; xx+=1.0f)
  644. {
  645. const float scale = 1.2f;
  646. const float spacing = 2.2f;
  647. const float yAdj = -0.8f;
  648. float mtx[16];
  649. bx::mtxSRT(mtx
  650. , scale/xend
  651. , scale/xend
  652. , scale/xend
  653. , 0.0f
  654. , 0.0f
  655. , 0.0f
  656. , 0.0f + (xx/xend)*spacing - (1.0f + (scale-1.0f)*0.5f - 1.0f/xend)
  657. , yAdj/yend + (yy/yend)*spacing - (1.0f + (scale-1.0f)*0.5f - 1.0f/yend)
  658. , 0.0f
  659. );
  660. uniforms.m_glossiness = xx*(1.0f/xend);
  661. uniforms.m_reflectivity = (yend-yy)*(1.0f/yend);
  662. uniforms.m_metalOrSpec = 0.0f;
  663. uniforms.submit();
  664. bgfx::setTexture(0, s_texCube, lightProbes[currentLightProbe].m_tex);
  665. bgfx::setTexture(1, s_texCubeIrr, lightProbes[currentLightProbe].m_texIrr);
  666. meshSubmit(meshOrb, 1, programMesh, mtx);
  667. }
  668. }
  669. }
  670. // Advance to next frame. Rendering thread will be kicked to
  671. // process submitted rendering primitives.
  672. bgfx::frame();
  673. }
  674. meshUnload(meshBunny);
  675. meshUnload(meshOrb);
  676. // Cleanup.
  677. bgfx::destroyProgram(programMesh);
  678. bgfx::destroyProgram(programSky);
  679. bgfx::destroyUniform(u_camPos);
  680. bgfx::destroyUniform(u_flags);
  681. bgfx::destroyUniform(u_params);
  682. bgfx::destroyUniform(u_mtx);
  683. bgfx::destroyUniform(s_texCube);
  684. bgfx::destroyUniform(s_texCubeIrr);
  685. for (uint8_t ii = 0; ii < LightProbe::Count; ++ii)
  686. {
  687. lightProbes[ii].destroy();
  688. }
  689. uniforms.destroy();
  690. imguiDestroy();
  691. // Shutdown bgfx.
  692. bgfx::shutdown();
  693. return 0;
  694. }