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