I’ve made a program wich uses reflexion with the stencil buffer, and it works very well. But I’ve a question to make it work better.
First I explain what I do : i draw the scene to be reflected, then i draw the mirror with stencil writing, i scale(1,-1,1), blend, invert culling, and then redraw the scene with a stencil test. Since I want to draw many objects in the scene, I have to use the z-buffer again. So i clear it after drawing the mirror, and it works perfectly, because where the reflected scene is hide, stencil is 0. But i was wondering if there was another way than clearing depth-buffer. I mean, can i draw the mirror with the depth test but without writing into the z-buffer ? And will it go faster with this method rather than with mine ?

There is a way to do this without clearing the z-buffer. First you draw the reflection(s), then the floor (with blending), and then you draw the object(s) to be reflected… No problems with cleaning the z-buffer.

And to answer another of your questions. You can disable z-buffer with glDepthMask(GL_FALSE | GL_TRUE). GL_FALSE disable z-buffer updates (still doing z-buffer tests), and GL_TRUE enables i again.


But in your technique, I can’t know where to draw the reflection. I mean, the reflection needs to be drawn only in the mirror and not elsewhere. If I draw the reflection first, and if for example a part of the reflection is outside the mirror, it will be drawn entirely whereas it didn’t need to. And if i draw the mirror first to know where not to draw the reflection, the mirror will hide the reflection with th depth test.
Do you understand what I want to say (my english is very poor) ?

Yes, I know what you mean, and I forgot about it.

1: disable updates to z- and frame-buffer
2: draw mirror to stencil buffer
3: enable updates to z- and frame-buffer
4: draw reflections
5: draw mirror
6: draw objects

Hope this is correct…

You disable updates to buffers with commands similar to the one i mentioned above.


[This message has been edited by Bob (edited 02-24-2000).]

OK, now I understand. But your technique is not as god as mine because you’ve to draw twice the mirror while I draw it only once

Not as good as mine? Heh, depends on how you look at it, I don’t have to clear my depthbuffer

You have to remember one thing, you are clearing your depthbuffer in the middle of the scene, and that may cause problems later. What if you want to draw something behind the object to be reflected AFTER you drawn the reflections? This is where you get problems, you just cleared your depthbuffer and there is NO WAY OpenGL knows there is something “in front” of what you are drawing.

If you use my technique, you can atleast save th depthbuffer.

Remember: reflections make a scene more realistic, and if you want to make it more realistic, you have to offer something else, speed in this case.

I’m not saying your technique is bad, if this is all you need, then there is no problems with what you are doing. But if you want to draw some more objects, it might be nice with an untouched dephbuffer.


[This message has been edited by Bob (edited 02-25-2000).]

Yeah, but if I use glDepthMask() instead of clearing z-buffer, I only draw the mirror once, and if it’s hide, it will go faster because stencil will be blank, whereas you draw all the reflection even if there is something hiding the mirror.


Duh… just start recursing the database from furthest to nearest “node”, drawing portals as you go. When you are recursing to closer node, just draw the portal ( write only to depth+stencil buffer ).

This way the depth buffer is updated with the portal polygon’s “shape”, and depth as it it located in the node we are about to draw next.

The stencil refvalue should start from 0x0, and increase every time you recurse ( ++refvalue ). This way you do not have to CLEAR the stencil, just draw the portal polygon with depth+stencil.


  • don’t have to clear at each recursion, which is good for the fillrate


  • cannot recurse more than 256 times with 8bit stencil ( heck, this should be plenty :wink:


  • If you use stencils for fx, then the clearing should be done, ofcourse.

Jukka / T3D

Well, we can keep on tossing comments back and forth for ages…
There are several ways of doing reflections, and all techniques has it’s own pros and cons. Just test what technique that works best for you, then use it.

By the way, jukka3d… Don’t you have to sort all objects/polygons if you want to draw them from back to front? And as far as I know, sorting is not too fast when sorting loads of stuff