#include "graphics/opengl/vertex_array.hpp"

#include <glad/gl.h>

#include <memory>

using namespace kuiper::gl;

vertex_array::s_ptr vertex_array::make() noexcept {
    GLuint name = 0;
    glGenVertexArrays(1, &name);

    if (!name)
        return nullptr;

    return std::make_shared<vertex_array>(name);
}

void vertex_array::destroy() noexcept {
    if (m_id)
        glDeleteVertexArrays(1, &m_id);

    return;
}
void vertex_array::bind() const noexcept {
    return glBindVertexArray(m_id);
}

void vertex_array::enable_attrib(std::uint32_t index) noexcept {
    bind();
    return glEnableVertexAttribArray(index);
}

void vertex_array::disable_attrib(std::uint32_t index) noexcept {
    bind();
    return glDisableVertexAttribArray(index);
}

void vertex_array::set_binding_divisor(std::uint32_t binding, std::uint32_t divisor) noexcept {
    bind();
    return glVertexBindingDivisor(binding, divisor);
}

void vertex_array::set_attrib_divisor(std::uint32_t attrib, std::uint32_t divisor) noexcept {
    bind();
    return glVertexAttribDivisor(attrib, divisor);
}
