PDA

View Full Version : stencil shadows on back face



narayan0
04-21-2005, 08:47 PM
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

zero_t9
04-21-2005, 09:52 PM
glEnable( GL_CULL_FACE ) ?


Originally posted by narayan0:
Hi,

[/CODE]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

dorbie
04-22-2005, 01:47 AM
I hope you mean back faces w.r.t. the light.

Your problem is a more fundamental conceptual one. To get the basic lighting math correct the shadow term needs to modulate direct illumination only, for specular and diffuse terms. And ambient illumination needs to be left alone for both front and back faces.

If you get this right it will make little difference if you shadow back faces because the lighting results being shadowed on the back faces will be almost zero for the most part.

Now some back faces (w.r.t. the light) may have at least one normal partly illuminated, so in general you probably want to apply shadows to back faces too just to handle illumination near the silhouettes consistently.

To support the direct illumination shadowing you need to split your rendering into direct and ambient illumination passes, or simply set all ambient terms to zero so that back faces are black even without shadows. You can add ambient later or add it in your zbuffer fill pass at the beginning.