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
|
#include "o3d/mesh.hpp"
#include <vector>
#include <array>
#include <cstdint>
#include <cstddef>
#include <tuple>
#include <unordered_map>
#include "math/vector.hpp"
#include "vulkan_utils.hpp"
using namespace engine::o3d;
template<size_t indices_size>
struct VertexIndicesHasher {
// taken from boost's container_hash
std::size_t operator()(const std::array<size_t, indices_size>& vertex_indices) const {
size_t h = 0;
for (const auto& idx : vertex_indices)
h ^= std::hash<size_t>{}(idx) + 0x9e3779b9 + (h << 6) + (h >> 2);
return h;
}
};
Mesh Mesh::plane(float width, float height) {
const float w2 = width / 2,
h2 = height / 2;
return {
{
{ -w2 / 2, 0.f, -h2 / 2 },
{ +w2 / 2, 0.f, -h2 / 2 },
{ +w2 / 2, 0.f, +h2 / 2 },
{ -w2 / 2, 0.f, +h2 / 2 },
},
{
{ 0.f, -1.f, 0.f },
{ 0.f, +1.f, 0.f },
},
{
{ 0.f, 0.f },
{ 1.f, 0.f },
{ 1.f, 1.f },
{ 0.f, 1.f },
},
{
{{ {{ 0, 0, 0 }}, {{ 1, 0, 1 }}, {{ 2, 0, 2 }} }},
{{ {{ 2, 0, 2 }}, {{ 3, 0, 3 }}, {{ 0, 0, 0 }} }},
{{ {{ 0, 1, 0 }}, {{ 3, 1, 3 }}, {{ 2, 1, 2 }} }},
{{ {{ 2, 1, 2 }}, {{ 1, 1, 1 }}, {{ 0, 1, 0 }} }},
}
};
}
std::tuple<std::vector<engine::vk::Vertex>, std::vector<uint16_t>> Mesh::linearize_indices() const & {
std::unordered_map<std::array<size_t, 3>, size_t, VertexIndicesHasher<3>> unique_vertices;
std::vector<engine::vk::Vertex> linearized_vertices;
std::vector<uint16_t> linearized_indices;
for (const auto& triangle_indices : this->indices) {
for (const auto& vertex_indices : triangle_indices) {
auto it = unique_vertices.find(vertex_indices);
if (it == unique_vertices.end()) {
linearized_vertices.emplace_back(this->vertices[vertex_indices[0]], this->normals[vertex_indices[1]], this->uvs[vertex_indices[2]]);
size_t idx = linearized_vertices.size() - 1;
unique_vertices.emplace(vertex_indices, idx);
linearized_indices.emplace_back(idx);
} else {
linearized_indices.emplace_back((*it).second);
}
}
}
// TODO: I'm not sure if passing vectors like that makes a copy or not, because they are passed
// to a structured which is instantly returned. I think that copy-ellision catches that
return std::tuple { linearized_vertices, linearized_indices };
}
|