aboutsummaryrefslogtreecommitdiff
path: root/src/o3d/mesh.cpp
diff options
context:
space:
mode:
authorvimene <vincent.menegaux@gmail.com>2026-01-13 02:04:52 +0100
committervimene <vincent.menegaux@gmail.com>2026-01-13 02:04:52 +0100
commitdb41d43345ea73cf7c1bbb29448e52ffb822e3e0 (patch)
tree4635d654e301b3f31f8d2626f3bc2c6f2a6e50a8 /src/o3d/mesh.cpp
parent7f08187a46e30925e4563585fab2c6f92400330a (diff)
downloadengine-db41d43345ea73cf7c1bbb29448e52ffb822e3e0.tar.gz
added textures for the hardware renderer
- removed "using" directive in .hpp - reverse order of arguments for quaternion rotation, i.e. q.rot(v) instead of v.rot(q), where q is a quaterinon and v a vector - pass the inverse of the view matrix to render_and_present_frame, to allow light calculation in shaders (we used to just pass the matrix of the quaternion of the transformation, i.e. discard scaling and translations) - added another mesh and texture (viking_room) for testing purposes - added transparent background option - added Quaternion::look_towards(), which is the equivalent of Matrix4::look_at() but only for rotations - various improvement to .obj parsing - load texture coordinates from .obj file - merged duplicate vertices in Mesh::linearize_indices()
Diffstat (limited to 'src/o3d/mesh.cpp')
-rw-r--r--src/o3d/mesh.cpp39
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);
+ }
}
}