View Full Version : reflections

04-25-2003, 01:44 AM
My problem is a bit difficult to explain, but i will try it.

If have a room. Inside this room is a mirror. To achieve the reflection i try this:

I first render the room (depth test on). Then i render the mirror (depth test on) into the stencil buffer. This way the stencil buffer is set to 1 only where the mirror is visible.
Now i render the mirrored scene where the stencil buffer is set to 1.

But now i run into a problem. The mirrored scene has to be drawn with the depth test on. However now the mirror itself is sometimes closer than the mirrored scene, so the scene isnīt rendered everywhere where it should be.

The only way i could think of to solve this, would be to first render the mirrored scene, then the mirror itself (depthfunc set to gl_always), and than render the rest.
However if the mirror is quite big and completely covered by a wall thatīs doubling not only vertex computations but also the needed fillrate.

Any ideas?


04-25-2003, 01:57 AM
Before rendering the mirrored scene, render the mirrored plane a second time to reset the depth values. You need the depth function always and must set the depth range to (1.0, 1.0). Additionally, you must setup up the stencil test in the same way as when rendering the mirrored scene, such that only depth values of visible parts of the mirror are resetted.


04-25-2003, 02:42 AM
Sounds very good, iīll try that.



04-25-2003, 03:26 AM
Perhaps I'm missing something but why not try rendering the mirror first (to the stencil buffer, with depth writes turned off). Then you can render either the mirrored scene or the normal scene with stencil tests (ie. as long as your using stencil testing you might as well use it to save on fill)

04-25-2003, 05:28 AM
here is a solution that work in my "rendering engine":
- set depth and color masks to false
- render the mirror in the stencil
- set depth and color masks to true
- render the scene in the mirror
- render the mirror (blending)
- render the scene

when rendering the scene in the mirror, reset lights positions and change toggle culling side.

04-25-2003, 05:41 AM
@yoyo: Thatīs exactly what i wanted to avoid, cause the mirrored scene will always be completely drawn, even if the whole mirror is covered by a wall or something.

@rgpc: if i first draw the mirror in the stencil buffer and than draw the real scene where stencil == 0, than nothing could cover parts of the mirror.

Anyway, floīs method works absolutely fine and seems to save a lot of fillrate if the mirror is covered by something.

Of course i am working on optimizing my engine, so that the mirror is never drawn, if it cannot be seen (i am working on a PVS). But still dynamic objects can be in front of the mirror (ok, my engine isnīt able to handle dynamic objects, yet, but i am planning for the future ;-)

Anyway, thanks for your suggestions.

04-25-2003, 06:53 AM
I am also using method described 2 posts up.
BTW, there is no reason to optimize code in one place, because youl get fps like 160, 10, 10, 10, 160, 200, use portals then or smthn.