]> git.localhorst.tv Git - blank.git/blob - src/shape.cpp
add null shape for void blocks
[blank.git] / src / shape.cpp
1 #include "shape.hpp"
2
3
4 namespace blank {
5
6 size_t NullShape::VertexCount() const {
7         return 0;
8 }
9
10 void NullShape::Vertices(std::vector<glm::vec3> &out, const glm::vec3 &pos) const {
11
12 }
13
14 void NullShape::Normals(std::vector<glm::vec3> &out) const {
15
16 }
17
18 size_t NullShape::OutlineCount() const {
19         return 0;
20 }
21
22 void NullShape::Outline(std::vector<glm::vec3> &out, const glm::vec3 &pos) const {
23
24 }
25
26 bool NullShape::Intersects(const Ray &, const glm::mat4 &, float &, glm::vec3 &) const {
27         return false;
28 }
29
30
31 CuboidShape::CuboidShape(const AABB &b)
32 : Shape()
33 , bb(b) {
34         bb.Adjust();
35 }
36
37
38 size_t CuboidShape::VertexCount() const {
39         return 36;
40 }
41
42 void CuboidShape::Vertices(std::vector<glm::vec3> &out, const glm::vec3 &pos) const {
43         out.reserve(36);
44         out.emplace_back(pos.x + bb.min.x, pos.y + bb.min.y, pos.z + bb.max.z); // front
45         out.emplace_back(pos.x + bb.max.x, pos.y + bb.min.y, pos.z + bb.max.z);
46         out.emplace_back(pos.x + bb.min.x, pos.y + bb.max.y, pos.z + bb.max.z);
47         out.emplace_back(pos.x + bb.max.x, pos.y + bb.min.y, pos.z + bb.max.z);
48         out.emplace_back(pos.x + bb.max.x, pos.y + bb.max.y, pos.z + bb.max.z);
49         out.emplace_back(pos.x + bb.min.x, pos.y + bb.max.y, pos.z + bb.max.z);
50         out.emplace_back(pos.x + bb.min.x, pos.y + bb.min.y, pos.z + bb.min.z); // back
51         out.emplace_back(pos.x + bb.min.x, pos.y + bb.max.y, pos.z + bb.min.z);
52         out.emplace_back(pos.x + bb.max.x, pos.y + bb.min.y, pos.z + bb.min.z);
53         out.emplace_back(pos.x + bb.max.x, pos.y + bb.min.y, pos.z + bb.min.z);
54         out.emplace_back(pos.x + bb.min.x, pos.y + bb.max.y, pos.z + bb.min.z);
55         out.emplace_back(pos.x + bb.max.x, pos.y + bb.max.y, pos.z + bb.min.z);
56         out.emplace_back(pos.x + bb.min.x, pos.y + bb.max.y, pos.z + bb.min.z); // top
57         out.emplace_back(pos.x + bb.min.x, pos.y + bb.max.y, pos.z + bb.max.z);
58         out.emplace_back(pos.x + bb.max.x, pos.y + bb.max.y, pos.z + bb.min.z);
59         out.emplace_back(pos.x + bb.max.x, pos.y + bb.max.y, pos.z + bb.min.z);
60         out.emplace_back(pos.x + bb.min.x, pos.y + bb.max.y, pos.z + bb.max.z);
61         out.emplace_back(pos.x + bb.max.x, pos.y + bb.max.y, pos.z + bb.max.z);
62         out.emplace_back(pos.x + bb.min.x, pos.y + bb.min.y, pos.z + bb.min.z); // bottom
63         out.emplace_back(pos.x + bb.max.x, pos.y + bb.min.y, pos.z + bb.min.z);
64         out.emplace_back(pos.x + bb.min.x, pos.y + bb.min.y, pos.z + bb.max.z);
65         out.emplace_back(pos.x + bb.max.x, pos.y + bb.min.y, pos.z + bb.min.z);
66         out.emplace_back(pos.x + bb.max.x, pos.y + bb.min.y, pos.z + bb.max.z);
67         out.emplace_back(pos.x + bb.min.x, pos.y + bb.min.y, pos.z + bb.max.z);
68         out.emplace_back(pos.x + bb.min.x, pos.y + bb.min.y, pos.z + bb.min.z); // left
69         out.emplace_back(pos.x + bb.min.x, pos.y + bb.min.y, pos.z + bb.max.z);
70         out.emplace_back(pos.x + bb.min.x, pos.y + bb.max.y, pos.z + bb.min.z);
71         out.emplace_back(pos.x + bb.min.x, pos.y + bb.max.y, pos.z + bb.min.z);
72         out.emplace_back(pos.x + bb.min.x, pos.y + bb.min.y, pos.z + bb.max.z);
73         out.emplace_back(pos.x + bb.min.x, pos.y + bb.max.y, pos.z + bb.max.z);
74         out.emplace_back(pos.x + bb.max.x, pos.y + bb.min.y, pos.z + bb.min.z); // right
75         out.emplace_back(pos.x + bb.max.x, pos.y + bb.max.y, pos.z + bb.min.z);
76         out.emplace_back(pos.x + bb.max.x, pos.y + bb.min.y, pos.z + bb.max.z);
77         out.emplace_back(pos.x + bb.max.x, pos.y + bb.min.y, pos.z + bb.max.z);
78         out.emplace_back(pos.x + bb.max.x, pos.y + bb.max.y, pos.z + bb.min.z);
79         out.emplace_back(pos.x + bb.max.x, pos.y + bb.max.y, pos.z + bb.max.z);
80 }
81
82 void CuboidShape::Normals(std::vector<glm::vec3> &out) const {
83         out.reserve(36);
84         out.insert(out.end(), 6, glm::vec3( 0.0f,  0.0f,  1.0f)); // front
85         out.insert(out.end(), 6, glm::vec3( 0.0f,  0.0f, -1.0f)); // back
86         out.insert(out.end(), 6, glm::vec3( 0.0f,  1.0f,  0.0f)); // top
87         out.insert(out.end(), 6, glm::vec3( 0.0f, -1.0f,  0.0f)); // bottom
88         out.insert(out.end(), 6, glm::vec3(-1.0f,  0.0f,  0.0f)); // left
89         out.insert(out.end(), 6, glm::vec3( 1.0f,  0.0f,  0.0f)); // right
90 }
91
92
93 size_t CuboidShape::OutlineCount() const {
94         return 24;
95 }
96
97 void CuboidShape::Outline(std::vector<glm::vec3> &out, const glm::vec3 &pos) const {
98         out.reserve(24);
99         out.emplace_back(pos.x + bb.min.x, pos.y + bb.min.y, pos.z + bb.min.z);
100         out.emplace_back(pos.x + bb.max.x, pos.y + bb.min.y, pos.z + bb.min.z);
101         out.emplace_back(pos.x + bb.max.x, pos.y + bb.min.y, pos.z + bb.min.z);
102         out.emplace_back(pos.x + bb.max.x, pos.y + bb.max.y, pos.z + bb.min.z);
103         out.emplace_back(pos.x + bb.max.x, pos.y + bb.max.y, pos.z + bb.min.z);
104         out.emplace_back(pos.x + bb.min.x, pos.y + bb.max.y, pos.z + bb.min.z);
105         out.emplace_back(pos.x + bb.min.x, pos.y + bb.max.y, pos.z + bb.min.z);
106         out.emplace_back(pos.x + bb.min.x, pos.y + bb.min.y, pos.z + bb.min.z);
107         out.emplace_back(pos.x + bb.min.x, pos.y + bb.min.y, pos.z + bb.min.z);
108         out.emplace_back(pos.x + bb.min.x, pos.y + bb.min.y, pos.z + bb.max.z);
109         out.emplace_back(pos.x + bb.max.x, pos.y + bb.min.y, pos.z + bb.min.z);
110         out.emplace_back(pos.x + bb.max.x, pos.y + bb.min.y, pos.z + bb.max.z);
111         out.emplace_back(pos.x + bb.max.x, pos.y + bb.max.y, pos.z + bb.min.z);
112         out.emplace_back(pos.x + bb.max.x, pos.y + bb.max.y, pos.z + bb.max.z);
113         out.emplace_back(pos.x + bb.min.x, pos.y + bb.max.y, pos.z + bb.min.z);
114         out.emplace_back(pos.x + bb.min.x, pos.y + bb.max.y, pos.z + bb.max.z);
115         out.emplace_back(pos.x + bb.min.x, pos.y + bb.min.y, pos.z + bb.max.z);
116         out.emplace_back(pos.x + bb.max.x, pos.y + bb.min.y, pos.z + bb.max.z);
117         out.emplace_back(pos.x + bb.max.x, pos.y + bb.min.y, pos.z + bb.max.z);
118         out.emplace_back(pos.x + bb.max.x, pos.y + bb.max.y, pos.z + bb.max.z);
119         out.emplace_back(pos.x + bb.max.x, pos.y + bb.max.y, pos.z + bb.max.z);
120         out.emplace_back(pos.x + bb.min.x, pos.y + bb.max.y, pos.z + bb.max.z);
121         out.emplace_back(pos.x + bb.min.x, pos.y + bb.max.y, pos.z + bb.max.z);
122         out.emplace_back(pos.x + bb.min.x, pos.y + bb.min.y, pos.z + bb.max.z);
123 }
124
125 bool CuboidShape::Intersects(const Ray &ray, const glm::mat4 &M, float &dist, glm::vec3 &normal) const {
126         return Intersection(ray, bb, M, &dist, &normal);
127 }
128
129
130 StairShape::StairShape(const AABB &bb, const glm::vec2 &clip)
131 : top({ { clip.x, clip.y, bb.min.z }, bb.max })
132 , bot({ bb.min, { bb.max.x, clip.y, bb.max.z } }) {
133
134 }
135
136
137 size_t StairShape::VertexCount() const {
138         return 60;
139 }
140
141 void StairShape::Vertices(std::vector<glm::vec3> &out, const glm::vec3 &pos) const {
142         out.reserve(60);
143         out.emplace_back(pos.x + top.min.x, pos.y + top.min.y, pos.z + top.max.z); // front, upper
144         out.emplace_back(pos.x + top.max.x, pos.y + top.min.y, pos.z + top.max.z);
145         out.emplace_back(pos.x + top.min.x, pos.y + top.max.y, pos.z + top.max.z);
146         out.emplace_back(pos.x + top.max.x, pos.y + top.min.y, pos.z + top.max.z);
147         out.emplace_back(pos.x + top.max.x, pos.y + top.max.y, pos.z + top.max.z);
148         out.emplace_back(pos.x + top.min.x, pos.y + top.max.y, pos.z + top.max.z);
149         out.emplace_back(pos.x + bot.min.x, pos.y + bot.min.y, pos.z + bot.max.z); // front, lower
150         out.emplace_back(pos.x + bot.max.x, pos.y + bot.min.y, pos.z + bot.max.z);
151         out.emplace_back(pos.x + bot.min.x, pos.y + bot.max.y, pos.z + bot.max.z);
152         out.emplace_back(pos.x + bot.max.x, pos.y + bot.min.y, pos.z + bot.max.z);
153         out.emplace_back(pos.x + bot.max.x, pos.y + bot.max.y, pos.z + bot.max.z);
154         out.emplace_back(pos.x + bot.min.x, pos.y + bot.max.y, pos.z + bot.max.z);
155         out.emplace_back(pos.x + top.min.x, pos.y + top.min.y, pos.z + top.min.z); // back, upper
156         out.emplace_back(pos.x + top.min.x, pos.y + top.max.y, pos.z + top.min.z);
157         out.emplace_back(pos.x + top.max.x, pos.y + top.min.y, pos.z + top.min.z);
158         out.emplace_back(pos.x + top.max.x, pos.y + top.min.y, pos.z + top.min.z);
159         out.emplace_back(pos.x + top.min.x, pos.y + top.max.y, pos.z + top.min.z);
160         out.emplace_back(pos.x + top.max.x, pos.y + top.max.y, pos.z + top.min.z);
161         out.emplace_back(pos.x + bot.min.x, pos.y + bot.min.y, pos.z + bot.min.z); // back, lower
162         out.emplace_back(pos.x + bot.min.x, pos.y + bot.max.y, pos.z + bot.min.z);
163         out.emplace_back(pos.x + bot.max.x, pos.y + bot.min.y, pos.z + bot.min.z);
164         out.emplace_back(pos.x + bot.max.x, pos.y + bot.min.y, pos.z + bot.min.z);
165         out.emplace_back(pos.x + bot.min.x, pos.y + bot.max.y, pos.z + bot.min.z);
166         out.emplace_back(pos.x + bot.max.x, pos.y + bot.max.y, pos.z + bot.min.z);
167         out.emplace_back(pos.x + top.min.x, pos.y + top.max.y, pos.z + top.min.z); // top, upper
168         out.emplace_back(pos.x + top.min.x, pos.y + top.max.y, pos.z + top.max.z);
169         out.emplace_back(pos.x + top.max.x, pos.y + top.max.y, pos.z + top.min.z);
170         out.emplace_back(pos.x + top.max.x, pos.y + top.max.y, pos.z + top.min.z);
171         out.emplace_back(pos.x + top.min.x, pos.y + top.max.y, pos.z + top.max.z);
172         out.emplace_back(pos.x + top.max.x, pos.y + top.max.y, pos.z + top.max.z);
173         out.emplace_back(pos.x + bot.min.x, pos.y + bot.max.y, pos.z + bot.min.z); // top, lower
174         out.emplace_back(pos.x + bot.min.x, pos.y + bot.max.y, pos.z + bot.max.z);
175         out.emplace_back(pos.x + top.min.x, pos.y + bot.max.y, pos.z + bot.min.z);
176         out.emplace_back(pos.x + top.min.x, pos.y + bot.max.y, pos.z + bot.min.z);
177         out.emplace_back(pos.x + bot.min.x, pos.y + bot.max.y, pos.z + bot.max.z);
178         out.emplace_back(pos.x + top.min.x, pos.y + bot.max.y, pos.z + bot.max.z);
179         out.emplace_back(pos.x + bot.min.x, pos.y + bot.min.y, pos.z + bot.min.z); // bottom
180         out.emplace_back(pos.x + bot.max.x, pos.y + bot.min.y, pos.z + bot.min.z);
181         out.emplace_back(pos.x + bot.min.x, pos.y + bot.min.y, pos.z + bot.max.z);
182         out.emplace_back(pos.x + bot.max.x, pos.y + bot.min.y, pos.z + bot.min.z);
183         out.emplace_back(pos.x + bot.max.x, pos.y + bot.min.y, pos.z + bot.max.z);
184         out.emplace_back(pos.x + bot.min.x, pos.y + bot.min.y, pos.z + bot.max.z);
185         out.emplace_back(pos.x + top.min.x, pos.y + top.min.y, pos.z + top.min.z); // left, upper
186         out.emplace_back(pos.x + top.min.x, pos.y + top.min.y, pos.z + top.max.z);
187         out.emplace_back(pos.x + top.min.x, pos.y + top.max.y, pos.z + top.min.z);
188         out.emplace_back(pos.x + top.min.x, pos.y + top.max.y, pos.z + top.min.z);
189         out.emplace_back(pos.x + top.min.x, pos.y + top.min.y, pos.z + top.max.z);
190         out.emplace_back(pos.x + top.min.x, pos.y + top.max.y, pos.z + top.max.z);
191         out.emplace_back(pos.x + bot.min.x, pos.y + bot.min.y, pos.z + bot.min.z); // left, lower
192         out.emplace_back(pos.x + bot.min.x, pos.y + bot.min.y, pos.z + bot.max.z);
193         out.emplace_back(pos.x + bot.min.x, pos.y + bot.max.y, pos.z + bot.min.z);
194         out.emplace_back(pos.x + bot.min.x, pos.y + bot.max.y, pos.z + bot.min.z);
195         out.emplace_back(pos.x + bot.min.x, pos.y + bot.min.y, pos.z + bot.max.z);
196         out.emplace_back(pos.x + bot.min.x, pos.y + bot.max.y, pos.z + bot.max.z);
197         out.emplace_back(pos.x + bot.max.x, pos.y + bot.min.y, pos.z + bot.min.z); // right
198         out.emplace_back(pos.x + top.max.x, pos.y + top.max.y, pos.z + top.min.z);
199         out.emplace_back(pos.x + bot.max.x, pos.y + bot.min.y, pos.z + bot.max.z);
200         out.emplace_back(pos.x + bot.max.x, pos.y + bot.min.y, pos.z + bot.max.z);
201         out.emplace_back(pos.x + top.max.x, pos.y + top.max.y, pos.z + top.min.z);
202         out.emplace_back(pos.x + top.max.x, pos.y + top.max.y, pos.z + top.max.z);
203 }
204
205 void StairShape::Normals(std::vector<glm::vec3> &out) const {
206         out.reserve(60);
207         out.insert(out.end(), 12, glm::vec3( 0.0f,  0.0f,  1.0f)); // front, x2
208         out.insert(out.end(), 12, glm::vec3( 0.0f,  0.0f, -1.0f)); // back, x2
209         out.insert(out.end(), 12, glm::vec3( 0.0f,  1.0f,  0.0f)); // top, x2
210         out.insert(out.end(),  6, glm::vec3( 0.0f, -1.0f,  0.0f)); // bottom
211         out.insert(out.end(), 12, glm::vec3(-1.0f,  0.0f,  0.0f)); // left, x2
212         out.insert(out.end(),  6, glm::vec3( 1.0f,  0.0f,  0.0f)); // right
213 }
214
215
216 size_t StairShape::OutlineCount() const {
217         return 36;
218 }
219
220 void StairShape::Outline(std::vector<glm::vec3> &out, const glm::vec3 &pos) const {
221         out.reserve(36);
222         out.emplace_back(pos.x + bot.min.x, pos.y + bot.min.y, pos.z + bot.min.z); // bottom
223         out.emplace_back(pos.x + bot.max.x, pos.y + bot.min.y, pos.z + bot.min.z);
224         out.emplace_back(pos.x + bot.max.x, pos.y + bot.min.y, pos.z + bot.min.z);
225         out.emplace_back(pos.x + bot.max.x, pos.y + bot.min.y, pos.z + bot.max.z);
226         out.emplace_back(pos.x + bot.max.x, pos.y + bot.min.y, pos.z + bot.max.z);
227         out.emplace_back(pos.x + bot.min.x, pos.y + bot.min.y, pos.z + bot.max.z);
228         out.emplace_back(pos.x + bot.min.x, pos.y + bot.min.y, pos.z + bot.max.z);
229         out.emplace_back(pos.x + bot.min.x, pos.y + bot.min.y, pos.z + bot.min.z);
230         out.emplace_back(pos.x + bot.min.x, pos.y + bot.max.y, pos.z + bot.min.z); // middle
231         out.emplace_back(pos.x + top.min.x, pos.y + bot.max.y, pos.z + bot.min.z);
232         out.emplace_back(pos.x + top.min.x, pos.y + bot.max.y, pos.z + bot.min.z);
233         out.emplace_back(pos.x + top.min.x, pos.y + bot.max.y, pos.z + bot.max.z);
234         out.emplace_back(pos.x + top.min.x, pos.y + bot.max.y, pos.z + bot.max.z);
235         out.emplace_back(pos.x + bot.min.x, pos.y + bot.max.y, pos.z + bot.max.z);
236         out.emplace_back(pos.x + bot.min.x, pos.y + bot.max.y, pos.z + bot.max.z);
237         out.emplace_back(pos.x + bot.min.x, pos.y + bot.max.y, pos.z + bot.min.z);
238         out.emplace_back(pos.x + top.min.x, pos.y + top.max.y, pos.z + bot.min.z); // top
239         out.emplace_back(pos.x + bot.max.x, pos.y + top.max.y, pos.z + bot.min.z);
240         out.emplace_back(pos.x + bot.max.x, pos.y + top.max.y, pos.z + bot.min.z);
241         out.emplace_back(pos.x + bot.max.x, pos.y + top.max.y, pos.z + bot.max.z);
242         out.emplace_back(pos.x + bot.max.x, pos.y + top.max.y, pos.z + bot.max.z);
243         out.emplace_back(pos.x + top.min.x, pos.y + top.max.y, pos.z + bot.max.z);
244         out.emplace_back(pos.x + top.min.x, pos.y + top.max.y, pos.z + bot.max.z);
245         out.emplace_back(pos.x + top.min.x, pos.y + top.max.y, pos.z + bot.min.z);
246         out.emplace_back(pos.x + bot.min.x, pos.y + bot.min.y, pos.z + bot.min.z); // verticals, ltr/btf
247         out.emplace_back(pos.x + bot.min.x, pos.y + bot.max.y, pos.z + bot.min.z);
248         out.emplace_back(pos.x + bot.min.x, pos.y + bot.min.y, pos.z + bot.max.z);
249         out.emplace_back(pos.x + bot.min.x, pos.y + bot.max.y, pos.z + bot.max.z);
250         out.emplace_back(pos.x + top.min.x, pos.y + top.min.y, pos.z + bot.min.z);
251         out.emplace_back(pos.x + top.min.x, pos.y + top.max.y, pos.z + bot.min.z);
252         out.emplace_back(pos.x + top.min.x, pos.y + top.min.y, pos.z + bot.max.z);
253         out.emplace_back(pos.x + top.min.x, pos.y + top.max.y, pos.z + bot.max.z);
254         out.emplace_back(pos.x + bot.max.x, pos.y + bot.min.y, pos.z + bot.min.z);
255         out.emplace_back(pos.x + bot.max.x, pos.y + top.max.y, pos.z + bot.min.z);
256         out.emplace_back(pos.x + bot.max.x, pos.y + bot.min.y, pos.z + bot.max.z);
257         out.emplace_back(pos.x + bot.max.x, pos.y + top.max.y, pos.z + bot.max.z);
258 }
259
260 bool StairShape::Intersects(const Ray &ray, const glm::mat4 &M, float &dist, glm::vec3 &norm) const {
261         float top_dist, bot_dist;
262         glm::vec3 top_norm, bot_norm;
263         bool top_hit = Intersection(ray, top, M, &top_dist, &top_norm);
264         bool bot_hit = Intersection(ray, bot, M, &bot_dist, &bot_norm);
265
266         if (top_hit) {
267                 if (bot_hit) {
268                         if (top_dist < bot_dist) {
269                                 dist = top_dist;
270                                 norm = top_norm;
271                                 return true;
272                         } else {
273                                 dist = bot_dist;
274                                 norm = bot_norm;
275                                 return true;
276                         }
277                 } else {
278                         dist = top_dist;
279                         norm = top_norm;
280                         return true;
281                 }
282         } else if (bot_hit) {
283                 dist = bot_dist;
284                 norm = bot_norm;
285                 return true;
286         } else {
287                 return false;
288         }
289 }
290
291
292 }