6#include <brenta/logger.hpp>
7#include <brenta/texture.hpp>
13using namespace brenta;
17 const std::string &type)
21 this->
id = this->
load(path, flip);
22 DEBUG(
"texture: created");
27 if (this->
id == 0)
return;
29 glDeleteTextures(1, &this->
id);
31 DEBUG(
"texture: deleted");
34unsigned int texture::get_id()
const
48 glBindTexture(GL_TEXTURE_2D,
texture);
49 read_image(path.c_str(), flip);
54 GLint filtering_min, GLint filtering_mag,
55 GLboolean has_mipmap, GLint mipmap_min,
58 glBindTexture(target,
id);
59 set_texture_wrapping(wrapping);
60 set_texture_filtering(filtering_min, filtering_mag);
61 set_mipmap(has_mipmap, mipmap_min, mipmap_mag);
64void texture::bind(GLenum target, GLint wrapping,
65 GLint filtering_min, GLint filtering_mag,
66 GLboolean has_mipmap, GLint mipmap_min,
70 filtering_mag, has_mipmap, mipmap_min, mipmap_mag);
73void texture::set_texture_wrapping(GLint wrapping)
75 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapping);
76 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapping);
79void texture::set_texture_filtering(GLint filtering_min, GLint filtering_mag)
81 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filtering_min);
82 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filtering_mag);
85void texture::set_mipmap(GLboolean hasMipmap, GLint mipmap_min,
88 if (!hasMipmap)
return;
90 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mipmap_min);
91 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mipmap_mag);
94void texture::read_image(
const char *path,
bool flip)
96 int width, height, nrChannels;
97 stbi_set_flip_vertically_on_load(flip);
98 unsigned char *data = stbi_load(path, &width, &height, &nrChannels, 0);
101 GLenum format = GL_RGB;
104 else if (nrChannels == 3)
106 else if (nrChannels == 4)
109 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format,
110 GL_UNSIGNED_BYTE, data);
111 glGenerateMipmap(GL_TEXTURE_2D);
115 ERROR(
"texture::read_image: failed to load texture at location: {}", path);
117 stbi_image_free(data);
static void active_texture(GLenum texture)
Activate a texture unit.
texture()
Empty constructor, does nothing.
static unsigned int load(const std::string &path, bool flip=true)
Load a texture from a file.
static void bind_id(GLenum target, unsigned int id, GLint wrapping=GL_REPEAT, GLint filtering_min=GL_NEAREST, GLint filtering_mag=GL_NEAREST, GLboolean has_mipmap=GL_TRUE, GLint mipmap_min=GL_LINEAR_MIPMAP_LINEAR, GLint mipmap_mag=GL_LINEAR)
Bind a texture.