Brenta Engine 1.1
Loading...
Searching...
No Matches
texture.cpp
1/*
2 * MIT License
3 *
4 * Copyright (c) 2024 Giovanni Santini
5
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 */
26
27#include "texture.hpp"
28#include "engine_logger.hpp"
29
30#include <glad/glad.h>
31#include <iostream>
32#include <stb_image.h> /* Image loading library */
33#include <string>
34
35using namespace brenta;
36
37unsigned int texture::load_texture(std::string path, GLint wrapping,
38 GLint filtering_min, GLint filtering_mag,
39 GLboolean hasMipmap, GLint mipmap_min,
40 GLint mipmap_mag, bool flip)
41{
42 unsigned int texture;
43 glGenTextures(1, &texture);
44 glBindTexture(GL_TEXTURE_2D, texture);
45 // Texture::BindTexture(GL_TEXTURE_2D, texture, wrapping, filtering_min,
46 // filtering_mag, hasMipmap, mipmap_min, mipmap_mag);
47
48 read_image(path.c_str(), flip);
49 return texture;
50}
51
53{
54 glActiveTexture(texture);
55}
56
57void texture::bind_texture(GLenum target, unsigned int texture, GLint wrapping,
58 GLint filtering_min, GLint filtering_mag,
59 GLboolean hasMipmap, GLint mipmap_min,
60 GLint mipmap_mag)
61{
62 glBindTexture(target, texture);
63 set_texture_wrapping(wrapping);
64 set_texture_filtering(filtering_min, filtering_mag);
65 set_mipmap(hasMipmap, mipmap_min, mipmap_mag);
66}
67
68void texture::set_texture_wrapping(GLint wrapping)
69{
70 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapping);
71 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapping);
72}
73
74void texture::set_texture_filtering(GLint filtering_min, GLint filtering_mag)
75{
76 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filtering_min);
77 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filtering_mag);
78}
79
80void texture::set_mipmap(GLboolean hasMipmap, GLint mipmap_min,
81 GLint mipmap_mag)
82{
83 if (hasMipmap)
84 {
85 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mipmap_min);
86 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mipmap_mag);
87 }
88}
89
90void texture::read_image(const char *path, bool flip)
91{
92 int width, height, nrChannels;
93 stbi_set_flip_vertically_on_load(flip);
94 unsigned char *data = stbi_load(path, &width, &height, &nrChannels, 0);
95 if (data)
96 {
97 GLenum format = GL_RGB;
98 if (nrChannels == 1)
99 format = GL_RED;
100 else if (nrChannels == 3)
101 format = GL_RGB;
102 else if (nrChannels == 4)
103 format = GL_RGBA;
104
105 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format,
106 GL_UNSIGNED_BYTE, data);
107 glGenerateMipmap(GL_TEXTURE_2D);
108 }
109 else
110 {
111 ERROR("Failed to load texture at location: {}", path);
112 }
113 stbi_image_free(data);
114}
Texture class.
Definition texture.hpp:41
static void active_texture(GLenum texture)
Activate a texture unit.
Definition texture.cpp:52
static unsigned int load_texture(std::string path, 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, bool flip=true)
Load a texture from a file.
Definition texture.cpp:37
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:57