diff options
Diffstat (limited to 'src/o3d')
| -rw-r--r-- | src/o3d/camera.hpp | 10 | ||||
| -rw-r--r-- | src/o3d/deriv_vertex.hpp | 4 | ||||
| -rw-r--r-- | src/o3d/mesh.cpp | 39 | ||||
| -rw-r--r-- | src/o3d/mesh.hpp | 3 | ||||
| -rw-r--r-- | src/o3d/vertex.hpp | 9 | ||||
| -rw-r--r-- | src/o3d/vertex_data.hpp | 5 |
6 files changed, 43 insertions, 27 deletions
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 <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); + } } } 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<engine::math::Vector3> vertices; std::vector<engine::math::Vector3> normals; - std::vector<std::array<std::array<size_t, 2>, 3>> indices; + std::vector<engine::math::Vector2> uvs; + std::vector<std::array<std::array<size_t, 3>, 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; }; } |
