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#include "../CaptureDef.h"
10
11#include <string>
12#include <iostream>
13#include "../Loader/STB/stb_image.h"
14#include "SMaterial.h"
15#include "../Settings.h"
16#include "../GLProgramHandle.h"
17
18using namespace CSE;
19
20unsigned int SEnvironmentMgr::m_width = 0;
21unsigned int SEnvironmentMgr::m_height = 0;
22unsigned int SEnvironmentMgr::m_planeVAO = 0;
23unsigned int SEnvironmentMgr::m_cubeVAO = 0;
24
25SEnvironmentMgr::SEnvironmentMgr() {
26
27}
28
29SEnvironmentMgr::~SEnvironmentMgr() {
30 glDeleteVertexArrays(1, &m_cubeVAO);
31 glDeleteBuffers(1, &m_cubeVBO);
32 glDeleteVertexArrays(1, &m_planeVAO);
33 glDeleteBuffers(1, &m_planeVBO);
34}
35
36void SEnvironmentMgr::RenderPBREnvironment() {
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}
242
243void SEnvironmentMgr::LoadCubeVAO() {
244 float vertices[] = {
245 // back face
246 -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, // bottom-left
247 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, // top-right
248 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, // bottom-right
249 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, // top-right
250 -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, // bottom-left
251 -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, // top-left
252 // front face
253 -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom-left
254 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, // bottom-right
255 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // top-right
256 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // top-right
257 -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, // top-left
258 -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom-left
259 // left face
260 -1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top-right
261 -1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top-left
262 -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom-left
263 -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom-left
264 -1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // bottom-right
265 -1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top-right
266 // right face
267 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top-left
268 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom-right
269 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top-right
270 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom-right
271 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top-left
272 1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // bottom-left
273 // bottom face
274 -1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, // top-right
275 1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f, // top-left
276 1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, // bottom-left
277 1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, // bottom-left
278 -1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, // bottom-right
279 -1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, // top-right
280 // top face
281 -1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // top-left
282 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom-right
283 1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, // top-right
284 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom-right
285 -1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // top-left
286 -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f // bottom-left
287 };
288 glGenVertexArrays(1, &m_cubeVAO);
289 glGenBuffers(1, &m_cubeVBO);
290 // fill buffer
291 glBindBuffer(GL_ARRAY_BUFFER, m_cubeVBO);
292 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
293 // link vertex attributes
294 glBindVertexArray(m_cubeVAO);
295 glEnableVertexAttribArray(0);
296 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*) 0);
297 glEnableVertexAttribArray(1);
298 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
299 glEnableVertexAttribArray(2);
300 glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
301 glBindBuffer(GL_ARRAY_BUFFER, 0);
302 glBindVertexArray(0);
303}
304
305void SEnvironmentMgr::RenderCubeVAO() {
306 // render Cube
307 glBindVertexArray(m_cubeVAO);
308 glDrawArrays(GL_TRIANGLES, 0, 36);
309 glBindVertexArray(0);
310}
311
312void SEnvironmentMgr::LoadPlaneVAO() {
313 float quadVertices[] = {
314 // positions // texture Coords
315 -1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
316 -1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
317 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
318 1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
319 };
320 // setup plane VAO
321 glGenVertexArrays(1, &m_planeVAO);
322 glGenBuffers(1, &m_planeVBO);
323 glBindVertexArray(m_planeVAO);
324 glBindBuffer(GL_ARRAY_BUFFER, m_planeVBO);
325 glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
326 glEnableVertexAttribArray(0);
327 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*) 0);
328 glEnableVertexAttribArray(1);
329 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*) (3 * sizeof(float)));
330}
331
332void SEnvironmentMgr::RenderPlaneVAO() {
333 glBindVertexArray(m_planeVAO);
334 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
335 glBindVertexArray(0);
336}
337
338unsigned int SEnvironmentMgr::GetWidth() {
339 return m_width;
340}
341
342void SEnvironmentMgr::SetWidth(unsigned int width) {
343 m_width = width;
344}
345
346unsigned int SEnvironmentMgr::GetHeight() {
347 return m_height;
348}
349
350void SEnvironmentMgr::SetHeight(unsigned int height) {
351 m_height = height;
352}
353
354unsigned int* SEnvironmentMgr::GetPointerWidth() {
355 return &m_width;
356}
357
358unsigned int* SEnvironmentMgr::GetPointerHeight() {
359 return &m_height;
360}
361
362void SEnvironmentMgr::BindPBREnvironmentMap(const GLProgramHandle* handle, int textureLayout) const {
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}
static GLProgramHandle * CreateProgramHandle(const GLchar *vertexSource, const GLchar *fragmentSource, GLProgramHandle *handle=nullptr)
Creates a program handle.