]> git.localhorst.tv Git - blank.git/blob - src/model/geometry.cpp
416d930c4f68d0c24a0f690851264b98556b855b
[blank.git] / src / model / geometry.cpp
1 #include "geometry.hpp"
2
3 #include <limits>
4
5
6 namespace blank {
7
8 bool Intersection(
9         const Ray &ray,
10         const AABB &aabb,
11         const glm::mat4 &M,
12         float *dist,
13         glm::vec3 *normal
14 ) noexcept {
15         float t_min = 0.0f;
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;
19
20         glm::vec3 t1(t_min, t_min, t_min), t2(t_max, t_max, t_max);
21
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);
26
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;
30
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]));
33
34                         if (t_max < t_min) {
35                                 return false;
36                         }
37                 } else {
38                         if (aabb.min[i] - e < 0.0f || -aabb.max[i] - e > 0.0f) {
39                                 return false;
40                         }
41                 }
42         }
43
44         glm::vec3 min_all(min(t1, t2));
45
46         if (dist) {
47                 *dist = t_min;
48         }
49         if (normal) {
50                 glm::vec4 norm(0.0f);
51                 if (min_all.x > min_all.y) {
52                         if (min_all.x > min_all.z) {
53                                 norm.x = t2.x < t1.x ? 1 : -1;
54                         } else {
55                                 norm.z = t2.z < t1.z ? 1 : -1;
56                         }
57                 } else if (min_all.y > min_all.z) {
58                         norm.y = t2.y < t1.y ? 1 : -1;
59                 } else {
60                         norm.z = t2.z < t1.z ? 1 : -1;
61                 }
62                 norm = M * norm;
63                 *normal = glm::vec3(norm);
64         }
65         return true;
66 }
67
68 bool CullTest(const AABB &box, const glm::mat4 &MVP) noexcept {
69         // transform corners into clip space
70         glm::vec4 corners[8] = {
71                 { box.min.x, box.min.y, box.min.z, 1.0f },
72                 { box.min.x, box.min.y, box.max.z, 1.0f },
73                 { box.min.x, box.max.y, box.min.z, 1.0f },
74                 { box.min.x, box.max.y, box.max.z, 1.0f },
75                 { box.max.x, box.min.y, box.min.z, 1.0f },
76                 { box.max.x, box.min.y, box.max.z, 1.0f },
77                 { box.max.x, box.max.y, box.min.z, 1.0f },
78                 { box.max.x, box.max.y, box.max.z, 1.0f },
79         };
80         for (glm::vec4 &corner : corners) {
81                 corner = MVP * corner;
82                 corner /= corner.w;
83         }
84
85         int hits[6] = { 0, 0, 0, 0, 0, 0 };
86
87         // check how many corners lie outside
88         for (const glm::vec4 &corner : corners) {
89                 if (corner.x >  1.0f) ++hits[0];
90                 if (corner.x < -1.0f) ++hits[1];
91                 if (corner.y >  1.0f) ++hits[2];
92                 if (corner.y < -1.0f) ++hits[3];
93                 if (corner.z >  1.0f) ++hits[4];
94                 if (corner.z < -1.0f) ++hits[5];
95         }
96
97         // if all corners are outside any given clip plane, the test is true
98         for (int hit : hits) {
99                 if (hit == 8) return true;
100         }
101
102         // otherwise the box might still get culled completely, but can't say for sure ;)
103         return false;
104 }
105
106 }