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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.