2 #include "distance.hpp"
3 #include "primitive.hpp"
4 #include "rotation.hpp"
8 #include <glm/gtx/io.hpp>
9 #include <glm/gtx/matrix_cross_product.hpp>
10 #include <glm/gtx/optimum_pow.hpp>
11 #include <glm/gtx/transform.hpp>
17 glm::mat3 find_rotation(const glm::vec3 &a, const glm::vec3 &b) noexcept {
18 glm::vec3 v(glm::cross(a, b));
20 // a and b are parallel
22 // a and b are identical
23 return glm::mat3(1.0f);
25 // a and b are opposite
26 // create arbitrary unit vector perpendicular to a and
27 // rotate 180° around it
29 if (std::abs(a.x - 1.0f) > std::numeric_limits<float>::epsilon()) {
34 glm::vec3 axis(glm::normalize(glm::cross(a, arb)));
35 return glm::mat3(glm::rotate(PI, axis));
38 float mv = glm::length2(v);
39 float c = glm::dot(a, b);
40 float f = (1 - c) / mv;
41 glm::mat3 vx(glm::matrixCross3(v));
42 return glm::mat3(1.0f) + vx + (glm::pow2(vx) * f);
45 std::ostream &operator <<(std::ostream &out, const AABB &box) {
46 return out << "AABB(" << box.min << ", " << box.max << ')';
49 std::ostream &operator <<(std::ostream &out, const Ray &ray) {
50 return out << "Ray(" << ray.orig << ", " << ray.dir << ')';
59 float t_max = std::numeric_limits<float>::infinity();
60 for (int i = 0; i < 3; ++i) {
61 float t1 = (box.min[i] - ray.orig[i]) * ray.inv_dir[i];
62 float t2 = (box.max[i] - ray.orig[i]) * ray.inv_dir[i];
63 t_min = std::max(t_min, std::min(t1, t2));
64 t_max = std::min(t_max, std::max(t1, t2));
67 return t_max >= t_min;
78 float t_max = std::numeric_limits<float>::infinity();
79 const glm::vec3 aabb_pos(M[3].x, M[3].y, M[3].z);
80 const glm::vec3 delta = aabb_pos - ray.orig;
82 glm::vec3 t1(t_min, t_min, t_min), t2(t_max, t_max, t_max);
84 for (int i = 0; i < 3; ++i) {
85 const glm::vec3 axis(M[i].x, M[i].y, M[i].z);
86 const float e = glm::dot(axis, delta);
87 const float f = glm::dot(axis, ray.dir);
89 if (std::abs(f) > std::numeric_limits<float>::epsilon()) {
90 t1[i] = (e + aabb.min[i]) / f;
91 t2[i] = (e + aabb.max[i]) / f;
93 t_min = std::max(t_min, std::min(t1[i], t2[i]));
94 t_max = std::min(t_max, std::max(t1[i], t2[i]));
100 if (aabb.min[i] - e > 0.0f || aabb.max[i] - e < 0.0f) {
110 glm::vec3 min_all(glm::min(t1, t2));
111 if (min_all.x > min_all.y) {
112 if (min_all.x > min_all.z) {
113 *normal = glm::vec3(t2.x < t1.x ? 1 : -1, 0, 0);
115 *normal = glm::vec3(0, 0, t2.z < t1.z ? 1 : -1);
117 } else if (min_all.y > min_all.z) {
118 *normal = glm::vec3(0, t2.y < t1.y ? 1 : -1, 0);
120 *normal = glm::vec3(0, 0, t2.z < t1.z ? 1 : -1);
128 const glm::mat4 &a_m,
130 const glm::mat4 &b_m,
134 glm::vec3 a_corners[8] = {
135 glm::vec3(a_m * glm::vec4(a_box.min.x, a_box.min.y, a_box.min.z, 1)),
136 glm::vec3(a_m * glm::vec4(a_box.min.x, a_box.min.y, a_box.max.z, 1)),
137 glm::vec3(a_m * glm::vec4(a_box.min.x, a_box.max.y, a_box.min.z, 1)),
138 glm::vec3(a_m * glm::vec4(a_box.min.x, a_box.max.y, a_box.max.z, 1)),
139 glm::vec3(a_m * glm::vec4(a_box.max.x, a_box.min.y, a_box.min.z, 1)),
140 glm::vec3(a_m * glm::vec4(a_box.max.x, a_box.min.y, a_box.max.z, 1)),
141 glm::vec3(a_m * glm::vec4(a_box.max.x, a_box.max.y, a_box.min.z, 1)),
142 glm::vec3(a_m * glm::vec4(a_box.max.x, a_box.max.y, a_box.max.z, 1)),
145 glm::vec3 b_corners[8] = {
146 glm::vec3(b_m * glm::vec4(b_box.min.x, b_box.min.y, b_box.min.z, 1)),
147 glm::vec3(b_m * glm::vec4(b_box.min.x, b_box.min.y, b_box.max.z, 1)),
148 glm::vec3(b_m * glm::vec4(b_box.min.x, b_box.max.y, b_box.min.z, 1)),
149 glm::vec3(b_m * glm::vec4(b_box.min.x, b_box.max.y, b_box.max.z, 1)),
150 glm::vec3(b_m * glm::vec4(b_box.max.x, b_box.min.y, b_box.min.z, 1)),
151 glm::vec3(b_m * glm::vec4(b_box.max.x, b_box.min.y, b_box.max.z, 1)),
152 glm::vec3(b_m * glm::vec4(b_box.max.x, b_box.max.y, b_box.min.z, 1)),
153 glm::vec3(b_m * glm::vec4(b_box.max.x, b_box.max.y, b_box.max.z, 1)),
156 glm::vec3 axes[15] = {
163 glm::normalize(glm::cross(glm::vec3(a_m[0]), glm::vec3(b_m[0]))),
164 glm::normalize(glm::cross(glm::vec3(a_m[0]), glm::vec3(b_m[1]))),
165 glm::normalize(glm::cross(glm::vec3(a_m[0]), glm::vec3(b_m[2]))),
166 glm::normalize(glm::cross(glm::vec3(a_m[1]), glm::vec3(b_m[0]))),
167 glm::normalize(glm::cross(glm::vec3(a_m[1]), glm::vec3(b_m[1]))),
168 glm::normalize(glm::cross(glm::vec3(a_m[1]), glm::vec3(b_m[2]))),
169 glm::normalize(glm::cross(glm::vec3(a_m[2]), glm::vec3(b_m[0]))),
170 glm::normalize(glm::cross(glm::vec3(a_m[2]), glm::vec3(b_m[1]))),
171 glm::normalize(glm::cross(glm::vec3(a_m[2]), glm::vec3(b_m[2]))),
174 depth = std::numeric_limits<float>::infinity();
178 for (const glm::vec3 &axis : axes) {
179 if (glm::any(glm::isnan(axis))) {
180 // can result from the cross products if A and B have parallel axes
184 float a_min = std::numeric_limits<float>::infinity();
185 float a_max = -std::numeric_limits<float>::infinity();
186 for (const glm::vec3 &corner : a_corners) {
187 float val = glm::dot(corner, axis);
188 a_min = std::min(a_min, val);
189 a_max = std::max(a_max, val);
192 float b_min = std::numeric_limits<float>::infinity();
193 float b_max = -std::numeric_limits<float>::infinity();
194 for (const glm::vec3 &corner : b_corners) {
195 float val = glm::dot(corner, axis);
196 b_min = std::min(b_min, val);
197 b_max = std::max(b_max, val);
200 if (a_max < b_min || b_max < a_min) return false;
202 float overlap = std::min(a_max, b_max) - std::max(a_min, b_min);
203 if (overlap < depth) {
211 normal = axes[min_axis];
216 std::ostream &operator <<(std::ostream &out, const Plane &plane) {
217 return out << "Plane(" << plane.normal << ", " << plane.dist << ')';
220 std::ostream &operator <<(std::ostream &out, const Frustum &frustum) {
221 return out << "Frustum(" << std::endl
222 << "\tleft: " << frustum.plane[0] << std::endl
223 << "\tright: " << frustum.plane[1] << std::endl
224 << "\tbottom: " << frustum.plane[2] << std::endl
225 << "\ttop: " << frustum.plane[3] << std::endl
226 << "\tnear: " << frustum.plane[4] << std::endl
227 << "\tfar: " << frustum.plane[5] << std::endl
231 bool CullTest(const AABB &box, const glm::mat4 &MVP) noexcept {
232 // transform corners into clip space
233 glm::vec4 corners[8] = {
234 { box.min.x, box.min.y, box.min.z, 1.0f },
235 { box.min.x, box.min.y, box.max.z, 1.0f },
236 { box.min.x, box.max.y, box.min.z, 1.0f },
237 { box.min.x, box.max.y, box.max.z, 1.0f },
238 { box.max.x, box.min.y, box.min.z, 1.0f },
239 { box.max.x, box.min.y, box.max.z, 1.0f },
240 { box.max.x, box.max.y, box.min.z, 1.0f },
241 { box.max.x, box.max.y, box.max.z, 1.0f },
244 // check how many corners lie outside
245 int hits[6] = { 0, 0, 0, 0, 0, 0 };
246 for (glm::vec4 &corner : corners) {
247 corner = MVP * corner;
248 // replacing this with *= 1/w is effectively more expensive
250 hits[0] += (corner.x > 1.0f);
251 hits[1] += (corner.x < -1.0f);
252 hits[2] += (corner.y > 1.0f);
253 hits[3] += (corner.y < -1.0f);
254 hits[4] += (corner.z > 1.0f);
255 hits[5] += (corner.z < -1.0f);
258 // if all corners are outside any given clip plane, the test is true
259 for (int hit : hits) {
260 if (hit == 8) return true;
263 // otherwise the box might still get culled completely, but can't say for sure ;)
267 bool CullTest(const AABB &box, const Frustum &frustum) noexcept {
268 for (const Plane &plane : frustum.plane) {
270 ((plane.normal.x > 0.0f) ? box.max.x : box.min.x),
271 ((plane.normal.y > 0.0f) ? box.max.y : box.min.y),
272 ((plane.normal.z > 0.0f) ? box.max.z : box.min.z)
274 const float dp = glm::dot(plane.normal, np);
275 // cull if nearest point is on the "outside" side of the plane
276 if (dp < -plane.dist) return true;