that is added to the texel coordinates before the lookup/comparison.
To add the PCF technique to the shadow mapping algorithm, we'll just make a few changes to
the shaders from the recipe Rendering shadows with shadow maps:
1. When setting up the FBO for the shadow map, make sure to use linear filtering on the
depth texture. Replace the corresponding lines with the following code:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR);
2. Use the following code for the shadeWithShadow function within the
fragment shader:
subroutine (RenderPassType)
void shadeWithShadow()
{
vec3 ambient = Light.Intensity * Material.Ka;
vec3 diffAndSpec = phongModelDiffAndSpec();
// The sum of the comparisons with nearby texels
float sum = 0;
// Sum contributions from texels around ShadowCoord
sum += textureProjOffset(ShadowMap, ShadowCoord,
ivec2(-1,-1));
sum += textureProjOffset(ShadowMap, ShadowCoord,
ivec2(-1,1));
sum += textureProjOffset(ShadowMap, ShadowCoord,
ivec2(1,1));
sum += textureProjOffset(ShadowMap, ShadowCoord,
ivec2(1,-1));
float shadow = sum * 0.25;
FragColor = vec4(ambient + diffAndSpec * shadow,1.0);
}
相比之前的版本改了两处:
1.texturefilter liner
2.临近点加权求和,采用textureProjOffset函数