]> git.localhorst.tv Git - blank.git/blob - src/shape.hpp
minor optimizations in chunk
[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 noexcept { return vtx_pos.size(); }
17         /// the number of vertex indices this shape has
18         size_t VertexIndexCount() const noexcept { return vtx_idx.size(); }
19
20         const Model::Normal &VertexNormal(size_t idx) const noexcept { return vtx_nrm[idx]; }
21         Model::Normal VertexNormal(
22                 size_t idx, const glm::mat4 &transform
23         ) const noexcept {
24                 return Model::Normal(transform * glm::vec4(vtx_nrm[idx], 0.0f));
25         }
26
27         /// fill given buffers with this shape's elements with an
28         /// optional transform and offset
29         void Vertices(
30                 Model::Positions &vertex,
31                 Model::Normals &normal,
32                 Model::Indices &index
33         ) const;
34         void Vertices(
35                 Model::Positions &vertex,
36                 Model::Normals &normal,
37                 Model::Indices &index,
38                 const glm::mat4 &transform,
39                 Model::Index idx_offset = 0
40         ) const;
41         void Vertices(
42                 BlockModel::Positions &vertex,
43                 BlockModel::Indices &index,
44                 const glm::mat4 &transform,
45                 BlockModel::Index idx_offset = 0
46         ) const;
47
48         /// the number of vertices this shape's outline has
49         size_t OutlineCount() const { return out_pos.size(); }
50         /// the number of vertex indices this shape's outline has
51         size_t OutlineIndexCount() const { return out_idx.size(); }
52
53         /// fill given buffers with this shape's outline's elements with
54         /// an optional offset
55         void Outline(
56                 OutlineModel::Positions &vertex,
57                 OutlineModel::Indices &index,
58                 const OutlineModel::Position &offset = { 0.0f, 0.0f, 0.0f },
59                 OutlineModel::Index idx_offset = 0
60         ) const;
61
62         /// Check if given ray would pass though this shape if it were
63         /// transformed with given matrix.
64         /// If true, dist and normal hold the intersection distance and
65         /// normal, otherwise their content is undefined.
66         virtual bool Intersects(
67                 const Ray &,
68                 const glm::mat4 &,
69                 float &dist,
70                 glm::vec3 &normal
71         ) const noexcept = 0;
72
73 protected:
74         void SetShape(const Model::Positions &pos, const Model::Normals &nrm, const Model::Indices &idx) {
75                 vtx_pos = pos;
76                 vtx_nrm = nrm;
77                 vtx_idx = idx;
78         }
79         void SetOutline(const OutlineModel::Positions &pos, const OutlineModel::Indices &idx) {
80                 out_pos = pos;
81                 out_idx = idx;
82         }
83
84 private:
85         Model::Positions vtx_pos;
86         Model::Normals vtx_nrm;
87         Model::Indices vtx_idx;
88
89         OutlineModel::Positions out_pos;
90         OutlineModel::Indices out_idx;
91
92 };
93
94
95 class NullShape
96 : public Shape {
97
98 public:
99         NullShape();
100
101         bool Intersects(const Ray &, const glm::mat4 &, float &, glm::vec3 &) const noexcept override;
102
103 };
104
105
106 class CuboidShape
107 : public Shape {
108
109 public:
110         CuboidShape(const AABB &bounds);
111
112         bool Intersects(const Ray &, const glm::mat4 &, float &, glm::vec3 &) const noexcept override;
113
114 private:
115         AABB bb;
116
117 };
118
119
120 class StairShape
121 : public Shape {
122
123 public:
124         StairShape(const AABB &bounds, const glm::vec2 &clip);
125
126         bool Intersects(const Ray &, const glm::mat4 &, float &, glm::vec3 &) const noexcept override;
127
128 private:
129         AABB top, bot;
130
131 };
132
133 }
134
135 #endif