Carmack's reversed vs unreversed

well, that was just a typo, so i still dont know what the problem is.
i also draw the shadow for each light, and than the light in unshadowed areas, this way i keep the rendering passes to minimum, but rendering all the lights, and then a quad over the scene to add shadows sound better in some way, because then shadow on shadow is darker, but i should first make this stencil buffer to start working.

This probably isnt your problem but I am just throwing it out there. Whats your stencil mask before you clear the stencil buffer? If the mask is set to 0 then you cannot write to the stencil buffer even with a clear.

Plus you can always read the stencil buffer back and take a look at it to see if its really being cleared.

no its not that, i forgot to ask a pixel format with stencil bits.

well, now the stencil works, but still, i have a strange problem - when i resize the window, parts of the quad get drawn when it should not. and if specify it to render the quad where stencil is 0 and clear the buffer to zero before drawing the quad, it works, again until i resize the window, after the there artifacts, it draws the quad in different places, without any reason.
do you know this error?

I tried that out with my app and it wasnt happening. Not sure what that could be. Is your viewport reflecting the changes to the window size before you clear?

i think so, i clear every frame. well i think it has something to do with the mask parameter, becausei changed it, and it stopped.

[This message has been edited by okapota (edited 02-11-2002).]

i was mistaken, it still happans. its something with the mask parameter. what is it? if i change it to 0, nothing happens, to 1 the artifact is seen. other apps change it to 0xfffff or ~0. what is it?

Just my 2c: when you clear the stencil buffer, you have to know how to clear it, eg set the stencil operation.

You do :
glClearStencil(0);
glClear(GL_STENCIL_BIT);
glStencilFunc(GL_ALWAYS, 1, ~0);
glEnable(GL_STENCIL_TEST);

but what’s the operation ?
Please try this :
glClearStencil(0);
glClear(GL_STENCIL_BIT);
glStencilFunc(GL_ALWAYS, 1, ~0);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
glEnable(GL_STENCIL_TEST);

first, it doesnt work. second why setting the stencilop after clearing?
any way its not that.

Right. my mistake.
I thought you drew somthing between glEnable(GL_STENCIL_TEST) and glDepthMask(1)
And if it’s the case, you should be using glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP)

Anyway, you have to look at two things :
First of all, make sure you set up a stencil buffer.
You can query how many stencil buffer bits lie in video memory with glGetIntegerv on GL_STENCIL_BITS .
Secondly, as noticed, you have to make sure that the stencil mask is set to 1 at least.

Another thing you could try is replacing glStencilFunc(GL_NOTEQUAL, 0, 0xFFFFFFFF) by glStencilFunc(GL_NEVER, 0, 0xFFFFFFFF)
If it still draw something then either there is no stencil buffer (eg GL_STENCIL_BITS return 0) or your driver/card does not upport stencil testing.

The mask parameter for the stencil buffer is different than the mask parameters for the other buffers. If you want to clear the entire stencil buffer for sure call glStencilMask( 0xffffffff ) or glStencilMask( ~0 ) before glClear(). They are the same. If you only do a glStencilMask( 1 ) then only the first bit will be cleared of each stencil value.

greate, now it works. many thanx.
but i see now the in the unrevesed method, you still need closed volumes, unless you extend them very very far away,so you cant see the end. because else, you would see a shadow where the shadow volume should be capped. am i right?

Thats right. I think people were saying that when you make your shadows you keep track of the farthest Z wrt to the camera. Then when you draw your shadows you change the projection matrix to make sure the far clip plane is just beyond this z value. You only do this when drawing the shadow volumes tho, that way you dont lose z precision for your other objects.

Kaycee : if you’re talking about drawing the objects in first pass with a good z precision, and then draw the shadows in second with a bad z precision (with far clipping plane becoming very far) I’m afraid it’s not possible. In fact, you can not make matching Z values for two different sets of near/far clipping planes because Z is not mapped linearily in the depth buffer.

That makes sense. I tried it out and the shadows were messed up. Then do I keep track of the Z and draw the whole scene like that? I guess most objects would be culled beyond the original far plane so I wouldnt have to worry about precision for them.

You have two cases : either you draw everything (eg objects + shadow volumes) with a “standard” far plane, either you draw everything with a “far” far plane.
In the first case, you’ll experience problems with shadow volumes capping, which is both hard and slow to implement.
In the second case, it’s easy and fast but you loose Z precision.
You can not combine both methods to take advantage of each because of non-linear Z mapping ; at least I couldn’t and cass seem to be confirming that. http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/005442.html

The only other solution is to use the “unreversed” algorithm, which work pretty well with any far plane, and is faster, BUT you have to make sure that your shadowing objects won’t project their shadow to the near plane.

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

By adding the MAC algorithm explained on http://www.tooltech-software.com you can preserve the z depth and still use the reversed method. You need to draw the shadow volume an extra pass for those volumes that extend beyond the far z plane but this is minor work in most cases…