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