Brenta Engine 1.2
Loading...
Searching...
No Matches
texture.cpp
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
6#include <brenta/logger.hpp>
7#include <brenta/texture.hpp>
8#include <glad/glad.h>
9#include <iostream>
10#include <stb_image.h>
11#include <string>
12
13using namespace brenta;
14
15texture::texture(const std::string &path,
16 bool flip,
17 const std::string &type)
18{
19 this->type = type;
20 this->path = path;
21 this->id = this->load(path, flip);
22 DEBUG("texture: created");
23}
24
25texture::~texture()
26{
27 if (this->id == 0) return;
28
29 glDeleteTextures(1, &this->id);
30 this->id = 0;
31 DEBUG("texture: deleted");
32}
33
34unsigned int texture::get_id() const
35{
36 return this->id;
37}
38
40{
41 glActiveTexture(texture);
42}
43
44unsigned int texture::load(const std::string &path, bool flip)
45{
46 unsigned int texture;
47 glGenTextures(1, &texture);
48 glBindTexture(GL_TEXTURE_2D, texture);
49 read_image(path.c_str(), flip);
50 return texture;
51}
52
53void texture::bind_id(GLenum target, unsigned int id, GLint wrapping,
54 GLint filtering_min, GLint filtering_mag,
55 GLboolean has_mipmap, GLint mipmap_min,
56 GLint mipmap_mag)
57{
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);
62}
63
64void texture::bind(GLenum target, GLint wrapping,
65 GLint filtering_min, GLint filtering_mag,
66 GLboolean has_mipmap, GLint mipmap_min,
67 GLint mipmap_mag)
68{
69 texture::bind_id(target, this->id, wrapping, filtering_min,
70 filtering_mag, has_mipmap, mipmap_min, mipmap_mag);
71}
72
73void texture::set_texture_wrapping(GLint wrapping)
74{
75 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapping);
76 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapping);
77}
78
79void texture::set_texture_filtering(GLint filtering_min, GLint filtering_mag)
80{
81 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filtering_min);
82 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filtering_mag);
83}
84
85void texture::set_mipmap(GLboolean hasMipmap, GLint mipmap_min,
86 GLint mipmap_mag)
87{
88 if (!hasMipmap) return;
89
90 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mipmap_min);
91 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mipmap_mag);
92}
93
94void texture::read_image(const char *path, bool flip)
95{
96 int width, height, nrChannels;
97 stbi_set_flip_vertically_on_load(flip);
98 unsigned char *data = stbi_load(path, &width, &height, &nrChannels, 0);
99 if (data)
100 {
101 GLenum format = GL_RGB;
102 if (nrChannels == 1)
103 format = GL_RED;
104 else if (nrChannels == 3)
105 format = GL_RGB;
106 else if (nrChannels == 4)
107 format = GL_RGBA;
108
109 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format,
110 GL_UNSIGNED_BYTE, data);
111 glGenerateMipmap(GL_TEXTURE_2D);
112 }
113 else
114 {
115 ERROR("texture::read_image: failed to load texture at location: {}", path);
116 }
117 stbi_image_free(data);
118}
Texture class.
Definition texture.hpp:20
static void active_texture(GLenum texture)
Activate a texture unit.
Definition texture.cpp:39
texture()
Empty constructor, does nothing.
Definition texture.hpp:64
static unsigned int load(const std::string &path, bool flip=true)
Load a texture from a file.
Definition texture.cpp:44
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.
Definition texture.cpp:53