开发者

Drawing a "3D-looking" line in OpenGL

开发者 https://www.devze.com 2023-03-01 11:53 出处:网络
When you draw a line in OpenGL, glLineWidth creates a fixed-size line, regardless how close the line is to you.

When you draw a line in OpenGL, glLineWidth creates a fixed-size line, regardless how close the line is to you.

I wanted to draw开发者_运维知识库 a line that will appear bigger when it's close. Now, I understand that if I use a rectangle to achieve this effect, it will look a bit pixelated once the polygon is far enough.

What I've previously done is to draw a normal GL_LINE up to the point where the line would get bigger than the pixel size, and then continue with a rectangle from that point. However, it's not as fast as just chucking everything down to a vertex array or VBO, as it had to be recalculated every frame.

What other methods are available? Or am I stuck with this?


I like to use a gradated texture like this to draw lines:

Drawing a "3D-looking" line in OpenGL

This is really the alpha of my texture. So you have a fully opaque center fading to fully transparent at the edges. Then you can draw your line using a rectangle with points:

(x1,y1,0,0), (x2,y1,1,0), (x1,y2,0,1), (x2,y2,1,1)

where the last two entries in each tuple are u and v of the texture. It ends up looking very smooth. You can even string together lots of very small rectangles to make curvy lines.


If you're just drawing a bunch of lines and want a quick and easy depth effect try adding fog. The attenuation of the lines as they recede makes our brains think they're 3d. This isn't going to work if the near ends are really close to the viewer.


If you want your lines be thicker on near end and thinner on far end, I suppose you have to model them from polygons.

0

精彩评论

暂无评论...
验证码 换一张
取 消