Better Shadows

This commit is contained in:
4VRDriver
2020-09-24 16:36:32 +02:00
parent dd44888a4d
commit 33dc801483
5 changed files with 32 additions and 12 deletions

View File

@@ -9,7 +9,7 @@ Size=894,195
Collapsed=0
[Window][Debug Utils]
Pos=14,6
Pos=13,15
Size=863,335
Collapsed=0

View File

@@ -78,7 +78,7 @@ void computeShading(
float computeAttenuation(vec3 lightPos, vec3 fragPos, float K_q);
float computeShadows(vec4 fragPosLightSpace);
float computeShadows(vec4 fragPosLightSpace, vec3 normal, vec3 lightDir);
void main() {
@@ -93,10 +93,6 @@ void main() {
fragmentColor += pointLightContribution(u_pointLight[i], normal, v_fragmentPosition, viewDir);
}
// This is not final: ambient lighting should not be affected of shadows.
float shadows = computeShadows(v_fragmentPositionLightSpace);
fragmentColor *= (1.0f - shadows);
//fragmentColor += spotLightContribution(u_spotLight, normal, v_fragmentPosition, viewDir);
f_color = vec4(fragmentColor, 1.0f);
@@ -114,7 +110,9 @@ vec3 directionalLightContribution(DirectionalLight light, vec3 normal, vec3 view
vec3 ambient, diffuse, specular;
computeShading(light.ambient, light.diffuse, light.specular, lightDir, viewDir, normal, ambient, diffuse, specular);
return (ambient + diffuse + specular) * 1.0f;
float shadows = computeShadows(v_fragmentPositionLightSpace, normal, lightDir);
return (ambient + (1.0f - shadows) * (diffuse + specular)) * 1.0f;
}
vec3 pointLightContribution(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir) {
@@ -191,7 +189,7 @@ float computeAttenuation(vec3 lightPos, vec3 fragPos, float K_q) {
}
float computeShadows(vec4 fragPosLightSpace) {
float computeShadows(vec4 fragPosLightSpace, vec3 normal, vec3 lightDir) {
// Perspective divide
vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
@@ -200,10 +198,25 @@ float computeShadows(vec4 fragPosLightSpace) {
projCoords *= 0.5f;
projCoords += 0.5f;
if(projCoords.z > 1.0f) return 0.0f;
float closestDepth = texture(u_texture_shadowMap, projCoords.xy).r;
float currentDepth = projCoords.z;
float shadow = currentDepth > closestDepth ? 1.0 : 0.0;
float bias = max(0.05 * (1.0 - dot(normal, lightDir)), 0.005);
float shadow = 0.0;
vec2 texelSize = 1.0 / textureSize(u_texture_shadowMap, 0);
for(int x = -1; x <= 1; ++x)
{
for(int y = -1; y <= 1; ++y)
{
float pcfDepth = texture(u_texture_shadowMap, projCoords.xy + vec2(x, y) * texelSize).r;
shadow += currentDepth - bias > pcfDepth ? 1.0 : 0.0;
}
}
shadow /= 9.0;
return shadow;
}

View File

@@ -2,4 +2,6 @@
void main() {
// Empty as we aren't rendering to any color buffer
}

View File

@@ -79,17 +79,22 @@ void World::calculateShadows(ShaderProgram *p_shaderProgram) {
glClear(GL_DEPTH_BUFFER_BIT);
// ConfigureShaderAndMatrices();
float near_plane = 0.1f, far_plane = 7.5f;
float near_plane = 1.0f, far_plane = 15.0f;
glm::mat4 lightProjection = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, near_plane, far_plane);
glm::mat4 lightView = glm::lookAt(-glm::vec3(-0.2f, -1.0f, -0.3f), glm::vec3( 0.0f, 0.0f, 0.0f), glm::vec3( 0.0f, 1.0f, 0.0f));
glm::mat4 lightView = glm::lookAt(-5.0f * glm::vec3(-0.2f, -1.0f, -0.3f), glm::vec3( 0.0f, 0.0f, 0.0f), glm::vec3( 0.0f, 1.0f, 0.0f));
glm::mat4 lightViewProjectionMatrix = lightProjection * lightView;
// Switch face culling (Peter panning)
glCullFace(GL_BACK);
// Draw scene from light perspective
// Draw all entities
for(auto it = entities.begin(); it != entities.end(); it++) {
it->drawShadows(lightViewProjectionMatrix, p_shaderProgram);
}
glCullFace(GL_FRONT);
depthMapFBO.unbind();
// Reset viewport size

View File

@@ -40,7 +40,7 @@ private:
//SpotLight spotLight;
// Shadows
const int SHADOW_RES = 1024;
const int SHADOW_RES = 4096;
DepthMap depthMapFBO;
};