bgfx/examples/18-ibl/ibl.cpp

540 lines
13 KiB
C++
Raw Normal View History

2014-02-13 23:46:34 -05:00
/*
* Copyright 2014 Dario Manesku. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#include <vector>
#include <string>
2014-02-13 23:46:34 -05:00
#include "common.h"
2014-05-03 18:18:28 -04:00
#include "bgfx_utils.h"
#include "imgui/imgui.h"
2014-06-23 02:47:14 -04:00
#include "nanovg/nanovg.h"
2014-02-13 23:46:34 -05:00
#include <bx/readerwriter.h>
2014-06-23 02:47:14 -04:00
#include <bx/string.h>
2014-02-13 23:46:34 -05:00
static float s_texelHalf = 0.0f;
struct Uniforms
{
void init()
{
m_time = 0.0f;
2014-05-26 22:31:37 -04:00
bx::mtxIdentity(m_mtx);
2014-02-13 23:46:34 -05:00
2015-05-28 18:27:00 -04:00
u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4);
u_flags = bgfx::createUniform("u_flags", bgfx::UniformType::Vec4);
u_camPos = bgfx::createUniform("u_camPos", bgfx::UniformType::Vec4);
u_rgbDiff = bgfx::createUniform("u_rgbDiff", bgfx::UniformType::Vec4);
u_rgbSpec = bgfx::createUniform("u_rgbSpec", bgfx::UniformType::Vec4);
2014-02-13 23:46:34 -05:00
}
// Call this once per frame.
void submitPerFrameUniforms()
{
2015-05-28 18:27:00 -04:00
bgfx::setUniform(u_mtx, m_mtx);
bgfx::setUniform(u_flags, m_flags);
bgfx::setUniform(u_camPos, m_camPosTime);
2014-02-13 23:46:34 -05:00
bgfx::setUniform(u_rgbDiff, m_rgbDiff);
bgfx::setUniform(u_rgbSpec, m_rgbSpec);
}
// Call this before each draw call.
void submitPerDrawUniforms()
{
bgfx::setUniform(u_params, m_params);
}
void destroy()
{
bgfx::destroyUniform(u_rgbSpec);
bgfx::destroyUniform(u_rgbDiff);
bgfx::destroyUniform(u_camPos);
bgfx::destroyUniform(u_flags);
bgfx::destroyUniform(u_params);
bgfx::destroyUniform(u_mtx);
}
union
{
struct
{
float m_glossiness;
float m_exposure;
float m_diffspec;
2015-05-28 18:27:00 -04:00
float m_time;
2014-02-13 23:46:34 -05:00
};
float m_params[4];
};
union
{
struct
{
float m_diffuse;
float m_specular;
float m_diffuseIbl;
float m_specularIbl;
};
float m_flags[4];
};
float m_mtx[16];
2015-05-28 18:27:00 -04:00
float m_camPosTime[4];
float m_rgbDiff[4];
float m_rgbSpec[4];
2014-02-13 23:46:34 -05:00
bgfx::UniformHandle u_mtx;
bgfx::UniformHandle u_params;
bgfx::UniformHandle u_flags;
bgfx::UniformHandle u_camPos;
bgfx::UniformHandle u_rgbDiff;
bgfx::UniformHandle u_rgbSpec;
};
static Uniforms s_uniforms;
2014-02-13 23:46:34 -05:00
struct PosColorTexCoord0Vertex
{
float m_x;
float m_y;
float m_z;
uint32_t m_rgba;
float m_u;
float m_v;
static void init()
{
ms_decl
.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
.end();
2014-02-13 23:46:34 -05:00
}
static bgfx::VertexDecl ms_decl;
};
bgfx::VertexDecl PosColorTexCoord0Vertex::ms_decl;
void screenSpaceQuad(float _textureWidth, float _textureHeight, bool _originBottomLeft = false, float _width = 1.0f, float _height = 1.0f)
{
if (bgfx::checkAvailTransientVertexBuffer(3, PosColorTexCoord0Vertex::ms_decl) )
{
bgfx::TransientVertexBuffer vb;
bgfx::allocTransientVertexBuffer(&vb, 3, PosColorTexCoord0Vertex::ms_decl);
PosColorTexCoord0Vertex* vertex = (PosColorTexCoord0Vertex*)vb.data;
const float zz = 0.0f;
const float minx = -_width;
const float maxx = _width;
const float miny = 0.0f;
const float maxy = _height*2.0f;
const float texelHalfW = s_texelHalf/_textureWidth;
const float texelHalfH = s_texelHalf/_textureHeight;
const float minu = -1.0f + texelHalfW;
const float maxu = 1.0f + texelHalfW;
float minv = texelHalfH;
float maxv = 2.0f + texelHalfH;
if (_originBottomLeft)
{
std::swap(minv, maxv);
minv -= 1.0f;
maxv -= 1.0f;
}
vertex[0].m_x = minx;
vertex[0].m_y = miny;
vertex[0].m_z = zz;
vertex[0].m_rgba = 0xffffffff;
vertex[0].m_u = minu;
vertex[0].m_v = minv;
vertex[1].m_x = maxx;
vertex[1].m_y = miny;
vertex[1].m_z = zz;
vertex[1].m_rgba = 0xffffffff;
vertex[1].m_u = maxu;
vertex[1].m_v = minv;
vertex[2].m_x = maxx;
vertex[2].m_y = maxy;
vertex[2].m_z = zz;
vertex[2].m_rgba = 0xffffffff;
vertex[2].m_u = maxu;
vertex[2].m_v = maxv;
bgfx::setVertexBuffer(&vb);
}
}
2014-06-29 00:56:10 -04:00
struct LightProbe
{
enum Enum
{
Wells,
Uffizi,
Pisa,
Ennis,
Grace,
Count
};
void load(const char* _name)
{
char filePath[512];
strcpy(filePath, _name);
strcat(filePath, "_lod.dds");
m_tex = loadTexture(filePath, BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP);
strcpy(filePath, _name);
strcat(filePath, "_irr.dds");
m_texIrr = loadTexture(filePath, BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP);
}
void destroy()
{
bgfx::destroyTexture(m_tex);
bgfx::destroyTexture(m_texIrr);
}
bgfx::TextureHandle m_tex;
bgfx::TextureHandle m_texIrr;
};
2014-02-13 23:46:34 -05:00
int _main_(int /*_argc*/, char** /*_argv*/)
{
uint32_t width = 1280;
uint32_t height = 720;
uint32_t debug = BGFX_DEBUG_TEXT;
uint32_t reset = BGFX_RESET_VSYNC;
bgfx::init();
bgfx::reset(width, height, reset);
// Enable debug text.
bgfx::setDebug(debug);
// Set views clear state.
bgfx::setViewClear(0
, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
2014-02-13 23:46:34 -05:00
, 0x303030ff
, 1.0f
, 0
);
// Imgui.
2015-01-24 02:02:56 -05:00
imguiCreate();
2014-02-13 23:46:34 -05:00
// Uniforms.
s_uniforms.init();
// Vertex declarations.
PosColorTexCoord0Vertex::init();
2014-06-29 00:56:10 -04:00
LightProbe lightProbes[LightProbe::Count];
lightProbes[LightProbe::Wells ].load("wells");
lightProbes[LightProbe::Uffizi].load("uffizi");
lightProbes[LightProbe::Pisa ].load("pisa");
lightProbes[LightProbe::Ennis ].load("ennis");
lightProbes[LightProbe::Grace ].load("grace");
LightProbe::Enum currentLightProbe = LightProbe::Wells;
2014-02-13 23:46:34 -05:00
2015-05-28 18:27:00 -04:00
bgfx::UniformHandle u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
bgfx::UniformHandle u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4);
bgfx::UniformHandle u_flags = bgfx::createUniform("u_flags", bgfx::UniformType::Vec4);
bgfx::UniformHandle u_camPos = bgfx::createUniform("u_camPos", bgfx::UniformType::Vec4);
bgfx::UniformHandle s_texCube = bgfx::createUniform("s_texCube", bgfx::UniformType::Int1);
bgfx::UniformHandle s_texCubeIrr = bgfx::createUniform("s_texCubeIrr", bgfx::UniformType::Int1);
2014-02-13 23:46:34 -05:00
2015-05-28 18:27:00 -04:00
bgfx::ProgramHandle programMesh = loadProgram("vs_ibl_mesh", "fs_ibl_mesh");
bgfx::ProgramHandle programSky = loadProgram("vs_ibl_skybox", "fs_ibl_skybox");
2014-02-13 23:46:34 -05:00
Mesh* meshBunny;
2014-12-31 19:33:05 -05:00
meshBunny = meshLoad("meshes/bunny.bin");
2014-02-13 23:46:34 -05:00
2014-02-14 01:56:50 -05:00
struct Settings
{
float m_speed;
float m_glossiness;
float m_exposure;
float m_diffspec;
float m_rgbDiff[3];
float m_rgbSpec[3];
bool m_diffuse;
bool m_specular;
bool m_diffuseIbl;
bool m_specularIbl;
2014-06-23 02:47:14 -04:00
bool m_showDiffColorWheel;
bool m_showSpecColorWheel;
ImguiCubemap::Enum m_crossCubemapPreview;
2014-02-14 01:56:50 -05:00
};
Settings settings;
settings.m_speed = 0.37f;
settings.m_glossiness = 1.0f;
settings.m_exposure = 0.0f;
settings.m_diffspec = 0.65f;
settings.m_rgbDiff[0] = 0.2f;
settings.m_rgbDiff[1] = 0.2f;
settings.m_rgbDiff[2] = 0.2f;
settings.m_rgbSpec[0] = 1.0f;
settings.m_rgbSpec[1] = 1.0f;
settings.m_rgbSpec[2] = 1.0f;
settings.m_diffuse = true;
settings.m_specular = true;
settings.m_diffuseIbl = true;
settings.m_specularIbl = true;
2015-01-31 22:45:18 -05:00
settings.m_showDiffColorWheel = true;
2014-06-23 02:47:14 -04:00
settings.m_showSpecColorWheel = false;
settings.m_crossCubemapPreview = ImguiCubemap::Cross;
2014-02-13 23:46:34 -05:00
float time = 0.0f;
2014-06-29 00:56:10 -04:00
int32_t leftScrollArea = 0;
2014-02-13 23:46:34 -05:00
entry::MouseState mouseState;
while (!entry::processEvents(width, height, debug, reset, &mouseState) )
{
imguiBeginFrame(mouseState.m_mx
, mouseState.m_my
, (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
| (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
, mouseState.m_mz
2014-02-13 23:46:34 -05:00
, width
, height
);
static int32_t rightScrollArea = 0;
2015-02-11 20:49:16 -05:00
imguiBeginScrollArea("Settings", width - 256 - 10, 10, 256, 540, &rightScrollArea);
2014-02-13 23:46:34 -05:00
imguiLabel("Shade:");
imguiSeparator();
2014-02-14 01:56:50 -05:00
imguiBool("Diffuse", settings.m_diffuse);
imguiBool("Specular", settings.m_specular);
imguiBool("IBL Diffuse", settings.m_diffuseIbl);
imguiBool("IBL Specular", settings.m_specularIbl);
2014-02-13 23:46:34 -05:00
imguiSeparatorLine();
2014-06-30 00:53:23 -04:00
imguiSlider("Speed", settings.m_speed, 0.0f, 1.0f, 0.01f);
2014-02-13 23:46:34 -05:00
imguiSeparatorLine();
imguiSeparator();
imguiSlider("Exposure", settings.m_exposure, -8.0f, 8.0f, 0.01f);
imguiSeparator();
2014-02-13 23:46:34 -05:00
imguiLabel("Environment:");
2014-06-29 00:56:10 -04:00
currentLightProbe = LightProbe::Enum(imguiChoose(currentLightProbe
, "Wells"
, "Uffizi"
, "Pisa"
, "Ennis"
, "Grace"
) );
2014-10-03 23:07:38 -04:00
static float lod = 0.0f;
if (imguiCube(lightProbes[currentLightProbe].m_tex, lod, settings.m_crossCubemapPreview, true) )
{
settings.m_crossCubemapPreview = ImguiCubemap::Enum( (settings.m_crossCubemapPreview+1) % ImguiCubemap::Count);
}
2015-06-06 20:12:29 -04:00
imguiSlider("Texture LOD", lod, 0.0f, 10.1f, 0.1f);
2014-02-13 23:46:34 -05:00
imguiEndScrollArea();
2014-06-23 23:08:08 -04:00
imguiBeginScrollArea("Settings", 10, 70, 256, 576, &leftScrollArea);
2014-02-13 23:46:34 -05:00
imguiLabel("Material properties:");
imguiSeparator();
2014-06-30 00:53:23 -04:00
imguiSlider("Diffuse - Specular", settings.m_diffspec, 0.0f, 1.0f, 0.01f);
imguiSlider("Glossiness" , settings.m_glossiness, 0.0f, 1.0f, 0.01f);
2014-02-13 23:46:34 -05:00
imguiSeparator();
2014-06-28 00:47:33 -04:00
imguiColorWheel("Diffuse color:", &settings.m_rgbDiff[0], settings.m_showDiffColorWheel);
2014-02-13 23:46:34 -05:00
imguiSeparator();
2014-06-28 00:47:33 -04:00
imguiColorWheel("Specular color:", &settings.m_rgbSpec[0], settings.m_showSpecColorWheel);
2014-02-13 23:46:34 -05:00
2014-06-23 02:47:14 -04:00
imguiSeparator();
2014-02-13 23:46:34 -05:00
imguiLabel("Predefined materials:");
imguiSeparator();
if (imguiButton("Gold") )
{
2014-02-14 01:56:50 -05:00
settings.m_glossiness = 0.8f;
settings.m_diffspec = 1.0f;
2014-02-13 23:46:34 -05:00
2014-02-14 01:56:50 -05:00
settings.m_rgbDiff[0] = 0.0f;
settings.m_rgbDiff[1] = 0.0f;
settings.m_rgbDiff[2] = 0.0f;
2014-02-13 23:46:34 -05:00
2014-02-14 01:56:50 -05:00
settings.m_rgbSpec[0] = 1.0f;
settings.m_rgbSpec[1] = 0.86f;
settings.m_rgbSpec[2] = 0.58f;
2014-02-13 23:46:34 -05:00
}
if (imguiButton("Copper") )
{
2014-02-14 01:56:50 -05:00
settings.m_glossiness = 0.67f;
settings.m_diffspec = 1.0f;
2014-02-13 23:46:34 -05:00
2014-02-14 01:56:50 -05:00
settings.m_rgbDiff[0] = 0.0f;
settings.m_rgbDiff[1] = 0.0f;
settings.m_rgbDiff[2] = 0.0f;
2014-02-13 23:46:34 -05:00
2014-02-14 01:56:50 -05:00
settings.m_rgbSpec[0] = 0.98f;
settings.m_rgbSpec[1] = 0.82f;
settings.m_rgbSpec[2] = 0.76f;
2014-02-13 23:46:34 -05:00
}
if (imguiButton("Titanium") )
{
2014-02-14 01:56:50 -05:00
settings.m_glossiness = 0.57f;
settings.m_diffspec = 1.0f;
2014-02-13 23:46:34 -05:00
2014-02-14 01:56:50 -05:00
settings.m_rgbDiff[0] = 0.0f;
settings.m_rgbDiff[1] = 0.0f;
settings.m_rgbDiff[2] = 0.0f;
2014-02-13 23:46:34 -05:00
2014-02-14 01:56:50 -05:00
settings.m_rgbSpec[0] = 0.76f;
settings.m_rgbSpec[1] = 0.73f;
settings.m_rgbSpec[2] = 0.71f;
2014-02-13 23:46:34 -05:00
}
if (imguiButton("Steel") )
{
2014-02-14 01:56:50 -05:00
settings.m_glossiness = 0.82f;
settings.m_diffspec = 1.0f;
2014-02-13 23:46:34 -05:00
2014-02-14 01:56:50 -05:00
settings.m_rgbDiff[0] = 0.0f;
settings.m_rgbDiff[1] = 0.0f;
settings.m_rgbDiff[2] = 0.0f;
2014-02-13 23:46:34 -05:00
2014-02-14 01:56:50 -05:00
settings.m_rgbSpec[0] = 0.77f;
settings.m_rgbSpec[1] = 0.78f;
settings.m_rgbSpec[2] = 0.77f;
2014-02-13 23:46:34 -05:00
}
imguiEndScrollArea();
2014-06-23 02:47:14 -04:00
2014-02-13 23:46:34 -05:00
imguiEndFrame();
2014-02-14 01:56:50 -05:00
s_uniforms.m_glossiness = settings.m_glossiness;
s_uniforms.m_exposure = settings.m_exposure;
s_uniforms.m_diffspec = settings.m_diffspec;
s_uniforms.m_flags[0] = float(settings.m_diffuse);
s_uniforms.m_flags[1] = float(settings.m_specular);
s_uniforms.m_flags[2] = float(settings.m_diffuseIbl);
s_uniforms.m_flags[3] = float(settings.m_specularIbl);
memcpy(s_uniforms.m_rgbDiff, settings.m_rgbDiff, 3*sizeof(float));
memcpy(s_uniforms.m_rgbSpec, settings.m_rgbSpec, 3*sizeof(float));
2014-02-13 23:46:34 -05:00
s_uniforms.submitPerFrameUniforms();
int64_t now = bx::getHPCounter();
static int64_t last = now;
const int64_t frameTime = now - last;
last = now;
const double freq = double(bx::getHPFrequency() );
const double toMs = 1000.0/freq;
2014-02-14 01:56:50 -05:00
time += (float)(frameTime*settings.m_speed/freq);
2015-05-28 18:27:00 -04:00
s_uniforms.m_camPosTime[3] = time;
2014-02-13 23:46:34 -05:00
// Use debug font to print information about this example.
bgfx::dbgTextClear();
bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/18-ibl");
bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Image based lightning.");
bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
float at[3] = { 0.0f, 0.0f, 0.0f };
float eye[3] = { 0.0f, 0.0f, -3.0f };
2014-05-26 22:31:37 -04:00
bx::mtxRotateXY(s_uniforms.m_mtx
2014-02-13 23:46:34 -05:00
, 0.0f
, time
);
float view[16];
float proj[16];
2014-05-26 22:31:37 -04:00
bx::mtxIdentity(view);
bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f);
2014-02-13 23:46:34 -05:00
bgfx::setViewTransform(0, view, proj);
2014-05-26 22:31:37 -04:00
bx::mtxLookAt(view, eye, at);
2015-05-28 18:27:00 -04:00
memcpy(s_uniforms.m_camPosTime, eye, 3*sizeof(float) );
2014-05-26 22:31:37 -04:00
bx::mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
2014-02-13 23:46:34 -05:00
bgfx::setViewTransform(1, view, proj);
bgfx::setViewRect(0, 0, 0, width, height);
bgfx::setViewRect(1, 0, 0, width, height);
// View 0.
2015-06-08 13:41:48 -04:00
bgfx::setTexture(0, s_texCube, lightProbes[currentLightProbe].m_tex);
2014-02-13 23:46:34 -05:00
bgfx::setProgram(programSky);
bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
screenSpaceQuad( (float)width, (float)height, true);
2014-06-23 02:47:14 -04:00
s_uniforms.submitPerDrawUniforms();
2014-02-13 23:46:34 -05:00
bgfx::submit(0);
// View 1.
float mtx[16];
2014-05-26 22:31:37 -04:00
bx::mtxSRT(mtx
2014-02-13 23:46:34 -05:00
, 1.0f
, 1.0f
, 1.0f
, 0.0f
, bx::pi+time
2014-02-13 23:46:34 -05:00
, 0.0f
, 0.0f
, -1.0f
, 0.0f
);
2015-06-08 13:41:48 -04:00
bgfx::setTexture(0, s_texCube, lightProbes[currentLightProbe].m_tex);
bgfx::setTexture(1, s_texCubeIrr, lightProbes[currentLightProbe].m_texIrr);
meshSubmit(meshBunny, 1, programMesh, mtx);
2014-02-13 23:46:34 -05:00
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
bgfx::frame();
}
meshUnload(meshBunny);
2014-02-13 23:46:34 -05:00
// Cleanup.
bgfx::destroyProgram(programMesh);
bgfx::destroyProgram(programSky);
bgfx::destroyUniform(u_camPos);
bgfx::destroyUniform(u_flags);
bgfx::destroyUniform(u_params);
bgfx::destroyUniform(u_mtx);
2015-05-28 18:27:00 -04:00
bgfx::destroyUniform(s_texCube);
bgfx::destroyUniform(s_texCubeIrr);
2014-02-13 23:46:34 -05:00
2014-06-29 00:56:10 -04:00
for (uint8_t ii = 0; ii < LightProbe::Count; ++ii)
2014-02-13 23:46:34 -05:00
{
lightProbes[ii].destroy();
}
s_uniforms.destroy();
imguiDestroy();
// Shutdown bgfx.
bgfx::shutdown();
return 0;
}