2 #include "distance.hpp"
3 #include "primitive.hpp"
4 #include "rotation.hpp"
7 #include <glm/gtx/matrix_cross_product.hpp>
8 #include <glm/gtx/optimum_pow.hpp>
9 #include <glm/gtx/transform.hpp>
14 glm::mat3 find_rotation(const glm::vec3 &a, const glm::vec3 &b) noexcept {
15 glm::vec3 v(cross(a, b));
17 // a and b are parallel
19 // a and b are identical
20 return glm::mat3(1.0f);
22 // a and b are opposite
23 // create arbitrary unit vector perpendicular to a and
24 // rotate 180° around it
26 if (std::abs(a.x - 1.0f) > std::numeric_limits<float>::epsilon()) {
31 glm::vec3 axis(normalize(cross(a, arb)));
32 return glm::mat3(glm::rotate(PI, axis));
35 float mv = length_squared(v);
37 float f = (1 - c) / mv;
38 glm::mat3 vx(matrixCross3(v));
39 return glm::mat3(1.0f) + vx + (pow2(vx) * f);
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;
54 glm::vec3 t1(t_min, t_min, t_min), t2(t_max, t_max, t_max);
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);
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;
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]));
72 if (aabb.min[i] - e > 0.0f || aabb.max[i] - e < 0.0f) {
78 glm::vec3 min_all(min(t1, t2));
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;
88 normal->z = t2.z < t1.z ? 1 : -1;
90 } else if (min_all.y > min_all.z) {
91 normal->y = t2.y < t1.y ? 1 : -1;
93 normal->z = t2.z < t1.z ? 1 : -1;
102 const glm::mat4 &a_m,
104 const glm::mat4 &b_m,
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)),
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)),
130 glm::vec3 axes[6] = {
131 glm::vec3(a_m * glm::vec4(1, 0, 0, 0)),
132 glm::vec3(a_m * glm::vec4(0, 1, 0, 0)),
133 glm::vec3(a_m * glm::vec4(0, 0, 1, 0)),
134 glm::vec3(b_m * glm::vec4(1, 0, 0, 0)),
135 glm::vec3(b_m * glm::vec4(0, 1, 0, 0)),
136 glm::vec3(b_m * glm::vec4(0, 0, 1, 0)),
139 depth = std::numeric_limits<float>::infinity();
143 for (const glm::vec3 &axis : axes) {
144 float a_min = std::numeric_limits<float>::infinity();
145 float a_max = -std::numeric_limits<float>::infinity();
146 for (const glm::vec3 &corner : a_corners) {
147 float val = glm::dot(corner, axis);
148 a_min = std::min(a_min, val);
149 a_max = std::max(a_max, val);
152 float b_min = std::numeric_limits<float>::infinity();
153 float b_max = -std::numeric_limits<float>::infinity();
154 for (const glm::vec3 &corner : b_corners) {
155 float val = glm::dot(corner, axis);
156 b_min = std::min(b_min, val);
157 b_max = std::max(b_max, val);
160 if (a_max < b_min || b_max < a_min) return false;
162 float overlap = std::min(a_max, b_max) - std::max(a_min, b_min);
163 if (overlap < depth) {
171 normal = axes[min_axis];
176 bool CullTest(const AABB &box, const glm::mat4 &MVP) noexcept {
177 // transform corners into clip space
178 glm::vec4 corners[8] = {
179 { box.min.x, box.min.y, box.min.z, 1.0f },
180 { box.min.x, box.min.y, box.max.z, 1.0f },
181 { box.min.x, box.max.y, box.min.z, 1.0f },
182 { box.min.x, box.max.y, box.max.z, 1.0f },
183 { box.max.x, box.min.y, box.min.z, 1.0f },
184 { box.max.x, box.min.y, box.max.z, 1.0f },
185 { box.max.x, box.max.y, box.min.z, 1.0f },
186 { box.max.x, box.max.y, box.max.z, 1.0f },
188 for (glm::vec4 &corner : corners) {
189 corner = MVP * corner;
193 int hits[6] = { 0, 0, 0, 0, 0, 0 };
195 // check how many corners lie outside
196 for (const glm::vec4 &corner : corners) {
197 if (corner.x > 1.0f) ++hits[0];
198 if (corner.x < -1.0f) ++hits[1];
199 if (corner.y > 1.0f) ++hits[2];
200 if (corner.y < -1.0f) ++hits[3];
201 if (corner.z > 1.0f) ++hits[4];
202 if (corner.z < -1.0f) ++hits[5];
205 // if all corners are outside any given clip plane, the test is true
206 for (int hit : hits) {
207 if (hit == 8) return true;
210 // otherwise the box might still get culled completely, but can't say for sure ;)