Fix shadow acne
This commit is contained in:
parent
0254b81882
commit
6510f720cb
@ -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;
|
||||||
|
@ -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
|
||||||
@ -24,13 +27,13 @@ mediump float ShadowCalculation(vec4 fragPosLightSpace)
|
|||||||
|
|
||||||
// If we're outside of [0.0, 1.0] in the coordinates, return 0
|
// 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;
|
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)
|
// get closest depth value from light's perspective (using [0,1] range fragPosLight as coords)
|
||||||
mediump float closestDepth = texture(shadowMap, projCoords.xy).r;
|
mediump float closestDepth = texture(shadowMap, projCoords.xy).r;
|
||||||
// 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;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user