Hi,
I’ve implemented stencil shadows the following way:
int i;
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
polygon->Draw();
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE);
glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
polygon->Draw();
glPushMatrix();
glColor4f(0.0, 0.0, 0.0, alphaValue);
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
float shadowMat[4][4];
float dot = a * lightpos[0] + b * lightpos[1] + c * lightpos[2] + d * lightpos[3];
shadowMat[0][0] = dot - lightpos[0] * a;
shadowMat[1][0] = 0.f - lightpos[0] * b;
shadowMat[2][0] = 0.f - lightpos[0] * c;
shadowMat[3][0] = 0.f - lightpos[0] * d;
shadowMat[0][1] = 0.f - lightpos[1] * a;
shadowMat[1][1] = dot - lightpos[1] * b;
shadowMat[2][1] = 0.f - lightpos[1] * c;
shadowMat[3][1] = 0.f - lightpos[1] * d;
shadowMat[0][2] = 0.f - lightpos[2] * a;
shadowMat[1][2] = 0.f - lightpos[2] * b;
shadowMat[2][2] = dot - lightpos[2] * c;
shadowMat[3][2] = 0.f - lightpos[2] * d;
shadowMat[0][3] = 0.f - lightpos[3] * a;
shadowMat[1][3] = 0.f - lightpos[3] * b;
shadowMat[2][3] = 0.f - lightpos[3] * c;
shadowMat[3][3] = dot - lightpos[3] * d;
glMultMatrixf((float *)shadowMat);
for(i = 0; i < numObjects; i++)
object[i].Draw();
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
glEnable(GL_LIGHTING);
glPopMatrix();
glPushMatrix();
glDisable(GL_STENCIL_TEST);
for(i = 0; i < numObjects; i++)
object[i].Draw();
glPopMatrix();
I was wondering how do I not draw the stencil shadows on the back face of the polygon that the shadows are being cast onto.
thanks