1 #include "geometry.hpp"
16 float t_max = std::numeric_limits<float>::infinity();
17 const glm::vec3 aabb_pos(M[3].x, M[3].y, M[3].z);
18 const glm::vec3 delta = aabb_pos - ray.orig;
20 glm::vec3 t1(t_min, t_min, t_min), t2(t_max, t_max, t_max);
22 for (int i = 0; i < 3; ++i) {
23 const glm::vec3 axis(M[i].x, M[i].y, M[i].z);
24 const float e = glm::dot(axis, delta);
25 const float f = glm::dot(axis, ray.dir);
27 if (std::abs(f) > std::numeric_limits<float>::epsilon()) {
28 t1[i] = (e + aabb.min[i]) / f;
29 t2[i] = (e + aabb.max[i]) / f;
31 t_min = std::max(t_min, std::min(t1[i], t2[i]));
32 t_max = std::min(t_max, std::max(t1[i], t2[i]));
38 if (aabb.min[i] - e > 0.0f || aabb.max[i] - e < 0.0f) {
44 glm::vec3 min_all(min(t1, t2));
50 if (min_all.x > min_all.y) {
51 if (min_all.x > min_all.z) {
52 normal->x = t2.x < t1.x ? 1 : -1;
54 normal->z = t2.z < t1.z ? 1 : -1;
56 } else if (min_all.y > min_all.z) {
57 normal->y = t2.y < t1.y ? 1 : -1;
59 normal->z = t2.z < t1.z ? 1 : -1;
74 glm::vec3 a_corners[8] = {
75 glm::vec3(a_m * glm::vec4(a_box.min.x, a_box.min.y, a_box.min.z, 1)),
76 glm::vec3(a_m * glm::vec4(a_box.min.x, a_box.min.y, a_box.max.z, 1)),
77 glm::vec3(a_m * glm::vec4(a_box.min.x, a_box.max.y, a_box.min.z, 1)),
78 glm::vec3(a_m * glm::vec4(a_box.min.x, a_box.max.y, a_box.max.z, 1)),
79 glm::vec3(a_m * glm::vec4(a_box.max.x, a_box.min.y, a_box.min.z, 1)),
80 glm::vec3(a_m * glm::vec4(a_box.max.x, a_box.min.y, a_box.max.z, 1)),
81 glm::vec3(a_m * glm::vec4(a_box.max.x, a_box.max.y, a_box.min.z, 1)),
82 glm::vec3(a_m * glm::vec4(a_box.max.x, a_box.max.y, a_box.max.z, 1)),
85 glm::vec3 b_corners[8] = {
86 glm::vec3(b_m * glm::vec4(b_box.min.x, b_box.min.y, b_box.min.z, 1)),
87 glm::vec3(b_m * glm::vec4(b_box.min.x, b_box.min.y, b_box.max.z, 1)),
88 glm::vec3(b_m * glm::vec4(b_box.min.x, b_box.max.y, b_box.min.z, 1)),
89 glm::vec3(b_m * glm::vec4(b_box.min.x, b_box.max.y, b_box.max.z, 1)),
90 glm::vec3(b_m * glm::vec4(b_box.max.x, b_box.min.y, b_box.min.z, 1)),
91 glm::vec3(b_m * glm::vec4(b_box.max.x, b_box.min.y, b_box.max.z, 1)),
92 glm::vec3(b_m * glm::vec4(b_box.max.x, b_box.max.y, b_box.min.z, 1)),
93 glm::vec3(b_m * glm::vec4(b_box.max.x, b_box.max.y, b_box.max.z, 1)),
97 glm::vec3(a_m * glm::vec4(1, 0, 0, 0)),
98 glm::vec3(a_m * glm::vec4(0, 1, 0, 0)),
99 glm::vec3(a_m * glm::vec4(0, 0, 1, 0)),
100 glm::vec3(b_m * glm::vec4(1, 0, 0, 0)),
101 glm::vec3(b_m * glm::vec4(0, 1, 0, 0)),
102 glm::vec3(b_m * glm::vec4(0, 0, 1, 0)),
105 depth = std::numeric_limits<float>::infinity();
109 for (const glm::vec3 &axis : axes) {
110 float a_min = std::numeric_limits<float>::infinity();
111 float a_max = -std::numeric_limits<float>::infinity();
112 for (const glm::vec3 &corner : a_corners) {
113 float val = glm::dot(corner, axis);
114 a_min = std::min(a_min, val);
115 a_max = std::max(a_max, val);
118 float b_min = std::numeric_limits<float>::infinity();
119 float b_max = -std::numeric_limits<float>::infinity();
120 for (const glm::vec3 &corner : b_corners) {
121 float val = glm::dot(corner, axis);
122 b_min = std::min(b_min, val);
123 b_max = std::max(b_max, val);
126 if (a_max < b_min || b_max < a_min) return false;
128 float overlap = std::min(a_max, b_max) - std::max(a_min, b_min);
129 if (overlap < depth) {
137 normal = axes[min_axis];
142 bool CullTest(const AABB &box, const glm::mat4 &MVP) noexcept {
143 // transform corners into clip space
144 glm::vec4 corners[8] = {
145 { box.min.x, box.min.y, box.min.z, 1.0f },
146 { box.min.x, box.min.y, box.max.z, 1.0f },
147 { box.min.x, box.max.y, box.min.z, 1.0f },
148 { box.min.x, box.max.y, box.max.z, 1.0f },
149 { box.max.x, box.min.y, box.min.z, 1.0f },
150 { box.max.x, box.min.y, box.max.z, 1.0f },
151 { box.max.x, box.max.y, box.min.z, 1.0f },
152 { box.max.x, box.max.y, box.max.z, 1.0f },
154 for (glm::vec4 &corner : corners) {
155 corner = MVP * corner;
159 int hits[6] = { 0, 0, 0, 0, 0, 0 };
161 // check how many corners lie outside
162 for (const glm::vec4 &corner : corners) {
163 if (corner.x > 1.0f) ++hits[0];
164 if (corner.x < -1.0f) ++hits[1];
165 if (corner.y > 1.0f) ++hits[2];
166 if (corner.y < -1.0f) ++hits[3];
167 if (corner.z > 1.0f) ++hits[4];
168 if (corner.z < -1.0f) ++hits[5];
171 // if all corners are outside any given clip plane, the test is true
172 for (int hit : hits) {
173 if (hit == 8) return true;
176 // otherwise the box might still get culled completely, but can't say for sure ;)