]> git.localhorst.tv Git - blank.git/blob - src/model/Shape.hpp
split geometry lib
[blank.git] / src / model / Shape.hpp
1 #ifndef BLANK_MODEL_SHAPE_HPP_
2 #define BLANK_MODEL_SHAPE_HPP_
3
4 #include "CollisionBounds.hpp"
5 #include "../geometry/primitive.hpp"
6 #include "../graphics/BlockMesh.hpp"
7 #include "../graphics/EntityMesh.hpp"
8 #include "../world/Block.hpp"
9
10 #include <memory>
11 #include <vector>
12 #include <glm/glm.hpp>
13
14
15 namespace blank {
16
17 class TokenStreamReader;
18
19 class Shape {
20
21 public:
22         struct Faces {
23                 bool face[Block::FACE_COUNT];
24                 Faces &operator =(const Faces &other) noexcept {
25                         for (int i = 0; i < Block::FACE_COUNT; ++i) {
26                                 face[i] = other.face[i];
27                         }
28                         return *this;
29                 }
30                 bool operator [](Block::Face f) const noexcept {
31                         return face[f];
32                 }
33         };
34
35
36 public:
37         Shape();
38
39         void Read(TokenStreamReader &);
40
41         bool FaceFilled(Block::Face face) const noexcept {
42                 return fill[face];
43         }
44
45         std::size_t VertexCount() const noexcept { return vertices.size(); }
46         std::size_t IndexCount() const noexcept { return indices.size(); }
47
48         const glm::vec3 &VertexNormal(size_t idx) const noexcept {
49                 return vertices[idx].normal;
50         }
51         glm::vec3 VertexNormal(size_t idx, const glm::mat4 &M) const noexcept {
52                 return glm::vec3(M * glm::vec4(VertexNormal(idx), 0.0f));
53         }
54
55         void Fill(
56                 EntityMesh::Buffer &,
57                 const std::vector<float> &tex_map
58         ) const;
59         void Fill(
60                 EntityMesh::Buffer &,
61                 const glm::mat4 &transform,
62                 const std::vector<float> &tex_map
63         ) const;
64         void Fill(
65                 BlockMesh::Buffer &,
66                 const glm::mat4 &transform,
67                 const std::vector<float> &tex_map,
68                 std::size_t idx_offset = 0
69         ) const;
70
71         size_t OutlineCount() const noexcept;
72         size_t OutlineIndexCount() const noexcept;
73         void Outline(PrimitiveMesh::Buffer &out) const;
74
75         bool Intersects(
76                 const Ray &,
77                 const glm::mat4 &,
78                 float &dist,
79                 glm::vec3 &normal
80         ) const noexcept;
81         bool Intersects(
82                 const glm::mat4 &M,
83                 const AABB &box,
84                 const glm::mat4 &box_M,
85                 float &depth,
86                 glm::vec3 &normal
87         ) const noexcept;
88
89 private:
90         static float TexR(const std::vector<float> &, std::size_t) noexcept;
91
92 private:
93         std::unique_ptr<CollisionBounds> bounds;
94         struct Vertex {
95                 glm::vec3 position;
96                 glm::vec3 normal;
97                 glm::vec2 tex_st;
98                 std::size_t tex_id;
99         };
100         std::vector<Vertex> vertices;
101         std::vector<std::size_t> indices;
102         Faces fill;
103
104 };
105
106 }
107
108 #endif