Files
portersky 40ae94788e style: fix include ordering across all source files
- C++ std headers first, then third-party, then local
- Apply consistently in cbt/ and scenes/
2026-05-05 22:40:51 +02:00

86 lines
2.0 KiB
C++

#include "glad/glad.h"
#include "cbt/opengl/texture.hpp"
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;
}
auto texture::operator=(texture&& other) noexcept -> texture& {
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(void const* 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;
}
}