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
15unsigned int texture::load_texture(std::string path, bool flip)
16{
17 unsigned int texture;
18 glGenTextures(1, &texture);
19 glBindTexture(GL_TEXTURE_2D, texture);
20 read_image(path.c_str(), flip);
21 return texture;
22}
23
25{
26 glActiveTexture(texture);
27}
28
29void texture::bind_texture(GLenum target, unsigned int texture, GLint wrapping,
30 GLint filtering_min, GLint filtering_mag,
31 GLboolean hasMipmap, GLint mipmap_min,
32 GLint mipmap_mag)
33{
34 glBindTexture(target, texture);
35 set_texture_wrapping(wrapping);
36 set_texture_filtering(filtering_min, filtering_mag);
37 set_mipmap(hasMipmap, mipmap_min, mipmap_mag);
38}
39
40void texture::set_texture_wrapping(GLint wrapping)
41{
42 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapping);
43 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapping);
44}
45
46void texture::set_texture_filtering(GLint filtering_min, GLint filtering_mag)
47{
48 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filtering_min);
49 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filtering_mag);
50}
51
52void texture::set_mipmap(GLboolean hasMipmap, GLint mipmap_min,
53 GLint mipmap_mag)
54{
55 if (hasMipmap)
56 {
57 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mipmap_min);
58 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mipmap_mag);
59 }
60}
61
62void texture::read_image(const char *path, bool flip)
63{
64 int width, height, nrChannels;
65 stbi_set_flip_vertically_on_load(flip);
66 unsigned char *data = stbi_load(path, &width, &height, &nrChannels, 0);
67 if (data)
68 {
69 GLenum format = GL_RGB;
70 if (nrChannels == 1)
71 format = GL_RED;
72 else if (nrChannels == 3)
73 format = GL_RGB;
74 else if (nrChannels == 4)
75 format = GL_RGBA;
76
77 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format,
78 GL_UNSIGNED_BYTE, data);
79 glGenerateMipmap(GL_TEXTURE_2D);
80 }
81 else
82 {
83 ERROR("texture::read_image: failed to load texture at location: {}", path);
84 }
85 stbi_image_free(data);
86}
Texture class.
Definition texture.hpp:20
static void active_texture(GLenum texture)
Activate a texture unit.
Definition texture.cpp:24
static unsigned int load_texture(std::string path, bool flip=true)
Load a texture from a file.
Definition texture.cpp:15
static void bind_texture(GLenum target, unsigned int texture, GLint wrapping=GL_REPEAT, GLint filtering_min=GL_NEAREST, GLint filtering_mag=GL_NEAREST, GLboolean hasMipmap=GL_TRUE, GLint mipmap_min=GL_LINEAR_MIPMAP_LINEAR, GLint mipmap_mag=GL_LINEAR)
Bind a texture.
Definition texture.cpp:29