Plane reflection and cube mapping...

Recommended Posts

ebrarian    122
Hi, I've been trying to implement reflection on a large floor. The reflection sort of works, but I have the feeling that it is not working correctly when I strafe across the floor : OK reflection : http://www.starmen.nl/reflection1.png Bad reflection : http://www.starmen.nl/reflection2.png How would I create the cube map for an planar reflection object anyway ? Any ideas ? Regards, EBrarian

Share on other sites
andur    781
A cube map only really works properly for a sphere. The further away from a sphere you get, the worse the cube map is going to approximate a reflection.

The first image sort of works since you are looking more or less at where the cube map was generated from. As you move further away from this point, it will look less and less accurate, as the reflection is only 100% correct for the point in space that it was generated at.

A much better option for planar reflections, is to use a planar stencil buffer mirror. This is a lot more costly to do however than a cube map, since you have to recompute it each frame.

Share on other sites
ebrarian    122
Hi Andur,
Thanks for your reply. I was already afraid cube mapping is not a general solution for reflection.

Is there any other general solution for reflection other than applying a plane reflection separately for each triangle in a mesh ?

Regards,
EBrarian

Share on other sites
andur    781
You don't want to do it for each triangle on each mesh, that would be far, far too slow. If your mesh is perfectly planar, you can calculate the plane equation that describes it, and use it to generate a single planar reflection. You can do this either by:

1. A render to texture approach, where you render out the reflection to a seperate texture, and then blend it into your mesh when you render it for real. (This has the advantage of letting you do all sorts of post-processing effects on the reflection, or render it at a lower resolution than the rest of the screen)

2. Or you can clear that region of the screen where the mesh is, via stencil-masking it, and render the reflection there. For a partial reflection, you can render your mesh partially transparent on top of the perfect stencil reflection. This isn't as flexible as approach #1.

Either of these methods break down for non-planar meshes. You can sometimes get away with it on some slightly non-flat surfaces, but, there's usually very noticeable artifacts/glitches as a result of trying that.

For online references on how to do this, these seem to be the right approach:

http://www.ziggyware.com/readarticle.php?article_id=116 (In XNA, but the concept/math directly applies)
http://www.pieterg.com/Tutorials/tutorial10.php

There was a sample in a really old directx sdk of this, but, its gone now.

You'll want to highly optimize your stencil mirrors as much as possible if you take this approach, as its basically rendering your scene twice. You'll want to cull away as much geometry as possible, set up clipping planes, and cull the mirror itself away if possible. With the render-to-texture approach, you might be able to cheap out and only re-render the reflection every couple of frames, depends on if you'll notice that or not.

Share on other sites
ebrarian    122
Ok,
thanks for the explanation and the references Andur. I guess I'll have to limit my reflection ambitions a little bit.

Regards,
EBrarian