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) { for (const RenderObject &obj : renderObjects) {
obj.render(shadowShader); 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); obj.render(shadowShader);
} } */
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
// reset viewport // reset viewport
@ -261,8 +262,8 @@ public:
int screen_width = 1280; int screen_width = 1280;
int screen_height = 720; int screen_height = 720;
int shadow_width = 1024; int shadow_width = 2048;
int shadow_height = 1024; int shadow_height = 2048;
unsigned int depthMap; unsigned int depthMap;
unsigned int depthMapFBO; unsigned int depthMapFBO;

View File

@ -17,6 +17,9 @@ uniform mediump float specularStrength;
mediump float ShadowCalculation(vec4 fragPosLightSpace) 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 // perform perspective divide
mediump vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w; mediump vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
// transform to [0,1] range // transform to [0,1] range
@ -30,7 +33,7 @@ mediump float ShadowCalculation(vec4 fragPosLightSpace)
// get depth of current fragment from light's perspective // get depth of current fragment from light's perspective
mediump float currentDepth = projCoords.z; mediump float currentDepth = projCoords.z;
// check whether current frag pos is in shadow // 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; return shadow;
} }