ibl.cpp 22 KB

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