PDA

View Full Version : shadow mapping with 2 light sources



onegod
06-26-2004, 12:13 AM
I have read the shadow mapping tutorial (the one there is a link to in the documentation section :
www.paulsprojects.net/tutorials/smt/smt.html (http://www.paulsprojects.net/tutorials/smt/smt.html) ).
I am interested in extending that progrma to multiple light sources. So I began with 2 light sources , located in different positions and now I get z-fighting and only the last shadow (the one casted from the 2nd light) is valid.

What is the correct method to cast shadows using shadow maps for multiple lights ?

I used 3 passes as suggested in that tutorial:
1. Render hte scene from the light(s) pov.
2. Render the scene from the eye pov (ambient light only)
3. Rendering the shadows.
Here is the code of the 3rd pass :

glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_S, GL_EYE_PLANE, textureMatrix1.GetRow(0));
glEnable(GL_TEXTURE_GEN_S);

glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_T, GL_EYE_PLANE, textureMatrix1.GetRow(1));
glEnable(GL_TEXTURE_GEN_T);

glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_R, GL_EYE_PLANE, textureMatrix1.GetRow(2));
glEnable(GL_TEXTURE_GEN_R);

glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_Q, GL_EYE_PLANE, textureMatrix1.GetRow(3));
glEnable(GL_TEXTURE_GEN_Q);

glBindTexture(GL_TEXTURE_2D, shadowMapTexture[0]);
glEnable(GL_TEXTURE_2D);

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_INTENSITY);

glAlphaFunc(GL_GEQUAL, 0.99f);
glEnable(GL_ALPHA_TEST);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glMultMatrixf(sceneRotate);
DrawScene(angle);
glPopMatrix();


glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_S, GL_EYE_PLANE, textureMatrix2.GetRow(0));
glEnable(GL_TEXTURE_GEN_S);

glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_T, GL_EYE_PLANE, textureMatrix2.GetRow(1));
glEnable(GL_TEXTURE_GEN_T);

glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_R, GL_EYE_PLANE, textureMatrix2.GetRow(2));
glEnable(GL_TEXTURE_GEN_R);

glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_Q, GL_EYE_PLANE, textureMatrix2.GetRow(3));
glEnable(GL_TEXTURE_GEN_Q);

glBindTexture(GL_TEXTURE_2D, shadowMapTexture[1]);
glEnable(GL_TEXTURE_2D);

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_INTENSITaY);

glAlphaFunc(GL_GEQUAL, 0.99f);
glEnable(GL_ALPHA_TEST);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glMultMatrixf(sceneRotate);
DrawScene(angle);
glPopMatrix();

glColorMask(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glDisable(GL_ALPHA_TEST);
glDepthMask(GL_TRUE);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glMultMatrixf(sceneRotate);
DrawScene(angle);
glPopMatrix();
glDisable(GL_TEXTURE_2D);

glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_GEN_R);
glDisable(GL_TEXTURE_GEN_Q);

glDisable(GL_ALPHA_TEST);

yooyo
06-26-2004, 01:07 AM
You should use additive blending for each shadowmap in pass 3. Disable depth write and set depth func to GL_EQUAL or GL_LEQUAL. Anyway... post some screenshots...

yooyo

onegod
06-26-2004, 10:29 PM
thanks for the fast reply.
I tried doing the same thing. It didn't work.
If you can point me to the exact lines I should add those functions it would help me.
As so - how do I post images ?

yooyo
06-27-2004, 03:20 AM
I don't understand what you doing in your code. Why using glAlphaFunc? Why you clear depthbuffer?
Anyway try to add:
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
on top of pass3 and remove all alpha stuff.
For correct lighting and shadowing it is better to do "lighting dark scene" instead of "darking light scene". You have to do...
1. Zfill
1.1 Disable depth write
2. Ambient + global illumination
3. Shadows:
for each light
- render shadow map / volume
- render light affected triangles (using additive blending)
4. Textures

Ambient + global illumination should be dark scene. Pass 3 do lighting dark scene. Before pass 4 you should get coreccted shadowed and lighted scene in framebuffer.

For screenshots... uhm.. upload somewhere and post link here using URL button below.

yooyo