Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. OpenGL where to start?
Forum Updated to NodeBB v4.3 + New Features

OpenGL where to start?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 5 Posters 638 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    idlefrog
    wrote on 15 Aug 2021, 07:29 last edited by
    #1

    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!

    R 1 Reply Last reply 15 Aug 2021, 09:19
    0
    • I idlefrog
      15 Aug 2021, 07:29

      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!

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 15 Aug 2021, 09:19 last edited by raven-worx
      #2

      @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.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      3
      • S Offline
        S Offline
        SimonSchroeder
        wrote on 16 Aug 2021, 09:30 last edited by
        #3

        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.

        1 Reply Last reply
        1
        • J Offline
          J Offline
          JoeCFD
          wrote on 16 Aug 2021, 17:31 last edited by
          #4

          https://doc.qt.io/qt-5/examples-widgets-opengl.html
          Qt OpenGL examples are here.

          1 Reply Last reply
          1
          • 8 Offline
            8 Offline
            8Observer8
            wrote on 5 Dec 2021, 00:31 last edited by
            #5

            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();
            }
            
            1 Reply Last reply
            0
            • 8 Offline
              8 Offline
              8Observer8
              wrote on 5 Dec 2021, 00:39 last edited by
              #6

              Some examples: https://forum.qt.io/post/649720

              1 Reply Last reply
              0
              • 8 Offline
                8 Offline
                8Observer8
                wrote on 5 Dec 2021, 05:39 last edited by
                #7

                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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved