OpenGL questions



  • Hi,

    i'm working on an OpenGL project and i was already finished with most of the OpenGL things, but i noticed that my old libraries (glew, glfw,..) were very limited about the Window itself.
    So i switched to Qt, since i already worked a lot of times with this library. So my aim is to port my old project over to a new one with Qt.
    Here is my old project: https://git.rwth-aachen.de/carstenf/OpenGL/tree/master/MshViewer

    If you wanna test it, you can get a finished build here: https://git.rwth-aachen.de/carstenf/OpenGL/tree/master/Release

    My Qt project is located in the same repository here: https://git.rwth-aachen.de/carstenf/OpenGL/tree/master/MeshViewerQt

    Is there any tutorial about how to use OpenGL with Qt the best way?? Since it looks very different from the OpenGL i used before.



  • Hi! I assume we're talking about a GUI using QWidgets. In this case it's pretty simple: You create a custom widget that inherits from QOpenGLWidget and QOpenGLFunctions, and you need to override a handful of functions. You can then add instances of your custom widget to mainwindow etc.

    mywidget.h

    #ifndef MYWIDGET_H
    #define MYWIDGET_H
    
    #include <QOpenGLWidget>
    #include <QOpenGLFunctions>
    
    class MyWidget: public QOpenGLWidget, protected QOpenGLFunctions
    {
        Q_OBJECT
    public:
        explicit MyWidget(QWidget *parent = nullptr);
    
    protected:
        void initializeGL() override;
        void resizeGL(int w, int h) override;
        void paintGL() override;
    };
    
    #endif // MYWIDGET_H
    

    mywidget.cpp

    #include "mywidget.h"
    
    MyWidget::MyWidget(QWidget *parent)
        : QOpenGLWidget(parent)
    {
    }
    
    void MyWidget::initializeGL()
    {
        initializeOpenGLFunctions();
        glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
    }
    
    void MyWidget::resizeGL(int w, int h)
    {
        Q_UNUSED(w)
        Q_UNUSED(h)
    }
    
    void MyWidget::paintGL()
    {
        glClear(GL_COLOR_BUFFER_BIT);
    }
    

    In your .pro file don't forget to add opengl:

    QT       += core gui opengl
    

    Placed on a QMainWindow together with a layout and a QPushButton the result looks like this:



  • Also gltriangle example can help.



  • Thank you :D i found this tutorial: http://www.trentreed.net/blog/qt5-opengl-part-2-3d-rendering/
    Currently i'm reading one chapter after the other and implement it for my widget instead of window. Looks similar for me.

    I used this tutorial first: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-1-opening-a-window/ and the steps seam to be the same, but Qt has different function names, but for me it's easier to have one powerful library that can do everything instead of multiple different libraries (glew, glfw, glut, glm,...).


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.