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
49
50
51
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
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
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
85
86 mat4 captureProjection = mat4::Perspective(90.0f, 1.0f, 0.1f, 10.0f);
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
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);
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
119
120 }
121 m_envCubemap->GenerateMipmap();
122
123 std::cout << " finished!\n";
124
125
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
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);
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
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
170 m_prefilterMap->GenerateMipmap();
171
172
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
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
203
204 }
205 std::cout << "Level " << mip << "..";
206 }
207 std::cout << " finished!\n";
208
209
210
211 std::cout << "[PBR] Backing a 2D LUT from the BRDF equations used...";
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
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
232
233 std::cout << " finished!\n";
234
235
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.