Reputation: 143
I am rendering lines at the same position as a mesh and when the lines are right on top of the mesh I get an effect where the lines start to clip and parts of them disappear, especially when moving around. What I eventually have to do is something like the screenshot below, move the lines off the mesh a little bit in order to get rid of that effect. Does anyone have any ideas on how I can render the lines so that they are right on the mesh without parts of it disappearing?
Upvotes: 0
Views: 281
Reputation: 43329
First of all, are these lines really lines? Or are they the mesh drawn a second time with glPolygonMode (GL_FRONT_AND_BACK, GL_LINES)
? In either case, the problem is that the filled polygons and your lines are generating (roughly) the same depths and passing/failing the depth test is not working like you want.
The solution is to add a very slight depth offset to one of the primitives, and the easiest way to do this is to use glPolygonOffset (...)
. But it is worth mentioning that this only works for filled primitives (e.g. GL_POLYGON
, GL_QUADS
, GL_TRIANGLES
) and not GL_LINES
or GL_POINTS
.
If it is the same mesh drawn twice, on the second pass you can do something like this:
glPolygonOffset (-0.1f, -1.0f);
glEnable (GL_POLYGON_OFFSET_LINE);
... Draw Mesh Second Time
glDisable (GL_POLYGON_OFFSET_LINE);
Otherwise, you will need to apply the depth offset when you draw the (solid) mesh the first time:
glPolygonOffset (0.1f, 1.0f);
glEnable (GL_POLYGON_OFFSET_FILL);
... Draw Mesh
glDisable (GL_POLYGON_OFFSET_FILL);
... Draw Lines
You may need to tweak the values used for glPolygonOffset (...)
, it depends on a number of factors including depth buffer precision and the implementation. Generally the second parameter is going to be more important, it is a constant offset applied to the depth. The first factor varies according to the change in depth (e.g. angle).
Upvotes: 2