-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeshModel.cpp
More file actions
156 lines (124 loc) · 3.98 KB
/
MeshModel.cpp
File metadata and controls
156 lines (124 loc) · 3.98 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
#include "MeshModel.h"
MeshModel::MeshModel()
{
}
MeshModel::MeshModel(std::vector<Mesh> newMeshList)
{
meshList = newMeshList;
model = glm::mat4(1.0f);
}
size_t MeshModel::getMeshCount()
{
return meshList.size();
}
Mesh * MeshModel::getMesh(size_t index)
{
if (index >= meshList.size())
{
throw std::runtime_error("Attempted to access invalid Mesh index!");
}
return &meshList[index];
}
glm::mat4 MeshModel::getModel()
{
return model;
}
void MeshModel::setModel(glm::mat4 newModel)
{
model = newModel;
}
void MeshModel::destroyMeshModel()
{
for (auto &mesh : meshList)
{
mesh.destroyBuffers();
}
}
std::vector<std::string> MeshModel::LoadMaterials(const aiScene * scene)
{
// Create 1:1 sized list of textures
std::vector<std::string> textureList(scene->mNumMaterials);
// Go through each material and copy its texture file name (if it exists)
for (size_t i = 0; i < scene->mNumMaterials; i++)
{
// Get the material
aiMaterial * material = scene->mMaterials[i];
// Initialise the texture to empty string (will be replaced if texture exists)
textureList[i] = "";
// Check for a Diffuse Texture (standard detail texture)
if (material->GetTextureCount(aiTextureType_DIFFUSE))
{
// Get the path of the texture file
aiString path;
if (material->GetTexture(aiTextureType_DIFFUSE, 0, &path) == AI_SUCCESS)
{
// Cut off any directory information aleady present
int idx = std::string(path.data).rfind("\\");
std::string fileName = std::string(path.data).substr(idx + 1);
textureList[i] = fileName;
}
}
}
return textureList;
}
std::vector<Mesh> MeshModel::LoadNode(VkPhysicalDevice newPhysicalDevice, VkDevice newDevice, VkQueue transferQueue, VkCommandPool transferCommandPool, aiNode * node, const aiScene * scene, std::vector<int> matToTex)
{
std::vector<Mesh> meshList;
// Go through each mesh at this node and create it, then add it to our meshList
for (size_t i = 0; i < node->mNumMeshes; i++)
{
meshList.push_back(
LoadMesh(newPhysicalDevice, newDevice, transferQueue, transferCommandPool, scene->mMeshes[node->mMeshes[i]], scene, matToTex)
);
}
// Go through each node attached to this node and load it, then append their meshes to this node's mesh list
for (size_t i = 0; i < node->mNumChildren; i++)
{
std::vector<Mesh> newList = LoadNode(newPhysicalDevice, newDevice, transferQueue, transferCommandPool, node->mChildren[i], scene, matToTex);
meshList.insert(meshList.end(), newList.begin(), newList.end());
}
return meshList;
}
Mesh MeshModel::LoadMesh(VkPhysicalDevice newPhysicalDevice, VkDevice newDevice, VkQueue transferQueue, VkCommandPool transferCommandPool, aiMesh * mesh, const aiScene * scene, std::vector<int> matToTex)
{
std::vector<Vertex> vertices;
std::vector<uint32_t> indices;
// Resize vertex list to hold all vertices for mesh
vertices.resize(mesh->mNumVertices);
// Go through each vertex and copy it across to our vertices
for (size_t i = 0; i < mesh->mNumVertices; i++)
{
// Set position
vertices[i].pos = { mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z };
// Set normal
vertices[i].normal = { mesh->mNormals[i].x, mesh->mNormals[i].y, mesh->mNormals[i].z };
// Set tex coords (if they exist)
if (mesh->mTextureCoords[0])
{
vertices[i].tex = { mesh->mTextureCoords[0][i].x, mesh->mTextureCoords[0][i].y };
}
else
{
vertices[i].tex = { 0.0f, 0.0f };
}
// Set colour (just use white for now)
vertices[i].col = { 1.0f, 1.0f, 1.0f };
}
// Iterate over indices through faces and copy across
for (size_t i = 0; i < mesh->mNumFaces; i++)
{
// Get a face
aiFace face = mesh->mFaces[i];
// Go through face's indices and add to list
for (size_t j = 0; j < face.mNumIndices; j++)
{
indices.push_back(face.mIndices[j]);
}
}
// Create new mesh with details and return it
Mesh newMesh = Mesh(newPhysicalDevice, newDevice, transferQueue, transferCommandPool, &vertices, &indices, matToTex[mesh->mMaterialIndex]);
return newMesh;
}
MeshModel::~MeshModel()
{
}