How to draw smooth line in OpenGL with antialiasing?

q0987 picture q0987 · Aug 18, 2010 · Viewed 52.3k times · Source

I need to draw a smooth line in OpenGL and here is what I have done:

glEnable( GL_LINE_SMOOTH );
glEnable( GL_POLYGON_SMOOTH );
glHint( GL_LINE_SMOOTH_HINT, GL_NICEST );
glHint( GL_POLYGON_SMOOTH_HINT, GL_NICEST );
glBegin( GL_LINE_STRIP );
    for( UINT uiPoint = 0; uiPoint < iNumPoints; ++uiPoint )
    {
        const Coord &Node = vecPoints[uiPoint];
        glVertex3f( Node.x, Node.y, Node.z );
    }
glEnd();

What else I can do?

Answer

Gretchen picture Gretchen · Aug 20, 2010

You also need to turn on blending for line smoothing to work. Try:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

and then drawing lines. It may also help to set the line width to a non-integral width.

As others have mentioned, this won't smooth polygon edges, but it will create antialiased lines.