next up previous contents
Next: 15.2 Haloed Lines Up: 15 Line Rendering Techniques Previous: 15 Line Rendering Techniques

15.1 Hidden Lines

This technique allows you to draw wireframe objects with the hidden lines removed, or drawn in a style different from the ones that are visible. This technique can clarify complex line drawings of objects, and improve their appearance [27] [4].

The algorithm assumes that the object is composed of polygons. The algorithm first renders the polygons of the objects, then the edges themselves, which make up the line drawing. During the first pass, only the depth buffer is updated. During the second pass, the depth buffer only allows edges that are not obscured by the objects polygons to be rendered.

Here's the algorithm in detail:

  1. Disable writing to the color buffer with glColorMask()
  2. Enable depth testing with glEnableGL_DEPTH_TEST(GL_DEPTH_TEST)
  3. Render the object as polygons
  4. Enable writing to the color buffer
  5. Render the object as edges

In order to improve the appearance of the edges (which may show depth buffer aliasing artifacts), use polygon offset or stencil decaling techniques to draw the polygon edges. The following technique works well, although its not completely general. Use the stencil buffer to mask where all the lines, both hidden and visible, are. Then use the stencil function to prevent the polygon rendering from updating the depth buffer where the stencil values have been set. When the visible lines are rendered, there is no depth value conflict, since the polygons never touched those pixels.

Here's the modified algorithm:

  1. Disable writing to the color buffer with glColorMask()
  2. Disable depth testing; glDisableGL_DEPTH_TEST(GL_DEPTH_TEST)
  3. Enable stenciling; glEnableGL_STENCIL_TEST(GL_STENCIL_TEST)
  4. Clear the stencil buffer
  5. Set the stencil buffer to set the stencil values to 1 where pixels are drawn; glStencilFuncGL_ALWAYS, 1, 1(GL_ALWAYS, 1, 1); glStencilOpGL_KEEP, GL_KEEP, GL_REPLACE(GL_KEEP, GL_KEEP, GL_REPLACE)
  6. Render the object as edges
  7. Use the stencil buffer to mask out pixels where the stencil value is 1; glStencilFuncGL_EQUAL, 1, 1(GL_EQUAL, 1, 1) and glStencilOpGL_KEEP, GL_KEEP, GL_KEEP(GL_KEEP, GL_KEEP, GL_KEEP)
  8. Render the object as polygons
  9. Turn off stenciling glDisableGL_STENCIL_TEST(GL_STENCIL_TEST)
  10. Enable writing to the color buffer
  11. Render the object as edges

The only problem with this algorithm is if the hidden and visible lines aren't all the same color, or interpolate colors between endpoints. In this case, it's possible for a hidden and visible line to overlap, in which case the most recent line will be the one that is drawn.

Instead of removing hidden lines, sometimes it's desirable to render them with a different color or pattern. This can be done with a modification of the algorithm:

  1. Leave the color depth buffer enabled for writing
  2. Set the color and/or pattern you want for the hidden lines
  3. Render the object as edges
  4. Disable writing to the color buffer
  5. Render the object as polygons
  6. Set the color and/or pattern you want for the visible lines
  7. Render the object as edges

In this technique, all the edges are drawn twice; first with the hidden line pattern, then with the visible one. Rendering the object as polygons updates the depth buffer, preventing the second pass of line drawing from effecting the hidden lines.


next up previous contents
Next: 15.2 Haloed Lines Up: 15 Line Rendering Techniques Previous: 15 Line Rendering Techniques

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