Fix shadow acne

This commit is contained in:
karl 2020-10-27 21:26:00 +01:00
parent 0254b81882
commit 6510f720cb
2 changed files with 10 additions and 6 deletions

View File

@ -211,9 +211,10 @@ public:
for (const RenderObject &obj : renderObjects) {
obj.render(shadowShader);
}
for (const RenderObject &obj : transparentRenderObjects) {
// Don't render transparent objects -- we just assume they don't cast any shadow
/* for (const RenderObject &obj : transparentRenderObjects) {
obj.render(shadowShader);
}
} */
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// reset viewport
@ -261,8 +262,8 @@ public:
int screen_width = 1280;
int screen_height = 720;
int shadow_width = 1024;
int shadow_height = 1024;
int shadow_width = 2048;
int shadow_height = 2048;
unsigned int depthMap;
unsigned int depthMapFBO;

View File

@ -17,6 +17,9 @@ uniform mediump float specularStrength;
mediump float ShadowCalculation(vec4 fragPosLightSpace)
{
// The bias varies depending on the angle to the light (the steeper the angle, the bigger the bias needs to be)
mediump float bias = max(0.005 * (1.0 - dot(Normal, lightDirection)), 0.0005);
// perform perspective divide
mediump vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
// transform to [0,1] range
@ -24,13 +27,13 @@ mediump float ShadowCalculation(vec4 fragPosLightSpace)
// If we're outside of [0.0, 1.0] in the coordinates, return 0
if (projCoords.x < 0.0 || projCoords.y < 0.0 || projCoords.x > 1.0 || projCoords.y > 1.0) return 0.0;
// get closest depth value from light's perspective (using [0,1] range fragPosLight as coords)
mediump float closestDepth = texture(shadowMap, projCoords.xy).r;
// get depth of current fragment from light's perspective
mediump float currentDepth = projCoords.z;
// check whether current frag pos is in shadow
mediump float shadow = currentDepth > closestDepth ? 1.0 : 0.0;
mediump float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0;
return shadow;
}