]> git.localhorst.tv Git - blank.git/blob - src/shape.hpp
minor optimization of noise generator
[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; }
17         /// the number of vertex indices this shape has
18         size_t VertexIndexCount() const { return vtx_idx; }
19
20         /// fill given buffers with this shape's elements with an
21         /// optional offset
22         virtual void Vertices(
23                 std::vector<glm::vec3> &vertex,
24                 std::vector<glm::vec3> &normal,
25                 std::vector<Model::Index> &index,
26                 const glm::vec3 &elem_offset = { 0.0f, 0.0f, 0.0f },
27                 Model::Index idx_offset = 0
28         ) const = 0;
29
30         /// the number of vertices this shape's outline has
31         size_t OutlineCount() const { return outl; }
32         /// the number of vertex indices this shape's outline has
33         size_t OutlineIndexCount() const { return outl_idx; }
34
35         /// fill given buffers with this shape's outline's elements with
36         /// an optional offset
37         virtual void Outline(
38                 std::vector<glm::vec3> &vertex,
39                 std::vector<OutlineModel::Index> &index,
40                 const glm::vec3 &offset = { 0.0f, 0.0f, 0.0f },
41                 OutlineModel::Index idx_offset = 0
42         ) const = 0;
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         Shape(size_t vtx, size_t vtx_idx, size_t outl, size_t outl_idx)
57         : vtx(vtx), vtx_idx(vtx_idx), outl(outl), outl_idx(outl_idx) { }
58
59 private:
60         size_t vtx;
61         size_t vtx_idx;
62         size_t outl;
63         size_t outl_idx;
64
65 };
66
67
68 class NullShape
69 : public Shape {
70
71 public:
72         NullShape();
73
74         void Vertices(
75                 std::vector<glm::vec3> &vertex,
76                 std::vector<glm::vec3> &normal,
77                 std::vector<Model::Index> &index,
78                 const glm::vec3 &elem_offset = { 0.0f, 0.0f, 0.0f },
79                 Model::Index idx_offset = 0
80         ) const override;
81
82         void Outline(
83                 std::vector<glm::vec3> &vertex,
84                 std::vector<OutlineModel::Index> &index,
85                 const glm::vec3 &offset = { 0.0f, 0.0f, 0.0f },
86                 OutlineModel::Index idx_offset = 0
87         ) const override;
88
89         bool Intersects(const Ray &, const glm::mat4 &, float &, glm::vec3 &) const override;
90
91 };
92
93
94 class CuboidShape
95 : public Shape {
96
97 public:
98         CuboidShape(const AABB &bounds);
99
100         void Vertices(
101                 std::vector<glm::vec3> &vertex,
102                 std::vector<glm::vec3> &normal,
103                 std::vector<Model::Index> &index,
104                 const glm::vec3 &elem_offset = { 0.0f, 0.0f, 0.0f },
105                 Model::Index idx_offset = 0
106         ) const override;
107
108         void Outline(
109                 std::vector<glm::vec3> &vertex,
110                 std::vector<OutlineModel::Index> &index,
111                 const glm::vec3 &offset = { 0.0f, 0.0f, 0.0f },
112                 OutlineModel::Index idx_offset = 0
113         ) const override;
114
115         bool Intersects(const Ray &, const glm::mat4 &, float &, glm::vec3 &) const override;
116
117 private:
118         AABB bb;
119
120 };
121
122
123 class StairShape
124 : public Shape {
125
126 public:
127         StairShape(const AABB &bounds, const glm::vec2 &clip);
128
129         void Vertices(
130                 std::vector<glm::vec3> &vertex,
131                 std::vector<glm::vec3> &normal,
132                 std::vector<Model::Index> &index,
133                 const glm::vec3 &elem_offset = { 0.0f, 0.0f, 0.0f },
134                 Model::Index idx_offset = 0
135         ) const override;
136
137         void Outline(
138                 std::vector<glm::vec3> &vertex,
139                 std::vector<OutlineModel::Index> &index,
140                 const glm::vec3 &offset = { 0.0f, 0.0f, 0.0f },
141                 OutlineModel::Index idx_offset = 0
142         ) const override;
143
144         bool Intersects(const Ray &, const glm::mat4 &, float &, glm::vec3 &) const override;
145
146 private:
147         AABB top, bot;
148
149 };
150
151 }
152
153 #endif