Alternatives to glPolygonOffset

Hi everyone,

Let’s say I need to render a mesh with some edges on top. With edges, I’m not referring necessarily to individual triangles edges, they could be a surface’s isocurves for example:
Snag_b87689b

Until now I was doing fine by using glPolygonOffset and setting to a positive value both the constant and the slope-scaled factors to draw the meshes so that edges appear (almost) always on top of the polygonal geometry.

Recently I’ve introduced some post-processing effects that rely on depth data, and I’d really like to re-use the content of the depth buffer I used to draw the scene onto instead of having to do a depth pass just for those effects.

The problem is that the slope-scaled bias I used to draw edges/iso-curves/… introduces additional discontinuities in depth where connected triangles with different slopes meet. This is a big issue for the post-processing stage.

So my first question is: is there a way to draw clean edges/iso-curves without introducing additional discontinuities (like a slope-scale bias would do)?

I also tried by using a different projection matrix for the edges and the polygonal meshes they are attached to: the meshes are drawn with a perspective projection matrix m1 (near = n1, far = f1) and the edges with m2 (near = n1+delta, far = f1+delta). However, the result seems quite bad in my application, it looks a lot like this:

In the image: Blender, from a quick look with RenderDoc it seems to me they go with the near+delta, far+delta approach when it comes to edges

My second question is: what could be a good way to compute an appropriate delta to offset the near and far planes when drawing edges on top of a mesh?

Is turning off depth writes (not depth testing, just the writes to the depth buffer) with glDepthMask when drawing these edges an option?

I use glPolygonOffset to draw the polygonal meshes, not to draw the edges. Edges are line primitives, where polygon offset has no effect.
I use the offset while drawing the meshes to make room for the edges.

And I’d like to find a way to draw edges on top of meshes without having to offset the meshes depth.

glPolygonOffset can be used for polygons, lines and/or points, depending upon which of GL_POLYGON_OFFSET_FILL, GL_POLYGON_OFFSET_LINE, or GL_POLYGON_OFFSET_POINT is enabled.

If you’re wanting to perform processing on the depth buffer, it would seem to make more sense to render the polygons without an offset, and the lines with an offset but with depth writes disabled.

Blockquote
glPolygonOffset can be used for polygons, lines and/or points, depending upon which of GL_POLYGON_OFFSET_FILL , GL_POLYGON_OFFSET_LINE , or GL_POLYGON_OFFSET_POINT is enabled.

Doesn’t that mean that I have to draw my edges/isocurves as trianlges with glPolygonMode(..., GL_LINE) ?
Reading the docs, I understood that polygon offset is meaningful only for polygons, (things that comes from glDraw*(GL_TRIANGLES/STRIP/FAN/..., ...), is it correct? (how could Ogl find a slope to apply a slope-scaled depth offset if the primitive doesn’t define e plane?)

At the moment, edges/isocurves in my application are defined as lines (without a normal to define which direction they are facing) and drawn as lines (glDraw*(GL_LINES)).

Ah, you’re right; it only applies to lines generated by rendering polygons with glPolygonMode(GL_LINE).

I’d consider implementing the offset in the shader when drawing the lines. OpenGL itself doesn’t know anything about the surfaces on which the lines are supposed to lie, but presumably the application does.

Essentially, the offset needs to account for the fact that when the fragments which form a line overlap those which form a triangle, the sampling locations (which ultimately determine depth values) will be offset by up to half a pixel. So if a line is rendered over a “steep” polygon (one almost edge-on to the viewpoint), the difference in depth values could be substantial.

1 Like