CSEngine
Loading...
Searching...
No Matches
CSE::SEnvironmentMgr Class Reference

Public Member Functions

void RenderPBREnvironment ()
 
void BindPBREnvironmentMap (const GLProgramHandle *handle, int textureLayout) const
 

Static Public Member Functions

static unsigned int GetWidth ()
 
static unsigned int * GetPointerWidth ()
 
static void SetWidth (unsigned int width)
 
static unsigned int GetHeight ()
 
static unsigned int * GetPointerHeight ()
 
static void SetHeight (unsigned int height)
 
static void RenderPlaneVAO ()
 
static void RenderCubeVAO ()
 

Detailed Description

Definition at line 12 of file SEnvironmentMgr.h.

Constructor & Destructor Documentation

◆ SEnvironmentMgr()

SEnvironmentMgr::SEnvironmentMgr ( )

Definition at line 25 of file SEnvironmentMgr.cpp.

25 {
26
27}

◆ ~SEnvironmentMgr()

SEnvironmentMgr::~SEnvironmentMgr ( )

Definition at line 29 of file SEnvironmentMgr.cpp.

29 {
30 glDeleteVertexArrays(1, &m_cubeVAO);
31 glDeleteBuffers(1, &m_cubeVBO);
32 glDeleteVertexArrays(1, &m_planeVAO);
33 glDeleteBuffers(1, &m_planeVBO);
34}

Member Function Documentation

◆ BindPBREnvironmentMap()

void SEnvironmentMgr::BindPBREnvironmentMap ( const GLProgramHandle handle,
int  textureLayout 
) const

Definition at line 362 of file SEnvironmentMgr.cpp.

362 {
363 if(handle == nullptr) return;
364 m_irradianceMap->Bind(handle->Uniforms.LightIrradiance, textureLayout);
365 m_prefilterMap->Bind(handle->Uniforms.LightPrefilter, textureLayout + 1);
366 m_brdfMap->Bind(handle->Uniforms.LightBrdfLut, textureLayout + 2);
367}

◆ GetHeight()

unsigned int SEnvironmentMgr::GetHeight ( )
static

Definition at line 346 of file SEnvironmentMgr.cpp.

346 {
347 return m_height;
348}

◆ GetPointerHeight()

unsigned int * SEnvironmentMgr::GetPointerHeight ( )
static

Definition at line 358 of file SEnvironmentMgr.cpp.

358 {
359 return &m_height;
360}

◆ GetPointerWidth()

unsigned int * SEnvironmentMgr::GetPointerWidth ( )
static

Definition at line 354 of file SEnvironmentMgr.cpp.

354 {
355 return &m_width;
356}

◆ GetWidth()

unsigned int SEnvironmentMgr::GetWidth ( )
static

Definition at line 338 of file SEnvironmentMgr.cpp.

338 {
339 return m_width;
340}

◆ RenderCubeVAO()

void SEnvironmentMgr::RenderCubeVAO ( )
static

Definition at line 305 of file SEnvironmentMgr.cpp.

305 {
306 // render Cube
307 glBindVertexArray(m_cubeVAO);
308 glDrawArrays(GL_TRIANGLES, 0, 36);
309 glBindVertexArray(0);
310}

◆ RenderPBREnvironment()

void SEnvironmentMgr::RenderPBREnvironment ( )

Definition at line 36 of file SEnvironmentMgr.cpp.

36 {
37
38 std::string cubemap_str = CSE::AssetMgr::LoadAssetFile(CSE::AssetsPath() + "Shader/PBR/IBL/cubemap.vert");
39 std::string etc_str = CSE::AssetMgr::LoadAssetFile(CSE::AssetsPath() + "Shader/PBR/IBL/equirectangular_to_cubemap.frag");
40 std::string irr_str = CSE::AssetMgr::LoadAssetFile(CSE::AssetsPath() + "Shader/PBR/IBL/irradiance_convolution.frag");
41 std::string pre_str = CSE::AssetMgr::LoadAssetFile(CSE::AssetsPath() + "Shader/PBR/IBL/prefilter.frag");
42 std::string brdf_v_str = CSE::AssetMgr::LoadAssetFile(CSE::AssetsPath() + "Shader/PBR/IBL/brdf.vert");
43 std::string brdf_f_str = CSE::AssetMgr::LoadAssetFile(CSE::AssetsPath() + "Shader/PBR/IBL/brdf.frag");
44
45 m_equirectangularToCubemapShader = ShaderUtil::CreateProgramHandle(cubemap_str.c_str(), etc_str.c_str());
46 m_irradianceShader = ShaderUtil::CreateProgramHandle(cubemap_str.c_str(), irr_str.c_str());
47 m_prefilterShader = ShaderUtil::CreateProgramHandle(cubemap_str.c_str(), pre_str.c_str());
48 m_brdfShader = ShaderUtil::CreateProgramHandle(brdf_v_str.c_str(), brdf_f_str.c_str());
49
50
51 // pbr: setup framebuffer
52 // ----------------------
53 unsigned int captureFBO;
54 unsigned int captureRBO;
55
56 glDisable(GL_CULL_FACE);
57
58 glGenRenderbuffers(1, &captureRBO);
59 glBindRenderbuffer(GL_RENDERBUFFER, captureRBO);
60 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, 512, 512);
61
62 glGenFramebuffers(1, &captureFBO);
63 glBindFramebuffer(GL_FRAMEBUFFER, captureFBO);
64 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
65 GL_RENDERBUFFER, captureRBO);
66
67 // pbr: load the HDR environment map
68 // ---------------------------------
69 stbi_set_flip_vertically_on_load(true);
70 std::string hdr_path = CSE::AssetsPath() + "Texture/hdr/newport_loft.hdr";
71 m_hdrTexture = SResource::Create<STexture>(hdr_path);
72 stbi_set_flip_vertically_on_load(false);
73
74 // pbr: setup cubemap to render to and attach to framebuffer
75 // ---------------------------------------------------------
76 m_envCubemap = new STexture(STexture::TEX_CUBE);
77 m_envCubemap->SetName("envCubemap.textureCubeMap");
78 m_envCubemap->SetAbsoluteID("envCubemap.textureCubeMap");
79 std::string hash = "CSEENV0000000001";
80 m_envCubemap->SetHash(hash);
81 m_envCubemap->InitTextureMipmap(512, 512);
82
83
84 // pbr: set up projection and view matrices for capturing data onto the 6 cubemap face directions
85 // ----------------------------------------------------------------------------------------------
86 mat4 captureProjection = mat4::Perspective(90.0f, 1.0f, 0.1f, 10.0f);
87 mat4 captureViews[] =
88 {
89 mat4::LookAt(vec3(0.0f, 0.0f, 0.0f), vec3(1.0f, 0.0f, 0.0f), vec3(0.0f, -1.0f, 0.0f)),
90 mat4::LookAt(vec3(0.0f, 0.0f, 0.0f), vec3(-1.0f, 0.0f, 0.0f), vec3(0.0f, -1.0f, 0.0f)),
91 mat4::LookAt(vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 1.0f, 0.0f), vec3(0.0f, 0.0f, 1.0f)),
92 mat4::LookAt(vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, -1.0f, 0.0f), vec3(0.0f, 0.0f, -1.0f)),
93 mat4::LookAt(vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 0.0f, 1.0f), vec3(0.0f, -1.0f, 0.0f)),
94 mat4::LookAt(vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 0.0f, -1.0f), vec3(0.0f, -1.0f, 0.0f))
95 };
96
97 // pbr: convert HDR equirectangular environment map to cubemap equivalent
98 // ----------------------------------------------------------------------
99 glUseProgram(m_equirectangularToCubemapShader->Program);
100 int hdrTextureLocation = m_equirectangularToCubemapShader->UniformLocation("EquirectangularMap")->id;
101 m_equirectangularToCubemapShader->SetUniformMat4("PROJECTION_MATRIX", captureProjection);
102
103 m_hdrTexture->Bind(hdrTextureLocation, 0);
104
105 LoadCubeVAO();
106 glGetError();
107 glViewport(0, 0, 512, 512); // don't forget to configure the viewport to the capture dimensions.
108 glBindFramebuffer(GL_FRAMEBUFFER, captureFBO);
109 std::cout << "[PBR] Generating Environment Cubemap...";
110 for (unsigned int i = 0; i < 6; ++i) {
111 m_equirectangularToCubemapShader->SetUniformMat4("VIEW_MATRIX", captureViews[i]);
112
113 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
114 m_envCubemap->GetTextureID(), 0);
115 glGetError();
116 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
117 RenderCubeVAO();
118// std::string save_str = CSE::AssetsPath() + "test" + std::to_string(i) + ".bmp";
119// saveScreenshot(save_str.c_str());
120 }
121 m_envCubemap->GenerateMipmap();
122
123 std::cout << " finished!\n";
124
125 // pbr: create an irradiance cubemap, and re-scale capture FBO to irradiance scale.
126 // --------------------------------------------------------------------------------
127 m_irradianceMap = new STexture(STexture::TEX_CUBE);
128 m_irradianceMap->SetName("irradiance.textureCubeMap");
129 m_irradianceMap->SetAbsoluteID("irradiance.textureCubeMap");
130 hash = "CSEENV0000000002";
131 m_irradianceMap->SetHash(hash);
132 m_irradianceMap->InitTexture(32, 32);
133
134 glBindFramebuffer(GL_FRAMEBUFFER, captureFBO);
135 glBindRenderbuffer(GL_RENDERBUFFER, captureRBO);
136 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, 32, 32);
137
138 // pbr: solve diffuse integral by convolution to create an irradiance (cube)map.
139 // -----------------------------------------------------------------------------
140 glUseProgram(m_irradianceShader->Program);
141 int envTextureLocation = m_irradianceShader->UniformLocation("EnvironmentMap")->id;
142 m_irradianceShader->SetUniformMat4("PROJECTION_MATRIX", captureProjection);
143 m_envCubemap->Bind(envTextureLocation, 0);
144
145 glViewport(0, 0, 32, 32); // don't forget to configure the viewport to the capture dimensions.
146 glBindFramebuffer(GL_FRAMEBUFFER, captureFBO);
147 std::cout << "[PBR] Generating Irradiance Cubemap...";
148 for (unsigned int i = 0; i < 6; ++i) {
149 m_irradianceShader->SetUniformMat4("VIEW_MATRIX", captureViews[i]);
150
151 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
152 m_irradianceMap->GetTextureID(), 0);
153 glGetError();
154 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
155 RenderCubeVAO();
156 }
157
158 std::cout << " finished!\n";
159
160 // pbr: create a pre-filter cubemap, and re-scale capture FBO to pre-filter scale.
161 // --------------------------------------------------------------------------------
162 m_prefilterMap = new STexture(STexture::TEX_CUBE);
163 m_prefilterMap->SetName("prefilter.textureCubeMap");
164 m_prefilterMap->SetAbsoluteID("prefilter.textureCubeMap");
165 hash = "CSEENV0000000003";
166 m_prefilterMap->SetHash(hash);
167 m_prefilterMap->InitTextureMipmap(128, 128);
168
169 // generate mipmaps for the cubemap so OpenGL automatically allocates the required memory.
170 m_prefilterMap->GenerateMipmap();
171
172 // pbr: run a quasi monte-carlo simulation on the environment lighting to create a prefilter (cube)map.
173 // ----------------------------------------------------------------------------------------------------
174 glUseProgram(m_prefilterShader->Program);
175 envTextureLocation = m_prefilterShader->UniformLocation("EnvironmentMap")->id;
176 m_prefilterShader->SetUniformMat4("PROJECTION_MATRIX", captureProjection);
177 m_envCubemap->Bind(envTextureLocation, 0);
178
179
180 glBindFramebuffer(GL_FRAMEBUFFER, captureFBO);
181 unsigned int maxMipLevels = 5;
182
183 std::cout << "[PBR] Backing Prefiltering Textures...";
184 for (unsigned int mip = 0; mip < maxMipLevels; ++mip) {
185 // reisze framebuffer according to mip-level size.
186 unsigned int mipWidth = 128 * std::pow(0.5, mip);
187 unsigned int mipHeight = 128 * std::pow(0.5, mip);
188 glBindRenderbuffer(GL_RENDERBUFFER, captureRBO);
189 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, mipWidth, mipHeight);
190 glViewport(0, 0, mipWidth, mipHeight);
191
192 float roughness = (float) mip / (float) (maxMipLevels - 1);
193 m_prefilterShader->SetUniformFloat("FLOAT_ROUGHNESS", roughness);
194 for (unsigned int i = 0; i < 6; ++i) {
195 m_prefilterShader->SetUniformMat4("VIEW_MATRIX", captureViews[i]);
196 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
197 m_prefilterMap->GetTextureID(), mip);
198
199 glGetError();
200 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
201 RenderCubeVAO();
202// std::string save_str = CSE::AssetsPath() + "mip_" + std::to_string(mip) + "_" + std::to_string(i) + ".bmp";
203// saveScreenshot(save_str.c_str());
204 }
205 std::cout << "Level " << mip << "..";
206 }
207 std::cout << " finished!\n";
208
209 // pbr: generate a 2D LUT from the BRDF equations used.
210 // ----------------------------------------------------
211 std::cout << "[PBR] Backing a 2D LUT from the BRDF equations used...";
212 m_brdfMap = new STexture();
213 m_brdfMap->SetName("brdfLUT.texture");
214 m_brdfMap->SetAbsoluteID("brdfLUT.texture");
215 hash = "CSEENV0000000004";
216 m_brdfMap->SetHash(hash);
217 m_brdfMap->InitTexture(512, 512, GL_RG, GL_RG16F, GL_FLOAT);
218
219 // then re-configure capture framebuffer object and render screen-space quad with BRDF shader.
220 glBindFramebuffer(GL_FRAMEBUFFER, captureFBO);
221 glBindRenderbuffer(GL_RENDERBUFFER, captureRBO);
222 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, 512, 512);
223 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_brdfMap->GetTextureID(), 0);
224
225 glViewport(0, 0, 512, 512);
226 glUseProgram(m_brdfShader->Program);
227 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
228 LoadPlaneVAO();
229 RenderPlaneVAO();
230
231// std::string save_str = CSE::AssetsPath() + "brdf.bmp";
232// saveScreenshot(save_str.c_str());
233 std::cout << " finished!\n";
234
235 //release fbo, rbo
236 glDeleteFramebuffers(1, &captureFBO);
237 glDeleteRenderbuffers(1, &captureRBO);
238
239 glBindFramebuffer(GL_FRAMEBUFFER, 0);
240 glEnable(GL_CULL_FACE);
241}
static GLProgramHandle * CreateProgramHandle(const GLchar *vertexSource, const GLchar *fragmentSource, GLProgramHandle *handle=nullptr)
Creates a program handle.

◆ RenderPlaneVAO()

void SEnvironmentMgr::RenderPlaneVAO ( )
static

Definition at line 332 of file SEnvironmentMgr.cpp.

332 {
333 glBindVertexArray(m_planeVAO);
334 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
335 glBindVertexArray(0);
336}

◆ SetHeight()

void SEnvironmentMgr::SetHeight ( unsigned int  height)
static

Definition at line 350 of file SEnvironmentMgr.cpp.

350 {
351 m_height = height;
352}

◆ SetWidth()

void SEnvironmentMgr::SetWidth ( unsigned int  width)
static

Definition at line 342 of file SEnvironmentMgr.cpp.

342 {
343 m_width = width;
344}

The documentation for this class was generated from the following files: