#include "cbt/opengl/buffer.hpp" #include "glad/glad.h" namespace cbt::opengl { buffer::buffer() { glGenBuffers(1, &m_id); } buffer::buffer(buffer_type type) : m_type(type) { glGenBuffers(1, &m_id); } buffer::~buffer() { if (m_id) { glDeleteBuffers(1, &m_id); } } buffer::buffer(buffer&& other) noexcept : m_id(other.m_id) , m_type(other.m_type) { other.m_id = 0; } buffer& buffer::operator=(buffer&& other) noexcept { if (this != &other) { if (m_id) { glDeleteBuffers(1, &m_id); } m_id = other.m_id; m_type = other.m_type; other.m_id = 0; } return *this; } auto buffer::bind() const -> void { glBindBuffer(static_cast(m_type), m_id); } auto buffer::unbind() const -> void { glBindBuffer(static_cast(m_type), 0); } auto buffer::upload(void const* data, size_t size) -> void { bind(); glBufferData(static_cast(m_type), size, data, GL_STATIC_DRAW); unbind(); } auto buffer::upload(std::vector data) -> void { upload(data.data(), data.size()); } auto buffer::upload_dynamic(void const* data, size_t size) -> void { bind(); glBufferData(static_cast(m_type), size, data, GL_DYNAMIC_DRAW); unbind(); } auto buffer::id() const -> GLuint { return m_id; } auto buffer::valid() const -> bool { return m_id != 0; } }