glDrawElements导致访问冲突读取

时间:2015-02-15 08:42:13

标签: opengl vbo glfw glew vao

我已修复它 - 工作解决方案可以在页面底部找到。

我正在尝试使用交错的vbo和vao。我在一个简单的三角形上测试它 - 这就是我的索引数据和顶点数据的样子:

vertex_col data[] = {
        vertex_col(vec3(0.5f, 0.5f, 0.5f), vec4(0.0f, 0.0f, 1.0f, 1.0f), vec3(0.0f, 0.0f, 0.0f)),
        vertex_col(vec3(-0.5f, -0.5f, -0.5f), vec4(0.0f, 1.0f, 0.0f, 1.0f), vec3(0.0f, 0.0f, 0.0f)),
        vertex_col(vec3(-0.5f, 0.5f, 0.5f), vec4(1.0f, 0.0f, 0.0f, 1.0f), vec3(0.0f, 0.0f, 0.0f)),
    }; 
    GLushort indices[] = {
        0, 1, 2
    };
    ModelLoader loader;
    model = loader.loadToVAO(indices, 3, data, 3);

这是vertex_col结构:

struct vertex_col {

public:
    vertex_col(const vec3& pos, const vec4& col, const vec3& normal) {
        this->pos = pos;
        this->colour = col;
        this->normal = normal;
    }
    vec3 pos;
    vec4 colour;
    vec3 normal;

};

这是我将数据加载到vao和vbo中的方式:

RawModel ModelLoader::loadToVAO(const GLushort *indices, int m, const vertex_col *vertices, int n) {
    GLuint vaoID = createVAO();

    GLuint vboID;
    glGenBuffers(1, &vboID);
    glBindBuffer(GL_ARRAY_BUFFER, vboID);
    glBufferData(GL_ARRAY_BUFFER, n * sizeof(vertex_col), vertices, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex_col), (void*) offsetof(vertex_col, pos));
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(vertex_col), (void*) offsetof(vertex_col, colour));
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(vertex_col), (void*) offsetof(vertex_col, normal));

    GLuint vboID_2;
    glGenBuffers(1, &vboID_2);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID_2);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, m * sizeof(GLushort), indices, GL_STATIC_DRAW);

    unbindVAO();
    return RawModel(vaoID, m);
}

GLuint ModelLoader::createVAO() {
    GLuint vaoID;
    glGenVertexArrays(1, &vaoID);
    glBindVertexArray(vaoID);
    return vaoID;
}

这就是我绘制vao的方式:

void Renderer::render(const RawModel &model) {
    shaders["basic"].use();
    glBindVertexArray(model.getVaoID());
    glDrawElements(GL_TRIANGLES, model.getVertexCount(), GL_UNSIGNED_SHORT, nullptr);
    glBindVertexArray(0);
    shaders["basic"].release();
}

最后一个操作导致此错误'访问冲突读取位置0x00000000',我不知道为什么。如果它与问题有关,我使用的是库glew和glfw。

修正版:

RawModel *ModelLoader::loadToVAO(const GLushort *indices, int m, const vertex_col *vertices, int n) {
    GLuint vaoID = createVAO();

    GLuint vboID = 0;
    glGenBuffers(1, &vboID);
    glBindBuffer(GL_ARRAY_BUFFER, vboID);
    glBufferData(GL_ARRAY_BUFFER, n * sizeof(vertex_col), vertices, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex_col), (void*) offsetof(vertex_col, pos));
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(vertex_col), (void*) offsetof(vertex_col, colour));
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(vertex_col), (void*) offsetof(vertex_col, normal));

    vboID = 0;
    glGenBuffers(1, &vboID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, m * sizeof(GLushort), indices, GL_STATIC_DRAW);

    unbindVAO();
    return new RawModel(vaoID, m);
}

0 个答案:

没有答案