gpt4 book ai didi

c++ - Qt OpenGL 段错误

转载 作者:行者123 更新时间:2023-11-28 04:44:00 25 4
gpt4 key购买 nike

我修改了网站 ( http://doc.qt.io/qt-5/qtgui-openglwindow-example.html) 中的示例以使用 3D 模型,并注意到当我从终端(没有调试器)执行我的程序时,我的程序崩溃了。当我在 Debug模式下从 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());

或者我的对象太大了。

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

最佳答案

既然你提到了终端,我假设你在 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,它不是二进制兼容的,因此会使您的程序崩溃。

关于c++ - Qt OpenGL 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49652917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com