当我使用索引缓冲区绘制对象时发生访问冲突

access violation when i use my index buffer to draw objects

我正在学习OpenGL,我正在尝试将其抽象化以方便我使用它。 但是当我在渲染时使用我的 IndexBuffer class 时出现访问冲突。

这是我的 IndexBuffer.h

代码
class IndexBuffer
{
public:
    IndexBuffer(void* data, int count);
    IndexBuffer(int count);

    IndexBuffer Bind();
    IndexBuffer UnBind();

    void AddData(void* data);

    ~IndexBuffer();
private:
    int count;
    size_t size;
    unsigned int id;
};

这是我的 IndexBuffer.cpp

代码
#include "IndexBuffer.h"
#include "glew/glew.h"

IndexBuffer::IndexBuffer( void* data, int count): id(0), count(count), size(sizeof(unsigned int)* count)
{
    glGenBuffers(1, &id);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);

    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * count, data, GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

IndexBuffer::IndexBuffer( int count) : id(0), count(count), size(sizeof(unsigned int)* count)
{
    glGenBuffers(1, &id);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);

    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * count, nullptr, GL_DYNAMIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

IndexBuffer IndexBuffer::Bind()
{
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
    return *this;
}

IndexBuffer IndexBuffer::UnBind()
{
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    return *this;
}

void IndexBuffer::AddData(void* data)
{
    Bind();
    glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(unsigned int) * count, data);
    UnBind();
}

IndexBuffer::~IndexBuffer()
{
    glDeleteBuffers(1, &id);
}

我用同样的方式编写了我的 VertexBuffer,它工作正常,但是我的索引缓冲区不起作用。

这是我的main.cpp

#include <iostream>
#include "glew/glew.h"
#include "glfw/glfw3.h"
#include "VertexBuffer.h"
#include "IndexBuffer.h"

struct Vertex
{
    float aPos[2];
};

int main() {

    GLFWwindow* window;

    if (glfwInit() == GLFW_FALSE)
    {
        return -1;
    }

    //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
    window = glfwCreateWindow(600, 600, "Hello world", nullptr, nullptr);

    glfwMakeContextCurrent(window);

    if (glewInit() != GLEW_OK)
    {
        return -2;
    }

    unsigned int index[3] = {
        0, 1, 2
    };

    Vertex data[] = {
        Vertex({-0.5f, -0.5f}),
        Vertex({ 0.5f, -0.5f}),
        Vertex({ 0.0f,  0.5f})
    };

    unsigned int VertexArrayObject;

    glGenVertexArrays(1, &VertexArrayObject);
    VertexBuffer buffer = VertexBuffer(sizeof(Vertex) * 3);
    IndexBuffer Ibuffer = IndexBuffer( 3);

    buffer.Bind();

    //glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6, data, GL_STATIC_DRAW);
    buffer.AddData(data);

    glBindVertexArray(VertexArrayObject);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, aPos));

    //glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
    buffer.UnBind();
    
    Ibuffer.Bind();

    Ibuffer.AddData(index);

    Ibuffer.UnBind();

    while (!glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        buffer.Bind();
        Ibuffer.Bind();
        glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glDeleteVertexArrays(1, &VertexArrayObject);

    glfwDestroyWindow(window);
    return 0;
}

有人能帮帮我吗?

简而言之,您有未定义的行为。

您的 类 不支持深层复制。 当 Bind 按值函数 returns 对象(即它本身)时,如:

IndexBuffer IndexBuffer::Bind()

调用了 IndexBuffer 的析构函数,它删除了之前分配的缓冲区,因此缓冲区的 id 是悬空的。

所有 Bind 应 return 参考实例:

IndexBuffer& IndexBuffer::Bind()