Brenta Engine 1.0
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
29#include <glad/glad.h>
30#include <iostream>
31#include <stb_image.h> /* Image loading library */
32#include <string>
33
34using namespace Brenta;
35
36unsigned int Texture::LoadTexture(std::string path, GLint wrapping,
37 GLint filtering_min, GLint filtering_mag,
38 GLboolean hasMipmap, GLint mipmap_min,
39 GLint mipmap_mag, bool flip)
40{
41 unsigned int texture;
42 glGenTextures(1, &texture);
43 glBindTexture(GL_TEXTURE_2D, texture);
44 // Texture::BindTexture(GL_TEXTURE_2D, texture, wrapping, filtering_min,
45 // filtering_mag, hasMipmap, mipmap_min, mipmap_mag);
46
47 ReadImage(path.c_str(), flip);
48 return texture;
49}
50
51void Texture::ActiveTexture(GLenum texture)
52{
53 glActiveTexture(texture);
54}
55
56void Texture::BindTexture(GLenum target, unsigned int texture, GLint wrapping,
57 GLint filtering_min, GLint filtering_mag,
58 GLboolean hasMipmap, GLint mipmap_min,
59 GLint mipmap_mag)
60{
61 glBindTexture(target, texture);
62 SetTextureWrapping(wrapping);
63 SetTextureFiltering(filtering_min, filtering_mag);
64 SetMipmap(hasMipmap, mipmap_min, mipmap_mag);
65}
66
67void Texture::SetTextureWrapping(GLint wrapping)
68{
69 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapping);
70 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapping);
71}
72
73void Texture::SetTextureFiltering(GLint filtering_min, GLint filtering_mag)
74{
75 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filtering_min);
76 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filtering_mag);
77}
78
79void Texture::SetMipmap(GLboolean hasMipmap, GLint mipmap_min, GLint mipmap_mag)
80{
81 if (hasMipmap)
82 {
83 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mipmap_min);
84 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mipmap_mag);
85 }
86}
87
88void Texture::ReadImage(const char *path, bool flip)
89{
90 int width, height, nrChannels;
91 stbi_set_flip_vertically_on_load(flip);
92 unsigned char *data = stbi_load(path, &width, &height, &nrChannels, 0);
93 if (data)
94 {
95 GLenum format = GL_RGB;
96 if (nrChannels == 1)
97 format = GL_RED;
98 else if (nrChannels == 3)
99 format = GL_RGB;
100 else if (nrChannels == 4)
101 format = GL_RGBA;
102
103 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format,
104 GL_UNSIGNED_BYTE, data);
105 glGenerateMipmap(GL_TEXTURE_2D);
106 }
107 else
108 {
109 std::cout << "Failed to load texture at location: " << path
110 << std::endl;
111 }
112 stbi_image_free(data);
113}
static void BindTexture(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:56
static void ActiveTexture(GLenum texture)
Activate a texture unit.
Definition texture.cpp:51
static unsigned int LoadTexture(std::string path, 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, bool flip=true)
Load a texture from a file.
Definition texture.cpp:36