【问题标题】:Qt OpenGL Segmentation FaultQt OpenGL 分段错误
【发布时间】:2018-09-14 03:04:45
【问题描述】:

我修改了网站 (http://doc.qt.io/qt-5/qtgui-openglwindow-example.html) 中的示例以使用 3D 模型,并注意到当我从终端(没有调试器)执行我的程序时,我的程序崩溃了。当我在调试模式下从 Qt Creator 执行它时,它永远不会崩溃,这就是我现在注意到的原因。我很难弄清楚这一点。我要渲染的对象大约有 4k 个顶点。

代码(还有一些关于 arduino 读数的附加代码,但肯定不是错误的原因):

#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();
}

我认为问题出在

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

或者我的对象太大了。

编辑:所有顶点数据都是静态分配的。

【问题讨论】:

  • 如果应用程序在调试模式下没有崩溃,那么您可能有一些无效的指针或数据。尝试评论一些行并添加一些std::cout 信息以获取罪魁祸首。

标签: c++ qt opengl qtgui


【解决方案1】:

既然你提到了终端,我假设你在 Linux 上运行。

请注意,QtCreator 不会简单地使用 gdb 运行您的程序。它还设置了running environment,确保正确设置LD_LIBRARY_PATH,让您的应用程序找到Qt 动态库。

如果您的程序在启动时立即崩溃,可能只是因为它没有使用正确的 Qt 库。想象一下,您通过发行包在您的系统上安装了 Qt 5.2,但您正在使用 Qt SDK 5.9 进行开发。当你用 QtCreator 运行你的软件时,它会设置 LD_LIBRARY_PATH 让你的软件使用 Qt 5.9。但是当你从终端运行它时,它会使用来自 /usr/lib 的 Qt 5.2,它不是二进制兼容的,因此会导致你的程序崩溃。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-15
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多