Jaav
Jaav

Reputation: 37

Qt OpenGL Segmentation Fault

I modified example from website (http://doc.qt.io/qt-5/qtgui-openglwindow-example.html) to work with 3D models and noticed that when I execute my program from terminal (without debugger) my program crushes. When I execute it from Qt Creator in debug mode it never crushes, and that's why I noticed that right now. I'm having bad time figuring this out. Object I'm trying to render is about 4k vertices.

Code (there is some additional code about arduino readings, but it's not error's cause for sure):

#include "h/modelwindow.h"

ModelWindow::ModelWindow(QString modelDestination) : m_program(Q_NULLPTR), angle(0.0f,0.0f,0.0f),
                                                     distance(0.0f,0.0f,0.0f)
{
    objectModel.LoadObject(modelDestination);
}

ModelWindow::~ModelWindow()
{
    delete m_program;
}

void ModelWindow::initialize()
{
    m_program = new QOpenGLShaderProgram(this);
    if(m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, QDir::currentPath()+"/shaders/basicshader.vert"))
    {
        qDebug() << "Vertex shader loaded";
    }
    else
    {
        close();
        qDebug() << "Unable to load vertex shader";
    }
    if(m_program->addShaderFromSourceFile(QOpenGLShader::Fragment,QDir::currentPath()+"/shaders/basicshader.frag"))
    {
        qDebug() << "Fragment shader loaded";
    }
    else
    {
        close();
        qDebug() << "Unable to load vertex fragment";
    }

    m_program->link();
    m_program->bind();

    m_positionAttribute = m_program->attributeLocation("posAttr");
    m_colorAttribute=m_program->attributeLocation("colAttr");
    m_matrixUniform = m_program->uniformLocation("matrix");
}

void ModelWindow::render()
{
    arduino.readLine();

    //arduino delay in seconds
    float aDelay = 0.1f;
    float threshold = 0.1f;
    float threshold2 = 0.001f;

    QVector3D temp = arduino.gyro()* aDelay;
    QVector3D temp2 = arduino.acc()* aDelay *aDelay * 10;

    if(temp.x() > threshold || temp.x() < -threshold)
    {
        angle.setX(angle.x()+temp.x());
    }
    if(temp.y() > threshold || temp.y() < -threshold)
    {
        angle.setY(angle.y()+temp.y());
    }
    if(temp.z() > threshold || temp.z() < -threshold)
    {
        angle.setZ(angle.z()+temp.z());
    }
    if(temp2.x() > threshold2 || temp2.x() < -threshold2)
    {
        distance.setX(distance.x()+temp2.x());
    }
    if(temp2.y() > threshold2 || temp2.y() < -threshold2)
    {
        distance.setY(distance.y()+temp2.y());
    }
    if(temp2.z() > threshold2 || temp2.z() < -threshold2)
    {
        distance.setZ(distance.z()+temp2.z());
    }

    qDebug() << "Distance: " << distance.y() << " m " << distance.z() << " m " << -distance.x() << " m";
    qDebug() << "Angle: " << angle.y() << " deg " << angle.z() << " deg " << -angle.x() << " deg";

    glViewport(0, 0, width(), height());
    glClear(GL_COLOR_BUFFER_BIT);
    m_program->bind();

    QMatrix4x4 matrix;
    matrix.perspective(60.0f, 4.0f/3.0f, 0.1f, 100.0f);

    //axes
    //board | OpenGL
    //  x   |   -z
    //  y   |   x
    //  z   |   y

    //translations
    matrix.translate(0.0f,0.0f,-4.0f);

    matrix.translate(-distance.y(),0.0f,distance.x());

    //rotations
    matrix.rotate(angle.x(), 0, 0, -1);
    matrix.rotate(angle.y(), 1, 0, 0);
    matrix.rotate(angle.z(), 0, -1, 0);

    m_program->setUniformValue(m_matrixUniform, matrix);

    QVector <QVector3D> colors;

    for(int i=0; i<objectModel.vertices().size(); ++i)
    {
        QVector3D temp(0.75f, 0.75f, 0.75f);
        colors.push_back(temp);
    }

    glVertexAttribPointer(m_positionAttribute, 3, GL_FLOAT, GL_TRUE, sizeof(QVector3D), &objectModel.vertices()[0]);
    glVertexAttribPointer(m_colorAttribute,3, GL_FLOAT, GL_FALSE, sizeof(QVector3D), &colors[0]);

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    glDrawArrays(GL_TRIANGLES, 0, objectModel.vertices().size());

    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);

    m_program->release();
}

I think problem is in

 glDrawArrays(GL_TRIANGLES, 0, objectModel.vertices().size());

or my object is too big.

EDIT: All of Vertex Data is allocated statically.

Upvotes: 0

Views: 1134

Answers (1)

Sergio Monteleone
Sergio Monteleone

Reputation: 2886

Since you mentioned the terminal, I'm assuming you are running on Linux.

Please be aware that QtCreator does not simply run your program with gdb. It also set up the running environment, ensuring LD_LIBRARY_PATH is set correctly to let your application find the Qt dynamic libraries.

If your program crashes immediately at startup, it may just be that it is not using the correct Qt libraries. Imagine you have Qt 5.2 installed on your system via distro packages, but you are developing with Qt SDK 5.9. When you run your software with QtCreator, it will set LD_LIBRARY_PATH to let your software use Qt 5.9. But when you run it from the terminal, it will use Qt 5.2 from /usr/lib, which is not binary compatible, and thus, crashes your program.

Upvotes: 1

Related Questions