View Full Version : Combining stencil shadow and stencil mirror

09-02-2003, 10:05 AM
Hello! How can I combine stencil shadow and stencil mirror? How should I setup the stencil test, clear value, etc.?

And another question. I've read in another thread that using infinite shadow volume eats up more fill rate that finite shadow volume. Is this true only in the case when I draw one big shadow quad on the screen (with scissor testing), or also in the case when I rerender my scene using diffuse/specular lighting?

Thanks in advance for any help.

09-02-2003, 11:51 AM
The fill issue refers to the rendering of the volumes, not the shading of the light or shadow fragments. It shouldn't be more fill or less fill with infinite volumes, however the zfail vs zpass can make a difference due to the need to draw end caps, both in geometry and fill. So, if you can use the zpass test with the eye outside the volume, however if the eye is inside use zfail testing, but then you need to cap. Ofcourse the applicability of mixing these approaches depends on your circumstances and whether you can test the eye + clip for containment and whether it's worthwhile w.r.t. your light or shadow beam tree(s).

The poster was probably associating infinite volumes to zfail stencil incs and decs when drawing the comparrison.

You can implement mirrors using simple masks of the stencil bits, but remember that illuminations and occlusion in the reflected scene may be complex to get correct. Another approach is render to texture (or equivalent) and texturing the mirror surface with the reflection image as a transparency effect after completing the scene, this is very good for semi transparent reflections.

09-02-2003, 12:28 PM
Thanks for the help. I read a topic in this forum discussing nvidia's infinit shadow volume algorithm and it was a bit confusing for me. I thought of what you said but after reading that topic I got confused. So only the shadow volume rendering consumes that much fill rate.

And thanks for the shadow+mirror help. So all I have to do is set exclusive masks in glStencilFunc for rendering the mirror and the shadows.

09-03-2003, 01:10 AM
Now that I though more about this problem I realized that it is more complicated than what I thought. http://www.opengl.org/discussion_boards/ubb/smile.gif The problem is that I need to use two kind of stencil tests: one to make the mirror's stencil mask and one to make the shadow masks. I need to combine these two stencil tests into one but how? Another problem is that I can't use glClear to clear the stencil buffer before shadow rendering because this would clear the mirror's mask too. So I need to clear the shadow mask somehow else. Is there anyone who has already combined stencil shadows and mirrors in one application?

09-03-2003, 04:17 AM
Bummer, I thought this was possible but you made me look more closely and there is missing mask functionality on stencil ops. I seem to recall IRIS GL supporting this, but I could be wrong. It is also more complex I see depending on your app structure.

OK the key here is to structure things correctly. There is a way that lets you use full stencil volumes while rendering and allows full mirror occlusion.

Once you've drawn the final scene without mirrors, clear stencil and draw a z tested stencil write on z pass mirror polygon to the mirror surface. Next draw a depth clear polygon to write some far z value stencil tested against theis plane (might as well make this your clear color too, if you have a sky backdrop this should include it), no z test stencil test only z writes on. Then draw your mirror scene as normal, with the usual mirror plane clipping, it should just work and you are free to use stencil as you like, you are relying on the portal cut in the depth buffer, and of course the clip plane. This basically clears the depthbuffer over the visible mirror surface handling complex depth occlusion at the same time.

WARNING, if you have a morror on a convex surface or equivalent situation you risk z passes behind the clipping plane but outside that surface original scene surface, so you may want to draw the extended mirror plane to the depth buffer (basically writing the mirror clip plane to the depth buffer excluding the portal) if you think you need it.

[This message has been edited by dorbie (edited 09-03-2003).]

09-03-2003, 06:50 AM
I use mirrors and shadows mixed in Gizmo3d by using a recursive approach where I increse the relative z value each time. i draw the mirrors first and recursively the contents in the mirrors. after trhat i render the shadows.

09-03-2003, 12:33 PM
This depth buffer approach seems to be a good idea, I'll try it. Thanks.

09-03-2003, 06:37 PM
Tooltech, I've advocated this for mirrors for a long time saying they've never really required stencil buffers, although it can be tricky and requires depth clear for 'convex' surfaces. People tend to not 'get it' when you try to explain it, but I guess you're more lucid about it than I've ever been.

One additional suggestion..... If you have stencil shadows etc in the scene and obviously lots of depth complexity you may want to implement only zpass stencil ops since you should never volume clip beyond the mirror eliminating the caps and if you render the plane of the mirror to the depth buffer prior to the pass you may eliminate a lot of extraneous pixel fill. It depends on your circumstances.

Use the clip sparingly too if you can, it can be slow and isn't always needed, same with the depth clear.

[This message has been edited by dorbie (edited 09-03-2003).]

09-03-2003, 09:43 PM
You are right. Exactly what I do. The only problem that remains is the loss of clip plane when using vertex shaders in the non position invariant version.