diff options
Diffstat (limited to 'src/o3d')
| -rw-r--r-- | src/o3d/mesh.cpp | 40 | ||||
| -rw-r--r-- | src/o3d/mesh.hpp | 21 |
2 files changed, 43 insertions, 18 deletions
diff --git a/src/o3d/mesh.cpp b/src/o3d/mesh.cpp index c1af5cd..ce6da30 100644 --- a/src/o3d/mesh.cpp +++ b/src/o3d/mesh.cpp @@ -1,8 +1,11 @@ #include "o3d/mesh.hpp" #include <vector> #include <array> +#include <cstdint> #include <cstddef> +#include <tuple> #include "math/vector.hpp" +#include "vulkan_utils.hpp" using namespace engine::o3d; @@ -11,20 +14,37 @@ Mesh Mesh::plane(float width, float height) { h2 = height / 2; return { { - {-w2 / 2, 0.f, -h2 / 2, 1.f}, - {+w2 / 2, 0.f, -h2 / 2, 1.f}, - {+w2 / 2, 0.f, +h2 / 2, 1.f}, - {-w2 / 2, 0.f, +h2 / 2, 1.f}, + { -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, -1.f, 0.f }, + { 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, 0 }}, {{ 1, 0 }}, {{ 2, 0 }} }}, + {{ {{ 2, 0 }}, {{ 3, 0 }}, {{ 0, 0 }} }}, + {{ {{ 0, 1 }}, {{ 3, 1 }}, {{ 2, 1 }} }}, + {{ {{ 2, 1 }}, {{ 1, 1 }}, {{ 0, 1 }} }}, } }; } + +std::tuple<std::vector<engine::vk::Vertex>, std::vector<uint16_t>> Mesh::linearize_indices() const & { + 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++); + } + } + + // 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 }; +} diff --git a/src/o3d/mesh.hpp b/src/o3d/mesh.hpp index 2c3a065..525d9bd 100644 --- a/src/o3d/mesh.hpp +++ b/src/o3d/mesh.hpp @@ -1,24 +1,29 @@ -#ifndef O3D_MESH_H -#define O3D_MESH_H +#ifndef O3D_MESH_HPP +#define O3D_MESH_HPP #include <vector> #include <array> #include <iterator> +#include <cstdint> #include <cstddef> +#include <tuple> #include "math/vector.hpp" +#include "vulkan_utils.hpp" namespace engine::o3d { -using engine::math::Vector3, engine::math::Vector4; - struct Mesh { static Mesh plane(float width, float height); - std::vector<Vector4> vertices; - std::vector<Vector3> normals; - std::vector<std::array<std::array<std::size_t, 2>, 3>> indices; + std::vector<engine::math::Vector3> vertices; + std::vector<engine::math::Vector3> normals; + std::vector<std::array<std::array<size_t, 2>, 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 + std::tuple<std::vector<engine::vk::Vertex>, std::vector<uint16_t>> linearize_indices() const &; }; } -#endif // O3D_MESH_H +#endif // O3D_MESH_HPP |
