]> git.localhorst.tv Git - blank.git/blob - src/geometry/primitive.hpp
cdeae86e5c68b21303b7c27c775d9ff657df8924
[blank.git] / src / geometry / primitive.hpp
1 #ifndef BLANK_GEOMETRY_PRIMITIVE_HPP_
2 #define BLANK_GEOMETRY_PRIMITIVE_HPP_
3
4 #include <algorithm>
5 #include <glm/glm.hpp>
6
7
8 namespace blank {
9
10 struct AABB {
11         glm::vec3 min;
12         glm::vec3 max;
13
14         void Adjust() noexcept {
15                 if (max.x < min.x) std::swap(max.x, min.x);
16                 if (max.y < min.y) std::swap(max.y, min.y);
17                 if (max.z < min.z) std::swap(max.z, min.z);
18         }
19
20         glm::vec3 Center() const noexcept {
21                 return min + (max - min) * 0.5f;
22         }
23
24         /// return distance between origin and farthest vertex
25         float OriginRadius() const noexcept {
26                 glm::vec3 high(glm::max(abs(min), abs(max)));
27                 return length(high);
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 /// matrices may translate and rotate, but must not scale/shear/etc
44 /// (basically the first three columns must have unit length)
45 bool Intersection(
46         const AABB &a_box,
47         const glm::mat4 &a_m,
48         const AABB &b_box,
49         const glm::mat4 &b_m,
50         float &depth,
51         glm::vec3 &normal) noexcept;
52
53 bool CullTest(const AABB &box, const glm::mat4 &MVP) noexcept;
54
55 }
56
57 #endif