Rich
Rich

Reputation: 7795

Is it possible to anti-alias line ends in OpenGL?

I'm drawing some 2D lines in OpenGL (using the AGL API). I've turned on line anti-aliasing using GL_LINE_SMOOTH and I've set GL_LINE_SMOOTH_HINT to GL_NICEST.

So for example, here's a magnified view of a diagonal line*:

Line with anti-aliased edges
(source: whileyouweregone.co.uk)

However, the problem is that my code draws some shapes which are built up of large numbers of vertical lines, and it seems the end points of lines are not anti-aliased. Here's one of those:

Lines without anti-aliased endpoints
(source: whileyouweregone.co.uk)

(Each step you can see is made up of 10 vertical lines, with the x coordinates of each line incrementing in steps of 1.0, and the y coordinates incrementing in steps of 0.1.)

Is there a way of getting OpenGL to also anti-alias the lines' end points?

Edit:

As per Calvin1602's answer, I've tried removing the GL_LINE_SMOOTH stuff and am instead enabling MSAA by adding the following to my AGL attributes array:

AGL_SAMPLE_BUFFERS_ARB, 1, AGL_SAMPLES_ARB, 4, AGL_MULTISAMPLE

...and calling:

glEnable(GL_MULTISAMPLE_ARB);

However, I still have the same problem. The line edges are anti-aliased (in a slightly different way), but the end-points aren't.

* Screenshots are from a Mac Pro running OS X 10.7.1 with a ATI Radeon HD 5770 1024 MB.

Edit 2:

Using MSAA doesn't work for me, but simply drawing all my lines as very thin rectangles as suggested by ltjax seems to have done the trick. (I didn't need to use a texture or a gradient.)

Upvotes: 0

Views: 2180

Answers (3)

genpfault
genpfault

Reputation: 52162

You could always implement "Fast Antialiasing Using Prefiltered Lines on Graphics Hardware".

Upvotes: 0

Calvin1602
Calvin1602

Reputation: 9547

Try disabling the GL_LINE_SMOOTH stuff, which hasn't a well defined behaviour across drivers, and use MSAA (multi-sample anti-aliasing).

This is enabled at context creation; it depends on the framework you're using. GLFW ? SDL? Other ? Home-made ?

Upvotes: 2

ltjax
ltjax

Reputation: 16007

Yes, full-screen anti-aliasing will do this for you. For example, multi-sampling will work and is widely supported. If you want to stick with just anti-aliased lines, you can emulate it by drawing quads instead (which are slightly larger than your line) and using either a predefined "gradient" texture or a shader that evaluates the distance from the real line.

Upvotes: 3

Related Questions