Search code examples
c++openglopengl-3

access violation when i use my index buffer to draw objects


I am learning OpenGL and I am trying to abstract it to make it convenient for me to use it. but I am getting access violations when I use my IndexBuffer class while rendering.

this is my code for 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;
};

and this is my code for 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);
}

I wrote my VertexBuffer in the same way and it works fine, But my index buffer just doesn't work.

this is my 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;
}

can anybody help me out?


Solution

  • In short you have undefined behaviour.

    Your classes doesn't support deep copy. When Bind function returns object (i.e itself) by value, like:

    IndexBuffer IndexBuffer::Bind()
    

    destructor of IndexBuffer is called, which deletes previously allocated buffer, so buffer's id is dangled.

    All Bind should return reference to instances:

    IndexBuffer& IndexBuffer::Bind()