Hello world!
In this recipe, we will learn about the pipeline of OpenGL and how to render a simple shape to the window. We will continue from the example project used in the previous recipe.
How to do it…
- First of all, go to
mainwindow.h
and add the following headers at the top of the source code:#include <QSurfaceFormat> #include <QOpenGLFunctions> #include <QtOpenGL> #include <GL/glu.h>
- Next, declare two private variables in
mainwindow.h
:private: QOpenGLContext* context; QOpenGLFunctions* openGLFunctions;
- After that, move over to
mainwindow.cpp
and set the surface format to compatibility profile. We also set the OpenGL version to 2.1 and create the OpenGL context using the format we just declared. Then, use the context we just created to access the OpenGL functions that are relevant only to the OpenGL version we have just set, by callingcontext->functions()
:MainWindow::MainWindow(QWidget *parent) { setSurfaceType(QWindow::OpenGLSurface); QSurfaceFormat...