tags:

views:

73

answers:

3

Hello all,

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?

Thank you

+2  A: 

GL_POLYGON_SMOOTH by itself does you no good. You need to force antialiasing when OpenGL context is created. What do you use to create OpenGL window? See if it supports antialiasing among its parameters. Or you could force antialiasing for all OpenGL programs with Nvidia or ATI tools... It all depends on your setup.

alxx
Hello alxx,I am using an existing platform that supports opengl renderring.
q0987
Sorry, that says nothing. Which library it uses? Are there parameters to start OpenGL (like color depth, stencil presence, z-buffer size and so on)?
alxx
+1  A: 

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.

Calvin1602
Hello Calvin,I am new to OpenGL and doesn't quite follow your terms:). Do you know where I can find some examples that can illustrate your idea?Thank you
q0987
@q0987: check this out: http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter22.html
Stringer Bell
@Stringer Bell nice reference, I didn't know it was in gpugems2 too
Calvin1602
+1  A: 

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.

Matthew Hall