]> git.localhorst.tv Git - blank.git/blob - src/model/geometry.cpp
new turn style
[blank.git] / src / model / geometry.cpp
1 #include "geometry.hpp"
2
3 #include <limits>
4 #include <glm/gtx/matrix_cross_product.hpp>
5 #include <glm/gtx/optimum_pow.hpp>
6 #include <glm/gtx/transform.hpp>
7
8
9 namespace blank {
10
11 glm::mat3 find_rotation(const glm::vec3 &a, const glm::vec3 &b) noexcept {
12         glm::vec3 v(cross(a, b));
13         if (iszero(v)) {
14                 // a and b are parallel
15                 if (iszero(a - b)) {
16                         // a and b are identical
17                         return glm::mat3(1.0f);
18                 } else {
19                         // a and b are opposite
20                         // create arbitrary unit vector perpendicular to a and
21                         // rotate 180° around it
22                         glm::vec3 arb(a);
23                         if (std::abs(a.x - 1.0f) > std::numeric_limits<float>::epsilon()) {
24                                 arb.x += 1.0f;
25                         } else {
26                                 arb.y += 1.0f;
27                         }
28                         glm::vec3 axis(normalize(cross(a, arb)));
29                         return glm::mat3(glm::rotate(PI, axis));
30                 }
31         }
32         float mv = length_squared(v);
33         float c = dot(a, b);
34         float f = (1 - c) / mv;
35         glm::mat3 vx(matrixCross3(v));
36         return glm::mat3(1.0f) + vx + (pow2(vx) * f);
37 }
38
39 bool Intersection(
40         const Ray &ray,
41         const AABB &aabb,
42         const glm::mat4 &M,
43         float *dist,
44         glm::vec3 *normal
45 ) noexcept {
46         float t_min = 0.0f;
47         float t_max = std::numeric_limits<float>::infinity();
48         const glm::vec3 aabb_pos(M[3].x, M[3].y, M[3].z);
49         const glm::vec3 delta = aabb_pos - ray.orig;
50
51         glm::vec3 t1(t_min, t_min, t_min), t2(t_max, t_max, t_max);
52
53         for (int i = 0; i < 3; ++i) {
54                 const glm::vec3 axis(M[i].x, M[i].y, M[i].z);
55                 const float e = glm::dot(axis, delta);
56                 const float f = glm::dot(axis, ray.dir);
57
58                 if (std::abs(f) > std::numeric_limits<float>::epsilon()) {
59                         t1[i] = (e + aabb.min[i]) / f;
60                         t2[i] = (e + aabb.max[i]) / f;
61
62                         t_min = std::max(t_min, std::min(t1[i], t2[i]));
63                         t_max = std::min(t_max, std::max(t1[i], t2[i]));
64
65                         if (t_max < t_min) {
66                                 return false;
67                         }
68                 } else {
69                         if (aabb.min[i] - e > 0.0f || aabb.max[i] - e < 0.0f) {
70                                 return false;
71                         }
72                 }
73         }
74
75         glm::vec3 min_all(min(t1, t2));
76
77         if (dist) {
78                 *dist = t_min;
79         }
80         if (normal) {
81                 if (min_all.x > min_all.y) {
82                         if (min_all.x > min_all.z) {
83                                 normal->x = t2.x < t1.x ? 1 : -1;
84                         } else {
85                                 normal->z = t2.z < t1.z ? 1 : -1;
86                         }
87                 } else if (min_all.y > min_all.z) {
88                         normal->y = t2.y < t1.y ? 1 : -1;
89                 } else {
90                         normal->z = t2.z < t1.z ? 1 : -1;
91                 }
92         }
93         return true;
94 }
95
96
97 bool Intersection(
98         const AABB &a_box,
99         const glm::mat4 &a_m,
100         const AABB &b_box,
101         const glm::mat4 &b_m,
102         float &depth,
103         glm::vec3 &normal
104 ) noexcept {
105         glm::vec3 a_corners[8] = {
106                 glm::vec3(a_m * glm::vec4(a_box.min.x, a_box.min.y, a_box.min.z, 1)),
107                 glm::vec3(a_m * glm::vec4(a_box.min.x, a_box.min.y, a_box.max.z, 1)),
108                 glm::vec3(a_m * glm::vec4(a_box.min.x, a_box.max.y, a_box.min.z, 1)),
109                 glm::vec3(a_m * glm::vec4(a_box.min.x, a_box.max.y, a_box.max.z, 1)),
110                 glm::vec3(a_m * glm::vec4(a_box.max.x, a_box.min.y, a_box.min.z, 1)),
111                 glm::vec3(a_m * glm::vec4(a_box.max.x, a_box.min.y, a_box.max.z, 1)),
112                 glm::vec3(a_m * glm::vec4(a_box.max.x, a_box.max.y, a_box.min.z, 1)),
113                 glm::vec3(a_m * glm::vec4(a_box.max.x, a_box.max.y, a_box.max.z, 1)),
114         };
115
116         glm::vec3 b_corners[8] = {
117                 glm::vec3(b_m * glm::vec4(b_box.min.x, b_box.min.y, b_box.min.z, 1)),
118                 glm::vec3(b_m * glm::vec4(b_box.min.x, b_box.min.y, b_box.max.z, 1)),
119                 glm::vec3(b_m * glm::vec4(b_box.min.x, b_box.max.y, b_box.min.z, 1)),
120                 glm::vec3(b_m * glm::vec4(b_box.min.x, b_box.max.y, b_box.max.z, 1)),
121                 glm::vec3(b_m * glm::vec4(b_box.max.x, b_box.min.y, b_box.min.z, 1)),
122                 glm::vec3(b_m * glm::vec4(b_box.max.x, b_box.min.y, b_box.max.z, 1)),
123                 glm::vec3(b_m * glm::vec4(b_box.max.x, b_box.max.y, b_box.min.z, 1)),
124                 glm::vec3(b_m * glm::vec4(b_box.max.x, b_box.max.y, b_box.max.z, 1)),
125         };
126
127         glm::vec3 axes[6] = {
128                 glm::vec3(a_m * glm::vec4(1, 0, 0, 0)),
129                 glm::vec3(a_m * glm::vec4(0, 1, 0, 0)),
130                 glm::vec3(a_m * glm::vec4(0, 0, 1, 0)),
131                 glm::vec3(b_m * glm::vec4(1, 0, 0, 0)),
132                 glm::vec3(b_m * glm::vec4(0, 1, 0, 0)),
133                 glm::vec3(b_m * glm::vec4(0, 0, 1, 0)),
134         };
135
136         depth = std::numeric_limits<float>::infinity();
137         int min_axis = 0;
138
139         int cur_axis = 0;
140         for (const glm::vec3 &axis : axes) {
141                 float a_min = std::numeric_limits<float>::infinity();
142                 float a_max = -std::numeric_limits<float>::infinity();
143                 for (const glm::vec3 &corner : a_corners) {
144                         float val = glm::dot(corner, axis);
145                         a_min = std::min(a_min, val);
146                         a_max = std::max(a_max, val);
147                 }
148
149                 float b_min = std::numeric_limits<float>::infinity();
150                 float b_max = -std::numeric_limits<float>::infinity();
151                 for (const glm::vec3 &corner : b_corners) {
152                         float val = glm::dot(corner, axis);
153                         b_min = std::min(b_min, val);
154                         b_max = std::max(b_max, val);
155                 }
156
157                 if (a_max < b_min || b_max < a_min) return false;
158
159                 float overlap = std::min(a_max, b_max) - std::max(a_min, b_min);
160                 if (overlap < depth) {
161                         depth = overlap;
162                         min_axis = cur_axis;
163                 }
164
165                 ++cur_axis;
166         }
167
168         normal = axes[min_axis];
169         return true;
170 }
171
172
173 bool CullTest(const AABB &box, const glm::mat4 &MVP) noexcept {
174         // transform corners into clip space
175         glm::vec4 corners[8] = {
176                 { box.min.x, box.min.y, box.min.z, 1.0f },
177                 { box.min.x, box.min.y, box.max.z, 1.0f },
178                 { box.min.x, box.max.y, box.min.z, 1.0f },
179                 { box.min.x, box.max.y, box.max.z, 1.0f },
180                 { box.max.x, box.min.y, box.min.z, 1.0f },
181                 { box.max.x, box.min.y, box.max.z, 1.0f },
182                 { box.max.x, box.max.y, box.min.z, 1.0f },
183                 { box.max.x, box.max.y, box.max.z, 1.0f },
184         };
185         for (glm::vec4 &corner : corners) {
186                 corner = MVP * corner;
187                 corner /= corner.w;
188         }
189
190         int hits[6] = { 0, 0, 0, 0, 0, 0 };
191
192         // check how many corners lie outside
193         for (const glm::vec4 &corner : corners) {
194                 if (corner.x >  1.0f) ++hits[0];
195                 if (corner.x < -1.0f) ++hits[1];
196                 if (corner.y >  1.0f) ++hits[2];
197                 if (corner.y < -1.0f) ++hits[3];
198                 if (corner.z >  1.0f) ++hits[4];
199                 if (corner.z < -1.0f) ++hits[5];
200         }
201
202         // if all corners are outside any given clip plane, the test is true
203         for (int hit : hits) {
204                 if (hit == 8) return true;
205         }
206
207         // otherwise the box might still get culled completely, but can't say for sure ;)
208         return false;
209 }
210
211 }