Files
cuber/cbt/opengl/buffer.cpp
T
portersky 30ddaf7d39 feat: render spinning 3D cube with glm
- Add glm dependency for matrix transformations
- Replace triangle with 6-face colored cube
- Add MVP shader uniforms (model, view, projection)
- Enable depth testing for proper 3D rendering
- Spin cube around (1, 0.5, 0.3) axis

style: fix trailing return type for move assignment operators

- buffer/texture/shader/vao: use auto fn() -> T& style
- Document trailing return type convention in AGENTS.md
2026-05-05 22:19:33 +02:00

73 lines
1.4 KiB
C++

#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;
}
auto buffer::operator=(buffer&& other) noexcept -> buffer& {
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<GLenum>(m_type), m_id);
}
auto buffer::unbind() const -> void {
glBindBuffer(static_cast<GLenum>(m_type), 0);
}
auto buffer::upload(void const* data, size_t size) -> void {
bind();
glBufferData(static_cast<GLenum>(m_type), size, data, GL_STATIC_DRAW);
unbind();
}
auto buffer::upload(std::vector<uint8_t> data) -> void {
upload(data.data(), data.size());
}
auto buffer::upload_dynamic(void const* data, size_t size) -> void {
bind();
glBufferData(static_cast<GLenum>(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;
}
}