-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresourcemanager.cpp
610 lines (509 loc) · 18.1 KB
/
resourcemanager.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
#include "resourcemanager.h"
#include "innpch.h"
#include <QDirIterator>
#include "constants.h"
#include "wavfilehandler.h"
#include <QDebug>
#include "soundmanager.h"
ResourceManager::~ResourceManager()
{
inst = nullptr;
}
void ResourceManager::LoadAssetFiles()
{
QDirIterator dirIt(QString::fromStdString(gsl::assetFilePath), QDirIterator::Subdirectories);
while (dirIt.hasNext())
{
dirIt.next();
if (QFileInfo(dirIt.filePath()).isFile())
{
auto fileInfo = QFileInfo(dirIt.filePath());
auto baseName = fileInfo.baseName().toStdString();
auto fileName = fileInfo.fileName().toStdString();
if (fileInfo.suffix() == "txt" || fileInfo.suffix() == "obj")
{
addMesh(baseName, fileName);
}
else if(fileInfo.suffix() == "bmp")
{
addTexture(baseName, fileName);
}
else if(fileInfo.suffix() == "wav")
{
loadWav(baseName, fileInfo.absoluteFilePath().toStdString());
}
}
}
}
void ResourceManager::addShader(const std::string &name, std::shared_ptr<Shader> shader)
{
if(shader && mShaders.find(name) == mShaders.end())
{
shader->mName = name;
mShaders[name] = shader;
}
}
std::shared_ptr<Shader> ResourceManager::getShader(const std::string &name)
{
if(mShaders.find(name) != mShaders.end())
{
return mShaders[name];
}
return nullptr;
}
void ResourceManager::addTexture(const std::string &name, const std::string &path, GLenum type)
{
if(mTextures.find(name) == mTextures.end())
{
if(!mIsInitialized)
{
initializeOpenGLFunctions();
mIsInitialized = true;
}
mTextures[name] = std::make_shared<Texture>(path, type);
}
}
std::shared_ptr<Texture> ResourceManager::getTexture(const std::string &name)
{
if(mTextures.find(name) != mTextures.end())
{
return mTextures[name];
}
return nullptr;
}
std::shared_ptr<MeshData> ResourceManager::addMesh(const std::string& name, const std::string& path, GLenum renderType)
{
if(mMeshes.find(name) != mMeshes.end())
{
return mMeshes[name];
}
if(!mIsInitialized)
{
initializeOpenGLFunctions();
mIsInitialized = true;
}
if(QString::fromStdString(path).contains(".obj"))
{
auto verticesIndicesPair = readObjFile(path);
if(!verticesIndicesPair.first.size()) return nullptr;
auto mesh = initializeMeshData(name, renderType, verticesIndicesPair);
auto [LOD1, LOD2] = initializeLODs(name, path, renderType);
setupLODs(mesh, LOD1, LOD2);
return mesh;
}
else
{
auto verticesIndicesPair = readTxtFile(path);
if(!verticesIndicesPair.first.size()) return nullptr;
auto mesh = initializeMeshData(name, renderType, verticesIndicesPair);
setupLODs(mesh);
return mesh;
}
return nullptr;
}
std::pair<std::shared_ptr<MeshData>, std::shared_ptr<MeshData>> ResourceManager::initializeLODs(const std::string& name, const std::string& path, GLenum renderType)
{
auto split = QString::fromStdString(path).split(".");
auto firstSplit = split[0].toStdString();
std::shared_ptr<MeshData> LOD1{nullptr};
std::shared_ptr<MeshData> LOD2{nullptr};
// LOD1
auto LOD1Name = name + "_L01.obj";
auto verticesIndicesPair = readObjFile(LOD1Name);
if(verticesIndicesPair.first.size())
{
LOD1 = initializeMeshData(LOD1Name, renderType, verticesIndicesPair);
// LOD2
auto LOD2Name = name + "_L02.obj";
verticesIndicesPair = readObjFile(LOD2Name);
if(verticesIndicesPair.first.size())
{
LOD2 = initializeMeshData(LOD2Name, renderType, verticesIndicesPair);
}
}
return {LOD1, LOD2};
}
void ResourceManager::setupLODs(std::shared_ptr<MeshData> baseMeshData, std::shared_ptr<MeshData> LOD1, std::shared_ptr<MeshData> LOD2)
{
if(LOD1)
{
baseMeshData->mVAOs[1] = LOD1->mVAOs[0];
baseMeshData->mVerticesCounts[1] = LOD1->mVerticesCounts[0];
baseMeshData->mIndicesCounts[1] = LOD1->mIndicesCounts[0];
if(LOD2)
{
baseMeshData->mVAOs[2] = LOD2->mVAOs[0];
baseMeshData->mVerticesCounts[2] = LOD2->mVerticesCounts[0];
baseMeshData->mIndicesCounts[2] = LOD2->mIndicesCounts[0];
mMeshes.erase(LOD2->mName);
}
else
{
baseMeshData->mVAOs[2] = LOD1->mVAOs[0];
baseMeshData->mVerticesCounts[2] = LOD1->mVerticesCounts[0];
baseMeshData->mIndicesCounts[2] = LOD1->mIndicesCounts[0];
}
mMeshes.erase(LOD1->mName);
}
else
{
baseMeshData->mVAOs[1] = baseMeshData->mVAOs[0];
baseMeshData->mVerticesCounts[1] = baseMeshData->mVerticesCounts[0];
baseMeshData->mIndicesCounts[1] = baseMeshData->mIndicesCounts[0];
baseMeshData->mVAOs[2] = baseMeshData->mVAOs[0];
baseMeshData->mVerticesCounts[2] = baseMeshData->mVerticesCounts[0];
baseMeshData->mIndicesCounts[2] = baseMeshData->mIndicesCounts[0];
}
}
void ResourceManager::setupLOD(std::shared_ptr<MeshData> baseMesh, std::shared_ptr<MeshData> LOD, unsigned int level)
{
const auto findMesh = [](std::map<std::string, std::shared_ptr<MeshData>>& map, std::shared_ptr<MeshData> mesh){
for (auto it = map.begin(); it != map.end(); ++it)
if (it->second == mesh)
return it;
return map.end();
};
if (baseMesh && LOD)
{
if (level == 0)
{
// If adding a lod to the base lod, just swap the meshes.
auto first = findMesh(mMeshes, baseMesh);
auto second = findMesh(mMeshes, LOD);
if (first == mMeshes.end() || second == mMeshes.end())
return;
first->second = std::move(second->second);
mMeshes.erase(second);
}
else if (0 < level && level < 3)
{
baseMesh->mVAOs[level] = LOD->mVAOs[0];
baseMesh->mVerticesCounts[level] = LOD->mVerticesCounts[0];
baseMesh->mIndicesCounts[level] = LOD->mIndicesCounts[0];
if (level == 1 && baseMesh->mVerticesCounts[1] < baseMesh->mVerticesCounts[2])
{
baseMesh->mVAOs[2] = LOD->mVAOs[0];
baseMesh->mVerticesCounts[2] = LOD->mVerticesCounts[0];
baseMesh->mIndicesCounts[2] = LOD->mIndicesCounts[0];
}
auto lodIt = findMesh(mMeshes, LOD);
if (lodIt != mMeshes.end())
mMeshes.erase(lodIt);
}
}
}
std::shared_ptr<MeshData> ResourceManager::initializeMeshData(const std::string& name, GLenum renderType, std::pair<std::vector<Vertex>, std::vector<GLuint>> data)
{
MeshData meshData;
meshData.mName = name;
meshData.mRenderType = renderType;
//Vertex Array Object - VAO
glGenVertexArrays( 1, &meshData.mVAOs[0] );
glBindVertexArray(meshData.mVAOs[0]);
//Vertex Buffer Object to hold vertices - VBO
GLuint vbo;
glGenBuffers( 1, &vbo );
glBindBuffer( GL_ARRAY_BUFFER, vbo );
glBufferData( GL_ARRAY_BUFFER, static_cast<GLsizei>(data.first.size() * sizeof(Vertex)), data.first.data(), GL_STATIC_DRAW );
// 1rst attribute buffer : vertices
glVertexAttribPointer(0, 3, GL_FLOAT,GL_FALSE, sizeof(Vertex), (GLvoid*)0);
glEnableVertexAttribArray(0);
// 2nd attribute buffer : colors
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
// 3rd attribute buffer : uvs
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof( Vertex ), (GLvoid*)(6 * sizeof(GLfloat )));
glEnableVertexAttribArray(2);
meshData.mVerticesCounts[0] = static_cast<unsigned>(data.first.size());
meshData.mIndicesCounts[0] = static_cast<unsigned>(data.second.size());
meshData.bounds = CalculateBounds(data.first);
if(meshData.mIndicesCounts[0])
{
//Second buffer - holds the indices (Element Array Buffer - EAB):
GLuint eab;
glGenBuffers(1, &eab);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eab);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, meshData.mIndicesCounts[0] * sizeof(GLuint), data.second.data(), GL_STATIC_DRAW);
}
auto mesh = std::make_shared<MeshData>(meshData);
mMeshes[name] = mesh;
glBindVertexArray(0);
return mesh;
}
MeshData::Bounds ResourceManager::CalculateBounds(const std::vector<Vertex> &vertices)
{
float minx{0.f}, miny{0.f}, minz{0.f},
maxx{0.f}, maxy{0.f}, maxz{0.f};
for (auto vertex : vertices)
{
minx = (vertex.get_xyz().x < minx) ? vertex.get_xyz().x : minx;
miny = (vertex.get_xyz().y < miny) ? vertex.get_xyz().y : miny;
minz = (vertex.get_xyz().z < minz) ? vertex.get_xyz().z : minz;
maxx = (maxx < vertex.get_xyz().x) ? vertex.get_xyz().x : maxx;
maxy = (maxy < vertex.get_xyz().y) ? vertex.get_xyz().y : maxy;
maxz = (maxz < vertex.get_xyz().z) ? vertex.get_xyz().z : maxz;
}
MeshData::Bounds b;
b.centre.x = (minx + maxx) / 2.0f;
b.centre.y = (miny + maxy) / 2.0f;
b.centre.z = (minz + maxz) / 2.0f;
b.radius = static_cast<float>(std::sqrt(std::pow((maxx - b.centre.x), 2) + std::pow((maxy - b.centre.y), 2) + std::pow((maxz - b.centre.z), 2)));
return b;
}
std::shared_ptr<MeshData> ResourceManager::getMesh(const std::string& name)
{
if(mMeshes.find(name) != mMeshes.end())
{
return mMeshes[name];
}
qDebug() << "Error ResourceManager: No mesh named " << QString::fromStdString(name) << " could be found";
return nullptr;
}
void ResourceManager::loadWav(const std::string& name, const std::string& path)
{
auto waveData = new wave_t();
if (!WavFileHandler::loadWave(path, waveData))
{
qDebug() << "Error ResourceManager: Failed loading wave file " << QString::fromStdString(name) << "!";
return;
}
std::ostringstream i2s;
i2s << waveData->dataSize;
mWavFiles[name] = waveData;
}
wave_t* ResourceManager::getWav(const std::string& name)
{
if(mWavFiles.find(name) != mWavFiles.end())
{
return mWavFiles[name];
}
return nullptr;
}
int ResourceManager::getSourceBuffer(const std::string& name)
{
if(mSourceBuffers.find(name) != mSourceBuffers.end())
{
return static_cast<int>(mSourceBuffers[name]);
}
return -1;
}
std::vector<std::string> ResourceManager::getAllMeshNames()
{
std::vector<std::string> returnStrings;
for(auto& mesh : mMeshes)
{
returnStrings.push_back(mesh.first);
}
return returnStrings;
}
std::vector<std::string> ResourceManager::getAllShaderNames()
{
std::vector<std::string> returnStrings;
for(auto& shader : mShaders)
{
if(shader.second->mRenderingType == ShaderType::Forward || shader.second->mRenderingType == ShaderType::Deferred)
returnStrings.push_back(shader.first);
}
return returnStrings;
}
std::vector<std::string> ResourceManager::getAllTextureNames()
{
std::vector<std::string> returnStrings;
for(auto& texture : mTextures)
{
returnStrings.push_back(texture.first);
}
return returnStrings;
}
std::vector<std::string> ResourceManager::getAllWavFileNames()
{
std::vector<std::string> returnStrings;
for(auto& wav : mWavFiles)
{
returnStrings.push_back(wav.first);
}
return returnStrings;
}
std::pair<std::vector<Vertex>, std::vector<GLuint>> ResourceManager::readObjFile(std::string filename, bool relative)
{
std::vector<Vertex> mVertices;
std::vector<GLuint> mIndices;
//Open File
std::ifstream fileIn;
if(relative)
{
std::string fileWithPath = gsl::assetFilePath + "Meshes/" + filename;
fileIn.open (fileWithPath, std::ifstream::in);
}
else
{
fileIn.open(filename, std::ifstream::in);
}
if(!fileIn)
{
qDebug() << "Error ResourceManager: Could not open file for reading: " << QString::fromStdString(filename);
return {};
}
//One line at a time-variable
std::string oneLine;
//One word at a time-variable
std::string oneWord;
std::vector<gsl::Vector3D> tempVertecies;
std::vector<gsl::Vector3D> tempNormals;
std::vector<gsl::Vector2D> tempUVs;
// std::vector<Vertex> mVertices; //made in VisualObject
// std::vector<GLushort> mIndices; //made in VisualObject
// Varible for constructing the indices vector
unsigned int temp_index = 0;
bool normalsPresent = false;
bool UVsPresent = false;
//Reading one line at a time from file to oneLine
while(std::getline(fileIn, oneLine))
{
//Doing a trick to get one word at a time
std::stringstream sStream;
//Pushing line into stream
sStream << oneLine;
//Streaming one word out of line
oneWord = ""; //resetting the value or else the last value might survive!
sStream >> oneWord;
if (oneWord == "#")
{
//Ignore this line
// qDebug() << "Line is comment " << QString::fromStdString(oneWord);
continue;
}
if (oneWord == "")
{
//Ignore this line
// qDebug() << "Line is blank ";
continue;
}
if (oneWord == "v")
{
// qDebug() << "Line is vertex " << QString::fromStdString(oneWord) << " ";
gsl::Vector3D tempVertex;
sStream >> oneWord;
tempVertex.x = std::stof(oneWord);
sStream >> oneWord;
tempVertex.y = std::stof(oneWord);
sStream >> oneWord;
tempVertex.z = std::stof(oneWord);
//Vertex made - pushing it into vertex-vector
tempVertecies.push_back(tempVertex);
continue;
}
if (oneWord == "vt")
{
// qDebug() << "Line is UV-coordinate " << QString::fromStdString(oneWord) << " ";
gsl::Vector2D tempUV;
sStream >> oneWord;
tempUV.x = std::stof(oneWord);
sStream >> oneWord;
tempUV.y = std::stof(oneWord);
//UV made - pushing it into UV-vector
tempUVs.push_back(tempUV);
UVsPresent = true;
continue;
}
if (oneWord == "vn")
{
// qDebug() << "Line is normal " << QString::fromStdString(oneWord) << " ";
gsl::Vector3D tempNormal;
sStream >> oneWord;
tempNormal.x = std::stof(oneWord);
sStream >> oneWord;
tempNormal.y = std::stof(oneWord);
sStream >> oneWord;
tempNormal.z = std::stof(oneWord);
//Vertex made - pushing it into vertex-vector
tempNormals.push_back(tempNormal);
normalsPresent = true;
continue;
}
if (oneWord == "f")
{
// qDebug() << "Line is a face " << QString::fromStdString(oneWord) << " ";
//int slash; //used to get the / from the v/t/n - format
int index, normal, uv;
for(int i = 0; i < 3; i++)
{
sStream >> oneWord; //one word read
std::stringstream tempWord(oneWord); //to use getline on this one word
std::string segment; //the numbers in the f-line
std::vector<std::string> segmentArray; //temp array of the numbers
while(std::getline(tempWord, segment, '/')) //splitting word in segments
{
segmentArray.push_back(segment);
}
index = std::stoi(segmentArray[0]);
--index;
if(UVsPresent)
{
uv = std::stoi(segmentArray[1]);
--uv;
}
if(normalsPresent)
{
normal = std::stoi(segmentArray[2]);
--normal;
}
Vertex tempVert(tempVertecies[index]);
if (UVsPresent)
{
tempVert.set_uv(tempUVs[uv].x, tempUVs[uv].y);
}
else
{
tempVert.set_uv(0.f, 0.f);
}
if(normalsPresent)
{
tempVert.set_normal(tempNormals[normal]);
}
else
{
tempVert.set_normal(0, 1, 0);
}
mVertices.push_back(tempVert);
mIndices.push_back(temp_index++);
}
//For some reason the winding order is backwards so fixing this by swapping the last two indices
//Update: this was because the matrix library was wrong - now it is corrected so this is no longer needed.
// unsigned int back = mIndices.size() - 1;
// std::swap(mIndices.at(back), mIndices.at(back-1));
continue;
}
}
//beeing a nice boy and closing the file after use
fileIn.close();
return {mVertices, mIndices};
}
std::pair<std::vector<Vertex>, std::vector<GLuint>> ResourceManager::readTxtFile(std::string filename)
{
std::vector<Vertex> mVertices;
std::vector<GLuint> mIndices;
std::ifstream inn;
std::string fileWithPath = gsl::assetFilePath + "Meshes/" + filename;
inn.open(fileWithPath);
if (inn.is_open()) {
int n;
Vertex vertex;
inn >> n;
mVertices.reserve(n);
for (int i=0; i<n; i++) {
inn >> vertex;
mVertices.push_back(vertex);
}
inn.close();
}
else
{
qDebug() << "Error ResourceManager: Could not open file for reading: " << QString::fromStdString(filename);
}
return {mVertices, mIndices};
}
ResourceManager* ResourceManager::inst{nullptr};
ResourceManager::ResourceManager(){
inst = this;
}