]> git.localhorst.tv Git - blank.git/blob - src/model/geometry.cpp
figure out depth and normal in box/box test
[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
69 bool Intersection(
70         const AABB &a_box,
71         const glm::mat4 &a_m,
72         const AABB &b_box,
73         const glm::mat4 &b_m,
74         float &depth,
75         glm::vec3 &normal
76 ) noexcept {
77         glm::vec3 a_corners[8] = {
78                 glm::vec3(a_m * glm::vec4(a_box.min.x, a_box.min.y, a_box.min.z, 1)),
79                 glm::vec3(a_m * glm::vec4(a_box.min.x, a_box.min.y, a_box.max.z, 1)),
80                 glm::vec3(a_m * glm::vec4(a_box.min.x, a_box.max.y, a_box.min.z, 1)),
81                 glm::vec3(a_m * glm::vec4(a_box.min.x, a_box.max.y, a_box.max.z, 1)),
82                 glm::vec3(a_m * glm::vec4(a_box.max.x, a_box.min.y, a_box.min.z, 1)),
83                 glm::vec3(a_m * glm::vec4(a_box.max.x, a_box.min.y, a_box.max.z, 1)),
84                 glm::vec3(a_m * glm::vec4(a_box.max.x, a_box.max.y, a_box.min.z, 1)),
85                 glm::vec3(a_m * glm::vec4(a_box.max.x, a_box.max.y, a_box.max.z, 1)),
86         };
87
88         glm::vec3 b_corners[8] = {
89                 glm::vec3(b_m * glm::vec4(b_box.min.x, b_box.min.y, b_box.min.z, 1)),
90                 glm::vec3(b_m * glm::vec4(b_box.min.x, b_box.min.y, b_box.max.z, 1)),
91                 glm::vec3(b_m * glm::vec4(b_box.min.x, b_box.max.y, b_box.min.z, 1)),
92                 glm::vec3(b_m * glm::vec4(b_box.min.x, b_box.max.y, b_box.max.z, 1)),
93                 glm::vec3(b_m * glm::vec4(b_box.max.x, b_box.min.y, b_box.min.z, 1)),
94                 glm::vec3(b_m * glm::vec4(b_box.max.x, b_box.min.y, b_box.max.z, 1)),
95                 glm::vec3(b_m * glm::vec4(b_box.max.x, b_box.max.y, b_box.min.z, 1)),
96                 glm::vec3(b_m * glm::vec4(b_box.max.x, b_box.max.y, b_box.max.z, 1)),
97         };
98
99         glm::vec3 axes[6] = {
100                 glm::vec3(a_m * glm::vec4(1, 0, 0, 0)),
101                 glm::vec3(a_m * glm::vec4(0, 1, 0, 0)),
102                 glm::vec3(a_m * glm::vec4(0, 0, 1, 0)),
103                 glm::vec3(b_m * glm::vec4(1, 0, 0, 0)),
104                 glm::vec3(b_m * glm::vec4(0, 1, 0, 0)),
105                 glm::vec3(b_m * glm::vec4(0, 0, 1, 0)),
106         };
107
108         depth = std::numeric_limits<float>::infinity();
109         int min_axis = 0;
110
111         int cur_axis = 0;
112         for (const glm::vec3 &axis : axes) {
113                 float a_min = std::numeric_limits<float>::infinity();
114                 float a_max = -std::numeric_limits<float>::infinity();
115                 for (const glm::vec3 &corner : a_corners) {
116                         float val = glm::dot(corner, axis);
117                         a_min = std::min(a_min, val);
118                         a_max = std::max(a_max, val);
119                 }
120
121                 float b_min = std::numeric_limits<float>::infinity();
122                 float b_max = -std::numeric_limits<float>::infinity();
123                 for (const glm::vec3 &corner : b_corners) {
124                         float val = glm::dot(corner, axis);
125                         b_min = std::min(b_min, val);
126                         b_max = std::max(b_max, val);
127                 }
128
129                 if (a_max < b_min || b_max < a_min) return false;
130
131                 float overlap = std::min(a_max, b_max) - std::max(a_min, b_min);
132                 if (overlap < depth) {
133                         depth = overlap;
134                         min_axis = cur_axis;
135                 }
136
137                 ++cur_axis;
138         }
139
140         normal = axes[min_axis];
141         return true;
142 }
143
144
145 bool CullTest(const AABB &box, const glm::mat4 &MVP) noexcept {
146         // transform corners into clip space
147         glm::vec4 corners[8] = {
148                 { box.min.x, box.min.y, box.min.z, 1.0f },
149                 { box.min.x, box.min.y, box.max.z, 1.0f },
150                 { box.min.x, box.max.y, box.min.z, 1.0f },
151                 { box.min.x, box.max.y, box.max.z, 1.0f },
152                 { box.max.x, box.min.y, box.min.z, 1.0f },
153                 { box.max.x, box.min.y, box.max.z, 1.0f },
154                 { box.max.x, box.max.y, box.min.z, 1.0f },
155                 { box.max.x, box.max.y, box.max.z, 1.0f },
156         };
157         for (glm::vec4 &corner : corners) {
158                 corner = MVP * corner;
159                 corner /= corner.w;
160         }
161
162         int hits[6] = { 0, 0, 0, 0, 0, 0 };
163
164         // check how many corners lie outside
165         for (const glm::vec4 &corner : corners) {
166                 if (corner.x >  1.0f) ++hits[0];
167                 if (corner.x < -1.0f) ++hits[1];
168                 if (corner.y >  1.0f) ++hits[2];
169                 if (corner.y < -1.0f) ++hits[3];
170                 if (corner.z >  1.0f) ++hits[4];
171                 if (corner.z < -1.0f) ++hits[5];
172         }
173
174         // if all corners are outside any given clip plane, the test is true
175         for (int hit : hits) {
176                 if (hit == 8) return true;
177         }
178
179         // otherwise the box might still get culled completely, but can't say for sure ;)
180         return false;
181 }
182
183 }