I have a problem:
I’ve implemented the z-pass Algorithm for stencil shadow volumes and everything works fine. But if I change it to z-fail nothing is correct anymore.
z-pass:
z-fail:
My code for drawing the shadows:
// store current OpenGL state
glPushAttrib(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_POLYGON_BIT | GL_STENCIL_BUFFER_BIT | GL_ENABLE_BIT);
glClearStencil(0);
// draw the scene to the depth buffer and
// to the color buffer because the shadows should not be totally black
glPushAttrib(GL_LIGHTING_BIT);
glLightfv(GL_LIGHT1, GL_DIFFUSE, black); // only ambient and emission light
glLightfv(GL_LIGHT1, GL_SPECULAR, black);
drawObjects(true); // draws the scene
glPopAttrib();
// ---------------------------------------
glDepthMask(0);
glColorMask(0,0,0,0);
glDisable(GL_CULL_FACE);
glEnable(GL_STENCIL_TEST);
glEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);
glActiveStencilFaceEXT(GL_BACK);
bool zpass = false; // works fine as long as zpass = false...
if (zpass) {
glStencilOp(GL_KEEP, // stencil test fail
GL_KEEP, // depth test fail
GL_DECR_WRAP_EXT); // depth test pass
} else { // zfail
glStencilOp(GL_KEEP, GL_INCR_WRAP_EXT, GL_KEEP);
}
glStencilMask(~0);
glStencilFunc(GL_ALWAYS, 0, ~0);
glActiveStencilFaceEXT(GL_FRONT);
if (zpass) {
glStencilOp(GL_KEEP, // stencil test fail
GL_KEEP, // depth test fail
GL_INCR_WRAP_EXT); // depth test pass
} else { // zfail
glStencilOp(GL_KEEP, GL_DECR_WRAP_EXT, GL_KEEP);
}
glStencilMask(~0);
glStencilFunc(GL_ALWAYS, 0, ~0);
drawHybridShadowVolume(); // draws the shadow volumes
// restore OpenGL state
glPopAttrib();
glPushAttrib(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_ENABLE_BIT);
// re-draw the model with the light enabled only where
// there's no shadow
glDepthFunc(GL_EQUAL);
glStencilFunc(GL_EQUAL, 0, ~0);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glEnable(GL_STENCIL_TEST);
glEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);
drawObjects(true);
// restore OpenGL state
glPopAttrib();
What am I doing wrong ?
With zfail enabled I also see sometimes the scene projected into the shadows. Anyone experienced that before?
Originally posted by Poons: Do you calculate the shadow volume caps?
I have to admit, I didn’t know that I have to!
But now I have added a
glEnable(GL_DEPTH_CLAMP_NV);
before drawing the shadow volumes and everything seems fine in spite of the far clipping plane of the light being visible in the shadow by “inverting” the shadow.
Here is an example:
Racoon: I’m going to test your code soon!
Edit: Racoon: With your version, there’s no shadow at all…