diff options
Diffstat (limited to 'src/o3d/mesh.cpp')
| -rw-r--r-- | src/o3d/mesh.cpp | 39 |
1 files changed, 32 insertions, 7 deletions
diff --git a/src/o3d/mesh.cpp b/src/o3d/mesh.cpp index ce6da30..fc5ff78 100644 --- a/src/o3d/mesh.cpp +++ b/src/o3d/mesh.cpp @@ -4,11 +4,23 @@ #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; @@ -24,23 +36,36 @@ Mesh Mesh::plane(float width, float height) { { 0.f, +1.f, 0.f }, }, { - {{ {{ 0, 0 }}, {{ 1, 0 }}, {{ 2, 0 }} }}, - {{ {{ 2, 0 }}, {{ 3, 0 }}, {{ 0, 0 }} }}, - {{ {{ 0, 1 }}, {{ 3, 1 }}, {{ 2, 1 }} }}, - {{ {{ 2, 1 }}, {{ 1, 1 }}, {{ 0, 1 }} }}, + { 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; - size_t n = 0; for (const auto& triangle_indices : this->indices) { for (const auto& vertex_indices : triangle_indices) { - linearized_vertices.emplace_back(this->vertices[vertex_indices[0]], this->normals[vertex_indices[1]]); - linearized_indices.emplace_back(n++); + 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); + } } } |
