如何在OpenGL中使用抗锯齿绘制平滑的线条?
我需要在 OpenGL 中画一条平滑的线,这就是我所做的:
glEnable( GL_LINE_SMOOTH );
glEnable( GL_POLYGON_SMOOTH );
glHint( GL_LINE_SMOOTH_HINT, GL_NICEST );
glHint( GL_POLYGON_SMOOTH_HINT, GL_NICEST );
glBegin( GL_LINE_STRIP );
for( UINT uiPoint = 0; uiPoint < iNumPoints; ++uiPoint )
{
const Coord &Node = vecPoints[uiPoint];
glVertex3f( Node.x, Node.y, Node.z );
}
glEnd();
我还能做什么?
I need to draw a smooth line in OpenGL and here is what I have done:
glEnable( GL_LINE_SMOOTH );
glEnable( GL_POLYGON_SMOOTH );
glHint( GL_LINE_SMOOTH_HINT, GL_NICEST );
glHint( GL_POLYGON_SMOOTH_HINT, GL_NICEST );
glBegin( GL_LINE_STRIP );
for( UINT uiPoint = 0; uiPoint < iNumPoints; ++uiPoint )
{
const Coord &Node = vecPoints[uiPoint];
glVertex3f( Node.x, Node.y, Node.z );
}
glEnd();
What else I can do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您还需要打开混合才能使线条平滑工作。尝试:
然后画线。将线宽设置为非整数宽度也可能有所帮助。
正如其他人提到的,这不会平滑多边形边缘,但会创建抗锯齿线。
You also need to turn on blending for line smoothing to work. Try:
and then drawing lines. It may also help to set the line width to a non-integral width.
As others have mentioned, this won't smooth polygon edges, but it will create antialiased lines.
您可以改为生成薄的、面向屏幕的多边形,并根据到线的距离设置片段的 Alpha。
示例:
假设您要绘制线段 [AB]。
(更多或更少 ShaderX5 中使用的技术)
但仅当您无法使用 MSAA 时才执行此操作。
You can generate thin, screen-oriented polygons instead, and set the fragment's alpha according to the distance to the line.
Example :
Suppose you want to draw segment [AB].
(more or less the technique used in ShaderX5)
But do this only if you can't use MSAA.
GL_POLYGON_SMOOTH 本身对你没有任何好处。创建 OpenGL 上下文时,您需要强制抗锯齿。你用什么来创建OpenGL窗口?查看其参数是否支持抗锯齿。或者您可以使用 Nvidia 或 ATI 工具强制所有 OpenGL 程序进行抗锯齿...这完全取决于您的设置。
GL_POLYGON_SMOOTH by itself does you no good. You need to force antialiasing when OpenGL context is created. What do you use to create OpenGL window? See if it supports antialiasing among its parameters. Or you could force antialiasing for all OpenGL programs with Nvidia or ATI tools... It all depends on your setup.
正如另一个答案提到的,启用抗锯齿功能会有所帮助 - 如何启用它取决于您的上下文创建,但此信息有望对 GLFW 用户有用。
如果使用 GLFW 2:
glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
如果使用 GLFW 3:
glfwWindowHint(GLFW_SAMPLES, 4);
...或您喜欢的任意数量的抗锯齿样本。在打开GLFW窗口之前需要设置以上提示。
As another answer mentioned, enabling antialiasing helps - how to enable this depends on your context creation, but this info will hopefully be of use for GLFW users.
If using GLFW 2:
glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
If using GLFW 3:
glfwWindowHint(GLFW_SAMPLES, 4);
...or whatever number of antialiasing samples you prefer. The above hints need to be set before opening the GLFW window.
无法保证
GL_LINE_SMOOTH
或GL_POLYGON_SMOOTH
会执行任何操作。尽管它们看起来像核心功能,但我的经验是,许多硬件并没有实现这些功能 - 那些确实没有一致地实现它的硬件,如果它们确实实现了它,性能损失可能会相当大。如果您查看 OpenGL 规范文档 - 这些文档为整个库提供了极好的参考,但是您不会在其中找到任何提及这些内容的内容,因为它们不是规范的一部分。您可以通过查看 glEnable 的规范来在某种程度上确认这一点:khronos.org/opengles/sdk/docs/man/xhtml/glEnable.xml
There is no guarantee the
GL_LINE_SMOOTH
orGL_POLYGON_SMOOTH
will do anything.Although they look like core functionality my experience is that lots of hardware doesn't implement these - the ones that do don't implement it consistently, and if they do implement it the performance hit can be considerable. if you look at the OpenGL specification documents - these provide excellent reference for the whole library, however you won't find any mention of these in there because they aren't part of the spec. You can confirm this to some degree by looking at the spec for glEnable: khronos.org/opengles/sdk/docs/man/xhtml/glEnable.xml
Nanovg 是一个渲染到 OpenGL 的 2D 绘图包。我将它与常规 OpenGL 一起使用来完成填充和描边 2D 路径和线条。 https://github.com/memononen/nanovg
Nanovg is a 2D drawing package that renders to OpenGL. I have used it alongside regular OpenGL to do filled and stroked 2D paths and lines. https://github.com/memononen/nanovg