]> git.localhorst.tv Git - blank.git/blob - src/model/geometry.hpp
fd2c42bb8c8fa7f643f7fcae8697aeac26496dd2
[blank.git] / src / model / geometry.hpp
1 #ifndef BLANK_MODEL_GEOMETRY_H_
2 #define BLANK_MODEL_GEOMETRY_H_
3
4 #include <algorithm>
5 #include <glm/glm.hpp>
6
7
8 namespace blank {
9
10 constexpr float PI = 3.141592653589793238462643383279502884;
11 constexpr float PI_0p25 = PI * 0.25f;
12 constexpr float PI_0p5 = PI * 0.5f;
13 constexpr float PI_1p5 = PI * 1.5f;
14 constexpr float PI_2p0 = PI * 2.0f;
15
16 constexpr float PI_inv = 1.0f / PI;
17 constexpr float PI_0p5_inv = 1.0f / PI_0p5;
18
19 constexpr float DEG_RAD_FACTOR = PI / 180.0f;
20 constexpr float RAD_DEG_FACTOR = 180.0f / PI;
21
22 constexpr float deg2rad(float d) {
23         return d * DEG_RAD_FACTOR;
24 }
25
26 constexpr float rad2deg(float r) {
27         return r * RAD_DEG_FACTOR;
28 }
29
30
31 template<class T>
32 T manhattan_distance(const glm::tvec3<T> &a, const glm::tvec3<T> &b) {
33         glm::tvec3<T> diff(abs(a - b));
34         return diff.x + diff.y + diff.z;
35 }
36
37 template<class T>
38 T manhattan_radius(const glm::tvec3<T> &v) {
39         glm::tvec3<T> a(abs(v));
40         return std::max(a.x, std::max(a.y, a.z));
41 }
42
43
44 struct AABB {
45         glm::vec3 min;
46         glm::vec3 max;
47
48         void Adjust() noexcept {
49                 if (max.x < min.x) std::swap(max.x, min.x);
50                 if (max.y < min.y) std::swap(max.y, min.y);
51                 if (max.z < min.z) std::swap(max.z, min.z);
52         }
53
54         glm::vec3 Center() const noexcept {
55                 return min + (max - min) * 0.5f;
56         }
57 };
58
59 struct Ray {
60         glm::vec3 orig;
61         glm::vec3 dir;
62 };
63
64 bool Intersection(
65         const Ray &,
66         const AABB &,
67         const glm::mat4 &M,
68         float *dist = nullptr,
69         glm::vec3 *normal = nullptr) noexcept;
70
71 bool Intersection(
72         const AABB &a_box,
73         const glm::mat4 &a_m,
74         const AABB &b_box,
75         const glm::mat4 &b_m,
76         float &depth,
77         glm::vec3 &normal) noexcept;
78
79 bool CullTest(const AABB &box, const glm::mat4 &MVP) noexcept;
80
81 }
82
83 #endif