Qt World Summit: Register Today!


OpenGL where to start?



  • A very general question about where one should start with learning OpenGL for Qt?
    What examples and or books should I try out first?
    Any recommendations are appreciated!


  • Moderators

    @idlefrog
    first you should learn OpenGL indepentendly from Qt to understand its concepts etc.
    Then you can check the scene graph and see how to use it in Qt.



  • I don't think there are any tutorials that start from the beginning explaining OpenGL and use Qt as a surrounding framework.

    If you don't know any OpenGL, learn it independently from Qt, e.g.:
    https://learnopengl.com/
    http://www.opengl-tutorial.org/
    https://open.gl/

    These will teach you the basics about OpenGL. If you know (and understand) OpenGL and just want to use it within Qt, then read the Qt documentation. Qt has an OpenGL widget, classes for framebuffers, textures, shaders, etc. These will make a lot of things easier as you don't have to deal with OpenGL directly. But this all assumes that you already know what the documentation is talking about. There's no explaination why and when you would use these.





  • This example draws a rectangle:

    44067304-d3fb-46d7-9a97-13a2227d3aac-image.png

    main.cpp

    // Add this line to .pro:
    // win32: LIBS += -lopengl32
    
    #ifdef _WIN32
    #include <windows.h>
    extern "C" __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
    extern "C" __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 0x00000001;
    #endif
    
    #include <QtWidgets/QApplication>
    #include <QtWidgets/QOpenGLWidget>
    #include <QtGui/QOpenGLShaderProgram>
    #include <QtGui/QOpenGLBuffer>
    #include <QtGui/QMatrix4x4>
    #include <QtGui/QSurfaceFormat>
    
    class Widget : public QOpenGLWidget
    {
        Q_OBJECT
    public:
        Widget()
        {
            setWindowTitle("Rectangle. Qt, OpenGL 3.3, C++");
            setFixedSize(QSize(400, 400));
            resize(400, 400);
        }
    
    private:
        QOpenGLShaderProgram m_program;
        QOpenGLBuffer m_vertPosBuffer;
        QOpenGLBuffer m_texCoordBuffer;
        QMatrix4x4 m_mvpMatrix;
        QMatrix4x4 m_projMatrix;
        QMatrix4x4 m_viewMatrix;
        QMatrix4x4 m_modelMatrix;
        int m_uMvpMatrixLocation;
    
        void initializeGL() override
        {
            glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
            glEnable(GL_DEPTH_TEST);
            const char *vertShaderSrc =
                    "#version 330\n"
                    "in vec3 aPosition;"
                    "uniform mat4 uMvpMatrix;"
                    "void main()"
                    "{"
                    "    gl_Position = uMvpMatrix * vec4(aPosition, 1.0);"
                    "}";
            const char *fragShaderSrc =
                    "#version 330\n"
                    "out vec4 fragColor;"
                    "void main()"
                    "{"
                    "    fragColor = vec4(0.9, 0.9, 1.0, 1.0);"
                    "}";
    
            m_program.addShaderFromSourceCode(QOpenGLShader::Vertex, vertShaderSrc);
            m_program.addShaderFromSourceCode(QOpenGLShader::Fragment, fragShaderSrc);
            m_program.link();
            m_program.bind();
    
            m_uMvpMatrixLocation = m_program.uniformLocation("uMvpMatrix");
    
            float vertPositions[] = {
                -0.5f, -0.5f, 0.f,
                0.5f, -0.5f, 0.f,
                -0.5f, 0.5f, 0.f,
                0.5f, 0.5f, 0.f
            };
            m_vertPosBuffer.create();
            m_vertPosBuffer.bind();
            m_vertPosBuffer.allocate(vertPositions, sizeof(vertPositions));
            m_program.bindAttributeLocation("aPosition", 0);
            m_program.setAttributeBuffer(0, GL_FLOAT, 0, 3);
            m_program.enableAttributeArray(0);
    
            m_viewMatrix.lookAt(QVector3D(0.f, 0.f, 50.f),
                                QVector3D(0.f, 0.f, 0.f),
                                QVector3D(0.f, 1.f, 0.f));
            m_modelMatrix.translate(QVector3D(0.f, 0.f, 0.f));
            m_modelMatrix.rotate(20.f, QVector3D(0.f, 0.f, 1.f));
            m_modelMatrix.scale(50.f, 70.f, 1.f);
        }
    
        void paintGL() override
        {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            m_mvpMatrix = m_projMatrix * m_viewMatrix * m_modelMatrix;
            m_program.bind();
            m_program.setUniformValue(m_uMvpMatrixLocation, m_mvpMatrix);
            glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
        }
    
        void resizeGL(int w, int h) override
        {
            glViewport(0, 0, w, h);
            m_projMatrix.setToIdentity();
            m_projMatrix.ortho(-100.f, 100.f, -100.f, 100.f, 100.f, -100.f);
        }
    };
    
    #include "main.moc"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QSurfaceFormat format;
        format.setSamples(8);
    
        Widget w;
        w.setFormat(format);
        QSurfaceFormat::setDefaultFormat(format);
        w.show();
        return a.exec();
    }
    




  • I used this lesson: https://learnopengl.com/Advanced-OpenGL/Stencil-testing to make this demo for windows in Qt OpenGL 3.3: SelectObjectByClick_OpenGL33_Qt5Cpp.zip

    alt text


Log in to reply