MOS
mesh.hpp
1 #pragma once
2 
3 #include <atomic>
4 #include <chrono>
5 #include <functional>
6 #include <initializer_list>
7 #include <memory>
8 #include <string>
9 #include <vector>
10 
11 #include <mos/core/tracked_container.hpp>
12 #include <mos/gfx/shape.hpp>
13 #include <mos/gfx/vertex.hpp>
14 
15 namespace mos::gfx {
16 
17 class Mesh;
18 using Shared_mesh = std::shared_ptr<Mesh>;
19 using Triangle_indices = std::array<int, 3>;
20 
22 class Mesh final : public Shape {
23 public:
24  using Positions = std::vector<glm::vec3>;
25  using Time_point = std::chrono::time_point<std::chrono::system_clock,
26  std::chrono::nanoseconds>;
27 
28  template <class Tv, class Te>
29  explicit Mesh(const Tv vertices_begin, const Tv vertices_end,
30  Te indices_begin, Te indices_end)
31  : vertices(vertices_begin, vertices_end),
32  indices(indices_begin, indices_end) {
33  calculate_sphere();
34  }
35 
36  explicit Mesh(const std::initializer_list<Vertex> &vertices,
37  const std::initializer_list<Triangle_indices> &triangles);
38 
39  explicit Mesh();
40 
42  static auto load(const std::string &path) -> Mesh;
43 
45  auto clear() -> void;
46 
48  auto positions() const -> Positions;
49 
50  auto mix(const Mesh &mesh1, const Mesh &mesh2, float amount) -> void;
51 
52  auto apply_transform(const glm::mat4 &transform) -> void;
53 
54  auto calculate_normals() -> void;
55 
56  auto calculate_flat_normals() -> void;
57 
58  auto calculate_tangents() -> void;
59 
60  auto calculate_sphere() -> void;
61 
62  auto centroid() const -> glm::vec3;
63 
64  auto radius() const -> float;
65 
68 
69 private:
70  struct Triangle {
71  Vertex &v0;
72  Vertex &v1;
73  Vertex &v2;
74  auto normal() const -> glm::vec3;
75  };
76 
77  static auto calculate_tangents(Vertex &v0, Vertex &v1, Vertex &v2) -> void;
78  void for_each_triangle(
79  const std::function<void(const Triangle &triangle)> &callback);
80 
81  glm::vec3 centroid_{0.0f};
82  float radius_{0.0f};
83 };
84 } // namespace mos::gfx
auto clear() -> void
Erease all vertices and indices.
Base class for geometric shapes.
Definition: shape.hpp:8
Definition: assets.hpp:17
auto positions() const -> Positions
Get only positions from vertices.
Geometric data description, vertices and indices.
Definition: mesh.hpp:22
Container with modified time stamp.
Definition: container.hpp:9
static auto load(const std::string &path) -> Mesh
Load from *.mesh file.
Vertex structure, supported by the renderer.
Definition: vertex.hpp:7