]> git.localhorst.tv Git - blank.git/blob - src/geometry.cpp
basic aiming
[blank.git] / src / geometry.cpp
1 #include "geometry.hpp"
2
3
4 namespace blank {
5
6 bool Intersection(const Ray &ray, const AABB &aabb, const glm::mat4 &M, float *dist) {
7         float t_min = 0.0f;
8         float t_max = 1.0e5f;
9         const glm::vec3 aabb_pos(M[3].x, M[3].y, M[3].z);
10         const glm::vec3 delta = aabb_pos - ray.orig;
11
12         { // X
13                 const glm::vec3 xaxis(M[0].x, M[0].y, M[0].z);
14                 const float e = glm::dot(xaxis, delta);
15                 const float f = glm::dot(ray.dir, xaxis);
16
17                 if (std::abs(f) > 0.001f) {
18                         float t1 = (e + aabb.min.x) / f;
19                         float t2 = (e + aabb.max.x) / f;
20
21                         if (t1 > t2) {
22                                 std::swap(t1, t2);
23                         }
24                         if (t1 > t_min) {
25                                 t_min = t1;
26                         }
27                         if (t2 < t_max) {
28                                 t_max = t2;
29                         }
30                         if (t_max < t_min) {
31                                 return false;
32                         }
33                 } else {
34                         if (aabb.min.x - e > 0.0f || aabb.max.x < 0.0f) {
35                                 return false;
36                         }
37                 }
38         }
39
40         { // Y
41                 const glm::vec3 yaxis(M[1].x, M[1].y, M[1].z);
42                 const float e = glm::dot(yaxis, delta);
43                 const float f = glm::dot(ray.dir, yaxis);
44
45                 if (std::abs(f) > 0.001f) {
46                         float t1 = (e + aabb.min.y) / f;
47                         float t2 = (e + aabb.max.y) / f;
48
49                         if (t1 > t2) {
50                                 std::swap(t1, t2);
51                         }
52                         if (t1 > t_min) {
53                                 t_min = t1;
54                         }
55                         if (t2 < t_max) {
56                                 t_max = t2;
57                         }
58                         if (t_max < t_min) {
59                                 return false;
60                         }
61                 } else {
62                         if (aabb.min.y - e > 0.0f || aabb.max.y < 0.0f) {
63                                 return false;
64                         }
65                 }
66         }
67
68         { // Z
69                 const glm::vec3 zaxis(M[2].x, M[2].y, M[2].z);
70                 const float e = glm::dot(zaxis, delta);
71                 const float f = glm::dot(ray.dir, zaxis);
72
73                 if (std::abs(f) > 0.001f) {
74                         float t1 = (e + aabb.min.z) / f;
75                         float t2 = (e + aabb.max.z) / f;
76
77                         if (t1 > t2) {
78                                 std::swap(t1, t2);
79                         }
80                         if (t1 > t_min) {
81                                 t_min = t1;
82                         }
83                         if (t2 < t_max) {
84                                 t_max = t2;
85                         }
86                         if (t_max < t_min) {
87                                 return false;
88                         }
89                 } else {
90                         if (aabb.min.z - e > 0.0f || aabb.max.z < 0.0f) {
91                                 return false;
92                         }
93                 }
94         }
95
96         if (dist) {
97                 *dist = t_min;
98         }
99         return true;
100 }
101
102 }