-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
127 lines (108 loc) · 3.38 KB
/
Model.cpp
File metadata and controls
127 lines (108 loc) · 3.38 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
#include "Model.h"
Model::Model()
{
}
void Model::LoadModel(const std::string& fileName) {
Assimp::Importer importer;
const aiScene *scene = importer.ReadFile(fileName, aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_JoinIdenticalVertices | aiProcess_FlipUVs);
if (!scene) {
printf("Model (%s) failed to load: %s",fileName, importer.GetErrorString());
return;
}
LoadNode(scene->mRootNode, scene);
LoadMaterials(scene);
}
void Model::RenderModel()
{
for (size_t i = 0; i < meshList.size(); i++) {
unsigned int materialIndex = meshToTex[i];
if (materialIndex < textureList.size() && textureList[materialIndex]) {
textureList[materialIndex]->UseTexture();
}
meshList[i]->RenderMesh();
}
}
void Model::ClearModel()
{
for (size_t i = 0; i < meshList.size(); i++) {
if (meshList[i]) {
delete meshList[i];
meshList[i] = nullptr;
}
}
for (size_t i = 0; i < textureList.size(); i++) {
if (textureList[i]) {
delete textureList[i];
textureList[i] = nullptr;
}
}
}
Model::~Model()
{
}
void Model::LoadNode(aiNode * node, const aiScene * scene)
{
for (size_t i = 0; i < node->mNumMeshes; i++) {
LoadMesh(scene->mMeshes[node->mMeshes[i]], scene);
}
for (size_t i = 0; i < node->mNumChildren; i++) {
LoadNode(node->mChildren[i], scene);
}
}
void Model::LoadMesh(aiMesh * mesh, const aiScene * scene)
{
std::vector<GLfloat> vertices;
std::vector<unsigned int> indices;
for (size_t i= 0; i < mesh->mNumVertices; i++) {
//插入顶点坐标
vertices.insert(vertices.end(), { mesh->mVertices[i].x,mesh->mVertices[i].y ,mesh->mVertices[i].z });
//插入texture
if (mesh->mTextureCoords[0]) {
vertices.insert(vertices.end(), { mesh->mTextureCoords[0][i].x,mesh->mTextureCoords[0][i].y });
}
else {
vertices.insert(vertices.end(), { 0.0f,0.0f });
}
//插入法向量
vertices.insert(vertices.end(), { -mesh->mNormals[i].x,-mesh->mNormals[i].y ,-mesh->mNormals[i].z });//
}
for (size_t i = 0; i < mesh->mNumFaces; i++) {
aiFace face = mesh->mFaces[i];
for (size_t j = 0; j < face.mNumIndices; j++) {
indices.push_back(face.mIndices[j]);
}
}
Mesh* newMesh = new Mesh();
newMesh->CreateMesh(&vertices[0], &indices[0], vertices.size(), indices.size());
meshList.push_back(newMesh);
meshToTex.push_back(mesh->mMaterialIndex);
}
void Model::LoadMaterials(const aiScene * scene)
{
textureList.resize(scene->mNumMaterials);
for (size_t i = 0; i < scene->mNumMaterials; i++) {
aiMaterial* material = scene->mMaterials[i];
textureList[i] = nullptr;
if (material->GetTextureCount(aiTextureType_DIFFUSE)) {
aiString path;
if (material->GetTexture(aiTextureType_DIFFUSE, 0, &path) == AI_SUCCESS) {
int idx = std::string(path.data).rfind("\\");
if (idx >= std::string(path.data).length()) {
idx = std::string(path.data).rfind("/");
}
std::string filename = std::string(path.data).substr(idx + 1);
std::string texPath = std::string("Textures/") + filename;
textureList[i] = new Texture(texPath.c_str());
if (!textureList[i]->LoadTexture()) {
printf("failed to load texture at: %s\n", texPath);
delete textureList[i];
textureList[i] = nullptr;
}
}
}
if (!textureList[i]) {
textureList[i] = new Texture("Textures/plain.png");
textureList[i]->LoadTextureA();
}
}
}