]> git.localhorst.tv Git - blank.git/blob - src/model/geometry.hpp
load models from assets
[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 DEG_RAD_FACTOR = PI / 180.0f;
17 constexpr float RAD_DEG_FACTOR = 180.0f / PI;
18
19 constexpr float deg2rad(float d) {
20         return d * DEG_RAD_FACTOR;
21 }
22
23 constexpr float rad2deg(float r) {
24         return r * RAD_DEG_FACTOR;
25 }
26
27
28 template<class T>
29 T manhattan_distance(const glm::tvec3<T> &a, const glm::tvec3<T> &b) {
30         glm::tvec3<T> diff(abs(a - b));
31         return diff.x + diff.y + diff.z;
32 }
33
34 template<class T>
35 T manhattan_radius(const glm::tvec3<T> &v) {
36         glm::tvec3<T> a(abs(v));
37         return std::max(a.x, std::max(a.y, a.z));
38 }
39
40
41 struct AABB {
42         glm::vec3 min;
43         glm::vec3 max;
44
45         void Adjust() noexcept {
46                 if (max.x < min.x) std::swap(max.x, min.x);
47                 if (max.y < min.y) std::swap(max.y, min.y);
48                 if (max.z < min.z) std::swap(max.z, min.z);
49         }
50
51         glm::vec3 Center() const noexcept {
52                 return min + (max - min) * 0.5f;
53         }
54 };
55
56 struct Ray {
57         glm::vec3 orig;
58         glm::vec3 dir;
59 };
60
61 bool Intersection(
62         const Ray &,
63         const AABB &,
64         const glm::mat4 &M,
65         float *dist = nullptr,
66         glm::vec3 *normal = nullptr) noexcept;
67
68 bool Intersection(
69         const AABB &a_box,
70         const glm::mat4 &a_m,
71         const AABB &b_box,
72         const glm::mat4 &b_m,
73         float &depth,
74         glm::vec3 &normal) noexcept;
75
76 bool CullTest(const AABB &box, const glm::mat4 &MVP) noexcept;
77
78 }
79
80 #endif