OpenGL 中的抗锯齿

发布于 2024-09-30 13:36:19 字数 494 浏览 1 评论 0原文

我刚刚开始使用 OpenGL 编程,正在构建一个时钟应用程序。我希望它看起来像这样简单: https://i.sstatic.net/E73ap.jpg

但是,我的应用程序看起来非常“非抗锯齿”: https://i.sstatic.net /LUx2v.png

我尝试了红皮书中提到的GL_SMOOTH_POLYGON方法。然而这似乎并没有什么作用。

我正在使用配备英特尔集成显卡的笔记本电脑。该卡不支持诸如GL_ARB_multisample之类的东西。

此时我可以选择哪些选项来使我的应用程序看起来抗锯齿?

I just started with OpenGL programming and I am building a clock application. I want it to look something simple like this: https://i.sstatic.net/E73ap.jpg

However, my application looks very "un-anti-aliased" : https://i.sstatic.net/LUx2v.png

I tried the GL_SMOOTH_POLYGON method mentioned in the Red Book. However that doesn't seem to do a thing.

I am working on a laptop with Intel integrated graphics. The card doesn't support things like GL_ARB_multisample.

What are my options at this point to my app look anti-aliased?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独留℉清风醉 2024-10-07 13:36:19

Intel 集成显卡因缺乏对 OpenGL 抗锯齿功能的支持而臭名昭著。不过,您可以解决这个问题。

第一个选项:手动超级采样
制作一个屏幕大小 2 倍的纹理。通过 FBO 将场景渲染到纹理,然后使用双线性插值以一半大小渲染纹理,使其填充屏幕。由于绘制像素增加了 4 倍,因此速度可能会非常慢(在复杂的场景中)。

将导致抗锯齿效果较弱(因此我不建议将其用于时钟等桌面软件)。亲自看看:
抗锯齿与超级采样

第二个选项:(高级)
使用着色器执行形态抗锯齿。这是一项新技术,我不知道实施起来有多容易。它被一些高级游戏使用。

第三个选项:
通过纹理模拟 OpenGL 的基元,充分利用纹理和双线性插值。 此处描述了该技术。

第四个选项:
为时钟的每个元素使用单独的纹理。

例如,对于小时箭头,不要使用形状像箭头的纯黑色 GL_POLYGON。相反,请使用旋转的 GL_QUAD,并使用在图像程序中绘制的小时箭头图像进行纹理化。然后双线性插值将在您旋转它时处理抗锯齿问题。

此选项花费最少的精力并且看起来非常好。

第五个选项:
使用支持软件渲染的库 -

  • Qt
  • Cairo
  • Windows GDI+
  • WPF
  • XRender

此类库包含自己的抗锯齿渲染算法,因此它们不依赖于您的显卡的抗锯齿功能。优点是:

  • 在每个平台上呈现相同的效果。 (在各种情况下,OpenGL 无法保证这一点 - 例如,屏幕截图中的粗对角线“勾号”线被渲染为平行四边形,而不是矩形)
  • 具有大量方便的绘图函数(“drawArc”、“drawText”、 “drawConcavePolygon”,这些将支持渐变和边框,您还可以获得诸如 Image 类之类的东西。)
  • 有些(例如 Qt)将提供更多桌面应用程序类型的功能。即使对于时钟应用程序来说,这也非常有用。例如:
    • 在 OpenGL 应用程序中,您可能会每 20 毫秒循环一次并重新渲染时钟,甚至不会三思而后行。这会占用不必要的 CPU 周期,并唤醒笔记本电脑上的 CPU,从而耗尽电池。相比之下,Qt 非常智能地知道何时必须重新绘制时钟的某些部分(例如,当时钟的右半部分不再被窗口覆盖时,或者当时钟将分钟箭头移动一步时)。
    • 一旦您开始为时钟实现托盘图标或设置对话框等功能,像 Qt 这样的库就可以让它变得轻而易举。很高兴对所有事情都使用同一个库。

缺点是性能较差,但这对于时钟应用程序来说根本不重要,当您考虑到我提到的智能重绘功能时,情况就会逆转。

对于时钟应用程序之类的东西,强烈推荐第五个选项。 OpenGL 主要用于游戏、3D 软件和音乐可视化等密集的图形内容。对于桌面应用程序来说,它的级别太低并且实现差异太大。

Intel integrated videocards are notorious for their lack of support for OpenGL antialiasing. You can work around that, however.

First option: Manual supersampling
Make a texture 2x times as big as the screen. Render your scene to the texture via FBO, then render the texture at half size so it fills the screen, with bilinear interpolation. Can be very slow (in complex scenes) due to the 4x increase in pixels to draw.

Will result in weak antialiasing (so I don't recommend it for desktop software like your clock). See for yourself:
antialiasing vs supersampling

Second option: (advanced)
Use a shader to perform Morphological Antialiasing. This is a new technique and I don't know how easy it is to implement. It's used by some advanced games.

Third option:
Use textures and bilinear interpolation to your advantage by emulating OpenGL's primitives via textures. The technique is described here.

Fourth option:
Use a separate texture for every element of your clock.

For example, for your hour-arrow, don't use a flat black GL_POLYGON shaped like your arrow. Instead, use a rotated GL_QUAD, textured with a hour-arrow image drawn in an image program. Then bilinear interpolation will take care of antialiasing it as you rotate it.

This option would take the least effort and looks very well.

Fifth option:
Use a library that supports software rendering -

  • Qt
  • Cairo
  • Windows GDI+
  • WPF
  • XRender
  • etc

Such libraries contain their own algorithms for antialiased rendering, so they don't depend on your videocard for antialiasing. The advantages are:

  • Will render the same on every platform. (this is not guaranteed with OpenGL in various cases - for example, the thick diagonal "tick" lines in your screenshot are rendered as parallelograms, rather than rectangles)
  • Has a big bunch of convenient drawing functions ("drawArc", "drawText", "drawConcavePolygon", and those will support gradients and borders. also you get things like an Image class.)
  • Some, like Qt, will provide much more desktop-app type functionality. This can be very useful even for a clock app. For example:
    • in an OpenGL app you'd probably loop every 20msec and re-render the clock, and not even think twice. This would hog unnecessary CPU cycles, and wake up the CPU on a laptop, depleting the battery. By contrast, Qt is very intelligent about when it must redraw parts of your clock (e.g., when the right half of the clock stops being covered by a window, or when your clock moves the minute-arrow one step).
    • once you get to implementing, e.g. a tray icon, or a settings dialog, for your clock, a library like Qt can make it a snap. It's nice to use the same library for everything.

The disadvantage is much worse performance, but that doesn't matter at all for a clock app, and it turns around when you take into account the intelligent-redrawing functionality I mentioned.

For something like a clock app, the fifth option is very much recommended. OpenGL is mainly useful for games, 3D software and intense graphical stuff like music visualizers. For desktop apps, it's too low-level and the implementations differ too much.

大海や 2024-10-07 13:36:19

以最终分辨率的两倍(或更多)将其绘制到帧缓冲区对象中,然后使用该图像作为在实际窗口中绘制的单个四边形的纹理。

Draw it into a framebuffer object at twice (or more) the final resolution and then use that image as a texture for a single quad drawn in the actual window.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文