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

 

table1957

 

table1977

The effects of specular reflection can be approximated by a two-pass technique using the stencil buffer. During the first pass, we render the reflected image of the scene. During the second pass, we 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. We compute the plane containing the mirror and define an eyepoint from which we wish to render the scene. During the first pass, we place the eyepoint at the desired location (using a gluLookAt() command or something similar). Next, we draw the scene as it looks reflected through the plane containing the mirror. This can be envisioned in two ways, shown in Figures 25 and 26. In the first illustration, we reflect the viewpoint. In the second illustration, we reflect the scene. The ways of considering the problem are equivalent. We present both 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 we ignore the fact that the mirror may not fill the entire field of view. That is to say, we 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 26 is drawn, but its reflection is not visible in the mirror. We'll fix this in the second pass.

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

One implementation problem during the first pass is that we should not draw the mirror or it will obscure our 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).

We 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.

We start the second pass by setting the eyepoint up at the ``real'' location. Next, we draw the mirror polygon. We wish to mask out portions of the reflected scene which we drew in the first pass, but which should not be visible. This is accomplished using the stencil buffer. First, we clear the stencil and depth buffers. Next, we draw the mirror polygon into the stencil buffer and depth buffers, setting the stencil value to 1. We may or may not wish to render the mirror polygon to the color buffers at this point. If we do, the mirror must not be opaque or it will completely obscure our reflected scene. We 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, we configure the stencil test to pass where ever the stencil buffer value is not equal to 1. We then clear the color buffers, which erases all parts of the reflected scene except those in the mirror polygon. After the clear, we disable the stencil test and draw the scene. The list of steps for the second pass is:

The frame is now complete.


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

David Blythe
Thu Jul 17 21:24:28 PDT 1997