next up previous contents
Next: Planar Reflections using Texture Up: 9.3.1 Planar Reflectors Previous: 9.3.1 Planar Reflectors

Planar Reflections and Refractions Using the Stencil Buffer

 

tex2html_wrap10891

 

tex2html_wrap10893

The effects of specular reflection can be approximated by a two-pass technique using the stencil buffer. During the first pass, you will render the reflected image of the scene. During the second pass, you will render the non-reflected view of the scene, using the stencil buffer to prevent the reflected image from being drawn over.

As an example, consider a model of a room with a mirror on one wall. Compute the plane containing the mirror and define an eye point from which you wish to render the scene. During the first pass, place the eye point at the desired location (using a gluLookAt() command or something similar). Next, draw the scene as it looks reflected through the plane containing the mirror. This can be envisioned in two ways, shown in Figures 47 and 48. In the first illustration, you reflect the viewpoint. In the second illustration, you reflect the scene. The ways of considering the problem are equivalent. Both are presented here since reflecting the viewpoint will tie into the next section, but many people seem to find reflecting the scene more intuitive. The sequence of steps for the first pass is as follows:

Objects drawn in the first pass look as they would when seen in the mirror, except that you ignore the fact that the mirror may not fill the entire field of view. That is to say, imagine that the entire plane containing the mirror is reflective, but in reality the mirror does not cover the entire plane. Parts of the scene may be drawn which will not be visible. For example, the lowest box in the scene in Figure 48 is drawn, but its reflection is not visible in the mirror. You will fix this in the second pass.

When rendering from the reflected eye point, points on the plane through which you reflect maintain the same position in eye space as when you render from the original eye point. For example, corners of the reflective polygon are in the same location when viewed from the reflected eye point as from the original viewpoint. This may seem more believable if one imagines that you are reflecting the scene, instead of the eye point.

One implementation problem during the first pass is that you should not draw the mirror or it will obscure your reflected image. This problem may be solved by backface culling, or by having the graphics application recognize the mirror (and objects in the same plane as the mirror).

You may wish to produce a magnified or minified reflection by moving the reflected viewpoint backwards or forwards along its line of sight. If the position is the same distance as the eye point from the mirror then an image of the same scale will result.

Start the second pass by setting the eye point up at the ``real'' location. Next, draw the mirror polygon. Mask out portions of the reflected scene which you drew in the first pass, but which should not be visible. This is accomplished using the stencil buffer. First, clear the stencil and depth buffers. Next, draw the mirror polygon into the stencil buffer and depth buffers, setting the stencil value to 1. You may or may not wish to render the mirror polygon to the color buffers at this point. If you do, the mirror must not be opaque or it will completely obscure our reflected scene. You can give the appearance of a dirty, not purely reflective, mirror by drawing it using one of the transparency techniques discussed in Section 10. After drawing the mirror, configure the stencil test to pass where ever the stencil buffer value is not equal to 1. Then clear the color buffers, which erases all parts of the reflected scene except those in the mirror polygon. After the clear, disable the stencil test and draw the scene. The list of steps for the second pass is:

The frame is now complete.

See Section 3 for more information on modeling.

next up previous contents
Next: Planar Reflections using Texture Up: 9.3.1 Planar Reflectors Previous: 9.3.1 Planar Reflectors