Smooth lines in OpenGL
I'm trying to draw a more or less smooth multi-segment line in OpenGL. However, I find that if the line is more than about 3 thick then the connections between the segments are not seamless. They sometimes have gaps in between. Is there a good way to make these connections smooth and brushless? I'm looking for something like the equivalent of BasicStroke.JOIN_MITER in Java.
a source to share
The most consistent and portable way to draw thinking lines using OpenGL is to use aligned polygons (otherwise known as billboarded). Different implementations of the OpenGL API treat line endpoints differently and GL_LINE_SMOOTH
produce very different results depending on the platform. Some implementations assume that thick lines greater than 1 will be poorly defined. As stated in the man files glLineWidth: [for antialiased lines] only width 1 is guaranteed to be supported; others are implementation dependent.
That being said, I used a simple 2-way hack to solve this very problem.
- Disable writing to depth buffer glDepthMask (GL_FALSE)
- Draw smooth lines of the desired thickness
- Draw smooth points at the end points of all lines that are the same size on the lines. (This step might not be needed to fill in the gap, but it should round off the points where the lines meet and make things a little smoother.)
- Enable write depth buffer glDepthMask (GL_TRUE)
You can also play with the depth buffer settings. The "gap" between the lines can be caused by the 2nd line failing the depth check on pixels that have been alpha blended with the background.
a source to share
Enabling smoothing with GL_LINE_SMOOTH changes the direction of the lines. You can try this. Also note that blending must be enabled for this.
The main thing is that you cannot control all aspects of drawing lines, using thin rectangles may work better, and after you write your own DrawThickLine function, it will no longer work ..
a source to share
To get something smooth, you can resort to Bézier curves or some other spline shape. I'm surprised it's not built into OpenGL, but there is a sample program on the website to generate Bezier curves using OpenGL .
a source to share