-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeshModel.cpp
More file actions
131 lines (104 loc) · 2.95 KB
/
Copy pathMeshModel.cpp
File metadata and controls
131 lines (104 loc) · 2.95 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
#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)
{
std::vector<std::string> textureList(scene->mNumMaterials);
for (size_t i = 0; i < scene->mNumMaterials; i++)
{
aiMaterial* material = scene->mMaterials[i];
textureList[i] = "";
if (material->GetTextureCount(aiTextureType_DIFFUSE))
{
aiString path;
if (material->GetTexture(aiTextureType_DIFFUSE, 0, &path) == AI_SUCCESS)
{
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;
for (size_t i = 0; i < node->mNumMeshes; i++)
{
meshList.push_back(
LoadMesh(newPhysicalDevice, newDevice, transferQueue, transferCommandPool, scene->mMeshes[node->mMeshes[i]], scene, matToTex)
);
}
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;
vertices.resize(mesh->mNumVertices);
for (size_t i = 0; i < mesh->mNumVertices; i++)
{
vertices[i].pos = { mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z };
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 };
}
vertices[i].col = { 1.0f, 1.0f, 1.0f };
}
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 = Mesh(newPhysicalDevice, newDevice, transferQueue, transferCommandPool, &vertices, &indices, matToTex[mesh->mMaterialIndex]);
return newMesh;
}
MeshModel::~MeshModel()
{
}