简体   繁体   中英

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

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.

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.

打开全屏抗锯齿并使用四边形将是我的首选。

Currently I am using 2 or 3 quads to do this, it is the simpliest way to do it.

  • If line thickness <= 1px, then you need only 2 quads.
  • If line thickness > 1px, then you need to add third quad in the middle.
  • The fading edge quads thickness must not change if the line thickness >= 1px.

In the image below you can see the quads with blue borders. White color means full opacity and black color means zero opacity (=fully transparent).

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM