Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: Shadow Volume problem

  1. #1
    Junior Member Newbie
    Join Date
    May 2012
    Posts
    2

    [SOLVED] Shadow Volume problem

    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:
    Code :
    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:
    Code :
    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
    Last edited by cookiehunter; 05-29-2012 at 03:18 AM.

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Dec 2007
    Location
    Hungary
    Posts
    985
    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.
    Disclaimer: This is my personal profile. Whatever I write here is my personal opinion and none of my statements or speculations are anyhow related to my employer and as such should not be treated as accurate or valid and in no case should those be considered to represent the opinions of my employer.
    Technical Blog: http://www.rastergrid.com/blog/

  3. #3
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,224
    Quote Originally Posted by aqnuep View Post
    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).

  4. #4
    Junior Member Newbie
    Join Date
    May 2012
    Posts
    2
    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •