QT:glDrawElements()上的程序崩溃

时间:2016-08-11 18:55:02

标签: c++ qt opengl crash

我想用Assimp将模型加载到OpenGL中。

我使用QT作为我的框架,为我提供所需的功能。

Baiscally,我的程序在gl.glDrawElements()的Mesh :: DrawMesh崩溃......

我打赌这与我的一项拨款有关,但我不知道。

我确信模型已正确加载,因为我比较了加载的结果;)

所以在这里我发布了初始化函数,它基本上为该网格设置了缓冲区等。我想那里可能出现了问题:

void Mesh::initialize()
{

vao->create();
vbo->create();
ebo->create();

vao->bind();

    vbo->bind(); //glBindBuffer(GL_ARRAY_BUFFER, vbo); // Bind vbo
    vbo->setUsagePattern(QOpenGLBuffer::StaticDraw);
    vbo->allocate(vertices.data(),vertices.size() * sizeof(Vertex)); //glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW); //Allocates space in bytes


    ebo->bind(); //glBindBuffer(GL_ARRAY_BUFFER, ebo);
    ebo->setUsagePattern(QOpenGLBuffer::StaticDraw);
    ebo->allocate(indices.data(),indices.size()*sizeof(GLuint)); //glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), indices.data(), GL_STATIC_DRAW); //Allocates space in bytes

    program->enableAttributeArray(0); //glEnableVertexAttribArray(0); //On layout = 0
    program->setAttributeBuffer(0,GL_FLOAT,0,sizeof(Vertex));//glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,sizeof(Vertex),0); // Stride is sizeof(Vertex) ofc, offset is 0 because we want to access Position

    program->enableAttributeArray(1); //glEnableVertexAttribArray(1);
    program->setAttributeBuffer(1,GL_FLOAT,offsetof(Vertex,Normal),3,sizeof(Vertex)); //glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,sizeof(Vertex), (GLvoid*) offsetof(Vertex, Normal));

    program->enableAttributeArray(2);
    program->setAttributeBuffer(2,GL_FLOAT,offsetof(Vertex,TextCoords),2,sizeof(Vertex));//glVertexAttribPointer(2,2,GL_FLOAT,GL_FALSE,sizeof(Vertex), (GLvoid*) offsetof(Vertex, TextCoords));

vao->release();

}

这是我绑定程序时调用的Draw方法:

void Mesh::DrawMesh(QOpenGLFunctions_3_3_Core& gl)
{
    vao->bind();
    qDebug() << vertices.size();
    gl.glDrawElements(GL_TRIANGLES, indices.size(),GL_UNSIGNED_INT,0);
    vao->release();
}

顶点着色器:

#version 330 core

layout(location=0) in vec3 position;
layout(location=1) in vec3 normal;
layout(location=2) in vec2 textCoords;

uniform mat4 MVP;

out vec4 color;

void main(void)
{
    gl_Position= MVP*vec4(position,1);
    color = vec4(0.5,0.5,0.5,0.5);
}

我的QOpenGLWidget的初始化函数:

void RenderingWindow::initializeGL()
{ 
this->initializeOpenGLFunctions();
program.create();
program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/testvert.vert"    );
program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/testfrag.frag");
program.link();
program.bind();

this->glEnable(GL_DEPTH_TEST);

Camera::instance().LookAt(0,0,10, 0,0,0 ,0,1,0);

model.SetProgram(&program);
model.LoadModel(*this, "C:/Users/TestCube.fbx");
program.release();

}

这是我的Vertex结构:

struct Vertex
{
    QVector3D Position;
    QVector3D Normal;
    QVector2D TextCoords;
};

0 个答案:

没有答案