z-fail shadow opengl stuff

I’m not getting any shadowed regions in my app. I rendered the shadow volume to the color buffer to see if it is correct, and it seemed fine. I’m a bit confused of the shadow volume rendering procedure, sice there are all kinds of implementations floating on the net, so I’ll just post what I did and so you could see if something is not in order.

// Draw mesh with ambient color (and depth info)

// Calculate shadow volume

glDepthFunc(GL_LEQUAL);
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glEnable(GL_CULL_FACE);
glDepthMask(0);
glColorMask(0, 0, 0, 0);
glStencilMask(1);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, ~0);
glClear(GL_STENCIL_BUFFER_BIT);

glCullFace(GL_FRONT);
glStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
// Draw shadow volume with caps

glCullFace(GL_BACK);
glStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
// Draw shadow volume with caps

glEnable(GL_TEXTURE_2D);
glColorMask(1, 1, 1, 1);
glStencilMask(0);
glStencilFunc(GL_EQUAL, 0, ~0);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glEnable(GL_POLYGON_OFFSET_FILL);

// Draw lit mesh
glDisable(GL_POLYGON_OFFSET_FILL);
glDisable(GL_STENCIL_TEST);
glDepthMask(1);

Change

glStencilMask(1);

to

glStencilMask((GLuint) ~0);

greatest thanks to you rgpc :cool: it worked perfectly after that. Finally.

Hmm, there’s still something, maybe with the caps. When I look inside the volume, to the direction of extrusion, I can see the “projection of the scene” cause a “hole” in my shadow. AFAIK the shadow volume must form a closed volume so that the volume with caps and all face outwards. I’m not extruding to infinity though, could the be an issue (the light source is distant)? Anyway, if someone could tell me how to extrude to infinity it would be great. Currently I’m just extruding to constant distance in a vertex program.

http://developer.nvidia.com/object/robust_shadow_volumes.html
http://developer.nvidia.com/object/fast_shadow_volumes.html

You don’t have to extrude to infinity but you do have to ensure that your near/far planes are not clipping the shadow volume. If you extrude too far, then the volume will be clipped by your far plane and a “hole” will appear in your shadow.

Extruding to infinity overcomes this problem, so does using the GL_NV_depth_clamp extension - which I believe is only available on NV hardware. Or you move your far plane out, or your far cap in.

http://www.gamasutra.com/features/20021011/lengyel_01.htm

As I rendered the volume into the color buffer, I can clearly see it fitting to the view frustum volume and still sifficiently crossing other objects, still it produces holes. I also double checked that the volume is fully closed with caps.

I managed to get the infinite view frustum/shadow volume and it did solve the problem.