-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelLoader.cpp
More file actions
455 lines (402 loc) · 14.9 KB
/
ModelLoader.cpp
File metadata and controls
455 lines (402 loc) · 14.9 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
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
#include "ModelLoader.h"
#include "assimp/Importer.hpp"
#include "assimp/postprocess.h"
#include "StaticMeshComponent.h"
#include "Skeleton.h"
ModelLoader &ModelLoader::GetInstance()
{
static ModelLoader instance;
return instance;
}
StaticMeshComponent *ModelLoader::LoadStaticModel(const std::string &filePath)
{
Assimp::Importer importer;
const aiScene *scene = importer.ReadFile(filePath, aiProcess_Triangulate | aiProcess_FlipUVs);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
ErrorBox(importer.GetErrorString());
mDirectory = filePath.substr(0, filePath.find_last_of('/'));
mModelMeshes.clear();
mModelMaterials.clear();
mModelVertexPositions.clear();
ProcessNode(scene->mRootNode, scene);
return new StaticMeshComponent(mModelMeshes, mModelMaterials, mModelVertexPositions);
}
void ModelLoader::ProcessNode(aiNode *node, const aiScene *scene)
{
for (int i = 0; i < node->mNumMeshes; i++)
ProcessMesh(scene->mMeshes[node->mMeshes[i]], scene);
for (int i = 0; i < node->mNumChildren; i++)
ProcessNode(node->mChildren[i], scene);
}
void ModelLoader::ProcessMesh(aiMesh *mesh, const aiScene *scene)
{
// load index buffer
std::vector<unsigned int> indices;
for (int i = 0; i < mesh->mNumFaces; i++)
{
for (int j = 0; j < mesh->mFaces[i].mNumIndices; j++)
indices.push_back(mesh->mFaces[i].mIndices[j]);
}
// load positions
std::vector<XMFLOAT3> positions;
for (int i = 0; i < mesh->mNumVertices; i++)
{
positions.push_back(XMFLOAT3(mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z));
mModelVertexPositions.push_back(positions[i]);
}
// load normals
std::vector<XMFLOAT3> normals;
if (mesh->HasNormals())
for (int i = 0; i < mesh->mNumVertices; i++)
normals.push_back(XMFLOAT3(mesh->mNormals[i].x, mesh->mNormals[i].y, mesh->mNormals[i].z));
else // if model has no normal, procedurally calculate them
{
std::vector<std::vector<XMFLOAT3>> accumulatedNormals(mesh->mNumVertices);
// calculate normal for each triangle and add them to each vertex in the triangle
for (int i = 0; i < indices.size(); i += 3)
{
XMFLOAT3 p0 = positions[indices[i]];
XMFLOAT3 p1 = positions[indices[i + 1]];
XMFLOAT3 p2 = positions[indices[i + 2]];
XMVECTOR v1 = XMLoadFloat3(&p1) - XMLoadFloat3(&p0);
XMVECTOR v2 = XMLoadFloat3(&p2) - XMLoadFloat3(&p0);
XMFLOAT3 triangleNormal;
XMStoreFloat3(&triangleNormal, XMVector3Normalize(XMVector3Cross(v1, v2)));
accumulatedNormals[indices[i]].push_back(triangleNormal);
accumulatedNormals[indices[i + 1]].push_back(triangleNormal);
accumulatedNormals[indices[i + 2]].push_back(triangleNormal);
}
// average vertex normals
for (std::vector<XMFLOAT3> vertexNormals : accumulatedNormals)
{
XMVECTOR sum = XMVectorZero();
for (XMFLOAT3 vertexNormal : vertexNormals)
sum += XMLoadFloat3(&vertexNormal);
XMFLOAT3 averagedNormal;
XMStoreFloat3(&averagedNormal, sum / XMVector3Length(sum));
normals.push_back(averagedNormal);
}
}
//load texture coordinates
std::vector<XMFLOAT2> textureCoordinates;
if (mesh->HasTextureCoords(0))
for (int i = 0; i < mesh->mNumVertices; i++)
textureCoordinates.push_back(XMFLOAT2(mesh->mTextureCoords[0][i].x, mesh->mTextureCoords[0][i].y));
else
for (int i = 0; i < mesh->mNumVertices; i++)
textureCoordinates.push_back(XMFLOAT2(0.0f, 0.0f));
// load tangents
std::vector<XMFLOAT3> tangents;
if (mesh->HasTangentsAndBitangents())
for (int i = 0; i < mesh->mNumVertices; i++)
tangents.push_back(XMFLOAT3(mesh->mTangents[i].x, mesh->mTangents[i].y, mesh->mTangents[i].z));
else // if model has no tangents, procedurally calculate them
{
std::vector<std::vector<XMFLOAT3>> accumulatedTangents(mesh->mNumVertices);
for (int i = 0; i < indices.size(); i += 3)
{
XMFLOAT3 p0 = positions[indices[i]];
XMFLOAT3 p1 = positions[indices[i + 1]];
XMFLOAT3 p2 = positions[indices[i + 2]];
XMVECTOR v1 = XMLoadFloat3(&p1) - XMLoadFloat3(&p0);
XMVECTOR v2 = XMLoadFloat3(&p2) - XMLoadFloat3(&p0);
float du1 = textureCoordinates[indices[i + 1]].x - textureCoordinates[indices[i]].x;
float dv1 = textureCoordinates[indices[i + 1]].y - textureCoordinates[indices[i]].y;
float du2 = textureCoordinates[indices[i + 2]].x - textureCoordinates[indices[i]].x;
float dv2 = textureCoordinates[indices[i + 2]].y - textureCoordinates[indices[i]].y;
float det = du1 * dv2 - dv1 * du2;
XMFLOAT3 triangleTangent;
XMStoreFloat3(&triangleTangent, (dv2 * v1 - dv1 * v2) / det);
accumulatedTangents[indices[i]].push_back(triangleTangent);
accumulatedTangents[indices[i + 1]].push_back(triangleTangent);
accumulatedTangents[indices[i + 2]].push_back(triangleTangent);
}
// average vertex tangents
for (std::vector<XMFLOAT3> vertexTangents : accumulatedTangents)
{
XMVECTOR sum = XMVectorZero();
for (XMFLOAT3 vertexTangent : vertexTangents)
sum += XMLoadFloat3(&vertexTangent);
XMFLOAT3 averagedTangent;
XMStoreFloat3(&averagedTangent, sum / XMVector3Length(sum));
tangents.push_back(averagedTangent);
}
}
// load vertex attributes and index buffer
Mesh modelMesh;
modelMesh.LoadAttribute("POSITION", &positions[0], positions.size());
modelMesh.LoadAttribute("NORMAL", &normals[0], normals.size());
modelMesh.LoadAttribute("TANGENT", &tangents[0], tangents.size());
modelMesh.LoadAttribute("TEX_COORD", &textureCoordinates[0], textureCoordinates.size());
modelMesh.LoadIndexBuffer(indices);
mModelMeshes.push_back(modelMesh);
// load materials
Material modelMaterial;
if (mesh->mMaterialIndex >= 0)
{
aiMaterial *material = scene->mMaterials[mesh->mMaterialIndex];
std::vector<Texture> diffuseMaps = LoadMaterials(material, aiTextureType_DIFFUSE);
modelMaterial.AddDiffuseMaps(diffuseMaps);
std::vector<Texture> specularMaps = LoadMaterials(material, aiTextureType_SPECULAR);
modelMaterial.AddSpecularMaps(specularMaps);
std::vector<Texture> normalMaps = LoadMaterials(material, aiTextureType_NORMALS);
modelMaterial.AddNormalMaps(normalMaps);
mModelMaterials.push_back(modelMaterial);
}
}
std::vector<Texture> ModelLoader::LoadMaterials(aiMaterial *material, aiTextureType aiType)
{
std::vector<Texture> textures;
for (int i = 0; i < material->GetTextureCount(aiType); i++)
{
aiString s;
material->GetTexture(aiType, i, &s);
std::string path(mDirectory + "/");
path.append(s.C_Str());
Texture texture = TextureManager::GetInstance().GetTexture(path);
textures.push_back(texture);
}
return textures;
}
//SkeletalMeshComponent *ModelLoader::LoadSkeletalModel(const std::string &filePath)
//{
// if (mSkeleton)
// delete mSkeleton;
//
// mSkeleton = new Skeleton;
// mSkeleton->Load(filePath);
//
// Assimp::Importer importer;
// const aiScene *scene = importer.ReadFile(filePath, aiProcess_Triangulate | aiProcess_FlipUVs);
//
// if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
// ErrorBox(importer.GetErrorString());
//
// mDirectory = filePath.substr(0, filePath.find_last_of('/'));
//
// mModelVertexPositions.clear();
// mModelMeshes.clear();
// mModelMaterials.clear();
//
// ProcessSkeletalNode(scene->mRootNode, scene);
//
// return new SkeletalMeshComponent(mModelVertexPositions, mModelMeshes, *mSkeleton, mModelTextures, mModelMaterial);
//}
//void ModelLoader::ProcessSkeletalNode(aiNode *node, const aiScene *scene)
//{
// for (int i = 0; i < node->mNumMeshes; i++)
// ProcessSkeletalMesh(scene->mMeshes[node->mMeshes[i]], scene);
//
// for (int i = 0; i < node->mNumChildren; i++)
// ProcessSkeletalNode(node->mChildren[i], scene);
//}
//
//void ModelLoader::ProcessSkeletalMesh(aiMesh *mesh, const aiScene *scene)
//{
// // load index buffer
// std::vector<unsigned int> indices;
//
// for (int i = 0; i < mesh->mNumFaces; i++)
// {
// for (int j = 0; j < mesh->mFaces[i].mNumIndices; j++)
// indices.push_back(mesh->mFaces[i].mIndices[j]);
// }
//
// // load positions
// for (int i = 0; i < mesh->mNumVertices; i++)
// mModelVertexPositions.push_back(XMFLOAT3(mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z));
//
// // load normals
// std::vector<XMFLOAT3> normals;
//
// if (mesh->HasNormals())
// for (int i = 0; i < mesh->mNumVertices; i++)
// normals.push_back(XMFLOAT3(mesh->mNormals[i].x, mesh->mNormals[i].y, mesh->mNormals[i].z));
// else // if model has no normal, procedurally calculate them
// {
// std::vector<std::vector<XMFLOAT3>> accumulatedNormals(mesh->mNumVertices);
//
// // calculate normal for each triangle and add them to each vertex in the triangle
// for (int i = 0; i < indices.size(); i += 3)
// {
// XMFLOAT3 p0 = mModelVertexPositions[indices[i]];
// XMFLOAT3 p1 = mModelVertexPositions[indices[i + 1]];
// XMFLOAT3 p2 = mModelVertexPositions[indices[i + 2]];
//
// XMVECTOR v1 = XMLoadFloat3(&p1) - XMLoadFloat3(&p0);
// XMVECTOR v2 = XMLoadFloat3(&p2) - XMLoadFloat3(&p0);
//
// XMFLOAT3 triangleNormal;
// XMStoreFloat3(&triangleNormal, XMVector3Normalize(XMVector3Cross(v1, v2)));
//
// accumulatedNormals[indices[i]].push_back(triangleNormal);
// accumulatedNormals[indices[i + 1]].push_back(triangleNormal);
// accumulatedNormals[indices[i + 2]].push_back(triangleNormal);
// }
//
// // average vertex normals
// for (std::vector<XMFLOAT3> vertexNormals : accumulatedNormals)
// {
// XMVECTOR sum = XMVectorZero();
//
// for (XMFLOAT3 vertexNormal : vertexNormals)
// sum += XMLoadFloat3(&vertexNormal);
//
// XMFLOAT3 averagedNormal;
// XMStoreFloat3(&averagedNormal, sum / XMVector3Length(sum));
//
// normals.push_back(averagedNormal);
// }
// }
//
// //load texture coordinates
// std::vector<XMFLOAT2> textureCoordinates;
//
// if (mesh->HasTextureCoords(0))
// for (int i = 0; i < mesh->mNumVertices; i++)
// textureCoordinates.push_back(XMFLOAT2(mesh->mTextureCoords[0][i].x, mesh->mTextureCoords[0][i].y));
// else
// for (int i = 0; i < mesh->mNumVertices; i++)
// textureCoordinates.push_back(XMFLOAT2(0.0f, 0.0f));
//
// // load bone ids and weights
// std::vector<std::vector<std::pair<unsigned int, float>>> boneIDWeights(mesh->mNumVertices);
//
// for (int i = 0; i < mesh->mNumBones; i++)
// {
// aiBone *bone = mesh->mBones[i];
// unsigned int boneID = mSkeleton->GetBoneIndex(bone->mName.C_Str());
// for (int i = 0; i < bone->mNumWeights; i++)
// {
// aiVertexWeight weight = bone->mWeights[i];
// boneIDWeights[weight.mVertexId].push_back(std::make_pair(boneID, weight.mWeight));
// }
// }
//
// // pick only 4 most influencing bones per vertex
// std::vector<XMINT4> boneIDs;
// std::vector<XMFLOAT4> boneWeights;
//
// for (int i = 0; i < mesh->mNumVertices; i++)
// {
// std::vector<std::pair<unsigned int, float>> vertexBoneData = boneIDWeights[i];
//
// if (vertexBoneData.size() > 4)
// {
// // take the 4 most influential weights and normalize
// std::sort(vertexBoneData.begin(), vertexBoneData.end(), [](const std::pair<int, float> &p1, std::pair<int, float> const &p2) -> bool { return p1.second > p2.second; }); // sort by weight (descending order)
//
// float sum = 0.0f;
// for (int i = 0; i < 4; i++)
// sum += vertexBoneData[i].second;
// for (int i = 0; i < 4; i++)
// vertexBoneData[i].second /= sum;
//
// boneIDs.emplace_back(vertexBoneData[0].first, vertexBoneData[1].first, vertexBoneData[2].first, vertexBoneData[3].first);
// boneWeights.emplace_back(vertexBoneData[0].second, vertexBoneData[1].second, vertexBoneData[2].second, vertexBoneData[3].second);
// }
// else if (vertexBoneData.size() <= 4)
// {
// int i = 0;
// float ids[4];
// float weights[4];
// while (i < vertexBoneData.size())
// {
// ids[i] = vertexBoneData[i].first;
// weights[i] = vertexBoneData[i].second;
// i++;
// }
// // fill extra bone ids and weights with zeroes
// while (i < 4)
// {
// ids[i] = 0;
// weights[i] = 0.0f;
// i++;
// }
//
// boneIDs.emplace_back(ids[0], ids[1], ids[2], ids[3]);
// boneWeights.emplace_back(weights[0], weights[1], weights[2], weights[3]);
// }
// }
//
// // load tangents
// std::vector<XMFLOAT3> tangents;
//
// if (mesh->HasTangentsAndBitangents())
// for (int i = 0; i < mesh->mNumVertices; i++)
// tangents.push_back(XMFLOAT3(mesh->mTangents[i].x, mesh->mTangents[i].y, mesh->mTangents[i].z));
// else // if model has no tangents, procedurally calculate them
// {
// std::vector<std::vector<XMFLOAT3>> accumulatedTangents(mesh->mNumVertices);
//
// for (int i = 0; i < indices.size(); i += 3)
// {
// XMFLOAT3 p0 = mModelVertexPositions[indices[i]];
// XMFLOAT3 p1 = mModelVertexPositions[indices[i + 1]];
// XMFLOAT3 p2 = mModelVertexPositions[indices[i + 2]];
//
// XMVECTOR v1 = XMLoadFloat3(&p1) - XMLoadFloat3(&p0);
// XMVECTOR v2 = XMLoadFloat3(&p2) - XMLoadFloat3(&p0);
//
// float du1 = textureCoordinates[indices[i + 1]].x - textureCoordinates[indices[i]].x;
// float dv1 = textureCoordinates[indices[i + 1]].y - textureCoordinates[indices[i]].y;
// float du2 = textureCoordinates[indices[i + 2]].x - textureCoordinates[indices[i]].x;
// float dv2 = textureCoordinates[indices[i + 2]].y - textureCoordinates[indices[i]].y;
//
// float det = du1 * dv2 - dv1 * du2;
//
// XMFLOAT3 triangleTangent;
// XMStoreFloat3(&triangleTangent, (dv2 * v1 - dv1 * v2) / det);
//
// accumulatedTangents[indices[i]].push_back(triangleTangent);
// accumulatedTangents[indices[i + 1]].push_back(triangleTangent);
// accumulatedTangents[indices[i + 2]].push_back(triangleTangent);
// }
//
// // average vertex tangents
// for (std::vector<XMFLOAT3> vertexTangents : accumulatedTangents)
// {
// XMVECTOR sum = XMVectorZero();
//
// for (XMFLOAT3 vertexTangent : vertexTangents)
// sum += XMLoadFloat3(&vertexTangent);
//
// XMFLOAT3 averagedTangent;
// XMStoreFloat3(&averagedTangent, sum / XMVector3Length(sum));
//
// tangents.push_back(averagedTangent);
// }
// }
//
// // load vertex attributes and index buffer
// Mesh modelMesh;
//
// modelMesh.LoadAttribute("POSITION", &mModelVertexPositions[0], mModelVertexPositions.size());
// modelMesh.LoadAttribute("NORMAL", &normals[0], normals.size());
// modelMesh.LoadAttribute("TANGENT", &tangents[0], tangents.size());
// modelMesh.LoadAttribute("TEX_COORD", &textureCoordinates[0], textureCoordinates.size());
// modelMesh.LoadAttribute("BONE_IDS", &boneIDs[0], boneIDs.size());
// modelMesh.LoadAttribute("BONE_WEIGHTS", &boneWeights[0], boneWeights.size());
// modelMesh.LoadIndexBuffer(indices);
//
// mModelMeshes.push_back(modelMesh);
//
// // load materials
// Material modelMaterial;
//
// if (mesh->mMaterialIndex >= 0)
// {
// aiMaterial *material = scene->mMaterials[mesh->mMaterialIndex];
//
// std::vector<Texture> diffuseMaps = LoadMaterials(material, aiTextureType_DIFFUSE);
// modelMaterial.AddDiffuseMaps(diffuseMaps);
//
// std::vector<Texture> specularMaps = LoadMaterials(material, aiTextureType_SPECULAR);
// modelMaterial.AddSpecularMaps(diffuseMaps);
//
// std::vector<Texture> normalMaps = LoadMaterials(material, aiTextureType_NORMALS);
// modelMaterial.AddNormalMaps(diffuseMaps);
//
// mModelMaterials.push_back(modelMaterial);
// }
//}