View Full Version : How to draw one line on top of another in OpenGL without Z-fighting

12-04-2012, 04:05 PM
I've asked this question on SO (http://stackoverflow.com/questions/13708661/how-to-draw-one-line-on-top-of-another-in-opengl-without-z-fighting), but with no answer so far (and with one close request, typical SO...)

I'm having the following problem. While glPolygonOffset works perfectly for meshes, for example when I'm trying to draw a wireframe outline on top of the object, it doesn't work for simple lines.

Here is how it works for meshes:

// draw object
gl::color( Colorf( 1, 1, 1 ) );
gl::draw( mVboMesh );

// overlay wireframe
glPolygonOffset( -1, -1 );
glColor3f( 0, 0, 1 );
gl::draw( mVboMesh );


For some reason it doesn't work for lines. What I'm trying to achieve is to draw a coordinate frame's arrows over a grid. I'm using the very same GL_POLYGON_OFFSET_LINE mode as when I was drawing lines, just like I was doing for the wireframe over the object. However in this case glPolygonOffset( -1, -1 ); makes absolutely no difference. I've tried it with huge values like 100 and it's the same. Absolutely no effect. Here is what I'm doing:

// enable 3D rendering
gl::enable( GL_CULL_FACE );

// drawing the grid
int size = 2000;
int step = 25;
gl::color( Colorf( 0.2f, 0.2f, 0.2f ) );
for( float i = -size; i <= size; i += step )
glBegin( GL_LINES );
glVertex3f( i, 0, -size );
glVertex3f( i, 0, size );
glVertex3f( -size, 0, i );
glVertex3f( size, 0, i );
glEnd( );

// drawing the arrows
glPolygonOffset( -1, -1 );

glBegin( GL_LINES );
gl::color( Colorf( 1, 0, 0 ) );
glVertex3f( 0, 0, 0 );
glVertex3f( 100, 0, 0 );
gl::color( Colorf( 0, 1, 0 ) );
glVertex3f( 0, 0, 0 );
glVertex3f( 0, 100, 0 );
gl::color( Colorf( 0, 0, 1 ) );
glVertex3f( 0, 0, 0 );
glVertex3f( 0, 0, 100 );
glEnd( );


// disable 3D rendering
gl::disable( GL_CULL_FACE );

and an example of the Z-fighting I get:


One hack I've tried and what worked perfectly is:

1. disable depth read, enable depth write
2. draw grid
3. draw arrows
4. enable depth read
5. draw other objects

However this is a very special case and while it works for a flat grid and arrows, it wouldn't work for pretty much anything else with a complex shape.

My questions are:

1. Why does glPolygonOffset not work for lines-on-lines while it works for lines-on-polygon?
2. How can I fix it, without resorting to the above hack, what only works in very specific cases?

// I'm using Cinder as a framework, but it shouldn't matter since I'm using raw OpenGL commands

Update (keeping it like this for reference)

I've checked the answer in the first comment (http://stackoverflow.com/questions/2020451/glpolygonoffset-bugs-with-lines), and tried that method as well, however that one doesn't work either, since the result depends on the distance from the camera.

// draw coordinate frame and grid
glDepthRange (0.01, 1.0);
drawGrid( 2000.0f, 25.0f );
glDepthRange (0.0, 0.99);
gl::drawCoordinateFrame( 100.0f, 5.0f, 2.0f );

glDepthRange (0.0, 1.0);

// draw object


SO post ends here

Can you help me with this issue? Should I calculate a custom value for the glDepthRange trick, on-the-fly? Or should I turn at least one of the objects into a polygon? How would you solve this problem properly? For example what would you do if you had a 3D polyline and you'd like to re-draw over one segment? In that case you cannot even turn it into a polygon.


12-04-2012, 04:10 PM
For whatever reason lines produce (slightly) different depth values compared to polys, so you will see problems like this. Maybe rendering lines as polys instead of GL_LINES would work better. glPolygonoffset does work, but I've found it unreliable in the past. And made worse by different hardware that gives different results, so generally I try and avoid it.

12-04-2012, 04:24 PM
But then what's the best way? Render them as small cylinders? Calculate depth-range shift on-the-fly, based on the camera distance / projection matrix?

12-04-2012, 05:08 PM
I have never found a one size fits all solution to this problem. For your case you could just disable depth testing since you always want them on top.

Dark Photon
12-04-2012, 06:35 PM
Alternatively, if you only want lines to be occluded by polygons, render lines with only depth testing (with GL_LEQUAL depth test), never depth writing. And render them last. Then you won't have lines fighting lines.

Yet another solution is Eric Lengyel's projection matrix offset trick, which basically lets you render something N depth steps closer to the eyepoint (scales better than PolygonOffset). A few links on that:

* glPolygonOffset whats the deal here? is it good or bad? (http://www.gamedev.net/topic/335012-glpolygonoffset-whats-the-deal-here-is-it-good-or-bad/page__whichpage__1%25EE%2580%258F) (gamedev.net)
* Projection Matrix Tricks (http://www.terathon.com/gdc07_lengyel.ppt) (Lengyel)
* Mathematics for 3D Game Programming and Computer Graphics (http://www.amazon.com/Mathematics-Programming-Computer-Graphics-Third/dp/1435458869) (Lengyel)

12-04-2012, 08:42 PM
1. Why does glPolygonOffset not work for lines-on-lines while it works for lines-on-polygon?

Because polygon offset only works for polygons, not lines. GL_POLYGON_OFFSET_LINE only applies to polygons whose fill mode is GL_LINE; similarly, GL_POLYGON_OFFSET_POINT applies to polygons with fill mode GL_POINT. See http://www.opengl.org/sdk/docs/man3/xhtml/glPolygonMode.xml. (http://www.opengl.org/sdk/docs/man3/xhtml/glPolygonMode.xml)

So, you can't use glPolygonOffset() to offset lines or points in Z. The reason it works for lines-on-polygons is because it's the polygon that is shifted away from the camera, not the line.