]> git.localhorst.tv Git - blank.git/blob - src/model/Shape.hpp
plug box intersection into shapes
[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         ) const noexcept = 0;
84
85 protected:
86         void SetShape(const Model::Positions &pos, const Model::Normals &nrm, const Model::Indices &idx) {
87                 vtx_pos = pos;
88                 vtx_nrm = nrm;
89                 vtx_idx = idx;
90         }
91         void SetOutline(const OutlineModel::Positions &pos, const OutlineModel::Indices &idx) {
92                 out_pos = pos;
93                 out_idx = idx;
94         }
95
96 private:
97         Model::Positions vtx_pos;
98         Model::Normals vtx_nrm;
99         Model::Indices vtx_idx;
100
101         OutlineModel::Positions out_pos;
102         OutlineModel::Indices out_idx;
103
104 };
105
106 }
107
108 #endif