【发布时间】: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信息以获取罪魁祸首。