Hardware shadow mapping

I tried to modify a Hardware shadow mapping project found at www.paulsprojects.net (this site has really many cool opengl projects!) in order to texture scene objects. However, when i bind textures (in the 3rd draw pass), shadows disappear. I fail to understand why that happen, if you dare to take a look at the code you may download it here .


In the demo, the active texture unit is used to store the depth map. In order to draw objects with a different texture while shadowing, you need to bind that different texture to a different texture unit.

Another way to think about it is that the depth texture functions like a “shadow blanket”. It is “draped” over the scene. In order for the other objects to use a texture while the shadow blanket texture is enabled, then that other texture must be bound to a different texture unit.

Hope that helps…

I’ve setup a multitexture system, so now it basically works, but there is still a problem: if you take a look at the code, the texture matrix is modified during the 3rd pass in order to calculate the shadow texture, so when i rotate the object its texture does not, because the coordinates are the shadow map texture ones. Probably with this code you can understand better what i mean:

	//Calculate texture matrix for projection
	//This matrix takes us from eye space to the light's clip space
	//It is postmultiplied by the inverse of the current view matrix when specifying texgen
	static MATRIX4X4 biasMatrix(0.5f, 0.0f, 0.0f, 0.0f,
								0.0f, 0.5f, 0.0f, 0.0f,
								0.0f, 0.0f, 0.5f, 0.0f,
								0.5f, 0.5f, 0.5f, 1.0f);	//bias from [-1, 1] to [0, 1]
	MATRIX4X4 textureMatrix=biasMatrix*lightProjectionMatrix*lightViewMatrix;

	//Set up texture coordinate generation.
	glTexGenfv(GL_S, GL_EYE_PLANE, textureMatrix.GetRow(0));

	glTexGenfv(GL_T, GL_EYE_PLANE, textureMatrix.GetRow(1));

	glTexGenfv(GL_R, GL_EYE_PLANE, textureMatrix.GetRow(2));

	glTexGenfv(GL_Q, GL_EYE_PLANE, textureMatrix.GetRow(3));

	//Bind & enable shadow map texture
	glActiveTextureARB( GL_TEXTURE0_ARB );
	glBindTexture(GL_TEXTURE_2D, shadowMapTexture);


	//Bind & enable object texture
	glActiveTextureARB( GL_TEXTURE1_ARB );
	glBindTexture(GL_TEXTURE_2D, object_texture);
	//Enable shadow comparison

	//Shadow comparison should be true (ie not in shadow) if r<=texture

	//Shadow comparison should generate an INTENSITY result

	//Set alpha test to discard false comparisons
	glAlphaFunc(GL_GEQUAL, 0.99f);


I should define eye_linear coordinates for the object_texture, but is it possible to do that ? I mean, texture coordinate definition is relative to the object or to each texture ?
Can i define for example sphere_map coordinates for ARB0, eye_linear for ARB1 and then use multitexturing ?
i’m really confused …

I’ve updated the project code with lastest modifications here

You don’t need texgen or comparison functions for the object texture. The coordinates passed should be relative to the texture. (e.g. one corner is 0,0 and the other is 1,1).

Thus the shadow texture coordinates are generated automatically using texgen.

The object texture coordinates should be passed manually - e.g. MultiTexCoord…

The comparison mode is only required for shadow.

Hope this helps…

Thanks i got it working. However, i made some tests playing around also with the nvidia SDK and i arrived to the point that stencil shadows and hardware shadow mapping have the same identical performances.
I have a 32000 triangles model, with stencil shadows i get about 37fps, with hardware shadow mapping about 34 (so even slower). I have a P4 3.2 and a Geforce6800GT.
I supposed Hw shadow mapping would be much much faster. Is it so or am i mistaken somewhere ? There are other solutions in order to build fast self-cast shadows ?

I think use GL_SGIX_shadow …
will more quickly.


Shadowmap v7 by Bobby G Chance Jr.

The specific bottleneck in a scene will vary quite widely. Are you CPU limited, vertex limited, raster limited, texture filtering limited, shader limited, or limited somewhere else? Depending on where it is, different techniques will give different results.

32,000 isn’t a very big scene for today’s cards, by the way.

I tried Bobby’s shadowmap, it uses a 235 triangles model and performances on a GF6800 is around 30fps. Didn’t study the code but that is a very low performance …