# Artefacts with shadow mapping (GLSL)

This topic is 4428 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello! I´ve implemented a shadow-Mapping algorithm using GLSL. What i do is storing the Modelview and Projection matrices of the Lightsource. Then i calculate the matrix, which should translate my vertex to the lightspace:
glMatrixMode(GL_TEXTURE);
glLoadMatrixf( mBias);		// The bias matrix to convert to a 0 to 1 ratio
glMultMatrixf( lightProjection);	// The light's projection matrix
glMultMatrixf( lightModelView);	// The light's modelview matrix
glMultMatrixf( invMat); // the inverted camera-Modelview


In the Shader, the important lines are:
// vertexShader:
vec4 realPos = gl_ModelViewMatrix * gl_Vertex;
projCoord = gl_TextureMatrix[0] * realPos;
const float kTransparency = 0.3;


With the light initially positioned at 0/2/0 i got an beautyful shadow - but when i move the light ( or choose another "bad" lightposition, i got ugly artefacts. See the picture to see how it looks : or try: http://img492.imageshack.us/img492/2193/bild6dv.jpg ( Sorry, first time i use imageShack!) As you can see, the shadow is casted in both directions???!!! ( The yellow quad is the lightsource!) Can anyone tell me why? I am grateful for every suggestion!!! Thanks!

##### Share on other sites
Hi, looks like three issues here.

Looks like your texture matrix is not being calculated in the right order, hence the shadows dont line up correctly. I did this by loading the identity, then translating and scaling both by (0.5,0.5,0.5) which effectivly multiplys by the offset. I then set gluPerspective to add the light frustum and finally multiplied by the lights lookat matrix. (Hope that makes sense)

Next is that the depth texture does not look like its being stored as a depth texture, but a RGB texture, giving that gradient effect instead of solid on/off shadows.
glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 512, 512, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
I used this to stop that.

The last problem is this back-projection. This naturally happens when you do a texture projection. The easiest way to counter this is to check if the cooordinates from the texture projection are negative. This requires a shader that allows branching, so I have not tried this yet (Im on a geforce 3 at work).

Hope this helps.

##### Share on other sites
Let us review your three points:
1. As you can see in my code, i do that exactly like you - but then i multiply it with the inverse of the actual modelviewmatrix. I think that is important, so why you didn't do it? Can you explain me your projection?
2. glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 512, 512, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); is exactly what i use to initialize the texture.
3. What do you mean with back-projection. The wrong shadow on the cube or the wrong shadow in the background? I added
if ( ( projCoord.x < 0 ) || ( projCoord.y < 0 ) || ( projCoord.z < 0 ) || ( projCoord.w < 0 ))
projCoord = vec4( 0, 0, 0, 0);
in my vertex-shader - but the result is even worse than before or didn´t i understand that correctly?