FCT
载入中...
搜索中...
未找到
Assimp_ModelLoader.cpp
浏览该文件的文档.
2namespace FCT
3{
4 std::unique_ptr<ModelData> Assimp_ModelLoader::loadModel(const std::string& path)
5 {
6 Assimp::Importer importer;
7 const auto aiScene = importer.ReadFile(path,
8 aiProcess_CalcTangentSpace |
9 aiProcess_Triangulate |
10 aiProcess_JoinIdenticalVertices |
11 aiProcess_SortByPType);
12
13 auto ret = std::make_unique<ModelData>();
14 for (unsigned int i = 0; i < aiScene->mNumMeshes; i++)
15 {
16 const auto aiMesh = aiScene->mMeshes[i];
17 auto mesh = new ModelMesh();
18 mesh->name = aiMesh->mName.C_Str();
19 mesh->material = nullptr;
20
21 mesh->vertices.resize(aiMesh->mNumVertices);
22
23 if (aiMesh->HasPositions())
24 {
25 for (unsigned int j = 0; j < aiMesh->mNumVertices; j++) {
26 const auto& aiVertex = aiMesh->mVertices[j];
27 mesh->vertices[j].position = { aiVertex.x, aiVertex.y, aiVertex.z };
28 }
29 }
30
31 if (aiMesh->HasNormals())
32 {
33 for (unsigned int j = 0; j < aiMesh->mNumVertices; j++) {
34 const auto& aiNormal = aiMesh->mNormals[j];
35 mesh->vertices[j].normal = { aiNormal.x, aiNormal.y, aiNormal.z };
36 }
37 }
38
39 for (unsigned int texCoordSet = 0; texCoordSet < 8; texCoordSet++)
40 {
41 if (aiMesh->HasTextureCoords(texCoordSet))
42 {
43 for (unsigned int j = 0; j < aiMesh->mNumVertices; j++) {
44 const auto& aiTexCoord = aiMesh->mTextureCoords[texCoordSet][j];
45 mesh->vertices[j].texCoords[texCoordSet] = { aiTexCoord.x, aiTexCoord.y };
46 }
47 }
48 }
49
50 if (aiMesh->HasTangentsAndBitangents())
51 {
52 for (unsigned int j = 0; j < aiMesh->mNumVertices; j++) {
53 const auto& aiTangent = aiMesh->mTangents[j];
54 const auto& aiBitangent = aiMesh->mBitangents[j];
55 mesh->vertices[j].tangent = { aiTangent.x, aiTangent.y, aiTangent.z };
56 mesh->vertices[j].bitangent = { aiBitangent.x, aiBitangent.y, aiBitangent.z };
57 }
58 }
59
60 for (unsigned int colorSet = 0; colorSet < 8; colorSet++)
61 {
62 if (aiMesh->HasVertexColors(colorSet))
63 {
64 for (unsigned int j = 0; j < aiMesh->mNumVertices; j++) {
65 const auto& aiColor = aiMesh->mColors[colorSet][j];
66 mesh->vertices[j].colors[colorSet] = { aiColor.r, aiColor.g, aiColor.b, aiColor.a };
67 }
68 }
69 }
70
71 if (aiMesh->HasFaces())
72 {
73 mesh->indices.reserve(aiMesh->mNumFaces * 3);
74 for (unsigned int j = 0; j < aiMesh->mNumFaces; j++)
75 {
76 const aiFace& face = aiMesh->mFaces[j];
77 for (unsigned int k = 0; k < face.mNumIndices; k++)
78 {
79 mesh->indices.push_back(face.mIndices[k]);
80 }
81 }
82 }
83
84 ret->meshes.push_back(mesh);
85 }
86 return ret;
87 }
88}
std::unique_ptr< ModelData > loadModel(const std::string &path) override