-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
147 lines (127 loc) · 4.82 KB
/
Copy pathModel.cpp
File metadata and controls
147 lines (127 loc) · 4.82 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
//
// Created by galismac on 10/8/2022.
//
#include "Model.h"
#include "Resource.h"
#include "Platform.h"
#include "assimp_compat.h"
bool Model::isLoad() const {
return loadStatus;
}
bool Model::load() {
if (!Resource::CheckPath(modelPath)) {
return false;
}
dir = modelPath.substr(0, modelPath.find_last_of('/'));
Assimp::Importer importer;
const AiScene *scene = importer.ReadFile(modelPath,
aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_FlipUVs | aiProcess_CalcTangentSpace);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) // if is Not Zero
{
Platform::Debug("加载失败!");
return false;
}
processNode(scene->mRootNode, scene);
return true;
}
const string &Model::getPath() const {
return modelPath;
}
void Model::setPath(const string &path) {
modelPath = path;
}
Model::Model(string &path) : modelPath(path) {
}
Model::Model(string &&path) : modelPath(std::move(path)) {
}
void Model::processNode(AiNode *node, const AiScene *scene) {
for (int i = 0; i < node->mNumMeshes; i++) {
meshs.push_back(processMesh(scene->mMeshes[node->mMeshes[i]], scene));
}
for (int i = 0; i < node->mNumChildren; i++) {
processNode(node->mChildren[i], scene);
}
}
Mesh Model::processMesh(AiMesh *mesh, const AiScene *scene) {
vector<Vertex> vertices;
vector<unsigned int> indices;
vector<Texture> textures;
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
Vertex vertex;
glm::vec3 vector;
//处理位置
vector.x = mesh->mVertices[i].x;
vector.y = mesh->mVertices[i].y;
vector.z = mesh->mVertices[i].z;
vertex.Position = vector;
//处理法线
if (mesh->HasNormals()) {
vector.x = mesh->mNormals[i].x;
vector.y = mesh->mNormals[i].y;
vector.z = mesh->mNormals[i].z;
vertex.Normal = vector;
}
//处理纹理坐标
if (mesh->mTextureCoords[0]) {
glm::vec2 vec;
//假设使用纹理坐标类型0
vec.x = mesh->mTextureCoords[0][i].x;
vec.y = mesh->mTextureCoords[0][i].y;
vertex.TexCoords = vec;
// tangent
vector.x = mesh->mTangents[i].x;
vector.y = mesh->mTangents[i].y;
vector.z = mesh->mTangents[i].z;
vertex.Tangent = vector;
// bitangent
vector.x = mesh->mBitangents[i].x;
vector.y = mesh->mBitangents[i].y;
vector.z = mesh->mBitangents[i].z;
vertex.Bitangent = vector;
} else {
vertex.TexCoords = glm::vec2(0.0f, 0.0f);
vertex.Tangent = {0, 0, 0};
vertex.Bitangent= {0, 0, 0};
}
vertices.push_back(vertex);
}
// now wak through each of the mesh's faces (a face is a mesh its triangle) and retrieve the corresponding vertex indices.
for (unsigned int i = 0; i < mesh->mNumFaces; i++) {
aiFace face = mesh->mFaces[i];
// retrieve all indices of the face and store them in the indices vector
for (unsigned int j = 0; j < face.mNumIndices; j++)
indices.push_back(face.mIndices[j]);
}
aiMaterial *material = scene->mMaterials[mesh->mMaterialIndex];
// 1. diffuse maps
vector<Texture> diffuseMaps = loadMaterialTextures(material, aiTextureType_DIFFUSE, "texture_diffuse");
textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());
// 2. specular maps
vector<Texture> specularMaps = loadMaterialTextures(material, aiTextureType_SPECULAR, "texture_specular");
textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());
// 3. normal maps
std::vector<Texture> normalMaps = loadMaterialTextures(material, aiTextureType_HEIGHT, "texture_normal");
textures.insert(textures.end(), normalMaps.begin(), normalMaps.end());
// 4. height maps
std::vector<Texture> heightMaps = loadMaterialTextures(material, aiTextureType_AMBIENT, "texture_height");
textures.insert(textures.end(), heightMaps.begin(), heightMaps.end());
return Mesh(vertices, indices, textures);
}
vector<Texture> Model::loadMaterialTextures(aiMaterial *mat, aiTextureType type, string typeName) {
vector<Texture> textures;
for (unsigned int i = 0; i < mat->GetTextureCount(type); i++) {
aiString str;
mat->GetTexture(type, i, &str);
Texture texture(dir + "/" + str.C_Str());
texture.load();//马上加载...
textures.push_back(texture);
}
return textures;
}
std::ostream &operator<<(std::ostream &os, const Model &model) {
os << "loadStatus: " << model.loadStatus << " path: " << model.modelPath;
return os;
}
vector<Mesh> &Model::getMeshs() {
return meshs;
}