]> git.localhorst.tv Git - blank.git/blob - src/model/Shape.hpp
collect collisions for each entity
[blank.git] / src / model / Shape.hpp
1 #ifndef BLANK_MODEL_SHAPE_HPP_
2 #define BLANK_MODEL_SHAPE_HPP_
3
4 #include "BlockModel.hpp"
5 #include "Model.hpp"
6 #include "OutlineModel.hpp"
7
8 #include <vector>
9 #include <glm/glm.hpp>
10
11
12 namespace blank {
13
14 class AABB;
15 class Ray;
16
17 struct Shape {
18
19         /// the number of vertices (and normals) this shape has
20         size_t VertexCount() const noexcept { return vtx_pos.size(); }
21         /// the number of vertex indices this shape has
22         size_t VertexIndexCount() const noexcept { return vtx_idx.size(); }
23
24         const Model::Normal &VertexNormal(size_t idx) const noexcept { return vtx_nrm[idx]; }
25         Model::Normal VertexNormal(
26                 size_t idx, const glm::mat4 &transform
27         ) const noexcept {
28                 return Model::Normal(transform * glm::vec4(vtx_nrm[idx], 0.0f));
29         }
30
31         /// fill given buffers with this shape's elements with an
32         /// optional transform and offset
33         void Vertices(
34                 Model::Positions &vertex,
35                 Model::Normals &normal,
36                 Model::Indices &index
37         ) const;
38         void Vertices(
39                 Model::Positions &vertex,
40                 Model::Normals &normal,
41                 Model::Indices &index,
42                 const glm::mat4 &transform,
43                 Model::Index idx_offset = 0
44         ) const;
45         void Vertices(
46                 BlockModel::Positions &vertex,
47                 BlockModel::Indices &index,
48                 const glm::mat4 &transform,
49                 BlockModel::Index idx_offset = 0
50         ) const;
51
52         /// the number of vertices this shape's outline has
53         size_t OutlineCount() const { return out_pos.size(); }
54         /// the number of vertex indices this shape's outline has
55         size_t OutlineIndexCount() const { return out_idx.size(); }
56
57         /// fill given buffers with this shape's outline's elements with
58         /// an optional offset
59         void Outline(
60                 OutlineModel::Positions &vertex,
61                 OutlineModel::Indices &index,
62                 const OutlineModel::Position &offset = { 0.0f, 0.0f, 0.0f },
63                 OutlineModel::Index idx_offset = 0
64         ) const;
65
66         /// Check if given ray would pass though this shape if it were
67         /// transformed with given matrix.
68         /// If true, dist and normal hold the intersection distance and
69         /// normal, otherwise their content is undefined.
70         virtual bool Intersects(
71                 const Ray &,
72                 const glm::mat4 &,
73                 float &dist,
74                 glm::vec3 &normal
75         ) const noexcept = 0;
76
77         /// Check for intersection with given OBB.
78         /// The OBB is defined by box and box_M, M is applied to the shape.
79         virtual bool Intersects(
80                 const glm::mat4 &M,
81                 const AABB &box,
82                 const glm::mat4 &box_M,
83                 float &depth,
84                 glm::vec3 &normal
85         ) const noexcept = 0;
86
87 protected:
88         void SetShape(const Model::Positions &pos, const Model::Normals &nrm, const Model::Indices &idx) {
89                 vtx_pos = pos;
90                 vtx_nrm = nrm;
91                 vtx_idx = idx;
92         }
93         void SetOutline(const OutlineModel::Positions &pos, const OutlineModel::Indices &idx) {
94                 out_pos = pos;
95                 out_idx = idx;
96         }
97
98 private:
99         Model::Positions vtx_pos;
100         Model::Normals vtx_nrm;
101         Model::Indices vtx_idx;
102
103         OutlineModel::Positions out_pos;
104         OutlineModel::Indices out_idx;
105
106 };
107
108 }
109
110 #endif