directional lights, shadow volumes

i am having some issues with directional lights. there is a url below. the first two pictures show a cube that has been very slightly rotated in the second picture from its position in the first. but notice how drastically the lighting of the material changes. this material even flickers when the cube moves. i know it is not the face vertex orientation, or the normals. the shininess of the material is at 10.0f as well. the problem only appears with directional lights in opengl, ( the w coord is 0 ). i know the faces are rather large but still the vertices are not properly lit it seems. are there any tips for using directional lights that i may be missing?

http://contrarymotion.com/tests/examples.htm

my second problem has to do with shadow volumes and directional lights. see picture 3, the side of the cube. the shadow volume is fighting with the surface of the cube. this is an implementation of the zfail technique, my main reference is the mark kilgard paper on the nvidia website about creating robust shadow volumes. i cannot see a problem with my code. is this a common issue for the volume and face of the model to be fighting for pixels like this? i can certainly show some code if need be, but i first wanted to see if anyone has experienced this before with this technique.

i have no issues when using positional lights for both of the problems i have stated here.

The first problem looks like specular. That can potentially vary rapidly with small movements. You appear to have a lot of ambient and some specular contribution. The light also appears to be towards the far side of the cube. Try adding diffuse, reducing ambient and perhaps moving the light in front of the object to show the results.

The third image if using zfail stencil from Kilgard’s paper a.k.a. Carmack’s reverse is caused by a lack of zbuffer precision. The stencil volume is possibly zfighting with the cube face. Given the scenario I suspect that the near clip plane of your frustum is much to near the eye. Try pushing the near clip plane out a bit and pulling the far plane if too especially if it is very distant. There may be more wrong than this, but the sawtooth effect seems to at least be zbuffer related if this is stencil shadows.

Finally, you should not be modulating ambient terms by a shadow test, it just looks bad. Add specular in a separate pass, possibly your zfill pass, this will let yu independently modulate the direct illumination contributions.

P.S. looking again it seems that the stencil volume is parallel to the cube face, i.e. coplanar with the face because the light vector lies along that plane. The zbuffer therefore has a tough time resolving the intersection. This is made visible because you are modulating ambient terms and those don’t tend towards zero at the silhouette, if you didn’t modulate ambient the face at the side would not do this. Don’t place the light vector and the cube exactly along the face and you’ll be OK too. A lack of zbuffer invarience may be a contributing factor here but not the real solution.

here is the stencil shadow code basically.

  1. clear color and depth buffers
  2. disable all lights except the global ambient
  3. draw the scene with depth function GL_LESS
  4. disable color and depth buffer writes and zero the global ambient, clear the stencil buffer and set the stencil test to always pass
  5. render backfacing shadow volume polys and increment for polys that fail the depth test
  6. render frontfacing shadow volume polys and decrement for polys that fail the depth test
  7. enable lighting and set the stencil test to render pixels with a 0 value only
  8. draw the scene with depth function set to GL_EQUAL so only pixels that were previously drawn the first time the scene was rendered get rendered again ( i am assuming the lack of precision is in here?? ) i have tried GL_LEQUAL as well and get the same results.

the far clip plane is set at infinity using a special projection matrix and i have read that this can cause lack of depth precision. i have tried seemingly all kinds of combinations of ambient, diffuse, specular and i still get the same results in each problem in the original post. the light vector is parallel to the planes on the side of the cube, certainly. i know i can avoid it by moving the light. is this normal behavior for opengl when lighting a vertex or face that is parallel to the directional light vector ( problem 1 in original post )??

thanks for the response.

Read the end of my post w.r.t. ambient. That will fix your problem.

that makes perfect sense and does work. if i get rid of the ambient contributions on the material and any global ambient the fighting is not visible. it helps the first problem i had as well althuogh i did have the light vector at an odd position. but because of that i ended up noticing the inconsistencies and had to know why it was happening. thanks a lot.