-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexture.cpp
More file actions
376 lines (306 loc) · 13.4 KB
/
texture.cpp
File metadata and controls
376 lines (306 loc) · 13.4 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
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
365
366
367
368
369
370
371
372
373
374
375
376
#include "texture.h"
#include "debug.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <iostream>
uint64_t TextureBase::activeCirculationCount = UINT64_C(0);
namespace {
float SGLToLinear(float s) {
if (s <= 0.04045F)
return s / 12.92F;
else
return powf((s + 0.055F) / 1.055F, 2.4F);
}
}
TextureBase::TextureBox::TextureBox(GLuint texture) :
texture(texture) {
static uint64_t totalCirculationIDCount = UINT64_C(0);
circulationID = totalCirculationIDCount++;
++activeCirculationCount;
}
TextureBase::TextureBox::~TextureBox() {
--activeCirculationCount;
glDeleteTextures(1, &texture);
}
TextureBase::TextureBase() :
textureBox(nullptr) { }
TextureBase::TextureBase(const TextureBase& other) :
textureBox(other.textureBox) { }
TextureBase& TextureBase::operator=(const TextureBase& other) {
textureBox = other.textureBox;
return *this;
}
Texture2D::Descriptor::Image::Image(const std::string& fileDirectory, bool srgb, bool noAlpha) {
this->name = fileDirectory;
dataNeedsToBeLoaded = true;
width = 0;
height = 0;
internalFormat = srgb ? (noAlpha ? GL_SRGB8 : GL_SRGB8_ALPHA8) : (noAlpha ? GL_RGB8 : GL_RGBA8);
format = GL_RGBA;
type = GL_UNSIGNED_BYTE;
data = 0;
}
Texture2D::Descriptor::Image::Image(int width, int height, GLint internalFormat, GLenum format, GLenum type, void* data) :
width(width),
height(height),
internalFormat(internalFormat),
format(format),
type(type),
data(data),
dataNeedsToBeLoaded(false) { }
Texture2D::Descriptor::Descriptor(const Image& image, bool linear, bool mipmaps, GLint clamp) :
image(image),
clamp(clamp),
mipmaps(mipmaps),
minFilter(mipmaps ? GL_LINEAR_MIPMAP_LINEAR : (linear ? GL_LINEAR : GL_NEAREST)),
magFilter(linear ? GL_LINEAR : GL_NEAREST) { }
Texture2D::Texture2D() { }
Texture2D::Texture2D(const Descriptor& descriptor) {
if ((descriptor.image.width == 0 || descriptor.image.height == 0) && !descriptor.image.dataNeedsToBeLoaded) {
Console::Error("Texture2D " + descriptor.image.name + " has size of 0,0");
return;
}
void* data = descriptor.image.data;
int width = descriptor.image.width;
int height = descriptor.image.height;
if (descriptor.image.dataNeedsToBeLoaded) {
int numComponents;
stbi_set_flip_vertically_on_load(true);
data = stbi_load(("./Resources/" + descriptor.image.name + ".png").c_str(), &width, &height, &numComponents, 4);
//std::cerr << "loaded " << fileDirectory << " " << std::to_string(data) << std::endl;
// WARNING: does numComponents need to be checked?
if (data == nullptr)
Console::Error("Image loading failed for: " + descriptor.image.name + ".png");
}
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, descriptor.clamp);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, descriptor.clamp);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, descriptor.minFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, descriptor.magFilter);
glTexImage2D(GL_TEXTURE_2D, 0, descriptor.image.internalFormat, width, height, 0, descriptor.image.format, descriptor.image.type, data);
if (descriptor.mipmaps)
glGenerateMipmap(GL_TEXTURE_2D);
if (descriptor.image.dataNeedsToBeLoaded)
stbi_image_free(data);
textureBox = std::shared_ptr<TextureBox>(new TextureBox(texture));
}
Texture2D::Texture2D(const Texture2D& other) : TextureBase(other) { }
Texture2DArray::Descriptor::ImageArray::Element::Element(const std::string& fileDirectory) :
name(fileDirectory),
dataNeedsToBeLoaded(true),
width(0),
height(0),
data(0) { }
Texture2DArray::Descriptor::ImageArray::ImageArray(bool srgb) :
processedDirectory(""),
srgb(srgb),
internalFormat(GL_RGBA8),
format(GL_RGBA),
type(GL_UNSIGNED_BYTE) { }
Texture2DArray::Descriptor::ImageArray::ImageArray(std::string processedDirectory, GLint internalFormat, bool srgb) :
processedDirectory(processedDirectory),
internalFormat(internalFormat),
srgb(srgb),
format(GL_RGBA),
type(GL_UNSIGNED_BYTE) { }
Texture2DArray::Descriptor::ImageArray& Texture2DArray::Descriptor::ImageArray::AddElement(const Element& element) {
if ((element.width == 0 || element.height == 0) && !element.dataNeedsToBeLoaded) {
Console::Error("Texture Array element needs to have a size larger than 0,0");
return *this;
}
elements.push_back(element);
return *this;
}
Texture2DArray::Descriptor::Descriptor(const ImageArray& imageArray, bool linear, bool mipmaps, GLint clamp) :
imageArray(imageArray),
anisotropic(false),
clamp(clamp),
mipmaps(mipmaps),
minFilter(mipmaps ? GL_LINEAR_MIPMAP_LINEAR : (linear ? GL_LINEAR : GL_NEAREST)),
magFilter(linear ? GL_LINEAR : GL_NEAREST) { }
Texture2DArray::Descriptor& Texture2DArray::Descriptor::WithAnisotropicFiltering() {
anisotropic = true;
return *this;
}
Texture2DArray::Texture2DArray() { }
Texture2DArray::Texture2DArray(const Texture2DArray& other) : TextureBase(other) { }
Texture2DArray::Texture2DArray(const Descriptor& descriptor) {
if (descriptor.imageArray.elements.size() == 0 && descriptor.imageArray.processedDirectory == "") {
Console::Error("Texture2D Array has 0 layers");
return;
}
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D_ARRAY, texture);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, descriptor.clamp);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, descriptor.clamp);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, descriptor.minFilter);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, descriptor.magFilter);
if (descriptor.anisotropic) {
GLfloat largest;
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY, &largest);
glTexParameterf(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_ANISOTROPY, largest);
}
if (descriptor.imageArray.processedDirectory == "") {
int lastWidth = -1;
int lastHeight = -1;
uint8_t* all = nullptr;
int maxIndex = 0;
int j = 0;
for (const Descriptor::ImageArray::Element& element : descriptor.imageArray.elements) {
// TODO: fix that images not from disk can't be loaded (element.dataNeedsToBeLoaded = false will break everything)
uint8_t* data = nullptr;
int width = element.width;
int height = element.height;
if (element.dataNeedsToBeLoaded) {
int numComponents;
stbi_set_flip_vertically_on_load(true);
data = stbi_load(("./Resources/" + element.name + ".png").c_str(), &width, &height, &numComponents, 4);
//std::cerr << "loaded " << fileDirectory << " " << std::to_string(data) << std::endl;
// WARNING: does numComponents need to be checked?
if (data == nullptr)
Console::Error("Image loading failed for: " + element.name + ".png");
}
if (lastWidth == -1) {
lastWidth = width;
lastHeight = height;
maxIndex = width * height * 4;
all = new uint8_t[maxIndex * descriptor.imageArray.elements.size()];
if (width == 0 || height == 0)
Console::Error("Size of first layer has size of zero");
} else if (lastWidth != width || lastHeight != height) {
Console::Error("Size of this layer (" + std::to_string(width) + "," + std::to_string(height) + ") does not match size of last layer (" + std::to_string(width) + "," + std::to_string(height) + ")");
}
//memcpy(all, data, sizeof(uint8_t) * 4 * maxIndex);
if (descriptor.imageArray.srgb) {
for (int i = 0; i < maxIndex; ++i)
if (i % 4 != 3)
all[i + j * maxIndex] = static_cast<uint8_t>(floorf(SGLToLinear(data[i] / 255.0F) * 255.0F));
else
all[i + j * maxIndex] = data[i];
} else {
for (int i = 0; i < maxIndex; ++i)
all[i + j * maxIndex] = data[i];
}
if (element.dataNeedsToBeLoaded)
stbi_image_free(data);
++j;
}
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, descriptor.imageArray.internalFormat, lastWidth, lastHeight, static_cast<GLsizei>(descriptor.imageArray.elements.size()), 0, descriptor.imageArray.format, descriptor.imageArray.type, all);
delete[] all;
} else {
uint8_t* data;
int width;
int height;
int numComponents;
stbi_set_flip_vertically_on_load(true);
data = stbi_load(("./Resources/" + descriptor.imageArray.processedDirectory + ".png").c_str(), &width, &height, &numComponents, 4);
if (data == nullptr)
Console::Error("Image array loading failed for: " + descriptor.imageArray.processedDirectory + ".png");
if (descriptor.imageArray.srgb) {
for (int i = 0; i < width * height * 4; ++i)
if (i % 4 != 3)
data[i] = static_cast<uint8_t>(floorf(SGLToLinear(data[i] / 255.0F) * 255.0F));
else
data[i] = data[i];
}
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, descriptor.imageArray.internalFormat, width, width, height / width, 0, descriptor.imageArray.format, descriptor.imageArray.type, data);
stbi_image_free(data);
}
if (descriptor.mipmaps)
glGenerateMipmap(GL_TEXTURE_2D_ARRAY);
textureBox = std::shared_ptr<TextureBox>(new TextureBox(texture));
}
Texture2DQuadAtlas::Texture2DQuadAtlas() { }
Texture2DQuadAtlas::Texture2DQuadAtlas(GLint internalFormat, unsigned int initialSize, bool linear) {
if (initialSize == 0) {
Console::Error("Texture2DQuadAtlas has initial size of 0");
return;
}
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, linear ? GL_LINEAR : GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, initialSize, initialSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
textureBox = std::shared_ptr<TextureBox>(new TextureBox(texture));
atlasData = std::shared_ptr<AtlasData>(new AtlasData(initialSize));
}
Texture2DQuadAtlas::Texture2DQuadAtlas(const Texture2DQuadAtlas& other) :
TextureBase(other),
atlasData(other.atlasData) { }
bool Texture2DQuadAtlas::AddTexture(const std::string& name, unsigned char* textureData, unsigned int textureSize, unsigned int segmentPosX, unsigned int segmentPosY, unsigned int segmentSize, StateQuadTree* treeNode) {
if (treeNode->GetState() == false || textureSize > segmentSize)
return false;
if (textureSize == segmentSize && (*treeNode)[0] == nullptr && (*treeNode)[1] == nullptr && (*treeNode)[2] == nullptr && (*treeNode)[3] == nullptr) {
Bind();
glTexSubImage2D(GL_TEXTURE_2D, 0, segmentPosX, segmentPosY, textureSize, textureSize, GL_RGBA, GL_UNSIGNED_BYTE, textureData);
float totalSize = static_cast<float>(atlasData->atlasSize);
atlasData->atlasTexInfoMap[name] = FVector4(segmentPosX / totalSize, segmentPosY / totalSize, textureSize / totalSize, textureSize / totalSize);
treeNode->FalsifyState();
return true;
} else if (segmentSize > 1) {
unsigned int newSize = segmentSize / 2;
if ((*treeNode)[0] == nullptr)
treeNode->Subdivide();
for (unsigned int i = 0; i < StateQuadTree::CHILDREN_COUNT; ++i)
if (AddTexture(name, textureData, textureSize, segmentPosX + (i % 2) * newSize, segmentPosY + (i / 2) * newSize, newSize, (*treeNode)[i]))
return true;
}
return false;
}
FVector4 Texture2DQuadAtlas::GetTexInfo(const std::string& name) {
auto info = atlasData->atlasTexInfoMap.find(name);
if (info == atlasData->atlasTexInfoMap.end()) {
int width;
int height;
int numComponents;
stbi_set_flip_vertically_on_load(true);
unsigned char* data = stbi_load(("./Resources/" + name + ".png").c_str(), &width, &height, &numComponents, 4);
if (width != height) {
Console::Error("Unable to add texture '" + name + "' to atlas as it does not have square dimensions");
return FVector4(0.0F, 0.0F, 1.0F, 1.0F);
}
if (!AddTexture(name, data, width, 0, 0, atlasData->atlasSize, &atlasData->availabilityTracker))
Console::Error("Unable to find position to add texture '" + name + "' to atlas");
stbi_image_free(data);
info = atlasData->atlasTexInfoMap.find(name);
if (info == atlasData->atlasTexInfoMap.end()) {
// Only if texture addition failed
return FVector4(0.0F, 0.0F, 1.0F, 1.0F);
}
}
return info->second;
}
void Texture2DQuadAtlas::SetTexInfoRegion(const std::string& src, const std::string& dest, const FVector4& subregion) {
FVector4 base = GetTexInfo(src);
atlasData->atlasTexInfoMap[dest] = FVector4(base.x + subregion.x * base.z, base.y + subregion.y * base.w, subregion.z * base.z, subregion.w * base.w);
}
Cubemap::Descriptor::Descriptor(int width, int height, GLint internalFormat, bool linear, bool mipmaps) :
width(width),
height(height),
internalFormat(internalFormat),
mipmaps(mipmaps),
minFilter(mipmaps ? GL_LINEAR_MIPMAP_LINEAR : (linear ? GL_LINEAR : GL_NEAREST)),
magFilter(linear ? GL_LINEAR : GL_NEAREST) { }
Cubemap::Cubemap() { }
Cubemap::Cubemap(const Descriptor& descriptor) {
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_CUBE_MAP, texture);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, descriptor.minFilter);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, descriptor.magFilter);
for (GLuint i = 0; i < 6; ++i)
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, descriptor.internalFormat, descriptor.width, descriptor.height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
if (descriptor.mipmaps)
glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
textureBox = std::shared_ptr<TextureBox>(new TextureBox(texture));
}
Cubemap::Cubemap(const Cubemap& other) : TextureBase(other) { }