在 OpenGL 中渲染矢量图形?
有没有办法加载矢量图形文件,然后使用 OpenGL 渲染它?这是一个模糊的问题,因为我对矢量图形的文件格式不太了解。不过我知道 SVG。
将其转换为光栅并没有多大帮助,因为我想实时放大对象。
Is there a way to load a vector graphics file and then render it using OpenGL? This is a vague question as I don't know much about file formats for vector graphics. I know of SVG, though.
Turning it to raster isn't really helpful as I want to do real time zooming in on the objects.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我看到大多数答案都是关于 Qt 的,尽管最初的问题没有提到它。这是我仅就 OpenGL 而言的回答(这也从时间的流逝中受益匪浅,因为它不可能在 2010 年给出):
自 2011 年以来,最先进的技术是 Mark Kilgard 的宝贝,NV_path_rendering,目前只是一个供应商 (Nvidia) 扩展,您可能已经从其名称中猜到了。有很多相关材料:
NV_path_rendering 现在由 Google 的 Skia 库(如果可用)在幕后使用。 (Nvidia 在 2013 年末和 2014 年贡献了代码。)
您当然可以加载 SVG 等 https:// /www.youtube.com/watch?v=bCrohG6PJQE。它们还支持路径的 PostScript 语法。您还可以将路径渲染与其他 OpenGL (3D) 内容混合,如演示所示:
一个新贵是 NanoVG,它的供应商支持甚至更少(或根本没有),目前正在开发和维护。 (https://github.com/memononen/nanovg) 考虑到 OpenGL 上的 2D 库的数量随着时间的推移,你正在冒很大的赌注,使用不受主要供应商支持的东西,以我的拙见。
I see most of the answers are about Qt somehow, even though the original question doesn't mention it. Here's my answer in terms of OpenGL alone (which also benefits greatly from the passage of time, as it could not have been given in 2010):
Since 2011, the state of the art is Mark Kilgard's baby, NV_path_rendering, which is currently only a vendor (Nvidia) extension as you might have guessed already from its name. There are a lot of materials on that:
NV_path_rendering is now used by Google's Skia library behind the scenes, when available. (Nvidia contributed the code in late 2013 and 2014.)
You can of course load SVGs and such https://www.youtube.com/watch?v=bCrohG6PJQE. They also support the PostScript syntax for paths. You can also mix path rendering with other OpenGL (3D) stuff, as demoed at:
An upstart having even less (or downright no) vendor support or academic glitz is NanoVG, which is currently developed and maintained. (https://github.com/memononen/nanovg) Given the number of 2D libraries over OpenGL that have come and gone over time, you're taking a big bet using something not supported by a major vendor, in my humble opinion.
让我扩展格雷格的答案。
确实,Qt 有一个 SVG 渲染器类,QSvgRenderer。此外,您在 Qt 中进行的任何绘图都可以在任何“QPaintDevice”上完成,我们对以下“绘图设备”感兴趣
因此,如果您决定使用 Qt,您的选择是:
想知道 QGLWidget 到底做了什么?好吧,当您向 QGLWidget 发出 Qt 渲染命令时,它们会被转换为 GL 调用。当 SVG 渲染器发出渲染命令时也会发生这种情况。所以最终,您的 SVG 将通过一堆 GL 图元(直线、多边形等)进行渲染。
这有一个缺点。不同的显卡实现 OpenGL 的方式略有不同,Qt 没有(也不能)解释所有这些差异。因此,举例来说,如果您的用户拥有便宜的板载 Intel 显卡,那么他的显卡不支持 OpenGL 抗锯齿,这意味着如果您将 SVG 直接渲染到 QGLWidget,您的 SVG 也会出现锯齿(锯齿状)。通过 QImage 可以避免此类问题。
当您实时缩放时,您也可以使用 QImage 方法。这仅取决于您需要的速度。您可能需要仔细的优化,例如重复使用相同的 QImage,并为您的 QPainter 启用剪辑。
Let me expand on Greg's answer.
It's true that Qt has a SVG renderer class, QSvgRenderer. Also, any drawing that you do in Qt can be done on any "QPaintDevice", where we're interested in the following "paint devices":
So, if you decide to use Qt, your options are:
Wondering what QGLWidget does exactly? Well, when you issue Qt rendering commands to a QGLWidget, they're translated to GL calls for you. And this also happens when the rendering commands are issued by the SVG renderer. So in the end, your SVG is going to end up being rendered via a bunch of GL primitives (lines, polygons, etc).
This has a disadvantage. Different videocards implement OpenGL slightly differently, and Qt does not (and can not) account for all those differences. So, for example, if your user has a cheap on-board Intel videocard, then his videocard doesn't support OpenGL antialiasing, and this means your SVG will also look aliased (jaggy), if you render it directly to a QGLWidget. Going through a QImage avoids such problems.
You can use the QImage method when you're zooming in realtime, too. It just depends on how fast you need it to be. You may need careful optimizations such as reusing the same QImage, and enabling clipping for your QPainter.
Qt 很好地支持使用 OpenGL 功能直接渲染 SVG 图像(请参阅 QSvgRenderer)。
我希望这有帮助。
Qt has good support for directly rendering SVG images using OpenGL functionality (see the documentation for QSvgRenderer).
I hope that helps.
它有 GL_LINES 和 GL_LINE_STRIP 等原语,用于在空间中绘制线条(如果这就是您的意思)。编辑:该网站有一些信息: http://www.falloutsoftware.com/tutorials/gl /gl2p5.htm
It has primitives like GL_LINES and GL_LINE_STRIP for drawing lines in space if that's what you mean. Edit: This site has some information: http://www.falloutsoftware.com/tutorials/gl/gl2p5.htm