Render 3D shapes
We have learned how to draw simple 2D shapes onscreen in the previous section. However, to fully utilize the OpenGL API, we also need to learn how to use it to render 3D images. In a nutshell, 3D images are simply an illusion created using 2D shapes stacked in a way that makes them look like 3D.
The main ingredient here is the depth value, which determines which shapes should appear in front or at the back of the other shapes. The primitive shape that is positioned behind another surface (with a shallower depth than another shape) will not be rendered (or partially rendered). OpenGL provides a simple way to achieve this, without too much technical hassle.
How to do it…
First, add the
QTimer
header to yourmainwindow.h
:#include <QTimer>
Then, add a private variable to your
MainWindow
class:private: QOpenGLContext* context; QOpenGLFunctions* openGLFunctions; float rotation;
We also add a public slot to
mainwindow.h
for later use:public slots: void updateAnimation...