#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(m_target), m_id); } auto texture::unbind() -> void { glBindTexture(static_cast(m_target), 0); } auto texture::upload(void const* data, int width, int height, texture_format format, texture_type type) -> void { bind(); glTexImage2D(static_cast(m_target), 0, static_cast(format), width, height, 0, static_cast(format), static_cast(type), data); unbind(); } auto texture::set_filter(GLenum min_filter, GLenum mag_filter) -> void { bind(); glTexParameteri(static_cast(m_target), GL_TEXTURE_MIN_FILTER, min_filter); glTexParameteri(static_cast(m_target), GL_TEXTURE_MAG_FILTER, mag_filter); unbind(); } auto texture::set_wrap(GLenum wrap_s, GLenum wrap_t) -> void { bind(); glTexParameteri(static_cast(m_target), GL_TEXTURE_WRAP_S, wrap_s); glTexParameteri(static_cast(m_target), GL_TEXTURE_WRAP_T, wrap_t); unbind(); } auto texture::generate_mipmaps() -> void { bind(); glGenerateMipmap(static_cast(m_target)); unbind(); } auto texture::id() const -> GLuint { return m_id; } auto texture::valid() const -> bool { return m_id != 0; } }