在 OpenGL 中渲染矢量图形? [英] Rendering Vector Graphics in OpenGL?

查看:48
本文介绍了在 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.

推荐答案

让我扩展一下 Greg 的回答.

Let me expand on Greg's answer.

Qt 确实有一个 SVG 渲染器类,QSvgRenderer.此外,您在 Qt 中所做的任何绘图都可以在任何QPaintDevice"上完成,我们对以下绘图设备"感兴趣:

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":

  • 一个 Qt 小部件;
  • 特别是基于 GL 的 Qt 小部件 (QGLWidget);
  • Qt 图像

因此,如果您决定使用 Qt,您的选择是:

So, if you decide to use Qt, your options are:

  1. 停止使用当前设置窗口(和 GL 上下文)的方法,并开始使用 QGLWidget 进行所有渲染,包括 SVG 渲染.这可能是一个很小的变化,具体取决于您的需要.QGLWidget 的功能没有特别限制.
  2. 使用 QSvgRenderer 渲染到 QImage,然后将来自该 QImage 的数据放入 GL 纹理(如您通常所做的那样),并以您想要的任何方式渲染(例如,放入矩形 GL_QUAD).性能可能比其他方法差,但对代码的改动最少.
  1. Stop using your current method of setting up the window (and GL context), and start using QGLWidget for all your rendering, including the SVG rendering. This might be a pretty small change, depending on your needs. QGLWidget isn't particularly limiting in its capabilities.
  2. Use QSvgRenderer to render to a QImage, then put the data from that QImage into a GL texture (as you normally would), and render it any way you want (e.g. into a rectangular GL_QUAD). Might have worse performance than the other method but requires the least change to your code.

想知道 QGLWidget 究竟做了什么?好吧,当您向 QGLWidget 发出 Qt 渲染命令时,它们会为您转换为 GL 调用.当 SVG 渲染器发出渲染命令时,也会发生这种情况.所以最后,您的 SVG 最终将通过一堆 GL 基元(线、多边形等)进行渲染.

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).

这有一个缺点.不同的显卡对 OpenGL 的实现略有不同,Qt 没有(也不能)解释所有这些差异.因此,例如,如果您的用户有一个便宜的板载 Intel 显卡,那么他的显卡不支持 OpenGL 抗锯齿,这意味着如果您直接将其渲染到 QGLWidget,您的 SVG 也会看起来有锯齿(锯齿状).通过 QImage 可以避免此类问题.

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.

当您进行实时缩放时,您也可以使用 QImage 方法.这仅取决于您需要多快.您可能需要仔细优化,例如重用相同的 QImage,并为 QPainter 启用剪辑.

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.

这篇关于在 OpenGL 中渲染矢量图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆