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