PDA

View Full Version : Shadow Volume problem



cookiehunter
05-28-2012, 07:02 AM
Hi all,
I have a problem calculating my shadows using shadow volumes and the stencil buffer. I have two triangles (triangle a and b). They have the same position except one of them is further away (triangle b), so triangle a is in front of triangle b. Now when the light is in front of them, the shadow of triangle a will cast correctly on triangle b. But when i move my light source behind triangle b, there is no shadow on triangle a. I am drawing my volumes like this:


glFrontFace(GL_CW);
glStencilFunc(GL_ALWAYS, 0x0, 0xff);
glStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
drawShadowVolume();
glFrontFace(GL_CCW);
glStencilFunc(GL_ALWAYS, 0x0, 0xff);
glStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
drawShadowVolume();
I think, thats the common way of doing it. I also think, i know the problem. If the light is behind the triangles, the directions of the shadow volumes are changed, because the shadow volume is pointing in the direction of the camera instead of away from it. So the face culling is not working like it should be. If i swap glFrontFace(GL_CW); and glFrontFace(GL_CCW); like this:


glFrontFace(GL_CCW);
glStencilFunc(GL_ALWAYS, 0x0, 0xff);
glStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
drawShadowVolume();
glFrontFace(GL_CW);
glStencilFunc(GL_ALWAYS, 0x0, 0xff);
glStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
drawShadowVolume();
then the shadow is working when the light is behind the triangles, but if the light is in front of them, it's not working (obviously). Now i don't know, how i could fix this.
Does anyone have a clue what i could change to get it to work properly?
If some screenshots or more code is needed, i will provide it.
Thanks in advance...
cookiehunter

aqnuep
05-28-2012, 03:58 PM
This the classic case when stencil shadow volumes fail if the camera is inside the shadow volume.

There are multiple solutions for your issue, but probably the most simple and most used one are Z-fail shadow volumes, aka Carmack's reverse. You should be able to find a lot of articles about it.

Dark Photon
05-28-2012, 07:03 PM
This the classic case when stencil shadow volumes fail if the camera is inside the shadow volume.

There are multiple solutions for your issue, but probably the most simple and most used one are Z-fail shadow volumes, aka Carmack's reverse. You should be able to find a lot of articles about it.

That's my recollection too. Depth Fail turns your near plane clipping problem into a far plane clipping problem (which you still have to deal with, but can be dealt with more simply). One solution is to use depth clamp, and another is to push the far clip out to infinity (effectively disabling far clipping). With depth fail you have to cap your shadow volumes (whereas with depth pass you don't, IIRC).

cookiehunter
05-29-2012, 03:16 AM
Thank you, i implemented z-Fail and now it is working. I had top and bottom caps anyway, so this was not a huge problem.