We assume that triangle mesh is rendered and then, we can draw the corresponding wireframe objects with their hidden lines removed as follows.
1. Disable writing to the color buffer with glColorMask.
2. Set the depth function to GL_LEQUAL.
3. Enable depth testing with glEnable(GL_DEPTH_TEST).
4. Render the object as triangles.
5. Enable writing to the color buffer.
6. Render the object as edges using such as GL_LINES, GL_LINE_LOOP, GL_LINE_STRIP, or method using glBegin(GL_POLYGON) and glPolygonMode(GL_FRONT_AND_BACK, GL_LINE).
The above algorithm works for the almost cases. However, depth rasterization artifacts from quantization errors may happen since the pixels at the edges of triangles rendered as polygon and the pixels from the edges rendered as line have depth values that are numerically close. To handel this problem, we may use the glPolygonOffset command which move the lines and polygons relative to each other.
- glEnable(GL_POLYGON_OFFSET_LINE): to offset the lines in front of the polygons
- glEnable(GL_POLYGON_OFFSET_FILL): to move polygon surfaces behind the lines
Example code fragments using glEnable(GL_POLYGON_OFFSET_FILL) are as follows.