Java – Smooth lines in OpenGL

javajoglopengl

I'm trying to a draw a more-or-less smooth multi-segment line in OpenGL. However I find that if the line is over a thickness about 3 then the joins between the segments are not seamless. They sometimes have gaps between them. Is there a good way of making these joins smooth and gapless? I'm looking for something like the equivalent of BasicStroke.JOIN_MITER in Java.

Best Solution

The most consistent and portable way to draw think lines using OpenGL is to use camera aligned (otherwise know as billboarded) polygons. Different implementations of the OpenGL API handle the end points of the lines differently and GL_LINE_SMOOTH produces drastically different results depending on the platform. Some implementations consider lines of thickness greater then 1 to be ill-defined. As stated in the glLineWidth manpages: [For anti-aliased lines] only width 1 is guaranteed to be supported; others depend on the implementation.

That being said, I have used a simple 2 pass hack to address this very problem.

  1. Disable writing to the depth buffer glDepthMask(GL_FALSE)
  2. Draw smoothed lines of desired thickness
  3. Draw smoothed points at the endpoints of all the lines that are the same size at the lines. (this step may not be needed to fill the gap, however it should round out the points where the lines meet and make everything look a little smoother)
  4. Re-enable the depth buffer for writing glDepthMask(GL_TRUE)

You may also want to play around with the depth buffer settings. The "gap" between the lines may be due to the 2nd line failing a depth test on pixels that have been alpha blended with the background.

Related Question