Search code examples
opengllineantialiasing

How to draw smooth line in OpenGL with antialiasing?


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?


Solution

  • You can generate thin, screen-oriented polygons instead, and set the fragment's alpha according to the distance to the line.

    Example :

       a (0,1)                                  b (0,1)
        +--------------------------------------+
      A |                                      | B
    ----+--------------------------------------+----
        |                                      |  
        +--------------------------------------+
       d (0,0)                                  c (0,0)
    

    Suppose you want to draw segment [AB].

    • Draw polygon abcd instead
    • Map the UVs (the (0,0) , (0,1))
    • bind a 8x1 black and white texture that is white only on the center
    • render with a fragment shader that set gl_FragColor.a from the texture

    (more or less the technique used in ShaderX5)

    But do this only if you can't use MSAA.