#include "VertexBuffer.h"

VertexBuffer::VertexBuffer() {
    glGenVertexArrays(1, &vertex_array);
    glGenBuffers(1, &vertex_buffer);
}

void VertexBuffer::set_data(int size, int dimension, const void *data, int flag) {
    // We can't just do size = sizeof(data) here because of array to pointer decay!
    this->size = size;

    bind_array();
    bind_buffer();

    // Create the data on the GPU
    glBufferData(GL_ARRAY_BUFFER, size, data, flag);

    // Specify the dimension of the vectors in the data, so that the GPU knows how much to read from
    // it at a time. (Currently only one type of vector is supported)
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, dimension, GL_FLOAT, false, dimension * sizeof(float), 0);

    glBindVertexArray(0);
}

void VertexBuffer::bind_buffer() {
    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
}

void VertexBuffer::bind_array() {
    glBindVertexArray(vertex_array);
}

void VertexBuffer::draw() {
    bind_array();
    glDrawArrays(GL_TRIANGLES, 0, size);
}