PDA

View Full Version : shadow mapping questions



Abdallah DIB
02-25-2009, 07:36 AM
i was trying to implement the shadow mapping technique but i have some problem in my code if anyone can help me .
here is a pseudo code :

//first pass from light postion
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadMatrixf(lightProjectionMatrix);

glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(lightViewMatrix);

glViewport(0, 0, W_shadow, H_shadow)
glCullFace(GL_FRONT);
DrawShadowedScene();
//get shadow texture
glBindTexture(GL_TEXTURE_2D, shadowMapTexture);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, shadow, shadow);

glCullFace(GL_BACK);
//second pass also from light position. reset view matrix
glClear( GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//bias for scaling for [-1 1] to [0 1]
Matrix textureMatrix=Bias*lightProjectionMatrix;
//texture generation for s,t,r,q using EYE_LINEAR ,the 4 plans //are the 4 lines of the texturematrix
//glTexGeni...glTexGenfv...glenable(GL_TEXTURE_GEN_. ..)
//draw the scene to generate s t r q
DrawShadowedScene();
//final pass : back to camera projection
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(cameraProjectionMatrix);

glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(cameraViewMatrix);
//shadow comparaison
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_ALPHA);
//draw with alpha test
glAlphaFunc(GL_GEQUAL, 0.99f);
glEnable(GL_ALPHA_TEST);
DrawShadowedScene();
//

i exlpain something for the second pass :
what i did in the second pass is that i reset the modeview matrix , so the texturematrix will contain only the lightprojectionmatix. and with eye linear texture generation the texturematrix will be post-multiplied with the inverse of the modelview matrix wich is an identity matrix . so the final texturematrix contain the light clip coordinate , and that ix excactly what i want to make texture comparaison . did i say something wrong. any idea why my method doesnt work ?

Komat
02-26-2009, 01:47 AM
What did you intended to archieve with the second pass? Normally there should be two passes. Render into the shadowmap from the position of the light and then render from the position of the camera while projecting the shadowmap texture to the scene.

Abdallah DIB
02-26-2009, 03:41 AM
thx for ur response
well i agree with u that i need only two passes.
but i am trying to implement this technique with 3 passes . 2 from light view and the final from camera view .
the second pass from the light view should project my shadow map to light clip space .
it is possible to do this? if no correct me if I'm wrong
tx

Komat
02-26-2009, 03:03 PM
The second pass does not make sense to me at all.

First, it renders the scene as if the camera was in zero so the geometry drawn on the screen does not have any relation to what is seen by the camera or the light. Second, it transforms the texture coordinates by something which depends only on the dimensions of the frustum of the light. This combined means that the result is independent on movement of the camera or light which does not seem right.

Also its only output is something written to the frame buffer and because it is not copied to any texture, it can not be used to generate texture coordinates for sampling of the shadowmap texture during the third pass.

Abdallah DIB
02-27-2009, 01:16 AM
okay perfect , i got it , tx u very much. i will try to re implement it using 2 pass.
tx