-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexture.cpp
More file actions
103 lines (92 loc) · 2.38 KB
/
Copy pathTexture.cpp
File metadata and controls
103 lines (92 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include"Texture.h"
Texture::Texture(const char* image, const char* texType, GLuint slot)
{
// Assigns the type of the texture to the texture object
type = texType;
// Generates an OpenGL texture object
int widthImg, heightImg, numColCh;
// Flips the image so it appears right side up
stbi_set_flip_vertically_on_load(true);
// Reads the image from a file and stores it in an array of bytes
unsigned char* bytes = stbi_load(image, &widthImg, &heightImg, &numColCh, 0);
// Creates the texture object
glGenTextures(1, &ID);
// Assigns the texture to a Texture Unit
glActiveTexture(GL_TEXTURE0 + slot);
unit = slot;
// Binds the texture to the texture object
glBindTexture(GL_TEXTURE_2D, ID);
// Configures the type of algorithm that is used to make the image smaller or bigger
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Configures the way the texture repeats (if it does at all)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
// Note that GL_CLAMP_TO_EDGE is not supported in OpenGL ES 2.0
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
if (numColCh == 4)
glTexImage2D
(
GL_TEXTURE_2D,
0,
GL_RGBA,
widthImg,
heightImg,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
bytes
);
else if (numColCh == 3)
glTexImage2D
(
GL_TEXTURE_2D,
0,
GL_RGBA,
widthImg,
heightImg,
0,
GL_RGB,
GL_UNSIGNED_BYTE,
bytes
);
else if (numColCh == 1)
glTexImage2D
(
GL_TEXTURE_2D,
0,
GL_RGBA,
widthImg,
heightImg,
0,
GL_RED,
GL_UNSIGNED_BYTE,
bytes
);
else
throw std::invalid_argument("Automatic Texture type recognition failed. Only 1, 3 or 4 channels are supported.");
// Generates MipMaps
glGenerateMipmap(GL_TEXTURE_2D);
// Deletes the image data as it is now stored in the OpenGL texture object
stbi_image_free(bytes);
// Unbinds the texture so that other textures can be used
glBindTexture(GL_TEXTURE_2D, 0);
}
void Texture::texUnit(Shader& shader, const char* uniform, GLuint unit)
{
GLuint texUni = glGetUniformLocation(shader.ID, uniform);
shader.Activate();
glUniform1i(texUni, unit);
}
void Texture::Bind()
{
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, ID);
}
void Texture::Unbind()
{
glBindTexture(GL_TEXTURE_2D, 0);
}
void Texture::Delete()
{
glDeleteTextures(1, &ID);
}