Shadow Mapping: Debugging Help

I have a problem with debugging my GLSL shadow mapping shader.
The problem is that the depth when read from the Shadow Map seems to be always 1.0.

But when I render the shadowmap, I get depths between 0.95 and 1.0. (normal depth image, rendering using smoothstep)

My Vertex Shader:


uniform highp mat4 ProjModelView;
uniform highp mat4 ShadowTexProj;

attribute mediump vec4 vertex;

varying highp vec4 shadowCoord;

void main() {
    gl_Position = ProjModelView*vertex;
    shadowCoord = ShadowTexProj*vertex;
}

Fragment Shader:


uniform sampler2D myTex;
uniform sampler2D depthTex;

varying highp vec4 shadowCoord;


void main() {
	highp float comp = (shadowCoord.z / shadowCoord.w);
	highp float depth = texture2D(depthTex, shadowCoord.xy).x;

	float varDot = max(dot(normalize(normal), lightDir), 0.0);

    gl_FragColor = depth == 1.0 ? vec4(1) : vec4(0);
}

Then I also wonder why the Bias matrix has look like:


QMatrix4x4(type:General
       0.5         0         0         0         
         0       0.5         0         0         
         0         0       0.5         0         
       0.5       0.5       0.5         1         
) 

instead of


// matBias.translate(0.5,0.5,0.5);
// matBias.scale(0.5);
// =>
QMatrix4x4(type:Translation,Scale
       0.5         0         0       0.5         
         0       0.5         0       0.5         
         0         0       0.5       0.5         
         0         0         0         1         
) 

You seem to forgot perspective division for the shadow map coords:


highp vec3 sc = shadowCoord.xyz / shadowCoord.w;
highp float comp = sc.z;
highp float depth = texture2D(depthTex, sc.xy).x;

yeah at some point I was using shadow2DProj there, so I introduced this error while playing around.
But even after the change all I get is a completely white teapot.

I quite ran out of ideas.

gl_FragColor = vec4(comp);

drawing a quad with the depth map using
gl_FragColor = vec4(smoothstep(0.95, 1.0, texture2D(depthTex, texCoord).x));

Make sure you have texture compare mode disabled. You could easily forgot about it after playing with shadow2DProj.
Having inappropriate sampler + compare_mode combinations is an undefined behavior according to the GL spec.

Heh, I don’t get you initial problem.
You said “depth when read from the Shadow Map seems to be always 1.0” and at the same time you read correct values from it (0.95-1.0).
There is an obvious GL state difference between these two read attempts :slight_smile:

great hint, not necessarily true: one time I read with
texture2D(depthTex, texCoord)

the other time with:
texture2D(depthTex, sc.xy)

so it came to me that maybe I just read at the wrong position. Therefore I plotted sc.x/y and obviously the bias matrix was not applied correctly. After I chose the one which I assumed to me more logical in my initial post it finally worked! Thanks for again for the tip.

here is my ugly shadowmapping render :smiley:

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.