-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssetLoader.cpp
More file actions
213 lines (176 loc) · 7.96 KB
/
AssetLoader.cpp
File metadata and controls
213 lines (176 loc) · 7.96 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
#include <limits>
#include <map>
// Using the Open Asset Import Library ("assimp" - http://assimp.sourceforge.net/)
//#include <assimp/assimp.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <assimp/Importer.hpp>
#include "AssetLoader.h"
#include "Mesh.h"
#include "Bounds.h"
bool AssetLoader::loadMeshData(const std::string & filename,
MeshDataGroup & meshDatas,
std::vector<std::shared_ptr<ImageBase>> & textures)
{
printf("AssetLoader::loadMeshData(%s, ...)\n", filename.c_str());
if(meshDataCache.contains(filename)) {
printf("Found mesh data cache entry for %s\n", filename.c_str());
meshDatas = meshDataCache.get(filename);
return true;
}
std::string basePath("");
auto lastSlash = filename.find_last_of("/");
if( lastSlash != std::string::npos) {
basePath = filename.substr(0, lastSlash);
}
Assimp::Importer importer;
const aiScene * scene = importer.ReadFile( filename,
aiProcess_Triangulate
| aiProcess_FindInvalidData
| aiProcess_GenSmoothNormals
//| aiProcess_GenNormals
| aiProcess_FixInfacingNormals
);
if( !scene) {
fprintf( stderr, "Failed to load %s\n", filename.c_str());
return false;
}
printf( "Loaded scene %s\n", filename.c_str());
printf( " - # meshes -> %u\n", scene->mNumMeshes);
printf( "Scene:"
" HasMeshes = %d (%u)"
" HasMaterials = %d (%u)"
" HasTextures = %d (%u)"
"\n",
(int) scene->HasMeshes(), scene->mNumMeshes,
(int) scene->HasMaterials(), scene->mNumMaterials,
(int) scene->HasTextures(), scene->mNumTextures
);
// Load textures
std::vector< std::string > diffuseTexturePaths;
std::map<unsigned int, GLuint> materialToDiffuseTextureIndex;
std::map<unsigned int, GLuint> diffuseTextureIndexToTextureID;
// Find paths to each texture
for( unsigned int materialIndex = 0; materialIndex < scene->mNumMaterials; materialIndex++) {
printf("Material %3u\n", materialIndex);
auto & material = scene->mMaterials[materialIndex];
int texIndex = 0;
aiString aipath;
while( AI_SUCCESS == material->GetTexture( aiTextureType_DIFFUSE, texIndex, &aipath)) {
std::string path( aipath.C_Str());
std::replace(path.begin(), path.end(), '\\', '/');
printf( " diffuse %d : %s\n", texIndex, path.c_str());
materialToDiffuseTextureIndex[materialIndex] = diffuseTexturePaths.size();
diffuseTexturePaths.push_back( path);
texIndex++;
}
}
// Load texture images
unsigned int textureIndex = 0;
for( const auto & path : diffuseTexturePaths) {
std::shared_ptr<ImageBase> texImage = loadImage(basePath + "/" + path);
textures.push_back(texImage);
GLuint texID = texImage->uploadGL();
// FIXME - should really map material index -> texture index -> texture id
// because we could have multiple textures per material
diffuseTextureIndexToTextureID[textureIndex] = texID;
textureIndex++;
}
// Extra mesh features
for( unsigned int mesh_index = 0; mesh_index < scene->mNumMeshes; ++mesh_index) {
aiMesh * aimesh = scene->mMeshes[mesh_index];
bool has_uv = aimesh->GetNumUVChannels() > 0 && aimesh->mNumUVComponents[0] >= 2;
printf( "Mesh[%u] Has Positions=%d(%u) Faces=%d(%u) "
"Normals=%d Bones=%d NumUV=%d (%d) "
"HasVCols=%d "
"Material=%u Name='%s'\n",
mesh_index,
(int) aimesh->HasPositions(), aimesh->mNumVertices,
(int) aimesh->HasFaces(), aimesh->mNumFaces,
(int) aimesh->HasNormals(),
(int) aimesh->HasBones(),
(int) aimesh->GetNumUVChannels(), (int) aimesh->mNumUVComponents[0],
(int) aimesh->HasVertexColors(0),
aimesh->mMaterialIndex,
aimesh->mName.C_Str()
);
meshDatas.emplace_back(std::make_shared<MeshData>());
auto & meshData = *meshDatas.back();
meshData.vertices.resize( aimesh->mNumVertices);
meshData.normals.resize( aimesh->mNumVertices);
meshData.indices.resize( aimesh->mNumFaces * 3);
if( has_uv) {
meshData.textureUVCoords.resize( aimesh->mNumVertices);
}
if(materialToDiffuseTextureIndex.find(aimesh->mMaterialIndex) != materialToDiffuseTextureIndex.end()) {
auto textureIndex = materialToDiffuseTextureIndex[aimesh->mMaterialIndex];
auto textureId = diffuseTextureIndexToTextureID[textureIndex];
meshData.setTexture(textureId);
}
aiColor3D diffuseColor(0.f,0.f,0.f);
auto & material = scene->mMaterials[aimesh->mMaterialIndex];
material->Get(AI_MATKEY_COLOR_DIFFUSE, diffuseColor);
meshData.diffuseColor = { diffuseColor.r, diffuseColor.g, diffuseColor.b };
for( unsigned int vi = 0; vi < aimesh->mNumVertices; ++vi) {
const auto v = aimesh->mVertices[vi];
const auto n = aimesh->mNormals[vi];
meshData.vertices[vi].set( v.x, v.y, v.z);
meshData.normals[vi].set( n.x, n.y, n.z);
if( has_uv) {
const auto tc = aimesh->mTextureCoords[0][vi];
meshData.textureUVCoords[vi] = { tc.x, 1.0f - tc.y };
}
}
uint32_t index = 0;
for( unsigned int ti = 0; ti < aimesh->mNumFaces; ++ti) {
const auto t = aimesh->mFaces[ti];
if(t.mNumIndices != 3) {
printf("WARNING: Face is not a triangle!\n");
}
meshData.indices[index++] = t.mIndices[0];
meshData.indices[index++] = t.mIndices[1];
meshData.indices[index++] = t.mIndices[2];
}
}
printf("Adding mesh data to mesh data cache for %s\n", filename.c_str());
meshDataCache.add(filename, meshDatas);
return true;
}
bool AssetLoader::loadMesh( const std::string & filename, Mesh & mesh,
bool normalizeScale, float normScaleFactor)
{
std::vector<std::shared_ptr<MeshData>> meshDatas;
std::vector<std::shared_ptr<ImageBase>> textures;
bool ok = loadMeshData(filename, meshDatas, textures);
if(!ok) {
return false;
}
for(auto & meshData : meshDatas) {
mesh.vertices.insert(mesh.vertices.end(), meshData->vertices.begin(), meshData->vertices.end());
mesh.normals.insert(mesh.normals.end(), meshData->normals.begin(), meshData->normals.end());
mesh.textureUVCoords.insert(mesh.textureUVCoords.end(), meshData->textureUVCoords.begin(), meshData->textureUVCoords.end());
mesh.indices.insert(mesh.indices.end(), meshData->indices.begin(), meshData->indices.end());
}
return true;
}
bool AssetLoader::loadMeshes( const std::string & filename,
std::vector<std::shared_ptr<Mesh>> & meshes,
std::vector<std::shared_ptr<ImageBase>> & textures,
bool normalizeScale,
float normScaleFactor)
{
std::vector<std::shared_ptr<MeshData>> meshDatas;
bool ok = loadMeshData(filename, meshDatas, textures);
if(!ok) {
return false;
}
for(auto & meshData : meshDatas) {
meshes.emplace_back(std::make_shared<Mesh>());
Mesh & mesh = *meshes.back();
mesh.vertices = meshData->vertices;
mesh.normals = meshData->normals;
mesh.textureUVCoords = meshData->textureUVCoords;
mesh.indices = meshData->indices;
}
return true;
}