Next: 16 Tuning Your OpenGL
Up: 15 Line Rendering Techniques
Previous: 15.2 Haloed Lines
Sometimes it can be useful for highlighting purposes to draw a silhouette
edge around a complex object. A silhouette edge defines the outer boundaries
of the object with respect to the viewer.
The stencil buffer can be used to render a silhouette edge around an object.
With this technique, you can render the object, then draw a silhouette
around it, or just draw the silhouette itself [40].
The object is drawn 4 times; each time displaced by one pixel in the x or y
direction. This offset must be done in window coordinates. An easy way to
do this is to change the viewport coordinates each time, changing the
viewport transform. The color and depth values are turned off, so only the
stencil buffer is affected.
Every time the object covers a pixel, it increments the pixel's stencil
value. When the four passes have been completed, the perimeter pixels of
the object will have stencil values of 2 or 3. The interior will have
values of 4, and all pixels surrounding the object exterior will have
values of 0 or 1.
Here is the algorithm in detail:
- If you want to see the object itself, render it in the usual way.
- Clear the stencil buffer to zero.
- Disable writing to the color and depth buffers
- Set the stencil function to always pass, set the stencil operation to increment
- Translate the object by +1 pixel in y, using glViewport()
- Render the object
- Translate the object by -2 pixels in y, using glViewport()
- Render the object
- Translate by +1 pixel x and +1 pixel in y
- Render
- Translate by -2 pixel in x
- Render
- Translate by +1 pixel in x. You should be back to the original position.
- Turn on the color and depth buffer
- Set the stencil function to pass if the stencil value is 2 or 3. Since the possible values range from 0 to 4, the stencil function can pass if stencil bit 1 is set (counting from 0).
- Rendering any primitive that covers the object will draw only the pixels of the silhouette. For a solid color silhouette, render a polygon of the color desired over the object.
Next: 16 Tuning Your OpenGL
Up: 15 Line Rendering Techniques
Previous: 15.2 Haloed Lines
David Blythe
Thu Jul 17 21:24:28 PDT 1997