next up previous contents
Next: 16 Tuning Your OpenGL Up: 15 Line Rendering Techniques Previous: 15.2 Haloed Lines

15.3 Silhouette Edges

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:

  1. If you want to see the object itself, render it in the usual way.
  2. Clear the stencil buffer to zero.
  3. Disable writing to the color and depth buffers
  4. Set the stencil function to always pass, set the stencil operation to increment
  5. Translate the object by +1 pixel in y, using glViewport()
  6. Render the object
  7. Translate the object by -2 pixels in y, using glViewport()
  8. Render the object
  9. Translate by +1 pixel x and +1 pixel in y
  10. Render
  11. Translate by -2 pixel in x
  12. Render
  13. Translate by +1 pixel in x. You should be back to the original position.
  14. Turn on the color and depth buffer
  15. 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).
  16. 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 up previous contents
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