Use glPolygonOffset to remove shadow acne

It's a 2002 trick from M. Kilgard
This commit is contained in:
Vincent Lejeune 2014-04-24 21:02:24 +02:00
parent 85e720a58f
commit 84ddd7119e
3 changed files with 7 additions and 9 deletions

View File

@ -53,7 +53,7 @@ float getShadowFactor(vec3 pos, float bias, int index)
float sum = 0.;
for (int i = 0; i < 4; i++)
{
sum += texture(shadowtex, vec4(shadowtexcoord + shadowoffset[i] / 2048., float(index), 0.5 * shadowcoord.z + 0.5 + bias));
sum += texture(shadowtex, vec4(shadowtexcoord + shadowoffset[i] / 2048., float(index), 0.5 * shadowcoord.z + 0.5));
}
return sum / 4.;
}

View File

@ -22,7 +22,7 @@ out vec4 Spec;
vec3 DecodeNormal(vec2 n);
vec3 getSpecular(vec3 normal, vec3 eyedir, vec3 lightdir, vec3 color, float roughness);
vec3 getShadowFactor(vec3 pos, float bias)
vec3 getShadowFactor(vec3 pos)
{
vec3 cascadeColor[] = vec3[](
vec3(1., 0., 0.),
@ -37,7 +37,7 @@ vec3 getShadowFactor(vec3 pos, float bias)
vec2 shadowtexcoord = shadowcoord.xy * 0.5 + 0.5;
if (shadowtexcoord.x < 0. || shadowtexcoord.x > 1. || shadowtexcoord.y < 0. || shadowtexcoord.y > 1.)
continue;
return cascadeColor[i] * texture(shadowtex, vec4(shadowtexcoord, float(i), 0.5 * shadowcoord.z + 0.5 + bias));
return cascadeColor[i] * texture(shadowtex, vec4(shadowtexcoord, float(i), 0.5 * shadowcoord.z + 0.5));
}
return vec3(1.);
}
@ -61,9 +61,7 @@ void main() {
vec3 outcol = NdotL * col;
// Shadows
float bias = 0.005 * tan(acos(NdotL)); // According to the slope
bias = clamp(bias, 0., 0.01);
vec3 factor = getShadowFactor(xpos.xyz, bias);
vec3 factor = getShadowFactor(xpos.xyz);
Diff = vec4(factor * NdotL * col, 1.);
Spec = vec4(factor * Specular, 1.);
return;

View File

@ -665,8 +665,8 @@ void IrrDriver::renderShadows(//ShadowImportanceProvider * const sicb,
irr_driver->setPhase(SHADOW_PASS);
glDisable(GL_BLEND);
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1.5, 0.);
glBindFramebuffer(GL_FRAMEBUFFER, m_rtts->getShadowFBO());
glViewport(0, 0, 1024, 1024);
glClear(GL_DEPTH_BUFFER_BIT);
@ -674,7 +674,7 @@ void IrrDriver::renderShadows(//ShadowImportanceProvider * const sicb,
glBindBufferBase(GL_UNIFORM_BUFFER, 0, SharedObject::ViewProjectionMatrixesUBO);
m_scene_manager->drawAll(scene::ESNRP_SOLID);
glCullFace(GL_BACK);
glDisable(GL_POLYGON_OFFSET_FILL);
glViewport(0, 0, UserConfigParams::m_width, UserConfigParams::m_height);