Part of the Khronos Group

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: PCF technical error (shadow map)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member Newbie
    Join Date
    Feb 2013

    PCF technical error (shadow map)

    What is wrong here?

    The code:

    Fragment Shader

    Code :
    #version 330
    layout (std140) uniform Material {
        vec4 diffuse;
        vec4 ambient;
        vec4 specular;
        vec4 emissive;
        float shininess;
        int texCount;
    uniform vec4 Lightambient;
    uniform vec4 Lightdiffuse;
    uniform vec4 Lightspecular;
    in vec4 ShadowCoord;
    in vec3 lightVec;
    in vec3 eyeVec;
    in vec2 TexCoord;
    uniform float invRadius;
    uniform sampler2D Textures;
    uniform sampler2D normalMap;
    uniform sampler2DShadow ShadowMap;
    out vec4 output;
    void main()
    	float distSqr = dot(lightVec, lightVec);
    	float att = clamp(1.0 - invRadius * sqrt(distSqr), 0.0, 1.0);
    	vec3 lVec = lightVec * inversesqrt(distSqr);
    	vec3 vVec = normalize(eyeVec);
    	vec4 base = texture2D(Textures, TexCoord);
    	vec3 bump = normalize( texture2D(normalMap, TexCoord).xyz * 2.0 - 1.0);
    	vec4 vAmbient = Lightambient * ambient;
    	float diffuseVec = max( dot(lVec, bump), 0.0 );
    	vec4 vDiffuse = Lightdiffuse * diffuseVec * diffuse;	
    	float specularVec = pow(clamp(dot(reflect(-lVec, bump), vVec), 0.0, 1.0), 
    	                 shininess );
    	vec4 vSpecular = Lightspecular * specular * specularVec;	
    	float shadow = 0.0;
    	if (ShadowCoord.w > 1.0)
                     shadow = textureProj(ShadowMap, ShadowCoord);
    	output = ( vAmbient*base + vDiffuse*base + vSpecular) * att * (shadow+0.2);

    Any suggestions?

    Thank you, best regards.

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Quote Originally Posted by Frankgrandson View Post
    What is wrong here?
    It's called self-shadowing.

    See Shadow (gritengine), in particular the diagram about 60% of the way down. Namely: this one.

    Main solution is to add some light-space bias to push the shadow map texels below the actual surface casting them (many techniques: constant bias, normal offset, depth gradiant, dual depth layer, midpoint, etc.. Casting light-space back-faces instead of light-space front faces is sometimes touted to fix it. But that breaks in a number of ways.

    Increasing the resolution of the shadow map tends to reduce the depth error, and reduce the amount of bias you need to fully push the shadows below the surface casting them.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts