lines lying on polygons

When I draw a line , on top of a polygon, and if the scene is rotated the line becomes a discontinuous line (dotted line) and at some angle even it becomes invisible . How can this be avoided ?

Thanks

Couple ways come to mind, if you know in advance which polygons are going to have lines drawn on them, you can use glPolygonOffset to add a depth bias to the polygons. Note: glPolygonOffset will not work with lines, hence the name. Or if you have a stencil buffer available, you could draw the lines into the stencil buffer and color buffer and then draw the polygons just into the color buffer and depth buffer using the appropriate stencil function. Though there are some limitations you’d have to overcome with the stencil buffer method (you wouldn’t want a line on a polygon behind another polygon from stenciling the nearer polygon).

Thank you very much, and it worked out. Only thing is the application became little bit slow after setting polygonoffset. Is there any way to overcome that ?

Thanks.

Originally posted by DFrey:
Couple ways come to mind, if you know in advance which polygons are going to have lines drawn on them, you can use glPolygonOffset to add a depth bias to the polygons. Note: glPolygonOffset will not work with lines, hence the name. Or if you have a stencil buffer available, you could draw the lines into the stencil buffer and color buffer and then draw the polygons just into the color buffer and depth buffer using the appropriate stencil function. Though there are some limitations you’d have to overcome with the stencil buffer method (you wouldn’t want a line on a polygon behind another polygon from stenciling the nearer polygon).

from " Advanced Graphics Programming Techniques Using OpenGL : Hidden Lines " :

Keep in mind, however, that glPolygonOffset() is designed to provide greater offsets for polygons viewed more edge-on than for polygons that are flatter relative to the screen. This means that additional work is done for each polygon which could slow down rendering. An advantage, however, is that once the parameters have been tuned for a particular OpenGL implementation, the same unmodified code should work well on other implementations.

Similar effects are available using glDepthRange() but both the polygons and the edges are drawn at the maximum speed for each type of primitive. This is done by moving the zNear value out a little bit from 0.0 while setting the zFar to 1.0 for all normal drawing. Then when the edges are drawn move the zNear value to 0.0 and reduce the zFar value by the same amount. The offset should be at least 0.00001, depending on the depth buffer accuracy and the amount of perspective used in the projection matrix, and may need to be significantly greater in many cases.

The general algorithm for an offset of EDGE_OFFSET is:

glDepthRange(EDGE_OFFSET, 1.0);
<draw all non-edge geometry>

glDepthRange(0.0, 1.0 - EDGE_OFFSET);
<draw all edges>

As with all algorithms described in this manual, it is up to the user to select the hidden line (or edge highlighting) method that best meets his needs after considering ease of implementation, speed, and image quality.

Cool, I learn something new everyday.