-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModelLoader.cpp
83 lines (66 loc) · 2.2 KB
/
ModelLoader.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
#include "ModelLoader.hpp"
ModelLoader::ModelLoader(const VkDevice& device, const VkPhysicalDevice& physicalDevice,
const VkCommandPool& commandPool, const VkQueue& queue,
const VkPhysicalDeviceProperties& properties)
:
device(device),
physicalDevice(physicalDevice),
commandPool(commandPool),
queue(queue),
properties(properties)
{
}
Model ModelLoader::loadModel(const std::string PATH, bool verticalFlipTexture)
{
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string warn;
std::vector<Vertex> vertices;
std::vector<uint32_t> indices;
std::vector<Texture> textures;
size_t index = PATH.find_last_of("\\");
std::string fileName = PATH.substr(index + 1).c_str();
std::string path = PATH.substr(0, index + 1).c_str();
if (!tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, PATH.c_str(), path.c_str())) {
throw std::runtime_error(warn);
}
for (const auto& shape : shapes) {
for (const auto& index : shape.mesh.indices) {
Vertex vertex{};
vertex.position = {
attrib.vertices[3 * index.vertex_index + 0],
attrib.vertices[3 * index.vertex_index + 1],
attrib.vertices[3 * index.vertex_index + 2]
};
if (attrib.texcoords.size() != 0) {
if (verticalFlipTexture)
vertex.texCoord = {
attrib.texcoords[2 * index.texcoord_index + 0],
1.0f - attrib.texcoords[2 * index.texcoord_index + 1]
};
else
vertex.texCoord = {
attrib.texcoords[2 * index.texcoord_index + 0],
attrib.texcoords[2 * index.texcoord_index + 1]
};
}
if (attrib.normals.size() != 0) {
vertex.normal = {
attrib.normals[3 * index.normal_index + 0],
attrib.normals[3 * index.normal_index + 1],
attrib.normals[3 * index.normal_index + 2]
};
}
vertices.push_back(vertex);
indices.push_back(static_cast<uint32_t>(indices.size()));
}
}
bool hasTexture = false;
for (size_t i = 0; i < materials.size(); i++) {
if (materials[i].diffuse_texname != "") {
textures.emplace_back(device, physicalDevice, commandPool, queue, path + materials[i].diffuse_texname, properties);
}
}
return Model(device, physicalDevice, commandPool, queue, vertices, indices, textures, fileName);
}