CSEngine
Loading...
Searching...
No Matches
SEnvironmentMgr.cpp
1//
2// Created by ounols on 19. 5. 4.
3//
4#include "SEnvironmentMgr.h"
5#include "ShaderUtil.h"
6#include "../AssetsDef.h"
7#include "../Matrix.h"
8#include "SkyboxUtil.h"
9
10#include <string>
11#include <iostream>
12#include "../Loader/STB/stb_image.h"
13#include "SMaterial.h"
14#include "../Settings.h"
15#include "../GLProgramHandle.h"
16
17using namespace CSE;
18
19unsigned int SEnvironmentMgr::m_width = 0;
20unsigned int SEnvironmentMgr::m_height = 0;
21unsigned int SEnvironmentMgr::m_planeVAO = 0;
22unsigned int SEnvironmentMgr::m_cubeVAO = 0;
23
24SEnvironmentMgr::SEnvironmentMgr() {
25}
26
27SEnvironmentMgr::~SEnvironmentMgr() {
28 glDeleteBuffers(1, &m_cubeVBO);
29 glDeleteBuffers(1, &m_planeVBO);
30}
31
32void SEnvironmentMgr::RenderPBREnvironment() {
33
34 std::string cubemap_str = CSE::AssetMgr::LoadAssetFile(CSE::AssetsPath() + "Shader/PBR/IBL/cubemap.vert");
35 std::string etc_str = CSE::AssetMgr::LoadAssetFile(CSE::AssetsPath() + "Shader/PBR/IBL/equirectangular_to_cubemap.frag");
36 std::string irr_str = CSE::AssetMgr::LoadAssetFile(CSE::AssetsPath() + "Shader/PBR/IBL/irradiance_convolution.frag");
37 std::string pre_str = CSE::AssetMgr::LoadAssetFile(CSE::AssetsPath() + "Shader/PBR/IBL/prefilter.frag");
38
39 m_equirectangularToCubemapShader = ShaderUtil::CreateProgramHandle(cubemap_str.c_str(), etc_str.c_str());
40 m_irradianceShader = ShaderUtil::CreateProgramHandle(cubemap_str.c_str(), irr_str.c_str());
41 m_prefilterShader = ShaderUtil::CreateProgramHandle(cubemap_str.c_str(), pre_str.c_str());
42
43 // pbr: setup framebuffer
44 // ----------------------
45 glDisable(GL_CULL_FACE);
46
47 glGenRenderbuffers(1, &m_captureRBO);
48 glBindRenderbuffer(GL_RENDERBUFFER, m_captureRBO);
49 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, 512, 512);
50
51 glGenFramebuffers(1, &m_captureFBO);
52 glBindFramebuffer(GL_FRAMEBUFFER, m_captureFBO);
53 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
54 GL_RENDERBUFFER, m_captureRBO);
55
56 // pbr: load the HDR environment map
57 // ---------------------------------
58 stbi_set_flip_vertically_on_load(true);
59 std::string hdr_path = CSE::AssetsPath() + "Texture/hdr/newport_loft.hdr";
60 m_hdrTexture = SResource::Create<STexture>(hdr_path);
61 stbi_set_flip_vertically_on_load(false);
62
63 // pbr: setup cubemap to render to and attach to framebuffer
64 // ---------------------------------------------------------
65 m_envCubemap = new STexture(STexture::TEX_CUBE);
66 m_envCubemap->SetName("envCubemap.textureCubeMap");
67 m_envCubemap->SetAbsoluteID("envCubemap.textureCubeMap");
68 std::string hash = "CSEENV0000000001";
69 m_envCubemap->SetHash(hash);
70 m_envCubemap->InitTextureMipmap(512, 512);
71
72
73 // pbr: set up projection and view matrices for capturing data onto the 6 cubemap face directions
74 // ----------------------------------------------------------------------------------------------
75 mat4 captureProjection = mat4::Perspective(90.0f, 1.0f, 0.1f, 10.0f);
76 mat4 captureViews[] =
77 {
78 mat4::LookAt(vec3(0.0f, 0.0f, 0.0f), vec3(1.0f, 0.0f, 0.0f), vec3(0.0f, -1.0f, 0.0f)),
79 mat4::LookAt(vec3(0.0f, 0.0f, 0.0f), vec3(-1.0f, 0.0f, 0.0f), vec3(0.0f, -1.0f, 0.0f)),
80 mat4::LookAt(vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 1.0f, 0.0f), vec3(0.0f, 0.0f, 1.0f)),
81 mat4::LookAt(vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, -1.0f, 0.0f), vec3(0.0f, 0.0f, -1.0f)),
82 mat4::LookAt(vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 0.0f, 1.0f), vec3(0.0f, -1.0f, 0.0f)),
83 mat4::LookAt(vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 0.0f, -1.0f), vec3(0.0f, -1.0f, 0.0f))
84 };
85
86 // pbr: convert HDR equirectangular environment map to cubemap equivalent
87 // ----------------------------------------------------------------------
88 glUseProgram(m_equirectangularToCubemapShader->Program);
89 int hdrTextureLocation = m_equirectangularToCubemapShader->UniformLocation("EquirectangularMap")->id;
90 m_equirectangularToCubemapShader->SetUniformMat4("PROJECTION_MATRIX", captureProjection);
91
92 m_hdrTexture->Bind(hdrTextureLocation, 0);
93
94 if(m_cubeVAO <= 0) LoadCubeVAO();
95 glGetError();
96 glViewport(0, 0, 512, 512); // don't forget to configure the viewport to the capture dimensions.
97 glBindFramebuffer(GL_FRAMEBUFFER, m_captureFBO);
98 std::cout << "[PBR] Generating Environment Cubemap...";
99 for (unsigned int i = 0; i < 6; ++i) {
100 m_equirectangularToCubemapShader->SetUniformMat4("VIEW_MATRIX", captureViews[i]);
101
102 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
103 m_envCubemap->GetTextureID(), 0);
104 glGetError();
105 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
106 RenderCubeVAO();
107// std::string save_str = CSE::AssetsPath() + "test" + std::to_string(i) + ".bmp";
108// saveScreenshot(save_str.c_str());
109 }
110 m_envCubemap->GenerateMipmap();
111
112 std::cout << " finished!\n";
113
114 // pbr: create an irradiance cubemap, and re-scale capture FBO to irradiance scale.
115 // --------------------------------------------------------------------------------
116 m_irradianceMap = new STexture(STexture::TEX_CUBE);
117 m_irradianceMap->SetName("irradiance.textureCubeMap");
118 m_irradianceMap->SetAbsoluteID("irradiance.textureCubeMap");
119 hash = "CSEENV0000000002";
120 m_irradianceMap->SetHash(hash);
121 m_irradianceMap->InitTexture(32, 32);
122
123 glBindFramebuffer(GL_FRAMEBUFFER, m_captureFBO);
124 glBindRenderbuffer(GL_RENDERBUFFER, m_captureRBO);
125 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, 32, 32);
126
127 // pbr: solve diffuse integral by convolution to create an irradiance (cube)map.
128 // -----------------------------------------------------------------------------
129 glUseProgram(m_irradianceShader->Program);
130 int envTextureLocation = m_irradianceShader->UniformLocation("EnvironmentMap")->id;
131 m_irradianceShader->SetUniformMat4("PROJECTION_MATRIX", captureProjection);
132 m_envCubemap->Bind(envTextureLocation, 0);
133
134 glViewport(0, 0, 32, 32); // don't forget to configure the viewport to the capture dimensions.
135 glBindFramebuffer(GL_FRAMEBUFFER, m_captureFBO);
136 std::cout << "[PBR] Generating Irradiance Cubemap...";
137 for (unsigned int i = 0; i < 6; ++i) {
138 m_irradianceShader->SetUniformMat4("VIEW_MATRIX", captureViews[i]);
139
140 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
141 m_irradianceMap->GetTextureID(), 0);
142 glGetError();
143 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
144 RenderCubeVAO();
145 }
146
147 std::cout << " finished!\n";
148
149 // pbr: create a pre-filter cubemap, and re-scale capture FBO to pre-filter scale.
150 // --------------------------------------------------------------------------------
151 m_prefilterMap = new STexture(STexture::TEX_CUBE);
152 m_prefilterMap->SetName("prefilter.textureCubeMap");
153 m_prefilterMap->SetAbsoluteID("prefilter.textureCubeMap");
154 hash = "CSEENV0000000003";
155 m_prefilterMap->SetHash(hash);
156 m_prefilterMap->InitTextureMipmap(128, 128);
157
158 // generate mipmaps for the cubemap so OpenGL automatically allocates the required memory.
159 m_prefilterMap->GenerateMipmap();
160
161 // pbr: run a quasi monte-carlo simulation on the environment lighting to create a prefilter (cube)map.
162 // ----------------------------------------------------------------------------------------------------
163 glUseProgram(m_prefilterShader->Program);
164 envTextureLocation = m_prefilterShader->UniformLocation("EnvironmentMap")->id;
165 m_prefilterShader->SetUniformMat4("PROJECTION_MATRIX", captureProjection);
166 m_envCubemap->Bind(envTextureLocation, 0);
167
168
169 glBindFramebuffer(GL_FRAMEBUFFER, m_captureFBO);
170 unsigned int maxMipLevels = 5;
171
172 std::cout << "[PBR] Backing Prefiltering Textures...";
173 for (unsigned int mip = 0; mip < maxMipLevels; ++mip) {
174 // reisze framebuffer according to mip-level size.
175 unsigned int mipWidth = 128 * std::pow(0.5, mip);
176 unsigned int mipHeight = 128 * std::pow(0.5, mip);
177 glBindRenderbuffer(GL_RENDERBUFFER, m_captureRBO);
178 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, mipWidth, mipHeight);
179 glViewport(0, 0, mipWidth, mipHeight);
180
181 float roughness = (float) mip / (float) (maxMipLevels - 1);
182 m_prefilterShader->SetUniformFloat("FLOAT_ROUGHNESS", roughness);
183 for (unsigned int i = 0; i < 6; ++i) {
184 m_prefilterShader->SetUniformMat4("VIEW_MATRIX", captureViews[i]);
185 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
186 m_prefilterMap->GetTextureID(), mip);
187
188 glGetError();
189 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
190 RenderCubeVAO();
191// std::string save_str = CSE::AssetsPath() + "mip_" + std::to_string(mip) + "_" + std::to_string(i) + ".bmp";
192// saveScreenshot(save_str.c_str());
193 }
194 std::cout << "Level " << mip << "..";
195 }
196 std::cout << " finished!\n";
197}
198
199void SEnvironmentMgr::LoadCubeVAO() {
200 float vertices[] = {
201 // back face
202 -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, // bottom-left
203 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, // top-right
204 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, // bottom-right
205 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, // top-right
206 -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, // bottom-left
207 -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, // top-left
208 // front face
209 -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom-left
210 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, // bottom-right
211 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // top-right
212 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // top-right
213 -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, // top-left
214 -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom-left
215 // left face
216 -1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top-right
217 -1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top-left
218 -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom-left
219 -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom-left
220 -1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // bottom-right
221 -1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top-right
222 // right face
223 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top-left
224 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom-right
225 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top-right
226 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom-right
227 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top-left
228 1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // bottom-left
229 // bottom face
230 -1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, // top-right
231 1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f, // top-left
232 1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, // bottom-left
233 1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, // bottom-left
234 -1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, // bottom-right
235 -1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, // top-right
236 // top face
237 -1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // top-left
238 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom-right
239 1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, // top-right
240 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom-right
241 -1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // top-left
242 -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f // bottom-left
243 };
244 glGenVertexArrays(1, &m_cubeVAO);
245 glGenBuffers(1, &m_cubeVBO);
246 // fill buffer
247 glBindBuffer(GL_ARRAY_BUFFER, m_cubeVBO);
248 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
249 // link vertex attributes
250 glBindVertexArray(m_cubeVAO);
251 glEnableVertexAttribArray(0);
252 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*) 0);
253 glEnableVertexAttribArray(1);
254 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
255 glEnableVertexAttribArray(2);
256 glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
257 glBindBuffer(GL_ARRAY_BUFFER, 0);
258 glBindVertexArray(0);
259}
260
261void SEnvironmentMgr::RenderCubeVAO() {
262 // render Cube
263 glBindVertexArray(m_cubeVAO);
264 glDrawArrays(GL_TRIANGLES, 0, 36);
265 glBindVertexArray(0);
266}
267
268void SEnvironmentMgr::LoadPlaneVAO() {
269 float quadVertices[] = {
270 // positions // texture Coords
271 -1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
272 -1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
273 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
274 1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
275 };
276 // setup plane VAO
277 glGenVertexArrays(1, &m_planeVAO);
278 glGenBuffers(1, &m_planeVBO);
279 glBindVertexArray(m_planeVAO);
280 glBindBuffer(GL_ARRAY_BUFFER, m_planeVBO);
281 glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
282 glEnableVertexAttribArray(0);
283 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*) 0);
284 glEnableVertexAttribArray(1);
285 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*) (3 * sizeof(float)));
286}
287
288void SEnvironmentMgr::RenderPlaneVAO() {
289 glBindVertexArray(m_planeVAO);
290 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
291 glBindVertexArray(0);
292}
293
294unsigned int SEnvironmentMgr::GetWidth() {
295 return m_width;
296}
297
298void SEnvironmentMgr::SetWidth(unsigned int width) {
299 m_width = width;
300}
301
302unsigned int SEnvironmentMgr::GetHeight() {
303 return m_height;
304}
305
306void SEnvironmentMgr::SetHeight(unsigned int height) {
307 m_height = height;
308}
309
310unsigned int* SEnvironmentMgr::GetPointerWidth() {
311 return &m_width;
312}
313
314unsigned int* SEnvironmentMgr::GetPointerHeight() {
315 return &m_height;
316}
317
318int SEnvironmentMgr::BindPBREnvironmentMap(const GLProgramHandle* handle, int textureLayout) const {
319 if(handle == nullptr) return 0;
320 int steps = 0;
321 if(handle->Uniforms.LightIrradiance != HANDLE_NULL)
322 m_irradianceMap->Bind(handle->Uniforms.LightIrradiance, textureLayout + steps++);
323 if(handle->Uniforms.LightPrefilter != HANDLE_NULL)
324 m_prefilterMap->Bind(handle->Uniforms.LightPrefilter, textureLayout + steps++);
325 return steps;
326}
327
328int SEnvironmentMgr::BindBRDFLUT(const GLProgramHandle* handle, int textureLayout) const {
329 if(handle == nullptr) return 0;
330 if(handle->Uniforms.LightBrdfLut != HANDLE_NULL) {
331 m_brdfMap->Bind(handle->Uniforms.LightBrdfLut, textureLayout);
332 return 1;
333 }
334 return 0;
335}
336
337void SEnvironmentMgr::RenderBRDFLUT() {
338 std::cout << "[PBR] Backing a 2D LUT from the BRDF equations used...";
339
340 std::string brdf_v_str = CSE::AssetMgr::LoadAssetFile(CSE::AssetsPath() + "Shaders/IBL/brdf.vert");
341 std::string brdf_f_str = CSE::AssetMgr::LoadAssetFile(CSE::AssetsPath() + "Shaders/IBL/brdf.frag");
342
343 m_brdfShader = ShaderUtil::CreateProgramHandle(brdf_v_str.c_str(), brdf_f_str.c_str());
344
345
346 // Create BRDF LUT texture
347 m_brdfMap = new STexture();
348 m_brdfMap->SetName("brdfLUT.texture");
349 m_brdfMap->SetAbsoluteID("brdfLUT.texture");
350 std::string hash = "CSEENV0000000004";
351 m_brdfMap->SetHash(hash);
352 m_brdfMap->InitTexture(512, 512, GL_RG, GL_RG16F, GL_FLOAT);
353
354 // Configure capture framebuffer for BRDF LUT rendering
355 glBindFramebuffer(GL_FRAMEBUFFER, m_captureFBO);
356 glBindRenderbuffer(GL_RENDERBUFFER, m_captureRBO);
357 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, 512, 512);
358 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_brdfMap->GetTextureID(), 0);
359
360 // Render screen-space quad with BRDF shader
361 glViewport(0, 0, 512, 512);
362 glUseProgram(m_brdfShader->Program);
363 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
364 if(m_planeVAO <= 0) LoadPlaneVAO();
365 RenderPlaneVAO();
366
367 std::cout << " finished!\n";
368}
369
370void SEnvironmentMgr::ReleaseRenderingResources() {
371 //release fbo, rbo
372 glDeleteFramebuffers(1, &m_captureFBO);
373 glDeleteRenderbuffers(1, &m_captureRBO);
374
375 m_captureFBO = 0;
376 m_captureRBO = 0;
377
378 glBindFramebuffer(GL_FRAMEBUFFER, 0);
379 glEnable(GL_CULL_FACE);
380}
381
382void SEnvironmentMgr::ReleaseVAO() {
383 glDeleteVertexArrays(1, &m_cubeVAO);
384 glDeleteVertexArrays(1, &m_planeVAO);
385}
static GLProgramHandle * CreateProgramHandle(const GLchar *vertexSource, const GLchar *fragmentSource, GLProgramHandle *handle=nullptr)
Creates a program handle.