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