Carmack's reversed Problem

I have a problem with “Carmack’s reverse”
Stencil Shadow.Is It correct way???

code:

//Set Stencil buffer //
glDepthMask(GL_FALSE);
glEnable(GL_STENCIL_TEST);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glStencilFunc(GL_ALWAYS,0,0xFFFFFFFFL);

//**********************//
glStencilOp(GL_KEEP,GL_INCR,GL_KEEP);
glCullFace(GL_FRONT);
drawShadowVolume();

//**********************//
glStencilOp(GL_KEEP,GL_DECR,GL_KEEP);
glCullFace(GL_BACK);
drawShadowVolume();

//Draw panel**//

glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
glColor4f(0.0f,0.0f,0.0f,0.5f);
glEnable( GL_BLEND );
glDisable(GL_CULL_FACE);
glBlendFunc( GL_SRC_ALPHA,
GL_ONE_MINUS_SRC_ALPHA );
glStencilFunc( GL_NOTEQUAL, 0, 0xFFFFFFFFL);
glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );

Thanks in Advance;

I believe that in reverse method you should render shadow volumes using GL_GREATER depth function. The idea is to render shadow volume only on parts where it is “behind” other scene geometry.
Code would look something like this:

glColorMask(0,0,0,0);
glDepthMask(0);
glStencilFunc(GL_ALWAYS, 0,0xffffffff);
glDepthFunc(GL_GREATER); //render behind scenery
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
glCullFace(GL_FRONT); //Render back faces
RenderVolume();

glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
glCullFace(GL_BACK); // render front faces
RenderVolume();

jeppa: that’s the right algorithm.
What’s wrong with this implementation ?

spartacus: your algorithm works fine, but that’s exactly what jeppa’s algorithm does there’s just a slight different philosophy, but results are simply the same.

I’ have tried this method,it does not work.

Thanks

Sandro

I have seen,some artifact in my demo.the shadow volumes does not work correctly with
Carmack’s reversed.

this is my projective matrix,is it correct?
gluPerspective(45.0f,width/height,0.1,100.0);

Thanks

Sandro

I don’t think the projection matrix have to do with shadow problems.
But heh, anyway this is correct.

If reversed do not work, try the “standard” unreversed algorithm.
It might work.

The standard algorithm work correctly,but
I have a problem when the shadow volumes
intersects the near clip plane.

Thanks

Sandro

jeppa,

Try this thread,
http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/003593.html

hey, that’s a very interesting thread. thanx.

but most of the links are broken, and I still can’t get understanding how to go through Carmack’s far plane problem.

…but most of the links are broken

Yes, all the NVIDIA links are broken. All the papers are still there though.

…and I still can’t get understanding how to go through Carmack’s far plane problem.

You’ll have to compute a MaxZ value that will completely enclose all your shadow volumes and use that when you specify your frustum far plane.

thanks for your reply,but I have use the Carmack’s Reversed with md2shader (demo from
Nvida):it does not work,artifact on screen

Thanks.

If you just change the stencil ops then it wont work. md2shader uses open ended shadow volumes - Carmack’s reverse requires closed volumes.

I change order of the vertexs of polygons???
es:0,1,2,3 ->3,2,1,0??

> You’ll have to compute a MaxZ value that will completely
> enclose all your shadow volumes and use that when
> you specify your frustum far plane.

That’s what I understood when I read the “other” thread, but I ended up in applying a 10000.0 far plane and I still get ugly effects. I computed vertex coordinates from OpenGL modelview matrix and the farthest vertex is at Z=1000.0f (approximatively).

Edit: Aw, I think I need to display the cap… If so, that’s a bad thing since it needs to draw more polygons…

[This message has been edited by vincoof (edited 02-04-2002).]

Originally posted by jeppa:
[b]I change order of the vertexs of polygons???
es:0,1,2,3 ->3,2,1,0??

[/b]

Well, if you declare shadow volume verts in wrong order, it will generate wrong results. So you could try to change shadow volume vert order. By the way what kind of artifacts you get on the screen? (screenshot maybe?)

Vincoof: There is a great difference between rendering with depth func GL_GREATER and GL_LESS. With GL_GREATER you can eliminate problems when shadow volume intersects with near clipping plane. Or am I completely wrong? These things can get quite confusing

Yes, quite confusing somehow ^^

well, let’s take a look at jeppa’s code and your (spartacus) code :

Jeppa wrote :
glStencilOp(GL_KEEP,GL_INCR,GL_KEEP);

Assuming he didn’t change the depth function, so it is something like GL_LEQUAL

Spartacus wrote :
glDepthFunc(GL_GREATER); //render behind scenery
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);

The stencil operations become :

Jeppa’s stencil :

  • if stencil fail, KEEP stencil value
  • if stencil ok, and zfail (eg z is NOT LEQUAL), then INCRement stencil value,
  • if stencil ok, and zpass (eg z is LEQUAL), then KEEP stencil value.

Spartacus’ stencil :

  • if stencil fail, KEEP stencil value
  • if stencil ok, and zfail (eg z is NOT GREATER), then KEEP stencil value,
  • if stencil ok, and zpass (eg z is GREATER), then INCRement stencil value.

Huh, please explain me where’s the diff

yes my scene it is render with :
glDepthFunc(GL_LEQUAL);

okay, I give up
I can force my application not to project shadows on the near plane, so I will do with it and stick to the “unreversed” style.

Thanx for the nice discussion anyway. It is really interesting.

Well this is how I do it:

  1. Render scene lighting enabled
  2. Render back faces of shadow volume using depth func GL_GREATER and increment stencil. Basically this simply renders only parts of volume that are behind rendered scene geometry.
  3. Render front faces using GL_GREATER and decrement stencil.
  4. Render scene unlit whre stencil other than 0.

This way shadow volume can intersect with near clipping plane, and still create correct shadow. Offcourse shadow volume caps should also be rendered to create the right effect. This makes life lot easier because you don’t have to project shadow volume cap to near plane.

Vincoof: Check out this thread http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/003712.html
There seems to be quite good explanations about the technique.

[This message has been edited by Spartacus (edited 02-04-2002).]

[This message has been edited by Spartacus (edited 02-04-2002).]