]> git.localhorst.tv Git - blank.git/blob - src/model/shape.cpp
figure out depth and normal in box/box test
[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         float depth;
184         glm::vec3 normal;
185         return Intersection(bb, M, box, box_M, depth, normal);
186 }
187
188
189 StairShape::StairShape(const AABB &bb, const glm::vec2 &clip)
190 : Shape()
191 , top({ { bb.min.x, clip.y, bb.min.z }, { bb.max.x, bb.max.y, clip.x } })
192 , bot({ bb.min, { bb.max.x, clip.y, bb.max.z } }) {
193         SetShape({
194                 { top.min.x, top.min.y, top.max.z }, // front, upper
195                 { top.max.x, top.min.y, top.max.z },
196                 { top.min.x, top.max.y, top.max.z },
197                 { top.max.x, top.max.y, top.max.z },
198                 { bot.min.x, bot.min.y, bot.max.z }, // front, lower
199                 { bot.max.x, bot.min.y, bot.max.z },
200                 { bot.min.x, bot.max.y, bot.max.z },
201                 { bot.max.x, bot.max.y, bot.max.z },
202                 { bot.min.x, bot.min.y, bot.min.z }, // back
203                 { bot.min.x, top.max.y, bot.min.z },
204                 { top.max.x, bot.min.y, bot.min.z },
205                 { top.max.x, top.max.y, bot.min.z },
206                 { top.min.x, top.max.y, top.min.z }, // top, upper
207                 { top.min.x, top.max.y, top.max.z },
208                 { top.max.x, top.max.y, top.min.z },
209                 { top.max.x, top.max.y, top.max.z },
210                 { bot.min.x, bot.max.y, top.max.z }, // top, lower
211                 { bot.min.x, bot.max.y, bot.max.z },
212                 { bot.max.x, bot.max.y, top.max.z },
213                 { bot.max.x, bot.max.y, bot.max.z },
214                 { bot.min.x, bot.min.y, bot.min.z }, // bottom
215                 { bot.max.x, bot.min.y, bot.min.z },
216                 { bot.min.x, bot.min.y, bot.max.z },
217                 { bot.max.x, bot.min.y, bot.max.z },
218                 { top.min.x, top.min.y, top.min.z }, // left, upper
219                 { top.min.x, top.min.y, top.max.z },
220                 { top.min.x, top.max.y, top.min.z },
221                 { top.min.x, top.max.y, top.max.z },
222                 { bot.min.x, bot.min.y, bot.min.z }, // left, lower
223                 { bot.min.x, bot.min.y, bot.max.z },
224                 { bot.min.x, bot.max.y, bot.min.z },
225                 { bot.min.x, bot.max.y, bot.max.z },
226                 { top.max.x, top.min.y, top.min.z }, // right, upper
227                 { top.max.x, top.max.y, top.min.z },
228                 { top.max.x, top.min.y, top.max.z },
229                 { top.max.x, top.max.y, top.max.z },
230                 { bot.max.x, bot.min.y, bot.min.z }, // right, lower
231                 { bot.max.x, bot.max.y, bot.min.z },
232                 { bot.max.x, bot.min.y, bot.max.z },
233                 { bot.max.x, bot.max.y, bot.max.z },
234         }, {
235                 {  0.0f,  0.0f,  1.0f }, // front x2
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 },
242                 {  0.0f,  0.0f,  1.0f },
243                 {  0.0f,  0.0f, -1.0f }, // back
244                 {  0.0f,  0.0f, -1.0f },
245                 {  0.0f,  0.0f, -1.0f },
246                 {  0.0f,  0.0f, -1.0f },
247                 {  0.0f,  1.0f,  0.0f }, // top x2
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 },
254                 {  0.0f,  1.0f,  0.0f },
255                 {  0.0f, -1.0f,  0.0f }, // bottom
256                 {  0.0f, -1.0f,  0.0f },
257                 {  0.0f, -1.0f,  0.0f },
258                 {  0.0f, -1.0f,  0.0f },
259                 { -1.0f,  0.0f,  0.0f }, // left x2
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 },
266                 { -1.0f,  0.0f,  0.0f },
267                 {  1.0f,  0.0f,  0.0f }, // right x2
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                 {  1.0f,  0.0f,  0.0f },
274                 {  1.0f,  0.0f,  0.0f },
275         }, {
276                  0,  1,  2,  2,  1,  3, // front, upper
277                  4,  5,  6,  6,  5,  7, // front, lower
278                  8,  9, 10, 10,  9, 11, // back
279                 12, 13, 14, 14, 13, 15, // top, upper
280                 16, 17, 18, 18, 17, 19, // top, lower
281                 20, 21, 22, 22, 21, 23, // bottom
282                 24, 25, 26, 26, 25, 27, // left, upper
283                 28, 29, 30, 30, 29, 31, // left, lower
284                 32, 33, 34, 34, 33, 35, // right, upper
285                 36, 37, 38, 38, 37, 39, // right, lower
286         });
287         SetOutline({
288                 { bot.min.x, bot.min.y, bot.min.z }, // bottom
289                 { bot.max.x, bot.min.y, bot.min.z },
290                 { bot.min.x, bot.min.y, bot.max.z },
291                 { bot.max.x, bot.min.y, bot.max.z },
292                 { bot.min.x, bot.max.y, top.max.z }, // middle
293                 { bot.max.x, bot.max.y, top.max.z },
294                 { bot.min.x, bot.max.y, bot.max.z },
295                 { bot.max.x, bot.max.y, bot.max.z },
296                 { top.min.x, top.max.y, top.min.z }, // top
297                 { top.max.x, top.max.y, top.min.z },
298                 { top.min.x, top.max.y, top.max.z },
299                 { top.max.x, top.max.y, top.max.z },
300         }, {
301                  0,  1,  1,  3,  3,  2,  2,  0, // bottom
302                  4,  5,  5,  7,  7,  6,  6,  4, // middle
303                  8,  9,  9, 11, 11, 10, 10 , 8, // top
304                  0,  8,  4, 10,  2,  6, // verticals, btf
305                  1,  9,  5, 11,  3,  7,
306         //       5,  8,  7, 10,
307         //       1,  9,  3, 11,
308         });
309 }
310
311 bool StairShape::Intersects(
312         const Ray &ray,
313         const glm::mat4 &M,
314         float &dist,
315         glm::vec3 &norm
316 ) const noexcept {
317         float top_dist, bot_dist;
318         glm::vec3 top_norm, bot_norm;
319         bool top_hit = Intersection(ray, top, M, &top_dist, &top_norm);
320         bool bot_hit = Intersection(ray, bot, M, &bot_dist, &bot_norm);
321
322         if (top_hit) {
323                 if (bot_hit) {
324                         if (top_dist < bot_dist) {
325                                 dist = top_dist;
326                                 norm = top_norm;
327                                 return true;
328                         } else {
329                                 dist = bot_dist;
330                                 norm = bot_norm;
331                                 return true;
332                         }
333                 } else {
334                         dist = top_dist;
335                         norm = top_norm;
336                         return true;
337                 }
338         } else if (bot_hit) {
339                 dist = bot_dist;
340                 norm = bot_norm;
341                 return true;
342         } else {
343                 return false;
344         }
345 }
346
347 bool StairShape::Intersects(
348         const glm::mat4 &M,
349         const AABB &box,
350         const glm::mat4 &box_M
351 ) const noexcept {
352         float depth;
353         glm::vec3 normal;
354         return Intersection(bot, M, box, box_M, depth, normal) || Intersection(top, M, box, box_M, depth, normal);
355 }
356
357 }