]> git.localhorst.tv Git - blank.git/blob - src/model/shape.cpp
plug box intersection into shapes
[blank.git] / src / model / shape.cpp
1 #include "Shape.hpp"
2 #include "shapes.hpp"
3
4
5 namespace blank {
6
7 void Shape::Vertices(
8         Model::Positions &vertex,
9         Model::Normals &normal,
10         Model::Indices &index
11 ) const {
12         for (const auto &pos : vtx_pos) {
13                 vertex.emplace_back(pos);
14         }
15         for (const auto &nrm : vtx_nrm) {
16                 normal.emplace_back(nrm);
17         }
18         for (auto idx : vtx_idx) {
19                 index.emplace_back(idx);
20         }
21 }
22
23 void Shape::Vertices(
24         Model::Positions &vertex,
25         Model::Normals &normal,
26         Model::Indices &index,
27         const glm::mat4 &transform,
28         Model::Index idx_offset
29 ) const {
30         for (const auto &pos : vtx_pos) {
31                 vertex.emplace_back(transform * glm::vec4(pos, 1.0f));
32         }
33         for (const auto &nrm : vtx_nrm) {
34                 normal.emplace_back(transform * glm::vec4(nrm, 0.0f));
35         }
36         for (auto idx : vtx_idx) {
37                 index.emplace_back(idx_offset + idx);
38         }
39 }
40
41 void Shape::Vertices(
42         BlockModel::Positions &vertex,
43         BlockModel::Indices &index,
44         const glm::mat4 &transform,
45         BlockModel::Index idx_offset
46 ) const {
47         for (const auto &pos : vtx_pos) {
48                 vertex.emplace_back(transform * glm::vec4(pos, 1.0f));
49         }
50         for (auto idx : vtx_idx) {
51                 index.emplace_back(idx_offset + idx);
52         }
53 }
54
55 void Shape::Outline(
56         OutlineModel::Positions &vertex,
57         OutlineModel::Indices &index,
58         const OutlineModel::Position &elem_offset,
59         OutlineModel::Index idx_offset
60 ) const {
61         for (const auto &pos : out_pos) {
62                 vertex.emplace_back(elem_offset + pos);
63         }
64         for (auto idx : out_idx) {
65                 index.emplace_back(idx_offset + idx);
66         }
67 }
68
69
70 NullShape::NullShape()
71 : Shape() {
72
73 }
74
75 bool NullShape::Intersects(
76         const Ray &,
77         const glm::mat4 &,
78         float &, glm::vec3 &
79 ) const noexcept {
80         return false;
81 }
82
83 bool NullShape::Intersects(
84         const glm::mat4 &,
85         const AABB &,
86         const glm::mat4 &
87 ) const noexcept {
88         return false;
89 }
90
91
92 CuboidShape::CuboidShape(const AABB &b)
93 : Shape()
94 , bb(b) {
95         bb.Adjust();
96         SetShape({
97                 { bb.min.x, bb.min.y, bb.max.z }, // front
98                 { bb.max.x, bb.min.y, bb.max.z },
99                 { bb.min.x, bb.max.y, bb.max.z },
100                 { bb.max.x, bb.max.y, bb.max.z },
101                 { bb.min.x, bb.min.y, bb.min.z }, // back
102                 { bb.min.x, bb.max.y, bb.min.z },
103                 { bb.max.x, bb.min.y, bb.min.z },
104                 { bb.max.x, bb.max.y, bb.min.z },
105                 { bb.min.x, bb.max.y, bb.min.z }, // top
106                 { bb.min.x, bb.max.y, bb.max.z },
107                 { bb.max.x, bb.max.y, bb.min.z },
108                 { bb.max.x, bb.max.y, bb.max.z },
109                 { bb.min.x, bb.min.y, bb.min.z }, // bottom
110                 { bb.max.x, bb.min.y, bb.min.z },
111                 { bb.min.x, bb.min.y, bb.max.z },
112                 { bb.max.x, bb.min.y, bb.max.z },
113                 { bb.min.x, bb.min.y, bb.min.z }, // left
114                 { bb.min.x, bb.min.y, bb.max.z },
115                 { bb.min.x, bb.max.y, bb.min.z },
116                 { bb.min.x, bb.max.y, bb.max.z },
117                 { bb.max.x, bb.min.y, bb.min.z }, // right
118                 { bb.max.x, bb.max.y, bb.min.z },
119                 { bb.max.x, bb.min.y, bb.max.z },
120                 { bb.max.x, bb.max.y, bb.max.z },
121         }, {
122                 {  0.0f,  0.0f,  1.0f }, // front
123                 {  0.0f,  0.0f,  1.0f },
124                 {  0.0f,  0.0f,  1.0f },
125                 {  0.0f,  0.0f,  1.0f },
126                 {  0.0f,  0.0f, -1.0f }, // back
127                 {  0.0f,  0.0f, -1.0f },
128                 {  0.0f,  0.0f, -1.0f },
129                 {  0.0f,  0.0f, -1.0f },
130                 {  0.0f,  1.0f,  0.0f }, // top
131                 {  0.0f,  1.0f,  0.0f },
132                 {  0.0f,  1.0f,  0.0f },
133                 {  0.0f,  1.0f,  0.0f },
134                 {  0.0f, -1.0f,  0.0f }, // bottom
135                 {  0.0f, -1.0f,  0.0f },
136                 {  0.0f, -1.0f,  0.0f },
137                 {  0.0f, -1.0f,  0.0f },
138                 { -1.0f,  0.0f,  0.0f }, // left
139                 { -1.0f,  0.0f,  0.0f },
140                 { -1.0f,  0.0f,  0.0f },
141                 { -1.0f,  0.0f,  0.0f },
142                 {  1.0f,  0.0f,  0.0f }, // right
143                 {  1.0f,  0.0f,  0.0f },
144                 {  1.0f,  0.0f,  0.0f },
145                 {  1.0f,  0.0f,  0.0f },
146         }, {
147                   0,  1,  2,  2,  1,  3, // front
148                   4,  5,  6,  6,  5,  7, // back
149                   8,  9, 10, 10,  9, 11, // top
150                  12, 13, 14, 14, 13, 15, // bottom
151                  16, 17, 18, 18, 17, 19, // left
152                  20, 21, 22, 22, 21, 23, // right
153         });
154         SetOutline({
155                 { bb.min.x, bb.min.y, bb.min.z }, // back
156                 { bb.max.x, bb.min.y, bb.min.z },
157                 { bb.min.x, bb.max.y, bb.min.z },
158                 { bb.max.x, bb.max.y, bb.min.z },
159                 { bb.min.x, bb.min.y, bb.max.z }, // front
160                 { bb.max.x, bb.min.y, bb.max.z },
161                 { bb.min.x, bb.max.y, bb.max.z },
162                 { bb.max.x, bb.max.y, bb.max.z },
163         }, {
164                 0, 1, 1, 3, 3, 2, 2, 0, // back
165                 4, 5, 5, 7, 7, 6, 6, 4, // front
166                 0, 4, 1, 5, 2, 6, 3, 7, // sides
167         });
168 }
169
170 bool CuboidShape::Intersects(
171         const Ray &ray,
172         const glm::mat4 &M,
173         float &dist, glm::vec3 &normal
174 ) const noexcept {
175         return Intersection(ray, bb, M, &dist, &normal);
176 }
177
178 bool CuboidShape::Intersects(
179         const glm::mat4 &M,
180         const AABB &box,
181         const glm::mat4 &box_M
182 ) const noexcept {
183         return Intersection(bb, M, box, box_M);
184 }
185
186
187 StairShape::StairShape(const AABB &bb, const glm::vec2 &clip)
188 : Shape()
189 , top({ { bb.min.x, clip.y, bb.min.z }, { bb.max.x, bb.max.y, clip.x } })
190 , bot({ bb.min, { bb.max.x, clip.y, bb.max.z } }) {
191         SetShape({
192                 { top.min.x, top.min.y, top.max.z }, // front, upper
193                 { top.max.x, top.min.y, top.max.z },
194                 { top.min.x, top.max.y, top.max.z },
195                 { top.max.x, top.max.y, top.max.z },
196                 { bot.min.x, bot.min.y, bot.max.z }, // front, lower
197                 { bot.max.x, bot.min.y, bot.max.z },
198                 { bot.min.x, bot.max.y, bot.max.z },
199                 { bot.max.x, bot.max.y, bot.max.z },
200                 { bot.min.x, bot.min.y, bot.min.z }, // back
201                 { bot.min.x, top.max.y, bot.min.z },
202                 { top.max.x, bot.min.y, bot.min.z },
203                 { top.max.x, top.max.y, bot.min.z },
204                 { top.min.x, top.max.y, top.min.z }, // top, upper
205                 { top.min.x, top.max.y, top.max.z },
206                 { top.max.x, top.max.y, top.min.z },
207                 { top.max.x, top.max.y, top.max.z },
208                 { bot.min.x, bot.max.y, top.max.z }, // top, lower
209                 { bot.min.x, bot.max.y, bot.max.z },
210                 { bot.max.x, bot.max.y, top.max.z },
211                 { bot.max.x, bot.max.y, bot.max.z },
212                 { bot.min.x, bot.min.y, bot.min.z }, // bottom
213                 { bot.max.x, bot.min.y, bot.min.z },
214                 { bot.min.x, bot.min.y, bot.max.z },
215                 { bot.max.x, bot.min.y, bot.max.z },
216                 { top.min.x, top.min.y, top.min.z }, // left, upper
217                 { top.min.x, top.min.y, top.max.z },
218                 { top.min.x, top.max.y, top.min.z },
219                 { top.min.x, top.max.y, top.max.z },
220                 { bot.min.x, bot.min.y, bot.min.z }, // left, lower
221                 { bot.min.x, bot.min.y, bot.max.z },
222                 { bot.min.x, bot.max.y, bot.min.z },
223                 { bot.min.x, bot.max.y, bot.max.z },
224                 { top.max.x, top.min.y, top.min.z }, // right, upper
225                 { top.max.x, top.max.y, top.min.z },
226                 { top.max.x, top.min.y, top.max.z },
227                 { top.max.x, top.max.y, top.max.z },
228                 { bot.max.x, bot.min.y, bot.min.z }, // right, lower
229                 { bot.max.x, bot.max.y, bot.min.z },
230                 { bot.max.x, bot.min.y, bot.max.z },
231                 { bot.max.x, bot.max.y, bot.max.z },
232         }, {
233                 {  0.0f,  0.0f,  1.0f }, // front x2
234                 {  0.0f,  0.0f,  1.0f },
235                 {  0.0f,  0.0f,  1.0f },
236                 {  0.0f,  0.0f,  1.0f },
237                 {  0.0f,  0.0f,  1.0f },
238                 {  0.0f,  0.0f,  1.0f },
239                 {  0.0f,  0.0f,  1.0f },
240                 {  0.0f,  0.0f,  1.0f },
241                 {  0.0f,  0.0f, -1.0f }, // back
242                 {  0.0f,  0.0f, -1.0f },
243                 {  0.0f,  0.0f, -1.0f },
244                 {  0.0f,  0.0f, -1.0f },
245                 {  0.0f,  1.0f,  0.0f }, // top x2
246                 {  0.0f,  1.0f,  0.0f },
247                 {  0.0f,  1.0f,  0.0f },
248                 {  0.0f,  1.0f,  0.0f },
249                 {  0.0f,  1.0f,  0.0f },
250                 {  0.0f,  1.0f,  0.0f },
251                 {  0.0f,  1.0f,  0.0f },
252                 {  0.0f,  1.0f,  0.0f },
253                 {  0.0f, -1.0f,  0.0f }, // bottom
254                 {  0.0f, -1.0f,  0.0f },
255                 {  0.0f, -1.0f,  0.0f },
256                 {  0.0f, -1.0f,  0.0f },
257                 { -1.0f,  0.0f,  0.0f }, // left x2
258                 { -1.0f,  0.0f,  0.0f },
259                 { -1.0f,  0.0f,  0.0f },
260                 { -1.0f,  0.0f,  0.0f },
261                 { -1.0f,  0.0f,  0.0f },
262                 { -1.0f,  0.0f,  0.0f },
263                 { -1.0f,  0.0f,  0.0f },
264                 { -1.0f,  0.0f,  0.0f },
265                 {  1.0f,  0.0f,  0.0f }, // right x2
266                 {  1.0f,  0.0f,  0.0f },
267                 {  1.0f,  0.0f,  0.0f },
268                 {  1.0f,  0.0f,  0.0f },
269                 {  1.0f,  0.0f,  0.0f },
270                 {  1.0f,  0.0f,  0.0f },
271                 {  1.0f,  0.0f,  0.0f },
272                 {  1.0f,  0.0f,  0.0f },
273         }, {
274                  0,  1,  2,  2,  1,  3, // front, upper
275                  4,  5,  6,  6,  5,  7, // front, lower
276                  8,  9, 10, 10,  9, 11, // back
277                 12, 13, 14, 14, 13, 15, // top, upper
278                 16, 17, 18, 18, 17, 19, // top, lower
279                 20, 21, 22, 22, 21, 23, // bottom
280                 24, 25, 26, 26, 25, 27, // left, upper
281                 28, 29, 30, 30, 29, 31, // left, lower
282                 32, 33, 34, 34, 33, 35, // right, upper
283                 36, 37, 38, 38, 37, 39, // right, lower
284         });
285         SetOutline({
286                 { bot.min.x, bot.min.y, bot.min.z }, // bottom
287                 { bot.max.x, bot.min.y, bot.min.z },
288                 { bot.min.x, bot.min.y, bot.max.z },
289                 { bot.max.x, bot.min.y, bot.max.z },
290                 { bot.min.x, bot.max.y, top.max.z }, // middle
291                 { bot.max.x, bot.max.y, top.max.z },
292                 { bot.min.x, bot.max.y, bot.max.z },
293                 { bot.max.x, bot.max.y, bot.max.z },
294                 { top.min.x, top.max.y, top.min.z }, // top
295                 { top.max.x, top.max.y, top.min.z },
296                 { top.min.x, top.max.y, top.max.z },
297                 { top.max.x, top.max.y, top.max.z },
298         }, {
299                  0,  1,  1,  3,  3,  2,  2,  0, // bottom
300                  4,  5,  5,  7,  7,  6,  6,  4, // middle
301                  8,  9,  9, 11, 11, 10, 10 , 8, // top
302                  0,  8,  4, 10,  2,  6, // verticals, btf
303                  1,  9,  5, 11,  3,  7,
304         //       5,  8,  7, 10,
305         //       1,  9,  3, 11,
306         });
307 }
308
309 bool StairShape::Intersects(
310         const Ray &ray,
311         const glm::mat4 &M,
312         float &dist,
313         glm::vec3 &norm
314 ) const noexcept {
315         float top_dist, bot_dist;
316         glm::vec3 top_norm, bot_norm;
317         bool top_hit = Intersection(ray, top, M, &top_dist, &top_norm);
318         bool bot_hit = Intersection(ray, bot, M, &bot_dist, &bot_norm);
319
320         if (top_hit) {
321                 if (bot_hit) {
322                         if (top_dist < bot_dist) {
323                                 dist = top_dist;
324                                 norm = top_norm;
325                                 return true;
326                         } else {
327                                 dist = bot_dist;
328                                 norm = bot_norm;
329                                 return true;
330                         }
331                 } else {
332                         dist = top_dist;
333                         norm = top_norm;
334                         return true;
335                 }
336         } else if (bot_hit) {
337                 dist = bot_dist;
338                 norm = bot_norm;
339                 return true;
340         } else {
341                 return false;
342         }
343 }
344
345 bool StairShape::Intersects(
346         const glm::mat4 &M,
347         const AABB &box,
348         const glm::mat4 &box_M
349 ) const noexcept {
350         return Intersection(bot, M, box, box_M) || Intersection(top, M, box, box_M);
351 }
352
353 }