Understanding Carmack's reversed

Hi,

I am trying to figure out if I am doing the right thing to generate shadow volumes using carmack’s reverse, Could somebody here help me?

This is what I do, but it does not work correctly
glDepthFunc(GL_LESS);
glStencilOp(GL_KEEP,GL_INCR,GL_KEEP);

  1. Render the Front Facing polygons from the mesh
  2. Render the Shadow Volume Cap (triangulated silhouette polygon at the far end of the shadow)
  3. Render the Front Facing Shadow Volume Quads

glStencilOp(GL_KEEP,GL_DECR,GL_KEEP);

  1. Render the Back Facing Shadow Volume Quads

This just dont work,

Could somebody please explain the right steps in this coz I somehow just cant visualize the Z-Fail case in my mind.

HELP!!!

Thanks,
-Sundar

You should do it like

  1. Draw scene to depthbuffer with glDepthFunc(GL_LEQUAL)
  2. Render front facing triangles of the shadow volume (including the cap at both sides) with glDepthFunc(GL_LESS) & glStencilOp(GL_KEEP,GL_INCR,GL_KEEP);
  3. As 2, but backfacing triangles glStencilOp(GL_KEEP,GL_DECR,GL_KEEP);
    4: Render scene with stencil test.

You may have some help of my demo or Carmacks reversed: http://esprit.campus.luth.se/~humus/?page=OpenGL

Hi,

This is what I do,

glDepthFunc(GL_LEQUAL);
pMesh->RenderGL();
glVertexPointer(3,GL_FLOAT,0,&m_Silhouette.Vertices[0]);

glDepthFunc(GL_LESS);
glDepthMask(0);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS,128,0xff);
glStencilOp(GL_KEEP,GL_INCR,GL_KEEP);
glColorMask(0,0,0,0);
// render the front facing polygons and the cap
if(pMesh) pMesh->RenderGL();
// Now the front facing silhouette walls
glFrontFace(GL_CW); // walls have reversed Culling
glDrawElements(GL_QUADS,m_Silhouette.Walls.GetSize(),GL_UNSIGNED_SHORT,&m_Silhouette.Walls[0]);
glFrontFace(GL_CCW);
glDrawElements(GL_TRIANGLES,m_Silhouette.Caps.GetSize(),GL_UNSIGNED_SHORT,&m_Silhouette.Caps[0]);

// now change the stencil op and render the back facing walls
glStencilOp(GL_KEEP,GL_DECR,GL_KEEP);
glDrawElements(GL_QUADS,m_Silhouette.Walls.GetSize(),GL_UNSIGNED_SHORT,&m_Silhouette.Walls[0]);

Note that my Shadow Polygons winding order is CW for front facing and CCW for back facing.

Is this right or am I missing somthing??

-Sundar

I don’t even get what you’re trying to do with that code
Especially this one is confusing: glStencilFunc(GL_ALWAYS,128,0xff);

Have you read the source of my demo above, I really think it would help, it’s not all that much code. Also check nVidia’s paper on the subject, it’s really helpful too.