feat: add OpenGL abstraction layer with RAII resources
- Replace window class with cbt::opengl::context - Add buffer resource (VBO, EBO, UBO, SSBO) with move semantics - Add texture resource with format/type enums and filtering - Add descriptor_set for Vulkan-style resource binding - All resources use RAII with proper cleanup
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
#include "cbt/opengl/texture.hpp"
|
||||
|
||||
#include "glad/glad.h"
|
||||
|
||||
namespace cbt::opengl {
|
||||
|
||||
texture::texture() {
|
||||
glGenTextures(1, &m_id);
|
||||
}
|
||||
|
||||
texture::texture(texture_target target)
|
||||
: m_target(target) {
|
||||
glGenTextures(1, &m_id);
|
||||
}
|
||||
|
||||
texture::~texture() {
|
||||
if (m_id) {
|
||||
glDeleteTextures(1, &m_id);
|
||||
}
|
||||
}
|
||||
|
||||
texture::texture(texture&& other) noexcept
|
||||
: m_id(other.m_id)
|
||||
, m_target(other.m_target) {
|
||||
other.m_id = 0;
|
||||
}
|
||||
|
||||
texture& texture::operator=(texture&& other) noexcept {
|
||||
if (this != &other) {
|
||||
if (m_id) {
|
||||
glDeleteTextures(1, &m_id);
|
||||
}
|
||||
m_id = other.m_id;
|
||||
m_target = other.m_target;
|
||||
other.m_id = 0;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
auto texture::bind(GLuint unit) -> void {
|
||||
glActiveTexture(GL_TEXTURE0 + unit);
|
||||
glBindTexture(static_cast<GLenum>(m_target), m_id);
|
||||
}
|
||||
|
||||
auto texture::unbind() -> void {
|
||||
glBindTexture(static_cast<GLenum>(m_target), 0);
|
||||
}
|
||||
|
||||
auto texture::upload(const void* data, int width, int height, texture_format format, texture_type type) -> void {
|
||||
bind();
|
||||
glTexImage2D(static_cast<GLenum>(m_target), 0,
|
||||
static_cast<GLenum>(format), width, height, 0,
|
||||
static_cast<GLenum>(format), static_cast<GLenum>(type), data);
|
||||
unbind();
|
||||
}
|
||||
|
||||
auto texture::set_filter(GLenum min_filter, GLenum mag_filter) -> void {
|
||||
bind();
|
||||
glTexParameteri(static_cast<GLenum>(m_target), GL_TEXTURE_MIN_FILTER, min_filter);
|
||||
glTexParameteri(static_cast<GLenum>(m_target), GL_TEXTURE_MAG_FILTER, mag_filter);
|
||||
unbind();
|
||||
}
|
||||
|
||||
auto texture::set_wrap(GLenum wrap_s, GLenum wrap_t) -> void {
|
||||
bind();
|
||||
glTexParameteri(static_cast<GLenum>(m_target), GL_TEXTURE_WRAP_S, wrap_s);
|
||||
glTexParameteri(static_cast<GLenum>(m_target), GL_TEXTURE_WRAP_T, wrap_t);
|
||||
unbind();
|
||||
}
|
||||
|
||||
auto texture::generate_mipmaps() -> void {
|
||||
bind();
|
||||
glGenerateMipmap(static_cast<GLenum>(m_target));
|
||||
unbind();
|
||||
}
|
||||
|
||||
auto texture::id() const -> GLuint {
|
||||
return m_id;
|
||||
}
|
||||
|
||||
auto texture::valid() const -> bool {
|
||||
return m_id != 0;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user