28

I have some simple SVG artwork (icon and glyph kind of things) which I want to display in an OpenGL app (developing in C++ on Debian, using Qt).

The obvious solution is to use the ImageMagick libs to convert the SVGs to raster images and texture map them onto some suitable polygons (or just use good old glDrawPixels).

However, I'm wondering if there's anything out there which will translate the SVG directly to a sequence of OpenGL calls and render it using OpenGL's lines, polygons and the like. Anyone know of anything which can do this ?

Community
  • 1
  • 1
timday
  • 24,582
  • 12
  • 83
  • 135
  • 2
    hmm.. tricky problem when bezier curves are involved. would be nice to see a solution as the two technologies complement each other nicely. – SpliFF Jun 22 '09 at 13:25
  • 1
    This question has been asked again at http://stackoverflow.com/questions/6287650/rendering-svg-with-opengl-and-opengl-es/ – Fizz Aug 06 '14 at 15:21
  • there's also tkzinc as a possibility – chris Oct 19 '10 at 05:21

5 Answers5

13

Qt can do this.
QSvgRenderer can take an SVG and paint it over a QGLWidget
Its possibly you'll need to fiddle around with the paintEvent() abit if you want to draw anything else on the QGLWidget other than the SVG.

feedc0de
  • 3,646
  • 8
  • 30
  • 55
shoosh
  • 76,898
  • 55
  • 205
  • 325
  • Thanks, very interesting (should have noticed that one myself). It's unclear to me from the documentation whether the vector aspect would be preserved completely in QPainter-on-QGLWidget or whether some intermediate rasterization happens. Ought to be easy enough to find out using GLintercept/GLtrace though... – timday Jun 22 '09 at 22:10
  • QPainter doesn't usually do any rasterization. There's no reason for it to start doing it in the case of SVG. – shoosh Jun 22 '09 at 23:39
  • After getting GLTrace to work with Qt4 apps on my platform (simple fix, sent to author), I can confirm the SVG is being drawn on the GLWidget using an impressive number of gl*calls. Trolltech provide a nice example of combining 2D & 3D worlds at http://doc.trolltech.com/4.4/opengl-overpainting.html. The only thing I can imagine wanting to do further would be capturing the SVG render calls in a GL display list (although the only reason to do that would be performance, and if that became a problem I'd be more likely to cache rendered bitmaps instead). – timday Jul 04 '09 at 12:36
  • I'd send a feature request for that using the bug report web form anyway. – shoosh Jul 04 '09 at 18:22
  • I also note that enabling automatic multisampling in the Nvidia drivers also makes the SVG graphics look very nice and antialiased (which wouldn't happen if Qt was just blitting some software-rendered bitmap). – timday Jul 05 '09 at 09:58
5

SVGL appears to address this but has been dormant for several years. Still you might be able to find some code of value there.

danio
  • 8,548
  • 6
  • 47
  • 55
  • The "sauvage" project mentioned by "dru" below appears to be the successor. Same author, but python rather than C++. – timday Jun 22 '09 at 17:10
2

It looks like Inkscape has some interesting export options you may find useful. They include DXF, PovRay, EPS, PS (PostScript), XAML, Latex and OpenDocument Drawing (ODG). Perhaps there is a converter for one of those and you could use Inkscape as an intermediary.

DXF in particular is a likely candidate since it is a common 3D format already. assimp is a good candidate for loading DXF.

SpliFF
  • 38,186
  • 16
  • 91
  • 120
  • Inkscape doesn't have yet a full implementation of SVG so I would imagine that its export options are also partial. – shoosh Jun 22 '09 at 16:38
  • Well actually all the SVG I'm using is created in Inkscape anyway, so as long as it can export all it's own stuff... no problem. – timday Jun 22 '09 at 22:18
  • 1
    You're going to more limited by the output format than SVG anyway. DXF is like a million years old, it isn't going to support crop marks or complex gradients. – SpliFF Jun 23 '09 at 01:53
2

My answer is going to about displaying vector graphics wtih OpenGL in general, because all solutions for this problem can support rather trivially SVG in particular, although none support animated SVGs (SMIL). Since there was nothing said about animation, I assume the question implied static SVGs only.

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:

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:

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.) One of the cairo devs (who is an Intel employee as well) seems to like it too http://lists.cairographics.org/archives/cairo/2013-March/024134.html, although I'm not [yet] aware of any concrete efforts for cairo to use NV_path_rendering.

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.

Fizz
  • 4,782
  • 1
  • 24
  • 51
1

I made a vector drawing program called Omber that has some ability to import SVG files, and it can export vector drawings to a "flattened" 3d model in glTF 2.0 format. The glTF format is pretty much just raw OpenGL data, so you can read it in and display it with minimal code. I wrote a little Pixi.js plugin for displaying glTF files containing vector art using WebGL.

Blender also has support for importing SVG files. You can then export the result to a 3d file format, but the Blender support for SVG is intended more for importing simple curves rather than complex drawings.

I'm sure there are other tools that can do stuff like this as well. One issue is that SVG predates modern graphics hardware, so a lot of its features are a real pain to implement with OpenGL, especially in an efficient way. Things like opacity on groups, gradients of irregular shapes, raster-based filter operations, etc. can get messy, so I'm not sure if the more esoteric features of SVG are generally supported by programs that can convert things to 3d formats.

Ming-Yee Iu
  • 844
  • 6
  • 5