PDA

View Full Version : Shadows on wrong side of Shadow Volumes



Phosphor
03-03-2011, 10:13 PM
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();

James A.
03-03-2011, 10:54 PM
A picture of the problem might help us a bit here ;)

Phosphor
03-03-2011, 11:24 PM
Sure thing and Thanks.

http://img291.imageshack.us/img291/7066/shadowvolumes.png (http://img291.imageshack.us/i/shadowvolumes.png/)

http://img19.imageshack.us/img19/6536/shadowvolumes2.png (http://img19.imageshack.us/i/shadowvolumes2.png/)

GuentherKrass
03-04-2011, 12:38 AM
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.

Phosphor
03-04-2011, 12:48 PM
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.

http://img19.imageshack.us/img19/5016/shadowvolumes3.png (http://img19.imageshack.us/i/shadowvolumes3.png/)

GuentherKrass
03-06-2011, 05:34 AM
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.