Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 377
q0987 Avatar asked Aug 18 '10 13:08

q0987


1 Answers

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.

like image 175
Calvin1602 Avatar answered Sep 19 '22 22:09

Calvin1602