Line |
Branch |
Exec |
Source |
1 |
|
|
#include <vk_mesh.h> |
2 |
|
|
#include <tiny_obj_loader.h> |
3 |
|
|
#include <iostream> |
4 |
|
|
|
5 |
|
|
VertexInputDescription Vertex::get_vertex_description() |
6 |
|
|
{ |
7 |
|
✗ |
VertexInputDescription description; |
8 |
|
|
|
9 |
|
|
//we will have just 1 vertex buffer binding, with a per-vertex rate |
10 |
|
✗ |
VkVertexInputBindingDescription mainBinding = {}; |
11 |
|
✗ |
mainBinding.binding = 0; |
12 |
|
✗ |
mainBinding.stride = sizeof(Vertex); |
13 |
|
✗ |
mainBinding.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; |
14 |
|
|
|
15 |
|
✗ |
description.bindings.push_back(mainBinding); |
16 |
|
|
|
17 |
|
|
//Position will be stored at Location 0 |
18 |
|
✗ |
VkVertexInputAttributeDescription positionAttribute = {}; |
19 |
|
✗ |
positionAttribute.binding = 0; |
20 |
|
✗ |
positionAttribute.location = 0; |
21 |
|
✗ |
positionAttribute.format = VK_FORMAT_R32G32B32_SFLOAT; |
22 |
|
✗ |
positionAttribute.offset = offsetof(Vertex, position); |
23 |
|
|
|
24 |
|
|
//Normal will be stored at Location 1 |
25 |
|
✗ |
VkVertexInputAttributeDescription normalAttribute = {}; |
26 |
|
✗ |
normalAttribute.binding = 0; |
27 |
|
✗ |
normalAttribute.location = 1; |
28 |
|
✗ |
normalAttribute.format = VK_FORMAT_R32G32B32_SFLOAT; |
29 |
|
✗ |
normalAttribute.offset = offsetof(Vertex, normal); |
30 |
|
|
|
31 |
|
|
//Position will be stored at Location 2 |
32 |
|
✗ |
VkVertexInputAttributeDescription colorAttribute = {}; |
33 |
|
✗ |
colorAttribute.binding = 0; |
34 |
|
✗ |
colorAttribute.location = 2; |
35 |
|
✗ |
colorAttribute.format = VK_FORMAT_R32G32B32_SFLOAT; |
36 |
|
✗ |
colorAttribute.offset = offsetof(Vertex, color); |
37 |
|
|
|
38 |
|
|
//UV will be stored at Location 2 |
39 |
|
✗ |
VkVertexInputAttributeDescription uvAttribute = {}; |
40 |
|
✗ |
uvAttribute.binding = 0; |
41 |
|
✗ |
uvAttribute.location = 3; |
42 |
|
✗ |
uvAttribute.format = VK_FORMAT_R32G32_SFLOAT; |
43 |
|
✗ |
uvAttribute.offset = offsetof(Vertex, uv); |
44 |
|
|
|
45 |
|
|
|
46 |
|
✗ |
description.attributes.push_back(positionAttribute); |
47 |
|
✗ |
description.attributes.push_back(normalAttribute); |
48 |
|
✗ |
description.attributes.push_back(colorAttribute); |
49 |
|
✗ |
description.attributes.push_back(uvAttribute); |
50 |
|
✗ |
return description; |
51 |
|
✗ |
} |
52 |
|
|
|
53 |
|
|
bool Mesh::load_from_obj(const char* filename) |
54 |
|
|
{ |
55 |
|
|
//attrib will contain the vertex arrays of the file |
56 |
|
✗ |
tinyobj::attrib_t attrib; |
57 |
|
|
//shapes contains the info for each separate object in the file |
58 |
|
✗ |
std::vector<tinyobj::shape_t> shapes; |
59 |
|
|
//materials contains the information about the material of each shape, but we wont use it. |
60 |
|
✗ |
std::vector<tinyobj::material_t> materials; |
61 |
|
|
|
62 |
|
|
//error and warning output from the load function |
63 |
|
✗ |
std::string warn; |
64 |
|
✗ |
std::string err; |
65 |
|
|
|
66 |
|
|
//load the OBJ file |
67 |
|
✗ |
tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, filename, |
68 |
|
|
nullptr); |
69 |
|
|
//make sure to output the warnings to the console, in case there are issues with the file |
70 |
|
✗ |
if (!warn.empty()) { |
71 |
|
✗ |
std::cout << "WARN: " << warn << std::endl; |
72 |
|
|
} |
73 |
|
|
//if we have any error, print it to the console, and break the mesh loading. |
74 |
|
|
//This happens if the file cant be found or is malformed |
75 |
|
✗ |
if (!err.empty()) { |
76 |
|
✗ |
std::cerr << err << std::endl; |
77 |
|
✗ |
return false; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
// Loop over shapes |
81 |
|
✗ |
for (size_t s = 0; s < shapes.size(); s++) { |
82 |
|
|
// Loop over faces(polygon) |
83 |
|
✗ |
size_t index_offset = 0; |
84 |
|
✗ |
for (size_t f = 0; f < shapes[s].mesh.num_face_vertices.size(); f++) { |
85 |
|
|
|
86 |
|
|
//hardcode loading to triangles |
87 |
|
✗ |
int fv = 3; |
88 |
|
|
|
89 |
|
|
// Loop over vertices in the face. |
90 |
|
✗ |
for (size_t v = 0; v < fv; v++) { |
91 |
|
|
// access to vertex |
92 |
|
✗ |
tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v]; |
93 |
|
|
|
94 |
|
|
//vertex position |
95 |
|
✗ |
tinyobj::real_t vx = attrib.vertices[3 * idx.vertex_index + 0]; |
96 |
|
✗ |
tinyobj::real_t vy = attrib.vertices[3 * idx.vertex_index + 1]; |
97 |
|
✗ |
tinyobj::real_t vz = attrib.vertices[3 * idx.vertex_index + 2]; |
98 |
|
|
//vertex normal |
99 |
|
✗ |
tinyobj::real_t nx = attrib.normals[3 * idx.normal_index + 0]; |
100 |
|
✗ |
tinyobj::real_t ny = attrib.normals[3 * idx.normal_index + 1]; |
101 |
|
✗ |
tinyobj::real_t nz = attrib.normals[3 * idx.normal_index + 2]; |
102 |
|
|
|
103 |
|
|
//vertex uv |
104 |
|
✗ |
tinyobj::real_t ux = attrib.texcoords[2 * idx.texcoord_index + 0]; |
105 |
|
✗ |
tinyobj::real_t uy = attrib.texcoords[2 * idx.texcoord_index + 1]; |
106 |
|
|
|
107 |
|
|
//copy it into our vertex |
108 |
|
|
Vertex new_vert; |
109 |
|
✗ |
new_vert.position.x = vx; |
110 |
|
✗ |
new_vert.position.y = vy; |
111 |
|
✗ |
new_vert.position.z = vz; |
112 |
|
|
|
113 |
|
✗ |
new_vert.normal.x = nx; |
114 |
|
✗ |
new_vert.normal.y = ny; |
115 |
|
✗ |
new_vert.normal.z = nz; |
116 |
|
|
|
117 |
|
|
|
118 |
|
✗ |
new_vert.uv.x = ux; |
119 |
|
✗ |
new_vert.uv.y = 1-uy; |
120 |
|
|
|
121 |
|
|
//we are setting the vertex color as the vertex normal. This is just for display purposes |
122 |
|
✗ |
new_vert.color = new_vert.normal; |
123 |
|
|
|
124 |
|
|
|
125 |
|
✗ |
_vertices.push_back(new_vert); |
126 |
|
|
} |
127 |
|
✗ |
index_offset += fv; |
128 |
|
|
} |
129 |
|
|
} |
130 |
|
|
|
131 |
|
✗ |
return true; |
132 |
|
✗ |
} |
133 |
|
|
|