Home > Enterprise >  Segmentation fault when running my QOpenGLWidget implementation
Segmentation fault when running my QOpenGLWidget implementation

Time:10-23

I'm trying to implement a minimum viable example for running a OpenGL2.1 compatible pipeline with a QOpenGLWidget. In this example, I'm trying to create a plane that is parallel to the xy-plane in view-space (with an orthographic projection matrix). I also want to use the color output variable of the vertex shader in the fragment shader.

When I run my code I get a Segfault when calling glDrawElements. I'm not an OpenGL expert and always struggle setting it up properly so maybe someone can tell me what I'm missing here.

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)

project(OpenGLExample)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

find_package(Qt6 REQUIRED COMPONENTS Widgets OpenGLWidgets)
set(CMAKE_AUTOMOC ON)

add_executable(MainApp main.cpp)
target_link_libraries(MainApp PRIVATE Qt6::Widgets Qt6::OpenGLWidgets)

main.cpp

#include <QApplication>
#include <QOpenGLWidget>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLBuffer>
#include <QOpenGLShader>
#include <QOpenGLShaderProgram>
#include <QVector3D>
#include <QOpenGLContext>

namespace {
    const QString vertexShader = ( "#version 120\n"
                                   "attribute vec3 pos;\n"
                                   "varying vec3 color;\n"
                                   "uniform mat4 mvp_mat;\n"
                                   "void main(){\n"
                                   "    gl_Position = mvp_mat * vec4(pos, 1);\n"
                                   "    color = (vec3(pos.x, pos.y, 0)   1) / 2;\n"
                                   "}" );
    const QString fragmentShader = ( "#version 120\n"
                                     "varying vec3 color;\n"
                                     "void main() {\n"
                                     "    gl_FragColor = vec4(color, 1);\n"
                                     "}" );
}

class OpenGLTestWidget: public QOpenGLWidget {
    Q_OBJECT

public:
    OpenGLTestWidget(QWidget* parent = nullptr)
        : QOpenGLWidget(parent)
        , vertexData({QVector3D(-0.5f, -0.5f, -0.5f), 
                      QVector3D(0.5f, -0.5f, -0.5f), 
                      QVector3D(0.5f, 0.5f, -0.5f), 
                      QVector3D(-0.5f, 0.5f, -0.5f)})
    {
        modelMatrix.setToIdentity();
        viewMatrix.setToIdentity();
        projectionMatrix.setToIdentity();
        projectionMatrix.ortho(-1.0, 1.0, -1.0, 1.0, 0, 1);
    }

    virtual ~OpenGLTestWidget() {
        cleanup();
    }
protected:
    void initializeGL() override {
        vao.create(); // added in edit
        vao.bind(); // added in edit

        vbo.create();
        vbo.bind();
        vbo.allocate(vertexData.data(), sizeof(QVector3D) * vertexData.size());

        program = new QOpenGLShaderProgram(this);
        program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShader);
        program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShader);
        program->link();
        program->bind();

        glEnable(GL_DEPTH_TEST);
        glEnable(GL_CULL_FACE);
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

        connect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &OpenGLTestWidget::cleanup);
    }

    void paintGL() override {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        const QMatrix4x4 mpvMatrix = projectionMatrix * viewMatrix * modelMatrix;
        program->setUniformValue("mpv_mat", mpvMatrix);

        const int vertexLocation = program->attributeLocation("pos");
        program->enableAttributeArray(vertexLocation);
        program->setAttributeBuffer(vertexLocation, GL_FLOAT, 0, 3, sizeof(QVector3D));

        // glDrawElements(GL_TRIANGLE_FAN, vertexData.size(), GL_UNSIGNED_SHORT, nullptr); // <-- ERROR
        glDrawArrays(GL_TRIANGLE_FAN, 0, vertexData.size()); // <-- no segfault but nothing renders
    }

private:
    QOpenGLVertexArrayObject vao; // added in edit
    QOpenGLBuffer vbo;
    QOpenGLShaderProgram* program;
    std::array<QVector3D, 4> vertexData;
    QMatrix4x4 modelMatrix;
    QMatrix4x4 viewMatrix;
    QMatrix4x4 projectionMatrix;

    void cleanup(){
        makeCurrent();
        delete program;
        vbo.destroy();
        vao.destroy(); // added in edit
        doneCurrent();
        disconnect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &OpenGLTestWidget::cleanup);
    }
};

#include "main.moc"

int main(int argc, char* argv[]) {
    QApplication a(argc, argv);
    OpenGLTestWidget test;
    test.show();
    return a.exec();
}

CodePudding user response:

  1. The Vertex Array Object is missing. Add the member variable

    QOpenGLVertexArrayObject vao;
    

    and call

    vao.create();
    vao.bind();
    

    before binding the VBO.

  2. The uniform names for the model-view-projection matrix do not match.

    program->setUniformValue("mpv_mat", mpvMatrix);
    

    needs to be

    program->setUniformValue("mvp_mat", mpvMatrix);
    
  3. The draw call glDrawElements is for indexed rendering. Instead, use

    glDrawArrays(GL_TRIANGLE_FAN, 0, vertexData.size());
    

Unsolicited advise: You may want to set up a QOpenGLDebugLogger. In this case, it wouldn't have mattered that much, but it can be useful in tracking down these kind of bugs.

  • Related