shadow map test

im trying to get the typical depth shadow working but seems this test is causing problems:

 
	//Enable shadow comparison
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);

	//Shadow comparison should be true (ie not in shadow) if r<=texture
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);

	//Shadow comparison should generate an INTENSITY result
	glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);

 

when i turn it on, my scene goes black, im wondering how to turn the parameters off or back to default after i finished with them? when i comment out the code, all i get is a projected depth texture as expected

It might be that your problem is with the setup of your Texture matrix (or glPolygonOffset()) and that the test is identifying everything in your scene as being in shadow.

Try using glPolygonOffset() (which you are probably already using) and changing the values to relatively extreme values to see if that improves things (I used something like glPolygonOffset ( 3.0f, 7.0f ) ).

To turn the comparison off you use glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);

You call this before you do your lighting render (ie. After you have done your “ambient” only render).

hrmm okay, now it seems nothing is happening,

i do:
pass 1, render from light, (render back faces)
pass 2, render scene (im currently just doing this with no lighting) and no projective texture
pass 3, render scene with projective texture (with 50% white)

now, on pass 3, if i set the depth comparison to on before, and turn it off after, but seems nothing is rendered (there is no colour difference) :-/ if i dont do the depth pass, i get a projected texture as expected

i assumed i didnt have to do polygon offset when rendering back faces, but ill give it a shot

ug polygon offset goes in when rendering from lights im guessing?

edit:

glPolygonOffset didnt seem to have any effect, i’ve uploaded a demo, left click turns on the depth test, clicing again turns it off

http://mathewstwins.customer.netspace.net.au/temp/shadowmap.zip

i assumed i didnt have to do polygon offset when rendering back faces

That is correct.

What does your texture matrix look like?

-SirKnight

Are you using the vertex/fragment shaders that are in the zip file? For everyone else’s reference this is the fragment shader:

uniform sampler2D shadowMap;

void main()
{
	vec4 shadowValue = texture2D(shadowMap, vec2(gl_TexCoord[0]));// vec2(0.5, 0.5)); //gl_TexCoord[0]));
	//vec4 shadowValue = texture2DProj(shadowMap, gl_TexCoord[0]);
	
	//if(shadowValue.z == projCoord.z)
	//	discard;
		
	gl_FragColor = shadowValue;

}

And this is the vertex shader:

void main()
{
	gl_TexCoord[0] = gl_TextureMatrix[0] * gl_Vertex; 
	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

I haven’t looked at GLSL so I can’t say if there’s something wrong with this or not.

no im not using them, i couldnt get it working, so tried via GLSL but it doesnt like the way i generate the texture coords…crashes my pc

texture matrix is:

given a world matrix for the light, invert it

 
glMatrixMode(GL_TEXTURE);
    glLoadIdentity();
    glTranslatef(0.5, 0.5, 0.0);
    glScalef(0.5, 0.5, 1.0);


glFrustum(xmin, xmax, ymin, ymax, znear, zfar);

// not sure if this is right?
glTranslatef(light.GetPosition()[0], light.GetPosition()[1], light.GetPosition()[2]);


glMultMatrixf((GLfloat *) inverseLightWorldMat);
glMatrixMode(GL_MODELVIEW);
 

actually, in that demo i had an extra translation before by -1 in Z right before translating by lights position,

this fixed the problem of nothing seeming to be projected onto the sphere, but now it seems the sphere gets most of the projected texture projected on to it, plus it looks as if the prjected texture is rotating the opposite way as on the cube?

fudging the initial scale to about 0.3, 0.3, 1.0 fixed the projection on the sphere but i shouldnt have to do this, obviously something is wrong…

if you d/l the zip again, i have included shadowmap2.exe right clicking changes initial scale from 0.3 to 0.5, and you can see how it looks as if the proj. texture is rotating the opposite way on the sphere

I can’t run the demo because I don’t have MSVCP70.dll (yet I have Visual Studio .NET).

Try using the -ve position of the light (ie.

glTranslatef(-light.GetPosition()[0], -light.GetPosition()[1], -light.GetPosition()[2]);

I also have

glRotatef(90.0f, 1.0f, 0.0f, 0.0f);

just before I do this translation. Not sure why, it might be something specific to my application.