-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathtexture.cpp
364 lines (272 loc) · 8.69 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "core.h"
#include "texture.h"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
namespace P3D::Graphics {
#pragma region Texture
//! Texture
SRef<Texture> Texture::_white;
int getChannelsFromFormat(int format) {
switch (format) {
case GL_RED:
case GL_GREEN:
case GL_BLUE:
case GL_ALPHA:
return 1;
case GL_RG:
return 2;
case GL_RGB:
return 3;
case GL_RGBA:
return 4;
default:
Log::warn("Unknown format: %x, assuming 4 channels", format);
return 4;
}
}
int getFormatFromChannels(int channels) {
switch (channels) {
case 1:
return GL_RED;
case 2:
return GL_RG;
case 3:
return GL_RGB;
case 4:
return GL_RGBA;
default:
Log::warn("Unknown amount of channels: %d, assuming RGB", channels);
return GL_RGB;
}
}
Texture Texture::load(const std::string& name) {
int width;
int height;
int channels;
//stbi_set_flip_vertically_on_load(true);
unsigned char* data = stbi_load(name.c_str(), &width, &height, &channels, 0);
if (data) {
int format = getFormatFromChannels(channels);
Texture texture(width, height, data, format);
stbi_image_free(data);
return texture;
} else {
Log::subject s(name);
Log::error("Failed to load texture");
return Texture();
}
}
SRef<Texture> Texture::white() {
if (_white == nullptr) {
Color buffer = Colors::WHITE;
_white = std::make_shared<Texture>(1, 1, &buffer, GL_RGBA);
}
return _white;
}
Texture::Texture() : width(0), height(0), internalFormat(0), format(0), target(0), type(0), unit(0), channels(0) {
}
Texture::Texture(int width, int height) : Texture(width, height, nullptr, GL_RGBA) {
}
Texture::Texture(int width, int height, const void* buffer, int format) : Texture(width, height, buffer, GL_TEXTURE_2D, format, format, GL_UNSIGNED_BYTE) {
}
Texture::Texture(int width, int height, const void* buffer, int target, int format, int internalFormat, int type) : width(width), height(height), target(target), format(format), internalFormat(internalFormat), type(type), unit(0) {
glGenTextures(1, &id);
channels = getChannelsFromFormat(format);
Texture::bind();
Texture::create(target, 0, internalFormat, width, height, 0, format, type, buffer);
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Texture::unbind();
}
void Texture::create(int target, int level, int internalFormat, int width, int height, int border, int format, int type, const void* buffer) {
glTexImage2D(target, 0, internalFormat, width, height, 0, format, type, buffer);
}
Texture::Texture(Texture&& other) : Bindable(other.id), width(other.width), height(other.height), internalFormat(other.internalFormat), format(other.format), target(other.target), type(other.type), unit(other.unit), channels(other.channels) {
other.id = 0;
other.width = 0;
other.height = 0;
other.type = 0;
other.internalFormat = 0;
other.format = 0;
other.channels = 0;
other.type = 0;
other.unit = 0;
}
Texture& Texture::operator=(Texture&& other) {
if (this != &other) {
id = 0;
width = 0;
height = 0;
type = 0;
internalFormat = 0;
format = 0;
channels = 0;
type = 0;
unit = 0;
std::swap(id, other.id);
std::swap(width, other.width);
std::swap(height, other.height);
std::swap(internalFormat, other.internalFormat);
std::swap(format, other.format);
std::swap(target, other.target);
std::swap(type, other.type);
std::swap(unit, other.unit);
std::swap(channels, other.channels);
}
return *this;
}
Texture::~Texture() {
if (id == 0)
return;
Log::warn("Closed texture #%d", id);
glDeleteTextures(1, &id);
}
void Texture::loadFrameBufferTexture(int width, int height) {
bind();
glCopyTexImage2D(target, 0, internalFormat, 0, 0, width, height, 0);
unbind();
}
void Texture::resize(int width, int height, const void* buffer) {
bind();
glTexImage2D(target, 0, internalFormat, width, height, 0, format, type, buffer);
unbind();
}
SRef<Texture> Texture::colored(const Color& color) {
bind();
unsigned char* buffer = (unsigned char*) malloc(width * height * channels);
glGetTexImage(target, 0, format, type, buffer);
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
for (int k = 0; k < channels; k++) {
int index = (i + height * j) * channels + k;
unsigned char value = static_cast<unsigned char>(buffer[index] * color.data[k]);
buffer[index] = value;
}
}
}
SRef<Texture> texture = std::make_shared<Texture>(width, height, buffer, format);
free(buffer);
return texture;
}
void Texture::generateMipmap() {
bind();
glGenerateMipmap(target);
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
}
void Texture::resize(int width, int height) {
resize(width, height, nullptr);
}
void Texture::bind(int unit) {
this->unit = unit;
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(target, id);
}
void Texture::bind() {
bind(unit);
}
void Texture::unbind() {
glBindTexture(target, 0);
}
void Texture::close() {
// Todo remove
}
float Texture::getAspect() const {
return static_cast<float>(width) / static_cast<float>(height);
}
int Texture::getWidth() const {
return width;
}
int Texture::getHeight() const {
return height;
}
int Texture::getInternalFormat() const {
return internalFormat;
}
int Texture::getFormat() const {
return format;
}
int Texture::getTarget() const {
return target;
}
int Texture::getType() const {
return type;
}
int Texture::getChannels() const {
return channels;
}
int Texture::getUnit() const {
return unit;
}
void Texture::setUnit(int unit) {
this->unit = unit;
}
#pragma endregion
#pragma region HDRTexture
//! HDRTexture
HDRTexture::HDRTexture(int width, int height) : HDRTexture(width, height, nullptr) {
};
HDRTexture::HDRTexture(int width, int height, const void* buffer) : Texture(width, height, buffer, GL_TEXTURE_2D, GL_RGBA, GL_RGBA16F, GL_FLOAT) {
}
#pragma endregion
#pragma region TextureMultisample
//! TextureMultisample
MultisampleTexture::MultisampleTexture(int width, int height, int samples) : Texture(width, height, nullptr, GL_TEXTURE_2D_MULTISAMPLE, 0, GL_RGBA, 0), samples(samples) {
glGenTextures(1, &id);
bind();
create(target, 0, internalFormat, width, height, 0, format, type, nullptr);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
unbind();
}
void MultisampleTexture::create(int target, int level, int internalFormat, int width, int height, int border, int format, int type, const void* buffer) {
glTexImage2DMultisample(target, samples, internalFormat, width, height, GL_TRUE);
}
void MultisampleTexture::resize(int width, int height) {
bind();
glTexImage2DMultisample(target, samples, internalFormat, width, height, GL_TRUE);
unbind();
}
#pragma endregion
#pragma region CubeMap
//! CubeMap
CubeMap::CubeMap(const std::string& right, const std::string& left, const std::string& top, const std::string& bottom, const std::string& front, const std::string& back) : Texture(0, 0, nullptr, GL_TEXTURE_CUBE_MAP, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE) {
bind();
load(right, left, top, bottom, front, back);
glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
unbind();
}
void CubeMap::create(int target, int level, int internalFormat, int width, int height, int border, int format, int type, const void* buffer) {
}
void CubeMap::load(const std::string& right, const std::string& left, const std::string& top, const std::string& bottom, const std::string& front, const std::string& back) {
unsigned char* data;
std::string faces[6] = { right, left, top, bottom, front, back };
stbi_set_flip_vertically_on_load(false);
for (int i = 0; i < 6; i++) {
data = stbi_load(faces[i].c_str(), &width, &height, &channels, 0);
format = internalFormat = getFormatFromChannels(channels);
if (data) {
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, format, width, height, 0, internalFormat, type, data);
//glGenerateMipmap(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i);
} else {
Log::subject s(faces[i]);
Log::error("Failed to load texture");
}
stbi_image_free(data);
}
}
void CubeMap::resize(int width, int height) {
Log::error("Resizing of cubemap not supported yet");
}
#pragma endregion
#pragma region DepthTexture
//! DepthTexture
DepthTexture::DepthTexture(int width, int height) : Texture(width, height, nullptr, GL_TEXTURE_2D, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_FLOAT) {
}
#pragma endregion
};