]> git.localhorst.tv Git - blank.git/blob - src/model/geometry.hpp
figure out depth and normal in box/box test
[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 struct AABB {
17         glm::vec3 min;
18         glm::vec3 max;
19
20         void Adjust() noexcept {
21                 if (max.x < min.x) std::swap(max.x, min.x);
22                 if (max.y < min.y) std::swap(max.y, min.y);
23                 if (max.z < min.z) std::swap(max.z, min.z);
24         }
25
26         glm::vec3 Center() const noexcept {
27                 return min + (max - min) * 0.5f;
28         }
29 };
30
31 struct Ray {
32         glm::vec3 orig;
33         glm::vec3 dir;
34 };
35
36 bool Intersection(
37         const Ray &,
38         const AABB &,
39         const glm::mat4 &M,
40         float *dist = nullptr,
41         glm::vec3 *normal = nullptr) noexcept;
42
43 bool Intersection(
44         const AABB &a_box,
45         const glm::mat4 &a_m,
46         const AABB &b_box,
47         const glm::mat4 &b_m,
48         float &depth,
49         glm::vec3 &normal) noexcept;
50
51 bool CullTest(const AABB &box, const glm::mat4 &MVP) noexcept;
52
53 }
54
55 #endif