6#include <brenta/renderer/opengl/texture.hpp>
7#include <brenta/logger.hpp>
16using namespace brenta;
18int Texture::tot_memory = 0;
20Texture::Texture(
const Config &conf)
22 this->type = conf.type;
23 this->target = conf.target;
24 this->properties = conf.properties;
28 this->
id = this->load_solid_color(conf.color.value());
32 Texture::tot_memory += this->memory;
34 EVENT(Logger::Event::Lifetime,
35 "texture: created {} from color r={},g={},b={},a={}",
36 this->
id, conf.color->r, conf.color->g,
37 conf.color->b, conf.color->a);
41 this->path = conf.path;
45 int loaded_memory = 0;
46 this->
id = this->load(this->path, loaded_memory, conf.properties.flipped);
49 this->memory = loaded_memory;
50 Texture::tot_memory += this->memory;
53 EVENT(Logger::Event::Lifetime,
"texture: created {} from path {}",
54 this->
id, this->path.string());
62 if (this->
id == 0)
return;
64 glDeleteTextures(1, &this->
id);
67 Texture::tot_memory -= this->memory;
70 EVENT(Logger::Event::Lifetime,
"texture: deleted {}", this->
id);
75Texture::Id Texture::get_id()
const
80std::filesystem::path Texture::get_path()
const
85Texture::Type Texture::get_type()
const
90Texture::Target Texture::get_target()
const
98 return this->properties;
101void Texture::active_texture(
int texture)
103 glActiveTexture(GL_TEXTURE0 + texture);
108unsigned int Texture::load_solid_color(
Color color)
111 GLint old_active_texture, old_texture_2d;
112 glGetIntegerv(GL_ACTIVE_TEXTURE, &old_active_texture);
113 glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_texture_2d);
116 glGenTextures(1, &texture);
117 glBindTexture(GL_TEXTURE_2D, texture);
119 unsigned char color_rgba[4] = {
120 (
unsigned char)(color.r * 255.0f),
121 (
unsigned char)(color.g * 255.0f),
122 (
unsigned char)(color.b * 255.0f),
123 (
unsigned char)(color.a * 255.0f)
126 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
127 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA,
128 GL_UNSIGNED_BYTE, &color_rgba[0]);
132 glBindTexture(GL_TEXTURE_2D, old_texture_2d);
133 glActiveTexture(old_active_texture);
139unsigned int Texture::load(
const std::filesystem::path &path,
140 int &loaded_bytes,
bool flipped)
143 GLint old_active_texture, old_texture_2d;
144 glGetIntegerv(GL_ACTIVE_TEXTURE, &old_active_texture);
145 glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_texture_2d);
148 glGenTextures(1, &texture);
149 glBindTexture(GL_TEXTURE_2D, texture);
150 loaded_bytes = read_image(path, flipped);
153 glBindTexture(GL_TEXTURE_2D, old_texture_2d);
154 glActiveTexture(old_active_texture);
159void Texture::bind_id(Texture::Target target, Texture::Id
id)
164void Texture::bind_id(Texture::Target target, Texture::Id
id,
167 glBindTexture(target,
id);
171 glTexParameteri(target, GL_TEXTURE_WRAP_S, prop.wrapping);
172 glTexParameteri(target, GL_TEXTURE_WRAP_T, prop.wrapping);
173 glTexParameteri(target, GL_TEXTURE_WRAP_R, prop.wrapping);
176 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, prop.filtering_min);
177 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, prop.filtering_mag);
180 if (!prop.has_mipmap)
return;
182 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, prop.mipmap_min);
183 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, prop.mipmap_mag);
191 Texture::bind_id(this->target, this->
id, this->properties);
195int Texture::read_image(
const std::filesystem::path &path,
bool flip)
197 int width, height, nrChannels;
199 stbi_set_flip_vertically_on_load(flip);
200 unsigned char *data = stbi_load(path.string().c_str(), &width,
201 &height, &nrChannels, 0);
204 GLenum format = GL_RGB;
207 else if (nrChannels == 3)
209 else if (nrChannels == 4)
212 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format,
213 GL_UNSIGNED_BYTE, data);
214 glGenerateMipmap(GL_TEXTURE_2D);
218 tot_memory += nrChannels * width * height;
222 ERROR(
"Texture::read_image: failed to load texture at location: {}",
225 stbi_image_free(data);
237 this->wrapping = wrapping;
243 this->filtering_min = filtering;
249 this->filtering_mag = filtering;
255 this->has_mipmap = mipmap;
261 this->mipmap_min = filtering;
267 this->mipmap_mag = filtering;
273 this->flipped = flipped;
285 this->conf.type = type;
291 this->conf.target = target;
297 this->conf.path = path;
303 this->conf.properties = prop;
309 this->conf.color = color;
315 this->watch_paths.push_back(path);
319Texture Texture::Builder::build()
324tenno::vector<std::filesystem::path> Texture::Builder::get_watch_paths()
const
326 return this->watch_paths;