ibl.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /*
  2. * Copyright 2014-2016 Dario Manesku. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  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_layout
  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::VertexLayout ms_layout;
  74. };
  75. bgfx::VertexLayout PosColorTexCoord0Vertex::ms_layout;
  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_layout) )
  79. {
  80. bgfx::TransientVertexBuffer vb;
  81. bgfx::allocTransientVertexBuffer(&vb, 3, PosColorTexCoord0Vertex::ms_layout);
  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::kFloatSmallest);
  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::kFloatSmallest);
  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::kFloatSmallest);
  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 = bx::InitNone;
  241. bx::Vec3 dest = bx::InitNone;
  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, const char* _url)
  338. : entry::AppI(_name, _description, _url)
  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.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  355. init.platformData.ndt = entry::getNativeDisplayHandle();
  356. init.platformData.type = entry::getNativeWindowHandleType(entry::kDefaultWindowHandle);
  357. init.resolution.width = m_width;
  358. init.resolution.height = m_height;
  359. init.resolution.reset = m_reset;
  360. bgfx::init(init);
  361. // Enable debug text.
  362. bgfx::setDebug(m_debug);
  363. // Set views clear state.
  364. bgfx::setViewClear(0
  365. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  366. , 0x303030ff
  367. , 1.0f
  368. , 0
  369. );
  370. // Imgui.
  371. imguiCreate();
  372. // Uniforms.
  373. m_uniforms.init();
  374. // Vertex declarations.
  375. PosColorTexCoord0Vertex::init();
  376. m_lightProbes[LightProbe::Bolonga].load("bolonga");
  377. m_lightProbes[LightProbe::Kyoto ].load("kyoto");
  378. m_currentLightProbe = LightProbe::Bolonga;
  379. u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
  380. u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4);
  381. u_flags = bgfx::createUniform("u_flags", bgfx::UniformType::Vec4);
  382. u_camPos = bgfx::createUniform("u_camPos", bgfx::UniformType::Vec4);
  383. s_texCube = bgfx::createUniform("s_texCube", bgfx::UniformType::Sampler);
  384. s_texCubeIrr = bgfx::createUniform("s_texCubeIrr", bgfx::UniformType::Sampler);
  385. m_programMesh = loadProgram("vs_ibl_mesh", "fs_ibl_mesh");
  386. m_programSky = loadProgram("vs_ibl_skybox", "fs_ibl_skybox");
  387. m_meshBunny = meshLoad("meshes/bunny.bin");
  388. m_meshOrb = meshLoad("meshes/orb.bin");
  389. }
  390. virtual int shutdown() override
  391. {
  392. meshUnload(m_meshBunny);
  393. meshUnload(m_meshOrb);
  394. // Cleanup.
  395. bgfx::destroy(m_programMesh);
  396. bgfx::destroy(m_programSky);
  397. bgfx::destroy(u_camPos);
  398. bgfx::destroy(u_flags);
  399. bgfx::destroy(u_params);
  400. bgfx::destroy(u_mtx);
  401. bgfx::destroy(s_texCube);
  402. bgfx::destroy(s_texCubeIrr);
  403. for (uint8_t ii = 0; ii < LightProbe::Count; ++ii)
  404. {
  405. m_lightProbes[ii].destroy();
  406. }
  407. m_uniforms.destroy();
  408. imguiDestroy();
  409. // Shutdown bgfx.
  410. bgfx::shutdown();
  411. return 0;
  412. }
  413. bool update() override
  414. {
  415. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  416. {
  417. imguiBeginFrame(m_mouseState.m_mx
  418. , m_mouseState.m_my
  419. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  420. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  421. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  422. , m_mouseState.m_mz
  423. , uint16_t(m_width)
  424. , uint16_t(m_height)
  425. );
  426. showExampleDialog(this);
  427. ImGui::SetNextWindowPos(
  428. ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
  429. , ImGuiCond_FirstUseEver
  430. );
  431. ImGui::SetNextWindowSize(
  432. ImVec2(m_width / 5.0f, m_height - 20.0f)
  433. , ImGuiCond_FirstUseEver
  434. );
  435. ImGui::Begin("Settings"
  436. , NULL
  437. , 0
  438. );
  439. ImGui::PushItemWidth(180.0f);
  440. ImGui::Text("Environment light:");
  441. ImGui::Indent();
  442. ImGui::Checkbox("IBL Diffuse", &m_settings.m_doDiffuseIbl);
  443. ImGui::Checkbox("IBL Specular", &m_settings.m_doSpecularIbl);
  444. if (ImGui::BeginTabBar("Cubemap", ImGuiTabBarFlags_None) )
  445. {
  446. if (ImGui::BeginTabItem("Bolonga") )
  447. {
  448. m_currentLightProbe = LightProbe::Bolonga;
  449. ImGui::EndTabItem();
  450. }
  451. if (ImGui::BeginTabItem("Kyoto") )
  452. {
  453. m_currentLightProbe = LightProbe::Kyoto;
  454. ImGui::EndTabItem();
  455. }
  456. ImGui::EndTabBar();
  457. }
  458. ImGui::SliderFloat("Texture LOD", &m_settings.m_lod, 0.0f, 10.1f);
  459. ImGui::Unindent();
  460. ImGui::Separator();
  461. ImGui::Text("Directional light:");
  462. ImGui::Indent();
  463. ImGui::Checkbox("Diffuse", &m_settings.m_doDiffuse);
  464. ImGui::Checkbox("Specular", &m_settings.m_doSpecular);
  465. const bool doDirectLighting = m_settings.m_doDiffuse || m_settings.m_doSpecular;
  466. if (doDirectLighting)
  467. {
  468. ImGui::SliderFloat("Light direction X", &m_settings.m_lightDir[0], -1.0f, 1.0f);
  469. ImGui::SliderFloat("Light direction Y", &m_settings.m_lightDir[1], -1.0f, 1.0f);
  470. ImGui::SliderFloat("Light direction Z", &m_settings.m_lightDir[2], -1.0f, 1.0f);
  471. ImGui::ColorWheel("Color:", m_settings.m_lightCol, 0.6f);
  472. }
  473. ImGui::Unindent();
  474. ImGui::Separator();
  475. ImGui::Text("Background:");
  476. ImGui::Indent();
  477. {
  478. if (ImGui::BeginTabBar("CubemapSelection", ImGuiTabBarFlags_None) )
  479. {
  480. if (ImGui::BeginTabItem("Irradiance") )
  481. {
  482. m_settings.m_bgType = m_settings.m_radianceSlider;
  483. ImGui::EndTabItem();
  484. }
  485. if (ImGui::BeginTabItem("Radiance") )
  486. {
  487. m_settings.m_bgType = 7.0f;
  488. ImGui::SliderFloat("Mip level", &m_settings.m_radianceSlider, 1.0f, 6.0f);
  489. ImGui::EndTabItem();
  490. }
  491. if (ImGui::BeginTabItem("Skybox") )
  492. {
  493. m_settings.m_bgType = 0.0f;
  494. ImGui::EndTabItem();
  495. }
  496. ImGui::EndTabBar();
  497. }
  498. }
  499. ImGui::Unindent();
  500. ImGui::Separator();
  501. ImGui::Text("Post processing:");
  502. ImGui::Indent();
  503. ImGui::SliderFloat("Exposure",& m_settings.m_exposure, -4.0f, 4.0f);
  504. ImGui::Unindent();
  505. ImGui::PopItemWidth();
  506. ImGui::End();
  507. ImGui::SetNextWindowPos(
  508. ImVec2(10.0f, 260.0f)
  509. , ImGuiCond_FirstUseEver
  510. );
  511. ImGui::SetNextWindowSize(
  512. ImVec2(m_width / 5.0f, 450.0f)
  513. , ImGuiCond_FirstUseEver
  514. );
  515. ImGui::Begin("Mesh"
  516. , NULL
  517. , 0
  518. );
  519. ImGui::Text("Mesh:");
  520. ImGui::Indent();
  521. ImGui::RadioButton("Bunny", &m_settings.m_meshSelection, 0);
  522. ImGui::RadioButton("Orbs", &m_settings.m_meshSelection, 1);
  523. ImGui::Unindent();
  524. const bool isBunny = (0 == m_settings.m_meshSelection);
  525. if (!isBunny)
  526. {
  527. m_settings.m_metalOrSpec = 0;
  528. }
  529. else
  530. {
  531. ImGui::Separator();
  532. ImGui::Text("Workflow:");
  533. ImGui::Indent();
  534. ImGui::RadioButton("Metalness", &m_settings.m_metalOrSpec, 0);
  535. ImGui::RadioButton("Specular", &m_settings.m_metalOrSpec, 1);
  536. ImGui::Unindent();
  537. ImGui::Separator();
  538. ImGui::Text("Material:");
  539. ImGui::Indent();
  540. ImGui::PushItemWidth(130.0f);
  541. ImGui::SliderFloat("Glossiness", &m_settings.m_glossiness, 0.0f, 1.0f);
  542. ImGui::SliderFloat(0 == m_settings.m_metalOrSpec ? "Metalness" : "Diffuse - Specular", &m_settings.m_reflectivity, 0.0f, 1.0f);
  543. ImGui::PopItemWidth();
  544. ImGui::Unindent();
  545. }
  546. ImGui::ColorWheel("Diffuse:", &m_settings.m_rgbDiff[0], 0.7f);
  547. ImGui::Separator();
  548. if ( (1 == m_settings.m_metalOrSpec) && isBunny )
  549. {
  550. ImGui::ColorWheel("Specular:", &m_settings.m_rgbSpec[0], 0.7f);
  551. }
  552. ImGui::End();
  553. imguiEndFrame();
  554. m_uniforms.m_glossiness = m_settings.m_glossiness;
  555. m_uniforms.m_reflectivity = m_settings.m_reflectivity;
  556. m_uniforms.m_exposure = m_settings.m_exposure;
  557. m_uniforms.m_bgType = m_settings.m_bgType;
  558. m_uniforms.m_metalOrSpec = float(m_settings.m_metalOrSpec);
  559. m_uniforms.m_doDiffuse = float(m_settings.m_doDiffuse);
  560. m_uniforms.m_doSpecular = float(m_settings.m_doSpecular);
  561. m_uniforms.m_doDiffuseIbl = float(m_settings.m_doDiffuseIbl);
  562. m_uniforms.m_doSpecularIbl = float(m_settings.m_doSpecularIbl);
  563. bx::memCopy(m_uniforms.m_rgbDiff, m_settings.m_rgbDiff, 3*sizeof(float) );
  564. bx::memCopy(m_uniforms.m_rgbSpec, m_settings.m_rgbSpec, 3*sizeof(float) );
  565. bx::memCopy(m_uniforms.m_lightDir, m_settings.m_lightDir, 3*sizeof(float) );
  566. bx::memCopy(m_uniforms.m_lightCol, m_settings.m_lightCol, 3*sizeof(float) );
  567. int64_t now = bx::getHPCounter();
  568. static int64_t last = now;
  569. const int64_t frameTime = now - last;
  570. last = now;
  571. const double freq = double(bx::getHPFrequency() );
  572. const float deltaTimeSec = float(double(frameTime)/freq);
  573. // Camera.
  574. const bool mouseOverGui = ImGui::MouseOverArea();
  575. m_mouse.update(float(m_mouseState.m_mx), float(m_mouseState.m_my), m_mouseState.m_mz, m_width, m_height);
  576. if (!mouseOverGui)
  577. {
  578. if (m_mouseState.m_buttons[entry::MouseButton::Left])
  579. {
  580. m_camera.orbit(m_mouse.m_dx, m_mouse.m_dy);
  581. }
  582. else if (m_mouseState.m_buttons[entry::MouseButton::Right])
  583. {
  584. m_camera.dolly(m_mouse.m_dx + m_mouse.m_dy);
  585. }
  586. else if (m_mouseState.m_buttons[entry::MouseButton::Middle])
  587. {
  588. m_settings.m_envRotDest += m_mouse.m_dx*2.0f;
  589. }
  590. else if (0 != m_mouse.m_scroll)
  591. {
  592. m_camera.dolly(float(m_mouse.m_scroll)*0.05f);
  593. }
  594. }
  595. m_camera.update(deltaTimeSec);
  596. bx::memCopy(m_uniforms.m_cameraPos, &m_camera.m_pos.curr.x, 3*sizeof(float) );
  597. // View Transform 0.
  598. float view[16];
  599. bx::mtxIdentity(view);
  600. const bgfx::Caps* caps = bgfx::getCaps();
  601. float proj[16];
  602. bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f, 0.0, caps->homogeneousDepth);
  603. bgfx::setViewTransform(0, view, proj);
  604. // View Transform 1.
  605. m_camera.mtxLookAt(view);
  606. bx::mtxProj(proj, 45.0f, float(m_width)/float(m_height), 0.1f, 100.0f, caps->homogeneousDepth);
  607. bgfx::setViewTransform(1, view, proj);
  608. // View rect.
  609. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  610. bgfx::setViewRect(1, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  611. // Env rotation.
  612. const float amount = bx::min(deltaTimeSec/0.12f, 1.0f);
  613. m_settings.m_envRotCurr = bx::lerp(m_settings.m_envRotCurr, m_settings.m_envRotDest, amount);
  614. // Env mtx.
  615. float mtxEnvView[16];
  616. m_camera.envViewMtx(mtxEnvView);
  617. float mtxEnvRot[16];
  618. bx::mtxRotateY(mtxEnvRot, m_settings.m_envRotCurr);
  619. bx::mtxMul(m_uniforms.m_mtx, mtxEnvView, mtxEnvRot); // Used for Skybox.
  620. // Submit view 0.
  621. bgfx::setTexture(0, s_texCube, m_lightProbes[m_currentLightProbe].m_tex);
  622. bgfx::setTexture(1, s_texCubeIrr, m_lightProbes[m_currentLightProbe].m_texIrr);
  623. bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
  624. screenSpaceQuad( (float)m_width, (float)m_height, true);
  625. m_uniforms.submit();
  626. bgfx::submit(0, m_programSky);
  627. // Submit view 1.
  628. bx::memCopy(m_uniforms.m_mtx, mtxEnvRot, 16*sizeof(float)); // Used for IBL.
  629. if (0 == m_settings.m_meshSelection)
  630. {
  631. // Submit bunny.
  632. float mtx[16];
  633. bx::mtxSRT(mtx, 1.0f, 1.0f, 1.0f, 0.0f, bx::kPi, 0.0f, 0.0f, -0.80f, 0.0f);
  634. bgfx::setTexture(0, s_texCube, m_lightProbes[m_currentLightProbe].m_tex);
  635. bgfx::setTexture(1, s_texCubeIrr, m_lightProbes[m_currentLightProbe].m_texIrr);
  636. m_uniforms.submit();
  637. meshSubmit(m_meshBunny, 1, m_programMesh, mtx);
  638. }
  639. else
  640. {
  641. // Submit orbs.
  642. for (float yy = 0, yend = 5.0f; yy < yend; yy+=1.0f)
  643. {
  644. for (float xx = 0, xend = 5.0f; xx < xend; xx+=1.0f)
  645. {
  646. const float scale = 1.2f;
  647. const float spacing = 2.2f;
  648. const float yAdj = -0.8f;
  649. float mtx[16];
  650. bx::mtxSRT(mtx
  651. , scale/xend
  652. , scale/xend
  653. , scale/xend
  654. , 0.0f
  655. , 0.0f
  656. , 0.0f
  657. , 0.0f + (xx/xend)*spacing - (1.0f + (scale-1.0f)*0.5f - 1.0f/xend)
  658. , yAdj/yend + (yy/yend)*spacing - (1.0f + (scale-1.0f)*0.5f - 1.0f/yend)
  659. , 0.0f
  660. );
  661. m_uniforms.m_glossiness = xx*(1.0f/xend);
  662. m_uniforms.m_reflectivity = (yend-yy)*(1.0f/yend);
  663. m_uniforms.m_metalOrSpec = 0.0f;
  664. m_uniforms.submit();
  665. bgfx::setTexture(0, s_texCube, m_lightProbes[m_currentLightProbe].m_tex);
  666. bgfx::setTexture(1, s_texCubeIrr, m_lightProbes[m_currentLightProbe].m_texIrr);
  667. meshSubmit(m_meshOrb, 1, m_programMesh, mtx);
  668. }
  669. }
  670. }
  671. // Advance to next frame. Rendering thread will be kicked to
  672. // process submitted rendering primitives.
  673. bgfx::frame();
  674. return true;
  675. }
  676. return false;
  677. }
  678. uint32_t m_width;
  679. uint32_t m_height;
  680. uint32_t m_debug;
  681. uint32_t m_reset;
  682. entry::MouseState m_mouseState;
  683. Uniforms m_uniforms;
  684. LightProbe m_lightProbes[LightProbe::Count];
  685. LightProbe::Enum m_currentLightProbe;
  686. bgfx::UniformHandle u_mtx;
  687. bgfx::UniformHandle u_params;
  688. bgfx::UniformHandle u_flags;
  689. bgfx::UniformHandle u_camPos;
  690. bgfx::UniformHandle s_texCube;
  691. bgfx::UniformHandle s_texCubeIrr;
  692. bgfx::ProgramHandle m_programMesh;
  693. bgfx::ProgramHandle m_programSky;
  694. Mesh* m_meshBunny;
  695. Mesh* m_meshOrb;
  696. Camera m_camera;
  697. Mouse m_mouse;
  698. Settings m_settings;
  699. };
  700. } // namespace
  701. ENTRY_IMPLEMENT_MAIN(
  702. ExampleIbl
  703. , "18-ibl"
  704. , "Image-based lighting."
  705. , "https://bkaradzic.github.io/bgfx/examples.html#ibl"
  706. );