-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeshManager.cpp_disabled
More file actions
181 lines (131 loc) · 4.87 KB
/
Copy pathMeshManager.cpp_disabled
File metadata and controls
181 lines (131 loc) · 4.87 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
#include "MeshManager.h"
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include "LogManager.h"
#include <vector>
#include "TextureManager.h"
MeshManager MeshManager::m_instance;
MeshManager::MeshManager() {
m_meshTable = new std::unordered_map<std::string, Mesh*>();
Log::Writeln("MeshManager started.");
}
MeshManager::~MeshManager() {
clearCache();
delete m_meshTable;
}
Mesh* MeshManager::getMesh( std::string meshName, unsigned int loadFlags /*= 0*/ ) {
//Log::Write("Mesh requested " + meshName + "\t");
try {
Mesh* mesh = getInstance().m_meshTable->at(meshName);
#pragma region Logging
/* *
Log::Write("returned from cache", Log::COLOR_LIGHT_AQUA);
if(mesh->isDummy())
Log::Warn("\twarning: dummy mesh!");
else
Log::Writeln("");
/* */
#pragma endregion
return mesh;
}
catch (std::out_of_range) { /* Mesh hasn't been cached/loaded */
getInstance().loadMesh(meshName, loadFlags);
return getInstance().getMesh(meshName, loadFlags);
}
}
void MeshManager::loadMesh( std::string meshName, unsigned int loadFlags /*= 0*/ ) {
Log::Write("loading mesh " + meshName + "\t");
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(meshName, loadFlags);
if(!scene) {
Log::Err("error: couldn't load mesh!");
Mesh* dummy = new Mesh();
dummy->setIndexArray(0, nullptr);
dummy->setVertexArray(0, nullptr);
m_meshTable->insert(std::make_pair<std::string, Mesh*>(meshName, dummy));
} else {
/* dump that shit into that mesh and put that gfx card to use son! */
Log::Writeln("...");
std::vector<vertex*> tempVertexArray;
std::vector<GLuint*> tempIndexArray;
vertex* tempVertexBuffer;
GLuint* tempIndexBuffer;
const aiVector3D zeroVec(0.0f, 0.0f, 0.0f);
/* Iterate through all meshes (elements/objects) in the file */
for(unsigned int i = 0; i < scene->mNumMeshes; i++) {
aiMesh* mesh = scene->mMeshes[i];
/* Collect all vertices */
for(unsigned int j = 0; j < mesh->mNumVertices; j++) {
vertex* vert = new vertex();
vert->position.x = mesh->mVertices[j].x;
vert->position.y = mesh->mVertices[j].y;
vert->position.z = mesh->mVertices[j].z;
if(&mesh->mNormals[j]) {
vert->normal.x = mesh->mNormals[j].x;
vert->normal.y = mesh->mNormals[j].y;
vert->normal.z = mesh->mNormals[j].z;
}
else {
vert->normal.x = zeroVec.x;
vert->normal.y = zeroVec.y;
vert->normal.z = zeroVec.z;
}
if(mesh->HasTextureCoords(0)) {
vert->texcoords.x = mesh->mTextureCoords[0]->x;
vert->texcoords.y = mesh->mTextureCoords[0]->y;
}
else {
vert->texcoords.x = zeroVec.x;
vert->texcoords.y = zeroVec.y;
}
tempVertexArray.push_back(vert);
}
/* Collect all indices */
for(unsigned int j = 0; j < mesh->mNumFaces; j++) {
for(int k = 0; k < 3 /* verts per face */; k++) {
tempIndexArray.push_back(new GLuint(mesh->mFaces[j].mIndices[k]));
}
}
}
for(unsigned int i = 0; i < scene->mNumMaterials; i++) {
const aiMaterial* material = scene->mMaterials[i];
if(material->GetTextureCount(aiTextureType_DIFFUSE) > 0) {
aiString path;
if(material->GetTexture(aiTextureType_DIFFUSE, 0, &path) == AI_SUCCESS) {
std::string fullPath = "test/mesh_test/" + std::string(path.data);
TextureManager::getTexture2D(fullPath);
}
}
}
tempVertexBuffer = new vertex[tempVertexArray.size()];
tempIndexBuffer = new GLuint[tempIndexArray.size()];
for(unsigned int i = 0; i < tempVertexArray.size(); i++)
tempVertexBuffer[i] = *tempVertexArray[i];
for(unsigned int i = 0; i < tempIndexArray.size(); i++)
tempIndexBuffer[i] = *tempIndexArray[i];
Mesh* mesh = new Mesh();
mesh->setVertexArray(tempVertexArray.size(), tempVertexBuffer);
mesh->setIndexArray(tempIndexArray.size(), tempIndexBuffer);
meshBufferIDs& bufferIDs = mesh->getBufferIDs();
glGenBuffers(meshBufferIDs::length, (GLuint*)&bufferIDs);
glBindBuffer(GL_ARRAY_BUFFER, bufferIDs.vertex);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferIDs.index);
glBufferData(GL_ARRAY_BUFFER, sizeof(tempVertexBuffer[0])*tempVertexArray.size(), tempVertexBuffer, GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(tempIndexBuffer[0])*tempIndexArray.size(), tempIndexBuffer, GL_STATIC_DRAW);
m_meshTable->insert(std::make_pair<std::string, Mesh*>(meshName, mesh));
Log::Success("mesh " + meshName + " loaded!");
}
}
void MeshManager::clearCache() {
/* Loops the mesh table and free data held by the meshes */
for(std::unordered_map<std::string, Mesh*>::iterator it = getInstance().m_meshTable->begin();
it != getInstance().m_meshTable->end(); ++it) {
//std::string meshName = it->first;
Mesh* mesh = it->second;
delete[] mesh->getVertexArray();
delete[] mesh->getIndexArray();
glDeleteBuffers(meshBufferIDs::length, (GLuint*)&mesh->getBufferIDs());
}
getInstance().m_meshTable->clear();
Log::Writeln("MeshManager cache cleared");
}