-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTexture.cpp
157 lines (122 loc) · 3.78 KB
/
Texture.cpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "Texture.h"
Texture::Texture(const char* image, const char* texType, GLuint slot, GLenum format, GLenum pixelType)
{
type = texType;
int widthImg, heightImg, numColorChannels;
stbi_set_flip_vertically_on_load(true);
unsigned char* bytes = stbi_load(image, &widthImg, &heightImg, &numColorChannels, 0);
glGenTextures(1, &ID);
glActiveTexture(GL_TEXTURE0 + slot);
unit = slot;
glBindTexture(GL_TEXTURE_2D, ID);
// GL_LINEAR : returns the weighted average of the four texture elements that are closest to the center of the pixel being textured.
// GL_NEAREST : returns the value of the texture element that is nearest (in Manhattan distance) to the center of the pixel being textured.
// Reference Link : https://www.khronos.org/registry/OpenGL-Refpages/es2.0/xhtml/glTexParameter.xml
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Show image with border color
// float flatColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
// glTexParameterfv(textType, GL_TEXTURE_BORDER_COLOR, flatColor);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, widthImg, heightImg, 0, format, pixelType, bytes);
// Generate smaller resolution versions
// glGenerateMipmap(textType);
// free image data as the data has been imported
stbi_image_free(bytes);
glBindTexture(GL_TEXTURE_2D, 0);
}
Texture::Texture(const char* image, const char* texType, GLuint slot)
{
type = texType;
int widthImg, heightImg, numColorChannels;
stbi_set_flip_vertically_on_load(true);
unsigned char* bytes = stbi_load(image, &widthImg, &heightImg, &numColorChannels, 0);
glGenTextures(1, &ID);
glActiveTexture(GL_TEXTURE0 + slot);
unit = slot;
glBindTexture(GL_TEXTURE_2D, ID);
// GL_LINEAR : returns the weighted average of the four texture elements that are closest to the center of the pixel being textured.
// GL_NEAREST : returns the value of the texture element that is nearest (in Manhattan distance) to the center of the pixel being textured.
// Reference Link : https://www.khronos.org/registry/OpenGL-Refpages/es2.0/xhtml/glTexParameter.xml
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Show image with border color
// float flatColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
// glTexParameterfv(textType, GL_TEXTURE_BORDER_COLOR, flatColor);
if (numColorChannels == 4)
{
glTexImage2D
(
GL_TEXTURE_2D,
0,
GL_RGBA,
widthImg,
heightImg,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
bytes
);
}
else if (numColorChannels == 3)
{
glTexImage2D
(
GL_TEXTURE_2D,
0,
GL_RGBA,
widthImg,
heightImg,
0,
GL_RGB,
GL_UNSIGNED_BYTE,
bytes
);
}
else if (numColorChannels == 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");
}
// Generate smaller resolution versions
glGenerateMipmap(GL_TEXTURE_2D);
// free image data as the data has been imported
stbi_image_free(bytes);
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);
}