Development/Architecture/KDE3/OpenGL Support

3D graphics with OpenGL

The de facto standard for rendering 3D graphics today is OpenGL. Implementations of this specification come with Microsoft Windows, Mac OS X and XFree86 and often support the hardware acceleration features offered by modern graphics cards. OpenGL itself only deals with rendering on a specified area of the framebuffer through a GL context and does not have any interactions with the toolkit of the environment

Qt offers the widget QGLWidget which encapsulates a window with an associated GL context. Basically, you use it by subclassing it and reimplementing some methods.

  • Instead of reimplementing paintEvent() and using QPainter to draw the widget's contents, you override paintGL() and use GL commands to render a scene. QLWidget will take care of making its GL context the current one before paintGL() is called, and it will flush afterwards.
  • The virtual method initializeGL() is called once before the first time resizeGL() or paintGL() are called. This can be used to construct display lists for objects, and make any initializations.
  • Instead of reimplementing resizeEvent(), you override resizeGL(). This can be used to set the viewport appropriately.
  • Instead of calling update() when the state of the scene has changed - for example when you animate it with a timer -, you should call updateGL(). This will trigger a repaint.

In general, QGLWidget behaves just like any other widget, i.e. for example you can process mouse events as usual, resize the widget and combine it with others in a layout.

OpenGL screenshot

Qt contains some examples of QGLWidget usage in its demo example. A collection of tutorials can be found here, and more information and a reference of OpenGL is available on the OpenGL homepage.

High-level interfaces

OpenGL is a relatively low-level interface for drawing 3D graphics. In the same way QCanvas gives the programmer a higher-level interface which details with objects and their properties, there are also high-level interfaces for 3D graphics. One of the most popular is Open Inventor. Originally a technology developed by SGI, there is today also the open source implementation Coin, complemented by a toolkit binding to Qt called SoQt.

The basic concept of Open Inventor is that of a scene. A scene can be loaded from disk and saved in a special format closely related to VRML. A scene consists of a collection of objects called nodes. Inventor already provides a rich collection of reusable nodes, such as cubes, cylinders and meshes, furthermore light sources, materials, cameras etc. Nodes are represented by C++ classes and can be combined and subclassed.

An introduction to Inventor can be found here (in general, you can substitute all mentions of SoXt by SoQt in this article).

Initial Author: Bernd Gehrmann

This page was last edited on 11 March 2007, at 21:35. Content is available under Creative Commons License SA 4.0 unless otherwise noted.