From db41d43345ea73cf7c1bbb29448e52ffb822e3e0 Mon Sep 17 00:00:00 2001 From: vimene Date: Tue, 13 Jan 2026 02:04:52 +0100 Subject: 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() --- src/o3d/camera.hpp | 10 +++------- src/o3d/deriv_vertex.hpp | 4 +--- src/o3d/mesh.cpp | 39 ++++++++++++++++++++++++++++++++------- src/o3d/mesh.hpp | 3 ++- src/o3d/vertex.hpp | 9 ++++----- src/o3d/vertex_data.hpp | 5 +---- 6 files changed, 43 insertions(+), 27 deletions(-) (limited to 'src/o3d') diff --git a/src/o3d/camera.hpp b/src/o3d/camera.hpp index eb10450..ff99c08 100644 --- a/src/o3d/camera.hpp +++ b/src/o3d/camera.hpp @@ -4,18 +4,14 @@ #include "math/mat4.hpp" #include "math/tform.hpp" -using - engine::math::Matrix4, - engine::math::Transform; - namespace engine::o3d { struct Camera { float fov; - Transform transform; + engine::math::Transform transform; - constexpr Matrix4 to_mat4(float aspect_ratio, float min_z, float max_z) const & { - return Matrix4::perspective(fov, aspect_ratio, min_z, max_z) * transform.to_inverse_mat4(); + constexpr engine::math::Matrix4 to_mat4(float aspect_ratio, float min_z, float max_z) const & { + return engine::math::Matrix4::perspective(fov, aspect_ratio, min_z, max_z) * transform.to_inverse_mat4(); } }; diff --git a/src/o3d/deriv_vertex.hpp b/src/o3d/deriv_vertex.hpp index 6fc32e0..5e8dc22 100644 --- a/src/o3d/deriv_vertex.hpp +++ b/src/o3d/deriv_vertex.hpp @@ -5,10 +5,8 @@ namespace engine::o3d { -using engine::math::Vector4; - struct DerivedVertex { - Vector4 vertex; + engine::math::Vector4 vertex; float b0, b1; constexpr DerivedVertex div_by_w() const & { 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 #include #include +#include #include "math/vector.hpp" #include "vulkan_utils.hpp" using namespace engine::o3d; +template +struct VertexIndicesHasher { + // taken from boost's container_hash + std::size_t operator()(const std::array& vertex_indices) const { + size_t h = 0; + for (const auto& idx : vertex_indices) + h ^= std::hash{}(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> Mesh::linearize_indices() const & { + std::unordered_map, size_t, VertexIndicesHasher<3>> unique_vertices; std::vector linearized_vertices; std::vector 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); + } } } diff --git a/src/o3d/mesh.hpp b/src/o3d/mesh.hpp index 525d9bd..d1d3f1c 100644 --- a/src/o3d/mesh.hpp +++ b/src/o3d/mesh.hpp @@ -17,7 +17,8 @@ struct Mesh { std::vector vertices; std::vector normals; - std::vector, 3>> indices; + std::vector uvs; + std::vector, 3>> indices; // TODO: find a better way to do this. This workaround is due to the fact that vulkan only // accepts a single index, not an index for each attributes diff --git a/src/o3d/vertex.hpp b/src/o3d/vertex.hpp index 1605f0d..8d02297 100644 --- a/src/o3d/vertex.hpp +++ b/src/o3d/vertex.hpp @@ -6,12 +6,11 @@ namespace engine::o3d { -using engine::math::Vector3, engine::math::Vector4; - struct Vertex { - Vector4 vertex; - Vector3 normal; - VertexData data; + engine::math::Vector4 vertex; + engine::math::Vector3 normal; + engine::math::Vector2 uv; + engine::o3d::VertexData data; }; } diff --git a/src/o3d/vertex_data.hpp b/src/o3d/vertex_data.hpp index f70100c..fa62271 100644 --- a/src/o3d/vertex_data.hpp +++ b/src/o3d/vertex_data.hpp @@ -3,9 +3,6 @@ #include "math/vector.hpp" -using - engine::math::Vector3; - namespace engine::o3d { struct VertexData { @@ -21,7 +18,7 @@ struct VertexData { }; } - Vector3 world_loc; + engine::math::Vector3 world_loc; }; } -- cgit v1.2.3