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