OpenGL frame timer with Qt

To compare impacts of different optimizations in terms of rendering duration, it can be useful to implement a frame timer.  A frame timer counts the amount of time that elapsed during a paint() call. Since version 3.3 OpenGL provides the  GL_TIME_ELAPSED  query which returns the time between to calls in nanoseconds. The code below shows a very basic example in plain C/OpenGL.

Since the availability of those queries depends on the underlying platform, hardware and OpenGL version, Qt provides QOpenGLTimerQuery as a wrapper class for that functionality. The same code for the use within a QGLWidget can be seen in the following snippet.

 

This post is part of a series documenting the progress of the development of a OpenGL/Qt/C++ based application for my fast realistic rendering course at the Universitat Politècnica de Catalunya. The complete source code can be found on github.

References

Calculating a bounding box

In order to implement some visibility algorithms later on, it is useful to calculate a more simple representation of the meshes. The most basic way  to do this, is to define a box with the size of the actual mesh’s maximum dimensions. Also, for a first try, the bounding box will be axis-aligned which makes it easier to compute.

Iterating over all vertices of the mesh allows to retrieve the minimum and maximum dimensions on all three axes. In the following code snippet the vertices are stored in a QVector<QVector3D>. The x, y and z components can be accessed via the corresponding methods x(), y() and z().

The retrieved dimensions can be used to calculate a bounding box for various visibility algorithms. To render the bounding box over the object in the actual scene, the glBegin(GL_LINES) command comes in handy. The next code snippet shows how to render the four lines in the front (z+).

In the end, the result will look something like this when applied on a capsule-shaped mesh:

Axis-aligned bounding box of a capsule
Axis-aligned bounding box of a capsule

This post is part of a series documenting the progress of the development of a OpenGL/Qt/C++ based application for my fast realistic rendering course at the Universitat Politècnica de Catalunya. The complete source code can be found on github.