Texturing in OpenGL
OpenGL allows us to map an image (also referred to as a texture) to a 3D shape or polygon. This process is also called texture mapping. Qt appears to be the best combination with OpenGL in this case because it provides an easy way to load images that belong to one of the common formats (BMP, JPEG, PNG, TARGA, TIFF, and so on) and you don't have to implement it by yourself. We will use the previous example with a spinning cube and try to map it with a texture!
How to do it…
First of all, open up
mainwindow.h
and add the following header to it:#include <QGLWidget>
Next, declare an array that stores the texture IDs created by OpenGL. We will be using it later when it comes to rendering:
private: QOpenGLContext* context; QOpenGLFunctions* openGLFunctions; float rotation; GLuint texID[1];
After that, open up
mainwindow.cpp
and add the following code toinitializeGL()
to load the texture file:void MainWindow::initializeGL() { // Enable Z-buffer depth test glEnable...