]> git.localhorst.tv Git - blank.git/blob - src/model/geometry.hpp
show player orientation in debug overlay
[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 struct AABB {
29         glm::vec3 min;
30         glm::vec3 max;
31
32         void Adjust() noexcept {
33                 if (max.x < min.x) std::swap(max.x, min.x);
34                 if (max.y < min.y) std::swap(max.y, min.y);
35                 if (max.z < min.z) std::swap(max.z, min.z);
36         }
37
38         glm::vec3 Center() const noexcept {
39                 return min + (max - min) * 0.5f;
40         }
41 };
42
43 struct Ray {
44         glm::vec3 orig;
45         glm::vec3 dir;
46 };
47
48 bool Intersection(
49         const Ray &,
50         const AABB &,
51         const glm::mat4 &M,
52         float *dist = nullptr,
53         glm::vec3 *normal = nullptr) noexcept;
54
55 bool Intersection(
56         const AABB &a_box,
57         const glm::mat4 &a_m,
58         const AABB &b_box,
59         const glm::mat4 &b_m,
60         float &depth,
61         glm::vec3 &normal) noexcept;
62
63 bool CullTest(const AABB &box, const glm::mat4 &MVP) noexcept;
64
65 }
66
67 #endif