是否可以在 OpenGL 中消除线端锯齿?
我正在 OpenGL 中绘制一些 2D 线条(使用 AGL API)。我已使用 GL_LINE_SMOOTH
打开线条抗锯齿功能,并将 GL_LINE_SMOOTH_HINT
设置为 GL_NICEST
。
例如,这是对角线的放大视图*:
但是,问题是我的代码绘制了一些由大量垂直线组成的形状,并且看起来线的终点没有抗锯齿。这是其中之一:
(你看到的每一步都是由10条垂直线组成,每条线的x坐标以1.0的步长递增,y坐标以0.1的步长递增。
)让 OpenGL 也对线条端点进行抗锯齿的方法?
编辑:
根据Calvin1602 的回答,我尝试删除 GL_LINE_SMOOTH
内容,而是通过添加来启用 MSAA以下是我的 AGL 属性数组:
AGL_SAMPLE_BUFFERS_ARB, 1, AGL_SAMPLES_ARB, 4, AGL_MULTISAMPLE
...并调用:
glEnable(GL_MULTISAMPLE_ARB);
但是,我仍然遇到同样的问题。线条边缘是抗锯齿的(方式略有不同),但端点不是。
* 屏幕截图来自运行 OS X 10.7.1 和 ATI Radeon HD 5770 1024 MB 的 Mac Pro。
编辑 2:
使用 MSAA 对我来说不起作用,但只是简单将我的所有线条绘制为非常细的矩形 按照ltjax的建议似乎已经成功了。 (我不需要使用纹理或渐变。)
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*:
(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:
(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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,全屏抗锯齿功能可以为您做到这一点。例如,多重采样将起作用并得到广泛支持。如果您只想坚持使用抗锯齿线,则可以通过绘制四边形(比您的线稍大)并使用预定义的“渐变”纹理或评估与真实线的距离的着色器来模拟它。
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.
尝试禁用 GL_LINE_SMOOTH 功能(该功能在驱动程序中没有明确定义的行为),并使用 MSAA(多样本抗锯齿)。
这是在上下文创建时启用的;这取决于您使用的框架。 GLFW?雪迪龙?其他 ?自制的?
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 ?
您始终可以实现“在图形硬件上使用预过滤线进行快速抗锯齿”。
You could always implement "Fast Antialiasing Using Prefiltered Lines on Graphics Hardware".