Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 3 123 LastLast
Results 1 to 10 of 30

Thread: Anti-Aliased Lines

  1. #1
    Junior Member Regular Contributor
    Join Date
    Apr 2005
    Location
    Toronto
    Posts
    122

    Anti-Aliased Lines

    So I have an application with a wireframe type draw and I was wondering if anyone has any useful tips/hints on doing really good line Anti-Aliasing with basic OpenGL?

    Currently I...

    1) Enable GL_MULTISAMPLE.
    2) Enable GL_LINE_SMOOTH.
    3) Set glLineWidth() to 0.5
    4) Enable GL_BLEND (with standard blend function)
    5) Set glDepthMask to FALSE
    6) Set glHint to GL_NICEST

    That is the basic stuff you can find in the RedBook. Anyone else have any suggestions for improving things?

  2. #2
    Member Regular Contributor
    Join Date
    Apr 2007
    Posts
    271

    Re: Anti-Aliased Lines

    If you enable GL_MULTISAMPLE (and you created a context such that when you query the value of GL_SAMPLE_BUFFERS, it returns 1),
    GL_LINE_SMOOTH is ignored and multisampling rasterization is used.

    ref: spec 2.1 page 107, section 3.4.4 Line Multisample Rasterization.

    http://www.opengl.org/registry/doc/g...1.20061201.pdf

    so 2) and 4) are useless in this case.

    For multisampling, I'm not sure that you want to set depth mask to FALSE.

  3. #3
    Junior Member Regular Contributor
    Join Date
    Apr 2005
    Location
    Toronto
    Posts
    122

    Re: Anti-Aliased Lines

    Quote Originally Posted by overlay
    If you enable GL_MULTISAMPLE (and you created a context such that when you query the value of GL_SAMPLE_BUFFERS, it returns 1),
    GL_LINE_SMOOTH is ignored and multisampling rasterization is used.
    Did not know that, thanks for the reply.

  4. #4
    Member Regular Contributor remdul's Avatar
    Join Date
    Mar 2004
    Location
    The Netherlands
    Posts
    335

    Re: Anti-Aliased Lines

    2) Enable GL_LINE_SMOOTH.
    3) Set glLineWidth() to 0.5
    4) Enable GL_BLEND (with standard blend function)
    5) Set glDepthMask to FALSE
    6) Set glHint to GL_NICEST

    ...is be enough for just anti-aliased lines.

    GL_MULTISAMPLE is for full anti-aliasing including solid triangles, and you must set up a multisampled framebuffer before it works.

  5. #5
    Junior Member Regular Contributor
    Join Date
    Apr 2005
    Location
    Toronto
    Posts
    122

    Re: Anti-Aliased Lines

    Quote Originally Posted by remdul
    ...is be enough for just anti-aliased lines.
    Yes and no. The line are anti-aliased but the quality is not all that good, especially when you start drawing lines over other lines (you get bleeding and bluring).

    Quote Originally Posted by remdul
    GL_MULTISAMPLE is for full anti-aliasing including solid triangles, and you must set up a multisampled framebuffer before it works.
    Yep, I have the framebuffer set up. I have it working, I was just wondering if there were things I could do to improve the quality.

    Right now, it looks "ok" but not "great"

  6. #6
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,262

    Re: Anti-Aliased Lines

    Draw wide lines (2px or 4px) with a shader, that uses gl_FragCoord.xy and writes an opacity value in gl_FragColor.a

  7. #7
    Junior Member Regular Contributor
    Join Date
    Apr 2005
    Location
    Toronto
    Posts
    122

    Re: Anti-Aliased Lines

    Quote Originally Posted by Ilian Dinev
    Draw wide lines (2px or 4px) with a shader, that uses gl_FragCoord.xy and writes an opacity value in gl_FragColor.a
    How do I use gl_FragCoord.xy?

    How does the x,y window relative coordinates of a given fragment help me determine if I am near the edge of a line (which I assume I nned to know to set the alpha value?)?

  8. #8
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,262

    Re: Anti-Aliased Lines

    search this board for it. There are two implementations, one by me that uses gl_FragCoord, and one that uses geometry shaders.

  9. #9
    Junior Member Regular Contributor
    Join Date
    Sep 2002
    Location
    Germany
    Posts
    205

    Re: Anti-Aliased Lines

    but note, that there are still issues with ATI and gl_FragCoord and FBOs...

  10. #10
    Junior Member Regular Contributor
    Join Date
    Apr 2005
    Location
    Toronto
    Posts
    122

    Re: Anti-Aliased Lines

    So I found the following vertex shader and fragment shader on the forums but they don't seem to work?

    Code :
    varying vec4 posix;
    void main(){
    	posix = ftransform();
    	gl_Position=posix + vec4(0,0,0,0.001);
    }
    Code :
    varying vec4 posix;
    void main(){
    	vec4 FinalColor = vec4(0.0,0.0,0.0,1.0);
     
    	vec2 ScreenSizeHalf = vec2(1680/2,1050/2);
    	vec2 posix1= ((posix.xy/posix.w)+1.0)*ScreenSizeHalf;
    	vec2 fpos = gl_FragCoord.xy;
    	float dist = distance(fpos,posix1);
    	FinalColor.w = 1.0-dist;
    	gl_FragColor = FinalColor;
    }

    I messed around with the code a little and it appears that the distance is always larger than 2.0? (I switched the second last line to "FinalColor.r = dist-2.0;" and the lines were still always red).

    What am I doing wrong?

    p.s. I am running a Nvidia Quadro FX570.
    p.p.s My monitor is a Dell widescreen running at 1680x1050.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •