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.
overdraw the lines by half the thickness on each end. That should remove the 'gaps' although it'll give a sharper line join rather than a smoother one.
Depending of the quality of the OpenGL implementation, the results may vary. I've noticed a lot of differences for smooth lines on different implementations.
You may want to use a different strategy to draw your line segments, such as using thin polygons.
Enabling anti-aliasing with GL_LINE_SMOOTH changes the way lines are drawn slightly. You may try that. Also note that blending will need to be enabled for that to work.
The main thing is that you're not going to be able to control every aspect of how lines are drawn using thin rectangles might work better, and after you write your own DrawThickLine function it won't be any more work...
To get something smooth you may want to resort to Bezier curves or some other form of splines. I'm surprised this isn't built into OpenGL, but the web site has an example program to generate Bezier curves using OpenGL.
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.
- Disable writing to the depth buffer glDepthMask(GL_FALSE)
- Draw smoothed lines of desired thickness
- 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)
- 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.
Draw quads, and use a texture or a fragment shader to stipple. A fragment shader could also provide antialiasing.