-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.cpp
149 lines (138 loc) · 3.76 KB
/
Model.cpp
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
#include "Model.h"
Model::Model()
{
std::cout<<"MeshCreated\n";
}
void Model::LoadModel(const std::string& FileName)
{
Assimp::Importer AImporter = Assimp::Importer();
const aiScene* Scene = AImporter.ReadFile(FileName, aiProcess_Triangulate| aiProcess_GenSmoothNormals | aiProcess_JoinIdenticalVertices|aiProcess_FlipUVs);
if (!Scene)
{
std::cout << "Model " << FileName << " Failed to load : " << AImporter.GetErrorString() << std::endl;
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]!=nullptr))
{
TextureList[MaterialIndex]->UseTexture();
}
else {
std::cout << "none\n";
}
MeshList[i]->RenderMesh();
}
}
void Model::ClearModel()
{
for (size_t i = 0; i < MeshList.size(); i++)
{
if (MeshList[i]!=nullptr)
{
delete MeshList[i];
MeshList[i] =nullptr;
}
if (TextureList[i]!=nullptr)
{
delete TextureList[i];
TextureList[i] = nullptr;
}
}
}
Model::~Model()
{
for (Mesh* i :MeshList)
delete i;
for (Texture* i : TextureList)
delete i;
MeshList.clear();
TextureList.clear();
MeshToTex.clear();
}
void Model::LoadNode(aiNode* NodeToLoad, const aiScene* SceneToLoad)
{
for (size_t i = 0; i < NodeToLoad->mNumMeshes; i++)
{
LoadMesh(SceneToLoad->mMeshes[NodeToLoad->mMeshes[i]], SceneToLoad);
}
for (size_t i = 0; i < NodeToLoad->mNumChildren; i++)
{
LoadNode(NodeToLoad->mChildren[i], SceneToLoad);
}
}
void Model::LoadMesh(aiMesh* MeshToLoad, const aiScene* Scene)
{
std::vector<GLfloat> Vertices;
std::vector<GLuint> Indices;
for (size_t i = 0; i < MeshToLoad->mNumVertices; i++)
{
Vertices.push_back(MeshToLoad->mVertices[i].x);
Vertices.push_back(MeshToLoad->mVertices[i].y);
Vertices.push_back(MeshToLoad->mVertices[i].z);
if (MeshToLoad->mTextureCoords[0])
{
Vertices.push_back(MeshToLoad->mTextureCoords[0][i].x);
Vertices.push_back(MeshToLoad->mTextureCoords[0][i].y);
}
else
{
Vertices.push_back(0.f);
Vertices.push_back(0.f);
}
Vertices.push_back(-MeshToLoad->mNormals[i].x);
Vertices.push_back(-MeshToLoad->mNormals[i].y);
Vertices.push_back(-MeshToLoad->mNormals[i].z);
}
for (size_t i = 0; i < MeshToLoad->mNumFaces; i++)
{
aiFace Face = MeshToLoad->mFaces[i];
for (size_t j = 0; j < Face.mNumIndices; j++)
{
Indices.push_back(Face.mIndices[j]);
}
}
Mesh* NewMesh = new Mesh();
NewMesh->CreateMesh(&Vertices.front(), &Indices.front(), Vertices.size(), Indices.size());
MeshList.push_back(NewMesh);
MeshToTex.push_back(MeshToLoad->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 StringPath;
if (Material->GetTexture(aiTextureType_DIFFUSE, 0, &StringPath) == AI_SUCCESS)
{
int Index = std::string(StringPath.data).rfind("\\");
//std::cout << Index << std::endl;
std::string FileName = std::string(StringPath.data).substr(Index + 1);
std::string TexturePath = std::string("Textures/Models/")+ FileName;
std::cout << "Tex path is" << TexturePath<< std::endl;
TextureList[i] = new Texture((char*)TexturePath.c_str());
if (!TextureList[i]->LoadTexture())
{
std::cout << "Failed to load texture at " << TexturePath << std::endl;
delete TextureList[i];
//TextureList[i] = new Texture((char*)"Textures/plain.png");
TextureList[i]->LoadTexture();
}
}
}
if (!TextureList[i])
{
TextureList[i] = new Texture((char*)"Textures/Defaults/plain.png");
}
}
}