Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw smooth lines in 2D scene with OpenGL without using GL_LINE_SMOOTH?

Tags:

c++

opengl

glsl

Since GL_LINE_SMOOTH is not hardware accelerated, nor supported on all GFX cards, how do you draw smooth lines in 2D mode, which would look as good as with GL_LINE_SMOOTH ?

Edit2: My current solution is to draw a line from 2 quads, which fade to zero transparency from edges and the colors in between those 2 quads would be the line color. it works good enough for basic smooth lines rendering and doesnt use texturing and thus is very fast to render.

like image 796
Rookie Avatar asked Apr 27 '12 14:04

Rookie


2 Answers

So, you want smooth lines without:

  • line smoothing.
  • full-screen antialiasing.
  • shaders.

Alright.

Your best bet is to use Valve's Alpha-Tested Magnification technique. The basic idea, for your needs, is to create a texture that represents the distance from the line, with the center of the texture being a distance of 1.0. This could probably be a 1D texture.

Then using the techniques described in the paper (many of which work with fixed-function, including the antialiased version), draw a quad that represents your lines. Obviously you'll need alpha blending (and thus it isn't order-independent). You use your line width to control the distance at which it becomes the appropriate color, thus allowing you to make narrow or wide lines.


Doing this with shaders is virtually identical to the above, except without the texture. Instead of accessing a distance texture, the distance is passed and interpolated from the vertex shader. For the left-edge of the quad, the vertex shader passes 0. For the right edge, it passes 1. You multiply this by 2, subtract 1, and take the absolute value.

That's your distance from the line (the line being the center of the quad). Then just use that distance exactly as Valve's algorithm does.

like image 50
Nicol Bolas Avatar answered Sep 29 '22 17:09

Nicol Bolas


Turning on full-screen anti-aliasing and using a quad would be my first choice.

like image 36
Edvard Pedersen Avatar answered Sep 29 '22 16:09

Edvard Pedersen