Introduction-to-Qt3D

From Qt Wiki
Revision as of 16:00, 14 January 2015 by Maintenance script (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Introduction to Qt 3D

NOTE: Qt 3D (along with Qt Quick 3D project) is undergoing heavy development. They are not yet part of Qt 5, and the API shown in this article may be subject to changes.

WARNING: The contents of this article were last updated in 2012. This article is heavily outdated.

Qt 3D is a set of C++ APIs for 3D programming built on top of Qt OpenGL. The newest project is called Qt Quick 3D which creates QML bindings to Qt 3D.

First, we’ll make a comparison of the previous OpenGL APIs: GLUT and Qt OpenGL.

GLUT (OpenGL Utility Toolkit)

GLUT was one the first (if not the first) toolkit to provide a portable API to handle window management for OpenGL programs. It uses callbacks to register drawing functions and more. There were also routines provided for drawing geometric primitives (both in solid and wireframe state) such as cubes and spheres.

A simple OpenGL program with just a 3D cube and some lighting could be as long as 140 lines of code when developed with GLUT.

3D Hello World Cube
3D cube drawn using OpenGL.

The code above follows a very basic OpenGL program structure. It has a initialize function (for doing some initial setup), a resize function (to handle window resizes) and a paint function (to draw the actual 3D objects).

Qt OpenGL

Qt OpenGL is a port of the OpenGL API to the Qt toolkit. It does a fairly good job at translating all of OpenGL basic functions
to a Qt widget. Now, instead of several functions being registered for callbacks, it is possible to take advantage of the SIGNAL/SLOT system of Qt.

One of the most commonly used approches for Qt OpenGL to write a 3D program is to subclass QGLWidget. QGLWidget provides three convenience methods that you can reimplement to perform the typical OpenGL tasks: paintGL, resizeGL and initializeGL (similar to the GLUT version of the program).

The program below follows the same basic structure of its GLUT previous version.

This version is still about 140 lines of code. Except that now we have the convenience of using it within a Qt class. That means, the drawing of the 3D objects can still be done with OpenGL standard functions, while leaving the window management and input handling part to the Qt API.

Using Qt 3D

Qt 3D was created to simplify the usage of the OpenGL standard API within a Qt application. It abstracts most of the setup previously required. Camera position, viewing volume, vertices definition and other initial settings become much simpler. Therefore, reduces the overall code needed for creating a basic 3D program.

Similar to QGLWidget, in Qt 3D there’s a class named QGLView which does most of the work regarding this initial settings. All we need to do is subclass it.

The code now looks easier to understand than its previous versions. There was no need for setting the cube’s vertices, no aspect ratio calculation, no lighting normals set. The real work is done by the QGLBuilder class and QGLPainter class. The first one (QGLBuilder) creates the geometry of the cube and adds it to the scene, while the second (QGLPainter) does the actual drawing of the cube.

The basic structure of the program is still there with the paintGL and initializeGL methods. There are also three more classes that handle typical OpenGL tasks: QGLCamera, QGLLightParameters and QGLMaterial.

References

The source code used in this article was based on examples from the following references:
OpenGL Programming Guide [glprogramming.com]
GLUT [opengl.org]
Qt OpenGL [qt.io]
Qt 3D [doc.qt.nokia.com]

Qt 3D latest snapshot can be obtained here: Qt Quick 3D [doc.qt.nokia.com]
Branch qt4 at the commit d5ee2 was used for the example on this article.

Categories: