Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Rendering issue in QOpenGLWindow



  • Hello everyone!
    I'm pretty new to Qt, so I hope, I posted this to the right subforum.
    I am trying to create an application with OpenGL Rendering, so I decided to use QOpenGLWindow for the job. However, I could barely find any tutorials on this, which show a simple example. Additionally, I'm not too familiar with OpenGL, so obviously, things don't work as expected.

    I tried to create a simple triangle in the viewport, however it seems that the vertex shader doesn't receive the coordinate data. When I try to render the data as a triangle, I see nothing. When I try to render it as GL_POINTS instead, I see a single pixel at the origin. If I add a constant to the coordinates in the vertex shader, I can move that pixel, so obviously I messed up the data pipeline from the vertex buffer to the shader and only zeroes arrive.

    I would appreciate any hints on what might have gone wrong with the way I set up the GL state.

    main.cpp:

    #include <QApplication>
    
    #include "Window.h"
    
    int main(int argc, char* argv[]) {
      QApplication app{argc, argv};
      
      QSurfaceFormat format;
      format.setDepthBufferSize(4);
      format.setSamples(24);
      format.setVersion(3, 1);
      format.setRenderableType(QSurfaceFormat::OpenGL);
      
      Window w;
      w.show();
      
      return app.exec();
    }
    

    Window.h:

    #ifndef QTGL__WINDOW_H
    #define QTGL__WINDOW_H
    
    #include <QOpenGLFunctions>
    #include <QOpenGLWindow>
    #include <QOpenGLBuffer>
    #include <QOpenGLShaderProgram>
    #include <QOpenGLVertexArrayObject>
    
    class Window : public QOpenGLWindow, protected QOpenGLFunctions {
    public:
      Window();
      ~Window();
    
    protected:
      void initializeGL() override;
      void resizeGL(int w, int h) override;
      void paintGL() override;
      
    private:
      QOpenGLVertexArrayObject vao_;
      QOpenGLBuffer vertices_;
      QOpenGLShaderProgram shader_;
    };
    
    #endif // QTGL__WINDOW_H
    
    

    Window.cpp:

    #include "Window.h"
    
    #include <QVector2D>
    
    const char *vertexShader = //
        "#version 130\n"
        "in vec2 pos;\n"
        "void main() {\n"
        "gl_Position = vec4(pos, 0.0, 1.0);\n"
        "}";
    
    const char *fragmentShader = //
        "#version 130\n"
        "out vec4 FragColor;\n"
        "void main() {\n"
        "FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
        "}\n";
    
    Window::Window()
        : vao_{this}, vertices_{QOpenGLBuffer::VertexBuffer}, shader_{this} {}
    
    Window::~Window() {
      vao_.destroy();
      vertices_.destroy();
    }
    
    void Window::initializeGL() {
      initializeOpenGLFunctions();
      glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
      
      shader_.addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShader);
      shader_.addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShader);
      shader_.link();
      shader_.bind();
      
      vao_.create();
      vao_.bind();
      
      QVector2D vertices[] = {{-1.0, -1.0}, //
                              {1.0, -1.0},  //
                              {0.0, 1.0}};
    
      vertices_.create();
      vertices_.setUsagePattern(QOpenGLBuffer::StaticDraw);
      vertices_.allocate(vertices, sizeof(vertices));
      vertices_.bind();
      shader_.enableAttributeArray("pos");
      shader_.setAttributeBuffer("pos", GL_FLOAT, 0, 2);
      
      vertices_.release();
      vao_.release();
      shader_.release();
    }
    
    void Window::resizeGL(int w, int h) { glViewport(0, 0, w, h); }
    
    void Window::paintGL() {
      glClear(GL_COLOR_BUFFER_BIT);
      
      shader_.bind();
      vao_.bind();
    //  glDrawArrays(GL_POINTS, 0, 3); // renders a single red pixel at 0/0
      glDrawArrays(GL_TRIANGLES, 0, 3); // renders nothing at all
      vao_.release();
      shader_.release();
    }
    

    Thank you for your time!



  • Anyone? Or could you maybe point me to a proper minimal (!) example using QOpenGLWidget and the QOpenGL helper classes?



  • @Julius-Aetherwing
    Sorry, I'm not familiar with OpenGL, too.
    But you can find some example code in Qt install dir, something like "C:\Qt\Examples\Qt-5.12.6\opengl"
    The docs of the examples can be found in All Qt Examples


Log in to reply