Im running in 32 bits,“the stencil buffer should be actvated”.This the routine im using for shadow, mainly NEHE tuts midified to work with many object.
this is call each frame:
glDisable(GL_LIGHTING);
glDepthMask(GL_FALSE);
glDepthFunc(GL_LEQUAL);
glEnable(GL_STENCIL_TEST);
glColorMask(0, 0, 0, 0);
glStencilFunc(GL_ALWAYS, 1, 0xffffffff);
// first pass, stencil operation decreases stencil value
glFrontFace(GL_CCW);
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
for(i=0;i<nbObject;i++)
glCallList(2000+i);
// second pass, stencil operation increases stencil value
glFrontFace(GL_CW);
glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
for(i=0;i<nbObject;i++)
glCallList(2000+i);
glFrontFace(GL_CCW);
glColorMask(1, 1, 1, 1);
//draw a shadowing rectangle covering the entire screen
glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glStencilFunc(GL_NOTEQUAL, 0, 0xffffffff);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glPushMatrix();
glLoadIdentity();
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(-0.1f, 0.1f,-0.10f);
glVertex3f(-0.1f,-0.1f,-0.10f);
glVertex3f( 0.1f, 0.1f,-0.10f);
glVertex3f( 0.1f,-0.1f,-0.10f);
glEnd();
glPopMatrix();
glDisable(GL_BLEND);
glDepthFunc(GL_LEQUAL);
glDepthMask(GL_TRUE);
glEnable(GL_LIGHTING);
glDisable(GL_STENCIL_TEST);
glShadeModel(GL_SMOOTH);
the for loop just do all the shadow volume(they are compiled once at loading this ways)
void DrawShadow(t3DObject pObject,float lp)
{
unsigned int i, j/,k/;
unsigned int p2;
sPoint v1, v2;
for (i=0; i<pObject->numOfFaces;i++)
{
if (object[i].visible)
for (j=0;j<3;j++)
{
//k = object[i].neigh[j];
//if ((!k) | | (!object[i].visible))
//{
// here we have an edge, we must draw a polygon
p2 =(j+1)%3;
//fprintf(fp,"%d
",(j+1)%3);
//calculate the length of the vector
v1.x = (object[i].p[j].x - lp[0])*100;
v1.y = (object[i].p[j].y - lp[1])*100;
v1.z = (object[i].p[j].z - lp[2])*100;
v2.x = (object[i].p[p2].x - lp[0])*100;
v2.y = (object[i].p[p2].y - lp[1])*100;
v2.z = (object[i].p[p2].z - lp[2])*100;
//draw the polygon
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(object[i].p[j].x,
object[i].p[j].y,
object[i].p[j].z);
glVertex3f(object[i].p[j].x + v1.x,
object[i].p[j].y + v1.y,
object[i].p[j].z + v1.z);
glVertex3f(object[i].p[p2].x,
object[i].p[p2].y,
object[i].p[p2].z);
glVertex3f(object[i].p[p2].x + v2.x,
object[i].p[p2].y + v2.y,
object[i].p[p2].z + v2.z);
glEnd();
//}
}
}
}