QT+OpenGL (1) initialization window and three important functions -by Zobol

2022-06-10 0 By

Preface to this chapter:This chapter explains how to use VS and QT to create a basic QOpenGLWidget window and the three core functions associated with it. Since version updates may be similar, the basic ones will not change. With QT’s help,We don’t need to download opengL, GLSL, cmake, just download a qt plugin and link it to VS. Note that when downloading QT, remember to follow the opengL module (although this module is free, but the qt website does not include the core three modules by default).The author:This tutorial is https://learnopengl-cn.github.io/ and I based on making the famous learnopengl teaching personal opengl opengl’s official website https://www.khronos.org/opengl to make the teaching notes.The reason why I choose Qt as auxiliary, because Qt integrated OpengL development, let us save a lot of original OpengL unnecessary compatible operations and processing, you can put more energy into the graphics development, personal ability is limited, if there is insufficient, please give me your advice.Development environment: OpengL + QT + VS2017 + Win10 (all source code is open source, and can be in my personal public number:Zobol magic library and personal blog https://github.com/zobolBlog/LearnOpenGLWithQT download) making video tutorial:Personal technology blog: https://www.bilibili.com/video/BV1X5411w7QV/ https://zobolblog.github.io/LearnOpenGLWithQT/Doc/01.html1.Create a new project and select GUI Application (the name of the new version is slightly changed, but just select the one with the GUI)2.Add the corresponding module OpengL, OpengL extension (new version separates debug and release, no matter) 3.Select QWidget as inheritance base class. This is qt’s official recommendation that QOpenGLWidget use QWidget as parent class.4. Create the QWidget project and check the project bar for any omissions.After compiling, if the window is displayed normally, it is normal.5. Replace the parent class by replacing the QWidget with the QOpenGLWidget with two sixes.Add corresponding header files, two.For the opengL version of the file, fill in at least after 3.3 (after this opengL version changes little).The QOpenGLFunctions_x_x_Core class is a wrapper around a version of OpenGL so that we can use the original functions. Otherwise, we would have to use another set of functions that are wrapped in Qt. We can also use them but not like learnopengL tutorials.Since they are all backward compatible, choose version 3.3 and your computer will do as well as it can.7. The three key functions, QOpenGLWidget, are made into protection functions that need to be inherited and re-implemented.8. Inherit in the source code, and implement.virtual void initializeGL();// Buffers objects vAO, vBO, eBO, shader, texture, camera.virtual void resizeGL(int w, int h);// Void paintGL() automatically calls virtual void paintGL();// Draw, render once.The update() function is added to the loop. 9. InitializeGL () is responsible for initialization, which buffers objects vAO, vBO, eBO, shader, texture, camera.This virtual function is called once before the first call to paintGL() or resizeGL(). Reimplement it in a subclass.This function should set up any required OpenGL resources and state. There is no need to call makeCurrent() because this has already been done when this function is called. Note however that the framebuffer is not yet available at this stage, So avoid charting draw calls from here. Defer such calls to paintGL() instead.This virtual function is called whenever the widget needs to be painted. Reimplement it in a subclass.There is noneed to call makeCurrent() because this has already been done when this function is called. Before invoking this function, the context and the framebuffer are bound, and the viewport is set up by a call to glViewport(). No other state is set and no clearing or drawing is performed by The framework.11.paintGL() function is rendered once.The update() function Sets the requested surface format.When the format is not explicitly set via this function,the format returned by QSurfaceFormat::defaultFormat() will be used. This means that when having multiple OpenGL widgets, individual calls to this function can be replaced by one single call to QSurfaceFormat::setDefaultFormat() before creating the first widget.12. I’m going to modify the contents of the paintGL function in the CPP file.13. As a result, seeing a black box is the correct QOpenGLWidget window.Helloopengl. h: #include #include “ui_helloopengl. h”#include #include class HelloOpenGL:public QOpenGLWidget{ Q_OBJECTpublic: HelloOpenGL(QWidget *parent = Q_NULLPTR);protected: virtual void initializeGL();// Buffers objects vAO, vBO, eBO, shader, texture, camera.virtual void resizeGL(int w, int h);// Void paintGL() automatically calls virtual void paintGL();// Draw, render once.Update () private: Ui::HelloOpenGLClass Ui;};CPP: #include “helloopengl.h “HelloOpenGL::HelloOpenGL(QWidget *parent) :QOpenGLWidget(parent){}void HelloOpenGL::initializeGL(){}void HelloOpenGL::resizeGL(int w, int h){}void HelloOpenGL::paintGL(){ update();}main.cpp:#include “HelloOpenGL.h”#include int main(int argc, char *argv[]){ QApplication a(argc, argv); HelloOpenGL w; w.show(); return a.exec();}