1

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

我正在尝试使用交错的 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);
}
4

0 回答 0