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 ).
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:
-
Render hte scene from the light(s) pov.
-
Render the scene from the eye pov (ambient light only)
-
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);