ibl.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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_SAMPLER_U_CLAMP|BGFX_SAMPLER_V_CLAMP|BGFX_SAMPLER_W_CLAMP);
  134. bx::snprintf(filePath, BX_COUNTOF(filePath), "textures/%s_irr.dds", _name);
  135. m_texIrr = loadTexture(filePath, BGFX_SAMPLER_U_CLAMP|BGFX_SAMPLER_V_CLAMP|BGFX_SAMPLER_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.0f, 0.0f, 0.0f };
  154. m_target.dest = { 0.0f, 0.0f, 0.0f };
  155. m_pos.curr = { 0.0f, 0.0f, -3.0f };
  156. m_pos.dest = { 0.0f, 0.0f, -3.0f };
  157. m_orbit[0] = 0.0f;
  158. m_orbit[1] = 0.0f;
  159. }
  160. void mtxLookAt(float* _outViewMtx)
  161. {
  162. bx::mtxLookAt(_outViewMtx, m_pos.curr, m_target.curr);
  163. }
  164. void orbit(float _dx, float _dy)
  165. {
  166. m_orbit[0] += _dx;
  167. m_orbit[1] += _dy;
  168. }
  169. void dolly(float _dz)
  170. {
  171. const float cnear = 1.0f;
  172. const float cfar = 100.0f;
  173. const bx::Vec3 toTarget = bx::sub(m_target.dest, m_pos.dest);
  174. const float toTargetLen = bx::length(toTarget);
  175. const float invToTargetLen = 1.0f / (toTargetLen + bx::kFloatMin);
  176. const bx::Vec3 toTargetNorm = bx::mul(toTarget, invToTargetLen);
  177. float delta = toTargetLen * _dz;
  178. float newLen = toTargetLen + delta;
  179. if ( (cnear < newLen || _dz < 0.0f)
  180. && (newLen < cfar || _dz > 0.0f) )
  181. {
  182. m_pos.dest = bx::mad(toTargetNorm, delta, m_pos.dest);
  183. }
  184. }
  185. void consumeOrbit(float _amount)
  186. {
  187. float consume[2];
  188. consume[0] = m_orbit[0] * _amount;
  189. consume[1] = m_orbit[1] * _amount;
  190. m_orbit[0] -= consume[0];
  191. m_orbit[1] -= consume[1];
  192. const bx::Vec3 toPos = bx::sub(m_pos.curr, m_target.curr);
  193. const float toPosLen = bx::length(toPos);
  194. const float invToPosLen = 1.0f / (toPosLen + bx::kFloatMin);
  195. const bx::Vec3 toPosNorm = bx::mul(toPos, invToPosLen);
  196. float ll[2];
  197. bx::toLatLong(&ll[0], &ll[1], toPosNorm);
  198. ll[0] += consume[0];
  199. ll[1] -= consume[1];
  200. ll[1] = bx::clamp(ll[1], 0.02f, 0.98f);
  201. const bx::Vec3 tmp = bx::fromLatLong(ll[0], ll[1]);
  202. const bx::Vec3 diff = bx::mul(bx::sub(tmp, toPosNorm), toPosLen);
  203. m_pos.curr = bx::add(m_pos.curr, diff);
  204. m_pos.dest = bx::add(m_pos.dest, diff);
  205. }
  206. void update(float _dt)
  207. {
  208. const float amount = bx::min(_dt / 0.12f, 1.0f);
  209. consumeOrbit(amount);
  210. m_target.curr = bx::lerp(m_target.curr, m_target.dest, amount);
  211. m_pos.curr = bx::lerp(m_pos.curr, m_pos.dest, amount);
  212. }
  213. void envViewMtx(float* _mtx)
  214. {
  215. const bx::Vec3 toTarget = bx::sub(m_target.curr, m_pos.curr);
  216. const float toTargetLen = bx::length(toTarget);
  217. const float invToTargetLen = 1.0f / (toTargetLen + bx::kFloatMin);
  218. const bx::Vec3 toTargetNorm = bx::mul(toTarget, invToTargetLen);
  219. const bx::Vec3 right = bx::normalize(bx::cross({ 0.0f, 1.0f, 0.0f }, toTargetNorm) );
  220. const bx::Vec3 up = bx::normalize(bx::cross(toTargetNorm, right) );
  221. _mtx[ 0] = right.x;
  222. _mtx[ 1] = right.y;
  223. _mtx[ 2] = right.z;
  224. _mtx[ 3] = 0.0f;
  225. _mtx[ 4] = up.x;
  226. _mtx[ 5] = up.y;
  227. _mtx[ 6] = up.z;
  228. _mtx[ 7] = 0.0f;
  229. _mtx[ 8] = toTargetNorm.x;
  230. _mtx[ 9] = toTargetNorm.y;
  231. _mtx[10] = toTargetNorm.z;
  232. _mtx[11] = 0.0f;
  233. _mtx[12] = 0.0f;
  234. _mtx[13] = 0.0f;
  235. _mtx[14] = 0.0f;
  236. _mtx[15] = 1.0f;
  237. }
  238. struct Interp3f
  239. {
  240. bx::Vec3 curr;
  241. bx::Vec3 dest;
  242. };
  243. Interp3f m_target;
  244. Interp3f m_pos;
  245. float m_orbit[2];
  246. };
  247. struct Mouse
  248. {
  249. Mouse()
  250. : m_dx(0.0f)
  251. , m_dy(0.0f)
  252. , m_prevMx(0.0f)
  253. , m_prevMy(0.0f)
  254. , m_scroll(0)
  255. , m_scrollPrev(0)
  256. {
  257. }
  258. void update(float _mx, float _my, int32_t _mz, uint32_t _width, uint32_t _height)
  259. {
  260. const float widthf = float(int32_t(_width));
  261. const float heightf = float(int32_t(_height));
  262. // Delta movement.
  263. m_dx = float(_mx - m_prevMx)/widthf;
  264. m_dy = float(_my - m_prevMy)/heightf;
  265. m_prevMx = _mx;
  266. m_prevMy = _my;
  267. // Scroll.
  268. m_scroll = _mz - m_scrollPrev;
  269. m_scrollPrev = _mz;
  270. }
  271. float m_dx; // Screen space.
  272. float m_dy;
  273. float m_prevMx;
  274. float m_prevMy;
  275. int32_t m_scroll;
  276. int32_t m_scrollPrev;
  277. };
  278. struct Settings
  279. {
  280. Settings()
  281. {
  282. m_envRotCurr = 0.0f;
  283. m_envRotDest = 0.0f;
  284. m_lightDir[0] = -0.8f;
  285. m_lightDir[1] = 0.2f;
  286. m_lightDir[2] = -0.5f;
  287. m_lightCol[0] = 1.0f;
  288. m_lightCol[1] = 1.0f;
  289. m_lightCol[2] = 1.0f;
  290. m_glossiness = 0.7f;
  291. m_exposure = 0.0f;
  292. m_bgType = 3.0f;
  293. m_radianceSlider = 2.0f;
  294. m_reflectivity = 0.85f;
  295. m_rgbDiff[0] = 1.0f;
  296. m_rgbDiff[1] = 1.0f;
  297. m_rgbDiff[2] = 1.0f;
  298. m_rgbSpec[0] = 1.0f;
  299. m_rgbSpec[1] = 1.0f;
  300. m_rgbSpec[2] = 1.0f;
  301. m_lod = 0.0f;
  302. m_doDiffuse = false;
  303. m_doSpecular = false;
  304. m_doDiffuseIbl = true;
  305. m_doSpecularIbl = true;
  306. m_showLightColorWheel = true;
  307. m_showDiffColorWheel = true;
  308. m_showSpecColorWheel = true;
  309. m_metalOrSpec = 0;
  310. m_meshSelection = 0;
  311. }
  312. float m_envRotCurr;
  313. float m_envRotDest;
  314. float m_lightDir[3];
  315. float m_lightCol[3];
  316. float m_glossiness;
  317. float m_exposure;
  318. float m_radianceSlider;
  319. float m_bgType;
  320. float m_reflectivity;
  321. float m_rgbDiff[3];
  322. float m_rgbSpec[3];
  323. float m_lod;
  324. bool m_doDiffuse;
  325. bool m_doSpecular;
  326. bool m_doDiffuseIbl;
  327. bool m_doSpecularIbl;
  328. bool m_showLightColorWheel;
  329. bool m_showDiffColorWheel;
  330. bool m_showSpecColorWheel;
  331. int32_t m_metalOrSpec;
  332. int32_t m_meshSelection;
  333. };
  334. class ExampleIbl : public entry::AppI
  335. {
  336. public:
  337. ExampleIbl(const char* _name, const char* _description)
  338. : entry::AppI(_name, _description)
  339. {
  340. }
  341. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  342. {
  343. Args args(_argc, _argv);
  344. m_width = _width;
  345. m_height = _height;
  346. m_debug = BGFX_DEBUG_NONE;
  347. m_reset = 0
  348. | BGFX_RESET_VSYNC
  349. | BGFX_RESET_MSAA_X16
  350. ;
  351. bgfx::Init init;
  352. init.type = args.m_type;
  353. init.vendorId = args.m_pciId;
  354. init.resolution.width = m_width;
  355. init.resolution.height = m_height;
  356. init.resolution.reset = m_reset;
  357. bgfx::init(init);
  358. // Enable debug text.
  359. bgfx::setDebug(m_debug);
  360. // Set views clear state.
  361. bgfx::setViewClear(0
  362. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  363. , 0x303030ff
  364. , 1.0f
  365. , 0
  366. );
  367. // Imgui.
  368. imguiCreate();
  369. // Uniforms.
  370. m_uniforms.init();
  371. // Vertex declarations.
  372. PosColorTexCoord0Vertex::init();
  373. m_lightProbes[LightProbe::Bolonga].load("bolonga");
  374. m_lightProbes[LightProbe::Kyoto ].load("kyoto");
  375. m_currentLightProbe = LightProbe::Bolonga;
  376. u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
  377. u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4);
  378. u_flags = bgfx::createUniform("u_flags", bgfx::UniformType::Vec4);
  379. u_camPos = bgfx::createUniform("u_camPos", bgfx::UniformType::Vec4);
  380. s_texCube = bgfx::createUniform("s_texCube", bgfx::UniformType::Int1);
  381. s_texCubeIrr = bgfx::createUniform("s_texCubeIrr", bgfx::UniformType::Int1);
  382. m_programMesh = loadProgram("vs_ibl_mesh", "fs_ibl_mesh");
  383. m_programSky = loadProgram("vs_ibl_skybox", "fs_ibl_skybox");
  384. m_meshBunny = meshLoad("meshes/bunny.bin");
  385. m_meshOrb = meshLoad("meshes/orb.bin");
  386. }
  387. virtual int shutdown() override
  388. {
  389. meshUnload(m_meshBunny);
  390. meshUnload(m_meshOrb);
  391. // Cleanup.
  392. bgfx::destroy(m_programMesh);
  393. bgfx::destroy(m_programSky);
  394. bgfx::destroy(u_camPos);
  395. bgfx::destroy(u_flags);
  396. bgfx::destroy(u_params);
  397. bgfx::destroy(u_mtx);
  398. bgfx::destroy(s_texCube);
  399. bgfx::destroy(s_texCubeIrr);
  400. for (uint8_t ii = 0; ii < LightProbe::Count; ++ii)
  401. {
  402. m_lightProbes[ii].destroy();
  403. }
  404. m_uniforms.destroy();
  405. imguiDestroy();
  406. // Shutdown bgfx.
  407. bgfx::shutdown();
  408. return 0;
  409. }
  410. bool update() override
  411. {
  412. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  413. {
  414. imguiBeginFrame(m_mouseState.m_mx
  415. , m_mouseState.m_my
  416. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  417. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  418. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  419. , m_mouseState.m_mz
  420. , uint16_t(m_width)
  421. , uint16_t(m_height)
  422. );
  423. showExampleDialog(this);
  424. ImGui::SetNextWindowPos(
  425. ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
  426. , ImGuiCond_FirstUseEver
  427. );
  428. ImGui::SetNextWindowSize(
  429. ImVec2(m_width / 5.0f, m_height - 20.0f)
  430. , ImGuiCond_FirstUseEver
  431. );
  432. ImGui::Begin("Settings"
  433. , NULL
  434. , 0
  435. );
  436. ImGui::PushItemWidth(180.0f);
  437. ImGui::Text("Environment light:");
  438. ImGui::Indent();
  439. ImGui::Checkbox("IBL Diffuse", &m_settings.m_doDiffuseIbl);
  440. ImGui::Checkbox("IBL Specular", &m_settings.m_doSpecularIbl);
  441. {
  442. float tabWidth = ImGui::GetContentRegionAvailWidth() / 2.0f;
  443. if (ImGui::TabButton("Bolonga", tabWidth, m_currentLightProbe == LightProbe::Bolonga) )
  444. {
  445. m_currentLightProbe = LightProbe::Bolonga;
  446. }
  447. ImGui::SameLine(0.0f,0.0f);
  448. if (ImGui::TabButton("Kyoto", tabWidth, m_currentLightProbe == LightProbe::Kyoto) )
  449. {
  450. m_currentLightProbe = LightProbe::Kyoto;
  451. }
  452. }
  453. ImGui::SliderFloat("Texture LOD", &m_settings.m_lod, 0.0f, 10.1f);
  454. ImGui::Unindent();
  455. ImGui::Separator();
  456. ImGui::Text("Directional light:");
  457. ImGui::Indent();
  458. ImGui::Checkbox("Diffuse", &m_settings.m_doDiffuse);
  459. ImGui::Checkbox("Specular", &m_settings.m_doSpecular);
  460. const bool doDirectLighting = m_settings.m_doDiffuse || m_settings.m_doSpecular;
  461. if (doDirectLighting)
  462. {
  463. ImGui::SliderFloat("Light direction X", &m_settings.m_lightDir[0], -1.0f, 1.0f);
  464. ImGui::SliderFloat("Light direction Y", &m_settings.m_lightDir[1], -1.0f, 1.0f);
  465. ImGui::SliderFloat("Light direction Z", &m_settings.m_lightDir[2], -1.0f, 1.0f);
  466. ImGui::ColorWheel("Color:", m_settings.m_lightCol, 0.6f);
  467. }
  468. ImGui::Unindent();
  469. ImGui::Separator();
  470. ImGui::Text("Background:");
  471. ImGui::Indent();
  472. {
  473. int32_t selection;
  474. if (0.0f == m_settings.m_bgType)
  475. {
  476. selection = UINT8_C(0);
  477. }
  478. else if (7.0f == m_settings.m_bgType)
  479. {
  480. selection = UINT8_C(2);
  481. }
  482. else
  483. {
  484. selection = UINT8_C(1);
  485. }
  486. float tabWidth = ImGui::GetContentRegionAvailWidth() / 3.0f;
  487. if (ImGui::TabButton("Skybox", tabWidth, selection == 0) )
  488. {
  489. selection = 0;
  490. }
  491. ImGui::SameLine(0.0f,0.0f);
  492. if (ImGui::TabButton("Radiance", tabWidth, selection == 1) )
  493. {
  494. selection = 1;
  495. }
  496. ImGui::SameLine(0.0f,0.0f);
  497. if (ImGui::TabButton("Irradiance", tabWidth, selection == 2) )
  498. {
  499. selection = 2;
  500. }
  501. if (0 == selection)
  502. {
  503. m_settings.m_bgType = 0.0f;
  504. }
  505. else if (2 == selection)
  506. {
  507. m_settings.m_bgType = 7.0f;
  508. }
  509. else
  510. {
  511. m_settings.m_bgType = m_settings.m_radianceSlider;
  512. }
  513. const bool isRadiance = (selection == 1);
  514. if (isRadiance)
  515. {
  516. ImGui::SliderFloat("Mip level", &m_settings.m_radianceSlider, 1.0f, 6.0f);
  517. }
  518. }
  519. ImGui::Unindent();
  520. ImGui::Separator();
  521. ImGui::Text("Post processing:");
  522. ImGui::Indent();
  523. ImGui::SliderFloat("Exposure",& m_settings.m_exposure, -4.0f, 4.0f);
  524. ImGui::Unindent();
  525. ImGui::PopItemWidth();
  526. ImGui::End();
  527. ImGui::SetNextWindowPos(
  528. ImVec2(10.0f, 260.0f)
  529. , ImGuiCond_FirstUseEver
  530. );
  531. ImGui::SetNextWindowSize(
  532. ImVec2(m_width / 5.0f, 450.0f)
  533. , ImGuiCond_FirstUseEver
  534. );
  535. ImGui::Begin("Mesh"
  536. , NULL
  537. , 0
  538. );
  539. ImGui::Text("Mesh:");
  540. ImGui::Indent();
  541. ImGui::RadioButton("Bunny", &m_settings.m_meshSelection, 0);
  542. ImGui::RadioButton("Orbs", &m_settings.m_meshSelection, 1);
  543. ImGui::Unindent();
  544. const bool isBunny = (0 == m_settings.m_meshSelection);
  545. if (!isBunny)
  546. {
  547. m_settings.m_metalOrSpec = 0;
  548. }
  549. else
  550. {
  551. ImGui::Separator();
  552. ImGui::Text("Workflow:");
  553. ImGui::Indent();
  554. ImGui::RadioButton("Metalness", &m_settings.m_metalOrSpec, 0);
  555. ImGui::RadioButton("Specular", &m_settings.m_metalOrSpec, 1);
  556. ImGui::Unindent();
  557. ImGui::Separator();
  558. ImGui::Text("Material:");
  559. ImGui::Indent();
  560. ImGui::PushItemWidth(130.0f);
  561. ImGui::SliderFloat("Glossiness", &m_settings.m_glossiness, 0.0f, 1.0f);
  562. ImGui::SliderFloat(0 == m_settings.m_metalOrSpec ? "Metalness" : "Diffuse - Specular", &m_settings.m_reflectivity, 0.0f, 1.0f);
  563. ImGui::PopItemWidth();
  564. ImGui::Unindent();
  565. }
  566. ImGui::ColorWheel("Diffuse:", &m_settings.m_rgbDiff[0], 0.7f);
  567. ImGui::Separator();
  568. if ( (1 == m_settings.m_metalOrSpec) && isBunny )
  569. {
  570. ImGui::ColorWheel("Specular:", &m_settings.m_rgbSpec[0], 0.7f);
  571. }
  572. ImGui::End();
  573. imguiEndFrame();
  574. m_uniforms.m_glossiness = m_settings.m_glossiness;
  575. m_uniforms.m_reflectivity = m_settings.m_reflectivity;
  576. m_uniforms.m_exposure = m_settings.m_exposure;
  577. m_uniforms.m_bgType = m_settings.m_bgType;
  578. m_uniforms.m_metalOrSpec = float(m_settings.m_metalOrSpec);
  579. m_uniforms.m_doDiffuse = float(m_settings.m_doDiffuse);
  580. m_uniforms.m_doSpecular = float(m_settings.m_doSpecular);
  581. m_uniforms.m_doDiffuseIbl = float(m_settings.m_doDiffuseIbl);
  582. m_uniforms.m_doSpecularIbl = float(m_settings.m_doSpecularIbl);
  583. bx::memCopy(m_uniforms.m_rgbDiff, m_settings.m_rgbDiff, 3*sizeof(float) );
  584. bx::memCopy(m_uniforms.m_rgbSpec, m_settings.m_rgbSpec, 3*sizeof(float) );
  585. bx::memCopy(m_uniforms.m_lightDir, m_settings.m_lightDir, 3*sizeof(float) );
  586. bx::memCopy(m_uniforms.m_lightCol, m_settings.m_lightCol, 3*sizeof(float) );
  587. int64_t now = bx::getHPCounter();
  588. static int64_t last = now;
  589. const int64_t frameTime = now - last;
  590. last = now;
  591. const double freq = double(bx::getHPFrequency() );
  592. const float deltaTimeSec = float(double(frameTime)/freq);
  593. // Camera.
  594. const bool mouseOverGui = ImGui::MouseOverArea();
  595. m_mouse.update(float(m_mouseState.m_mx), float(m_mouseState.m_my), m_mouseState.m_mz, m_width, m_height);
  596. if (!mouseOverGui)
  597. {
  598. if (m_mouseState.m_buttons[entry::MouseButton::Left])
  599. {
  600. m_camera.orbit(m_mouse.m_dx, m_mouse.m_dy);
  601. }
  602. else if (m_mouseState.m_buttons[entry::MouseButton::Right])
  603. {
  604. m_camera.dolly(m_mouse.m_dx + m_mouse.m_dy);
  605. }
  606. else if (m_mouseState.m_buttons[entry::MouseButton::Middle])
  607. {
  608. m_settings.m_envRotDest += m_mouse.m_dx*2.0f;
  609. }
  610. else if (0 != m_mouse.m_scroll)
  611. {
  612. m_camera.dolly(float(m_mouse.m_scroll)*0.05f);
  613. }
  614. }
  615. m_camera.update(deltaTimeSec);
  616. bx::memCopy(m_uniforms.m_cameraPos, &m_camera.m_pos.curr.x, 3*sizeof(float) );
  617. // View Transform 0.
  618. float view[16];
  619. bx::mtxIdentity(view);
  620. const bgfx::Caps* caps = bgfx::getCaps();
  621. float proj[16];
  622. bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f, 0.0, caps->homogeneousDepth);
  623. bgfx::setViewTransform(0, view, proj);
  624. // View Transform 1.
  625. m_camera.mtxLookAt(view);
  626. bx::mtxProj(proj, 45.0f, float(m_width)/float(m_height), 0.1f, 100.0f, caps->homogeneousDepth);
  627. bgfx::setViewTransform(1, view, proj);
  628. // View rect.
  629. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  630. bgfx::setViewRect(1, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  631. // Env rotation.
  632. const float amount = bx::min(deltaTimeSec/0.12f, 1.0f);
  633. m_settings.m_envRotCurr = bx::lerp(m_settings.m_envRotCurr, m_settings.m_envRotDest, amount);
  634. // Env mtx.
  635. float mtxEnvView[16];
  636. m_camera.envViewMtx(mtxEnvView);
  637. float mtxEnvRot[16];
  638. bx::mtxRotateY(mtxEnvRot, m_settings.m_envRotCurr);
  639. bx::mtxMul(m_uniforms.m_mtx, mtxEnvView, mtxEnvRot); // Used for Skybox.
  640. // Submit view 0.
  641. bgfx::setTexture(0, s_texCube, m_lightProbes[m_currentLightProbe].m_tex);
  642. bgfx::setTexture(1, s_texCubeIrr, m_lightProbes[m_currentLightProbe].m_texIrr);
  643. bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
  644. screenSpaceQuad( (float)m_width, (float)m_height, true);
  645. m_uniforms.submit();
  646. bgfx::submit(0, m_programSky);
  647. // Submit view 1.
  648. bx::memCopy(m_uniforms.m_mtx, mtxEnvRot, 16*sizeof(float)); // Used for IBL.
  649. if (0 == m_settings.m_meshSelection)
  650. {
  651. // Submit bunny.
  652. float mtx[16];
  653. bx::mtxSRT(mtx, 1.0f, 1.0f, 1.0f, 0.0f, bx::kPi, 0.0f, 0.0f, -0.80f, 0.0f);
  654. bgfx::setTexture(0, s_texCube, m_lightProbes[m_currentLightProbe].m_tex);
  655. bgfx::setTexture(1, s_texCubeIrr, m_lightProbes[m_currentLightProbe].m_texIrr);
  656. m_uniforms.submit();
  657. meshSubmit(m_meshBunny, 1, m_programMesh, mtx);
  658. }
  659. else
  660. {
  661. // Submit orbs.
  662. for (float yy = 0, yend = 5.0f; yy < yend; yy+=1.0f)
  663. {
  664. for (float xx = 0, xend = 5.0f; xx < xend; xx+=1.0f)
  665. {
  666. const float scale = 1.2f;
  667. const float spacing = 2.2f;
  668. const float yAdj = -0.8f;
  669. float mtx[16];
  670. bx::mtxSRT(mtx
  671. , scale/xend
  672. , scale/xend
  673. , scale/xend
  674. , 0.0f
  675. , 0.0f
  676. , 0.0f
  677. , 0.0f + (xx/xend)*spacing - (1.0f + (scale-1.0f)*0.5f - 1.0f/xend)
  678. , yAdj/yend + (yy/yend)*spacing - (1.0f + (scale-1.0f)*0.5f - 1.0f/yend)
  679. , 0.0f
  680. );
  681. m_uniforms.m_glossiness = xx*(1.0f/xend);
  682. m_uniforms.m_reflectivity = (yend-yy)*(1.0f/yend);
  683. m_uniforms.m_metalOrSpec = 0.0f;
  684. m_uniforms.submit();
  685. bgfx::setTexture(0, s_texCube, m_lightProbes[m_currentLightProbe].m_tex);
  686. bgfx::setTexture(1, s_texCubeIrr, m_lightProbes[m_currentLightProbe].m_texIrr);
  687. meshSubmit(m_meshOrb, 1, m_programMesh, mtx);
  688. }
  689. }
  690. }
  691. // Advance to next frame. Rendering thread will be kicked to
  692. // process submitted rendering primitives.
  693. bgfx::frame();
  694. return true;
  695. }
  696. return false;
  697. }
  698. uint32_t m_width;
  699. uint32_t m_height;
  700. uint32_t m_debug;
  701. uint32_t m_reset;
  702. entry::MouseState m_mouseState;
  703. Uniforms m_uniforms;
  704. LightProbe m_lightProbes[LightProbe::Count];
  705. LightProbe::Enum m_currentLightProbe;
  706. bgfx::UniformHandle u_mtx;
  707. bgfx::UniformHandle u_params;
  708. bgfx::UniformHandle u_flags;
  709. bgfx::UniformHandle u_camPos;
  710. bgfx::UniformHandle s_texCube;
  711. bgfx::UniformHandle s_texCubeIrr;
  712. bgfx::ProgramHandle m_programMesh;
  713. bgfx::ProgramHandle m_programSky;
  714. Mesh* m_meshBunny;
  715. Mesh* m_meshOrb;
  716. Camera m_camera;
  717. Mouse m_mouse;
  718. Settings m_settings;
  719. };
  720. } // namespace
  721. ENTRY_IMPLEMENT_MAIN(ExampleIbl, "18-ibl", "Image-based lighting.");