Irradiance Map

This commit is contained in:
Nigel Barink 2023-06-02 20:07:32 +02:00
parent 6fd22a85d8
commit ae516a8007
4 changed files with 106 additions and 9 deletions

35
Shaders/irradienceconv.fs Normal file
View File

@ -0,0 +1,35 @@
#version 460 core
out vec4 FragColor;
in vec3 localPos;
uniform samplerCube environmentMap;
const float PI = 3.14159265359;
void main(){
vec3 normal = normalize(localPos);
vec3 irradiance = vec3(0.0);
vec3 up = vec3(0.0, 1.0, 0.0);
vec3 right = normalize(cross(up, normal));
up = normalize(cross(normal, right));
float sampleDelta = 0.025;
float nrSamples = 0.0;
for(float phi = 0.0; phi < 2.0 * PI; phi += sampleDelta){
for(float theta = 0.0; theta < 0.5 * PI; theta += sampleDelta){
// spherical to cartesian
vec3 tangentSample = vec3(sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta));
// tangent space to world
vec3 sampleVec = tangentSample.x * right + tangentSample.y * up + tangentSample.z * normal;
irradiance += texture(environmentMap, sampleVec).rgb * cos(theta) * sin(theta);
nrSamples++;
}
}
irradiance = PI * irradiance * (1.0 / float(nrSamples));
FragColor = vec4(irradiance,1.0);
}

View File

@ -12,6 +12,8 @@ uniform sampler2D metallicMap;
uniform sampler2D normalMap; uniform sampler2D normalMap;
uniform sampler2D roughnessMap; uniform sampler2D roughnessMap;
uniform sampler2D aoMap; uniform sampler2D aoMap;
uniform samplerCube irradianceMap;
uniform vec3 lightPositions[4]; uniform vec3 lightPositions[4];
uniform vec3 lightColors[4]; uniform vec3 lightColors[4];
@ -21,9 +23,9 @@ const float PI = 3.14159265359;
// ratio Refraction vs Reflection (F function) // ratio Refraction vs Reflection (F function)
vec3 fresnelSchlick (float cosTheta, vec3 F0) vec3 fresnelSchlick (float cosTheta, vec3 F0, float roughness)
{ {
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0); return F0 + (max(vec3(1.0- roughness), F0)- F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
} }
// Calculate Normal distribution (D function) // Calculate Normal distribution (D function)
@ -102,7 +104,7 @@ void main(){
float NDF = DistributionGGX(N,H, roughness); float NDF = DistributionGGX(N,H, roughness);
float G = GeometrySmith(N,V,L, roughness); float G = GeometrySmith(N,V,L, roughness);
vec3 F = fresnelSchlick(max(dot(H,V), 0.0), F0); vec3 F = fresnelSchlick(max(dot(H,V), 0.0), F0, roughness);
vec3 kS = F; vec3 kS = F;
vec3 kD = vec3(1.0) - kS; vec3 kD = vec3(1.0) - kS;
@ -118,8 +120,15 @@ void main(){
Lo += (kD * albedo / PI + specular) * radiance * NdotL; Lo += (kD * albedo / PI + specular) * radiance * NdotL;
} }
// Calculate the ambient term and add it // Calculate the ambient term and add it
vec3 ambient = vec3(0.03) * albedo * ao; vec3 kS = fresnelSchlick(max(dot(N,V), 0.0), F0, roughness);
vec3 kD = 1.0 - kS;
kD *= 1.0 - metallic;
vec3 irradiance = texture(irradianceMap, N).rgb;
vec3 diffuse = irradiance* albedo;
vec3 ambient = (kD * diffuse) * ao;
vec3 color = ambient + Lo; vec3 color = ambient + Lo;
// HDR tonemapping // HDR tonemapping

13
Shaders/skybox2.vs Normal file
View File

@ -0,0 +1,13 @@
#version 460 core
layout (location = 0) in vec3 aPos;
out vec3 localPos;
uniform mat4 projection;
uniform mat4 view;
void main()
{
localPos = aPos;
gl_Position = projection * view * vec4(localPos ,1.0);
}

View File

@ -120,7 +120,7 @@ void renderCube() {
} }
unsigned int irradianceMap;
unsigned int envCubemap; unsigned int envCubemap;
unsigned int envMapVAO; unsigned int envMapVAO;
void Renderer::Setup() void Renderer::Setup()
@ -219,7 +219,46 @@ void Renderer::Setup()
renderCube(); renderCube();
} }
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Generate irradiancemap
glGenTextures(1, &irradianceMap);
glBindTexture(GL_TEXTURE_CUBE_MAP, irradianceMap);
for (unsigned int i = 0; i < 6; ++i) {
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB16F, 32, 32, 0, GL_RGB, GL_FLOAT, nullptr);
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindFramebuffer(GL_FRAMEBUFFER, captureFBO);
glBindRenderbuffer(GL_RENDERBUFFER, captureRBO);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, 32, 32);
auto irradianceShader = Shader();
irradianceShader.Load("../Shaders/skybox2.vs", "../Shaders/irradienceconv.fs");
irradianceShader.use();
irradianceShader.setInt("environmentMap", 0);
irradianceShader.setMat4("projection", captureProjection);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, envCubemap);
glViewport(0, 0, 32, 32);
glBindFramebuffer(GL_FRAMEBUFFER, captureFBO);
for (unsigned int i = 0; i < 6; ++i) {
irradianceShader.setMat4("view", captureViews[i]);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, irradianceMap, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
renderCube();
}
glViewport(0, 0, 800, 600); // restore viewport; glViewport(0, 0, 800, 600); // restore viewport;
glBindFramebuffer(GL_FRAMEBUFFER, 0);
std::vector<float> m_skyboxVertices = { std::vector<float> m_skyboxVertices = {
// positions // positions
@ -325,8 +364,6 @@ void Renderer::resize(int width, int height ) {
} }
unsigned int sphereVAO = 0; unsigned int sphereVAO = 0;
unsigned int indexCount; unsigned int indexCount;
void renderSphere() { void renderSphere() {
@ -419,7 +456,6 @@ void renderSphere() {
glDrawElements(GL_TRIANGLE_STRIP, indexCount, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLE_STRIP, indexCount, GL_UNSIGNED_INT, 0);
} }
void Renderer::Render(Scene& scene) void Renderer::Render(Scene& scene)
{ {
@ -503,6 +539,7 @@ void Renderer::Render(Scene& scene)
shader.setInt("metallicMap", 2); shader.setInt("metallicMap", 2);
shader.setInt("roughnessMap", 3); shader.setInt("roughnessMap", 3);
shader.setInt("aoMap", 4); shader.setInt("aoMap", 4);
shader.setInt("irradianceMap", 5);
shader.setMat4("projection", projection); shader.setMat4("projection", projection);
view = scene.MainCamera.GetViewMatrix(); view = scene.MainCamera.GetViewMatrix();
@ -521,10 +558,13 @@ void Renderer::Render(Scene& scene)
glActiveTexture(GL_TEXTURE3); glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, roughness); glBindTexture(GL_TEXTURE_2D, roughness);
glActiveTexture(GL_TEXTURE4); glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, ao); glBindTexture(GL_TEXTURE_2D, ao);
glActiveTexture(GL_TEXTURE5);
glBindTexture(GL_TEXTURE_CUBE_MAP, irradianceMap);
// Render Spheres // Render Spheres
model = glm::mat4(1.0f); model = glm::mat4(1.0f);
for (int row = 0; row < nrRows; ++row) { for (int row = 0; row < nrRows; ++row) {