-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathPBRTexturingBasicsApp.cpp
178 lines (154 loc) · 7.14 KB
/
PBRTexturingBasicsApp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "cinder/app/App.h"
#include "cinder/app/RendererGl.h"
#include "cinder/gl/gl.h"
#include "cinder/CameraUi.h"
#include "cinder/Log.h"
#include "CinderImGui.h"
using namespace ci;
using namespace ci::app;
using namespace std;
class PBRTexturingBasicsApp : public App {
public:
void setup() override;
void update() override;
void draw() override;
void resize() override;
CameraPersp mCamera;
CameraUi mCameraUi;
gl::BatchRef mModelBatch, mSkyBoxBatch;
gl::TextureCubeMapRef mIrradianceMap, mRadianceMap;
gl::Texture2dRef mNormalMap, mRoughnessMap, mMetallicMap;
int mGridSize;
bool mShowUi, mRotateModel;
float mRoughness, mMetallic, mSpecular;
Color mBaseColor;
float mGamma, mExposure, mTime;
};
void PBRTexturingBasicsApp::setup()
{
// add the common texture folder
addAssetDirectory( fs::path( __FILE__ ).parent_path().parent_path().parent_path() / "common/textures" );
// create a Camera and a Camera ui
mCamera = CameraPersp( getWindowWidth(), getWindowHeight(), 50.0f, 0.1f, 200.0f ).calcFraming( Sphere( vec3( 0.0f ), 2.0f ) );
mCameraUi = CameraUi( &mCamera, getWindow(), -1 );
// prepare ou rendering objects and shaders
auto pbrShader = gl::GlslProg::create( gl::GlslProg::Format().vertex( loadAsset( "PBR.vert" ) ).fragment( loadAsset( "PBR.frag" ) ) );
auto skyBoxShader = gl::GlslProg::create( gl::GlslProg::Format().vertex( loadAsset( "SkyBox.vert" ) ).fragment( loadAsset( "SkyBox.frag" ) ) );
mModelBatch = gl::Batch::create( geom::Teapot().subdivisions( 16 ) >> geom::Transform( glm::scale( vec3( 1.5f ) ) ), pbrShader );
mSkyBoxBatch = gl::Batch::create( geom::Cube().size( vec3( 100 ) ), skyBoxShader );
// load the prefiltered IBL Cubemaps
auto cubeMapFormat = gl::TextureCubeMap::Format().mipmap().internalFormat( GL_RGB16F ).minFilter( GL_LINEAR_MIPMAP_LINEAR ).magFilter( GL_LINEAR );
mIrradianceMap = gl::TextureCubeMap::createFromDds( loadAsset( "CathedralIrradiance.dds" ), cubeMapFormat );
mRadianceMap = gl::TextureCubeMap::createFromDds( loadAsset( "CathedralRadiance.dds" ), cubeMapFormat );
// load the material textures
auto textureFormat = gl::Texture2d::Format().mipmap().minFilter( GL_LINEAR_MIPMAP_LINEAR ).magFilter( GL_LINEAR );
mNormalMap = gl::Texture2d::create( loadImage( loadAsset( "normal.png" ) ) );
mRoughnessMap = gl::Texture2d::create( loadImage( loadAsset( "roughness.png" ) ), textureFormat );
mMetallicMap = gl::Texture2d::create( loadImage( loadAsset( "metallic.png" ) ), textureFormat );
// set the initial parameters and setup the ui
mGridSize = 5;
mRoughness = 1.0f;
mMetallic = 1.0f;
mSpecular = 1.0f;
mBaseColor = Color::white();
mGamma = 2.2f;
mExposure = 4.0f;
mTime = 0.0f;
mShowUi = false;
mRotateModel = false;
// prepare ui
ui::initialize();
getWindow()->getSignalKeyDown().connect( [this]( KeyEvent event ) {
if( event.getCode() == KeyEvent::KEY_SPACE ) mShowUi = !mShowUi;
} );
}
void PBRTexturingBasicsApp::resize()
{
mCamera.setAspectRatio( getWindowAspectRatio() );
}
void PBRTexturingBasicsApp::update()
{
// user interface
if( mShowUi ) {
ui::ScopedWindow window( "PBRBasics" );
if( ui::CollapsingHeader( "Material", nullptr, true, true ) ) {
ui::DragFloat( "Roughness", &mRoughness, 0.01f, 0.0f, 1.0f );
ui::DragFloat( "Metallic", &mMetallic, 0.01f, 0.0f, 1.0f );
ui::DragFloat( "Specular", &mSpecular, 0.01f, 0.0f, 1.0f );
ui::ColorEdit3( "Color", &mBaseColor[0] );
}
if( ui::CollapsingHeader( "Model", nullptr, true, true ) ) {
static int currentPrimitive = 1;
const static vector<string> primitives = { "Sphere", "Teapot", "Cube", "Capsule", "Torus", "TorusKnot" };
if( ui::Combo( "Primitive", ¤tPrimitive, primitives ) ) {
switch ( currentPrimitive ) {
case 0: mModelBatch->replaceVboMesh( gl::VboMesh::create( geom::Sphere().subdivisions( 64 ) ) ); break;
case 1: mModelBatch->replaceVboMesh( gl::VboMesh::create( geom::Teapot().subdivisions( 16 ) >> geom::Transform( glm::scale( vec3( 1.5f ) ) ) ) ); break;
case 2: mModelBatch->replaceVboMesh( gl::VboMesh::create( geom::Cube() ) ); break;
case 3: mModelBatch->replaceVboMesh( gl::VboMesh::create( geom::Capsule().subdivisionsAxis( 32 ).subdivisionsHeight( 32 ) ) ); break;
case 4: mModelBatch->replaceVboMesh( gl::VboMesh::create( geom::Torus().subdivisionsAxis( 32 ).subdivisionsHeight( 32 ) ) ); break;
case 5: mModelBatch->replaceVboMesh( gl::VboMesh::create( geom::TorusKnot().subdivisionsAxis( 128 ).subdivisionsHeight( 128 ).scale( vec3( 0.5f ) ) ) ); break;
}
}
ui::Checkbox( "Rotate", &mRotateModel );
}
if( ui::CollapsingHeader( "Environment", nullptr, true, true ) ) {
static int currentEnvironment = 2;
const static vector<string> environments = { "Bolonga", "Wells", "Cathedral" };
if( ui::Combo( "###Environments", ¤tEnvironment, environments ) ) {
auto cubeMapFormat = gl::TextureCubeMap::Format().mipmap().internalFormat( GL_RGB16F ).minFilter( GL_LINEAR_MIPMAP_LINEAR ).magFilter( GL_LINEAR );
mIrradianceMap = gl::TextureCubeMap::createFromDds( loadAsset( environments[currentEnvironment] + "Irradiance.dds" ), cubeMapFormat );
mRadianceMap = gl::TextureCubeMap::createFromDds( loadAsset( environments[currentEnvironment] + "Radiance.dds" ), cubeMapFormat );
}
}
if( ui::CollapsingHeader( "Rendering", nullptr, true, true ) ) {
ui::DragFloat( "Gamma", &mGamma, 0.01f, 0.0f );
ui::DragFloat( "Exposure", &mExposure, 0.01f, 0.0f );
}
}
if( mRotateModel ){
mTime += 0.025f;
}
}
void PBRTexturingBasicsApp::draw()
{
// clear window and set matrices
gl::clear( Color( 1, 0, 0 ) );
gl::setMatrices( mCamera );
// enable depth testing
gl::ScopedDepth scopedDepth( true );
// bind the textures
gl::ScopedTextureBind scopedTexBind0( mRadianceMap, 0 );
gl::ScopedTextureBind scopedTexBind1( mIrradianceMap, 1 );
gl::ScopedTextureBind scopedTexBind2( mNormalMap, 2 );
gl::ScopedTextureBind scopedTexBind3( mRoughnessMap, 3 );
gl::ScopedTextureBind scopedTexBind4( mMetallicMap, 4 );
auto shader = mModelBatch->getGlslProg();
shader->uniform( "uRadianceMap", 0 );
shader->uniform( "uIrradianceMap", 1 );
shader->uniform( "uNormalMap", 2 );
shader->uniform( "uRoughnessMap", 3 );
shader->uniform( "uMetallicMap", 4 );
shader->uniform( "uRadianceMapSize", (float) mRadianceMap->getWidth() );
shader->uniform( "uIrradianceMapSize", (float) mIrradianceMap->getWidth() );
// sends the base color, the specular opacity, roughness and metallic to the shader
shader->uniform( "uBaseColor", mBaseColor );
shader->uniform( "uSpecular", mSpecular );
shader->uniform( "uRoughness", mRoughness );
shader->uniform( "uMetallic", mMetallic );
// sends the tone-mapping uniforms
shader->uniform( "uExposure", mExposure );
shader->uniform( "uGamma", mGamma );
// render our test model
{
gl::ScopedMatrices scopedMatrices;
gl::setModelMatrix( glm::rotate( mTime, vec3( 0.123, 0.456, 0.789 ) ) );
mModelBatch->draw();
}
// render skybox
shader = mSkyBoxBatch->getGlslProg();
shader->uniform( "uExposure", mExposure );
shader->uniform( "uGamma", mGamma );
mSkyBoxBatch->draw();
}
CINDER_APP( PBRTexturingBasicsApp, RendererGl( RendererGl::Options().msaa( 16 ) ) )