Results 1 to 6 of 6

Thread: Shadows on wrong side of Shadow Volumes

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2011
    Posts
    5

    Shadows on wrong side of Shadow Volumes

    I'm very new to OpenGL.

    I have an object created from triangles. I draw shadow volumes from this object to infinity but, when compiled, they show up on side A or side B of the shadow volumes and I need them to show up on side C if that makes any sense.

    The shapes of the volumes are correct in that they are tangent to the object and on the side opposite the light source.

    Lastly, I don't know if the problem is in the order I'm sending the vertexes to glVertex3f and glVertex4f or in the way I'm drawing the scene.

    //------DRAW SCENE with lights off
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
    glEnable(GL_LIGHTING);
    glDisable(GL_LIGHT0);
    draw_scene();

    //------DRAW SHADOW VOLUMES with backface culling
    glEnable(GL_STENCIL_TEST);
    glStencilFunc(GL_ALWAYS, GL_FALSE, GL_FALSE); glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);
    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
    glDepthMask(GL_FALSE);
    glDepthFunc(GL_LEQUAL);
    glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
    draw_shadow_volumes();

    //------DRAW SHADOW VOLUMES with frontface culling
    glCullFace(GL_FRONT);
    glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
    draw_shadow_volumes();

    //------DRAW SCENE with lights on
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    glDepthMask(GL_TRUE);
    glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
    glStencilFunc(GL_EQUAL, 0, -1);
    glEnable(GL_LIGHT0);
    draw_scene();

  2. #2
    Senior Member Regular Contributor
    Join Date
    Oct 2009
    Posts
    132

    Re: Shadows on wrong side of Shadow Volumes

    A picture of the problem might help us a bit here

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2011
    Posts
    5

    Shadows on wrong side of Shadow Volumes

    Sure thing and Thanks.





  4. #4
    Junior Member Newbie
    Join Date
    Oct 2006
    Posts
    21

    Re: Shadows on wrong side of Shadow Volumes

    Seems you're trying to implement Z-Pass Stencil Shadows with shadowed areas indicated by a stencil value > 0.

    So you want to configure stencil to achieve the following:
    • (Front-Face-Pass) render shadow volumes such that for every polygon that survives z-test with the solid geometry (Z_PASS) and faces *towards* the camera, increase the stencil value
    • (Back-Face-Pass) render shadow volumes such that for every polygon that survives z-test with the solid geometry (Z_PASS) and faces *away* from the camera, decrease the stencil value

    Looking at your code: I guess that glStencilFunc(GL_ALWAYS, GL_FALSE, GL_FALSE)
    does not make any sense when writing to the stencil buffer since you're setting the mask (last parameter) to zero. In your case you want to write to all bits, so the mask should all be "111...1" or "~0".

    Again, when performing the lighting pass you want to restrict the drawing to areas with stencil value == 0 (no shadow) which is done by setting glStencilFunc( GL_EQUAL, 0, ~0 );
    meaning pass if reference (zero in this case) is equal to the value in the stencil buffer. In other words this results in passing of stencil test if stencil value == 0.

    Also, you may want to use GL_DECR_WRAP/GL_INCR_WRAP when specifying the stencil operation and check for an appropriate switching to Z-Fail shadows when the camera/front clipping plane is close to the shadow volumes.

  5. #5
    Junior Member Newbie
    Join Date
    Mar 2011
    Posts
    5

    Re: Shadows on wrong side of Shadow Volumes

    That is much better. Thanks for your help and explanation.

    Now it looks like I'm still not decrementing the stencil on pixels inside of the model on the second pass. Do I need to change glStencilFunc() for the back face pass? I still need to decrement all bits on zero. I tried altering the stencil function but I think I'm still getting the incremented bits from the whole shadow volume instead of just the incremented bits from the model.



  6. #6
    Junior Member Newbie
    Join Date
    Oct 2006
    Posts
    21

    Re: Shadows on wrong side of Shadow Volumes

    Do I need to change glStencilFunc() for the back face pass?
    No, you want to always draw the backfaces no matter what stencil value is in the stencil buffer.

    Two things to check:
    • Are your extruded shadow volumes consistently wound? Culling solely depends on the order the vertices are provided and by default counter-clockwise order is associated with front facing polygons.
    • Hard to tell, but from looking at the last image, the shadowing has no more "streaks" as your first screenshots had. Could it be, that just the floor is not correctly shadowed? Remember, that without any modifications the vanilla shadow volume algorithm needs solid geometry to work as expected. Try to place the motorcycle object on a cube rather than on a single plane.

    Do yourself a favour and add some debugging code to actually visualize the shadow volumes, preferably in wireframe mode. This makes error checking a lot easier.

Similar Threads

  1. Replies: 1
    Last Post: 10-05-2014, 10:43 PM
  2. shadow volumes
    By helda in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 11-20-2002, 06:51 AM
  3. Shadow volumes
    By vofka in forum OpenGL: Advanced Coding
    Replies: 9
    Last Post: 04-16-2002, 08:25 AM
  4. Shadows Volumes & Convex areas
    By UncleBuck in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 04-03-2001, 09:07 AM
  5. shadow maps vs shadow volumes
    By eigenafx in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 03-02-2001, 01:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean