1 #ifndef BLANK_GEOMETRY_PRIMITIVE_HPP_
2 #define BLANK_GEOMETRY_PRIMITIVE_HPP_
4 #include "../graphics/glm.hpp"
8 #include <glm/gtx/norm.hpp>
17 void Adjust() noexcept {
18 if (max.x < min.x) std::swap(max.x, min.x);
19 if (max.y < min.y) std::swap(max.y, min.y);
20 if (max.z < min.z) std::swap(max.z, min.z);
23 glm::vec3 Center() const noexcept {
24 return min + (max - min) * 0.5f;
27 /// return distance between origin and farthest vertex
28 float OriginRadius() const noexcept {
29 glm::vec3 high(glm::max(glm::abs(min), glm::abs(max)));
30 return glm::length(high);
34 std::ostream &operator <<(std::ostream &, const AABB &);
36 // TODO: this should really use setters/getters for dir and inv_dir so
37 // manipulating code doesn't "forget" to call Update()
44 void Update() noexcept {
48 /// get shortest distance of this ray's line to given point
49 float Distance(const glm::vec3 &point) const noexcept {
50 // d = |(x2-x1)×(x1-x0)|/|x2-x1|
51 // where x0 is point, and x1 and x2 are points on the line
52 // for derivation, see http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
54 // x2-x1 = dir, which means |x2-x1| is 1.0
55 return glm::length(glm::cross(dir, orig - point));
57 float DistanceSquared(const glm::vec3 &point) const noexcept {
58 return glm::length2(glm::cross(dir, orig - point));
62 std::ostream &operator <<(std::ostream &, const Ray &);
64 /// axis aligned boolean ray/box intersection test
65 /// if true, dist constains distance from ray's origin to intersection point
69 float &dist) noexcept;
71 /// detailed oriented ray/box intersection test
76 float *dist = nullptr,
77 glm::vec3 *normal = nullptr) noexcept;
79 /// matrices may translate and rotate, but must not scale/shear/etc
80 /// (basically the first three columns must have unit length)
87 glm::vec3 &normal) noexcept;
94 float &A() noexcept { return normal.x; }
95 float &B() noexcept { return normal.y; }
96 float &C() noexcept { return normal.z; }
97 float &D() noexcept { return dist; }
98 float A() const noexcept { return normal.x; }
99 float B() const noexcept { return normal.y; }
100 float C() const noexcept { return normal.z; }
101 float D() const noexcept { return dist; }
103 Plane(const glm::vec3 &n, float d)
104 : normal(n), dist(d) { }
105 Plane(const glm::vec4 &abcd)
106 : normal(abcd), dist(abcd.w) { }
108 void Normalize() noexcept {
109 const float l = glm::length(normal);
115 std::ostream &operator <<(std::ostream &, const Plane &);
119 Plane &Left() noexcept { return plane[0]; }
120 Plane &Right() noexcept { return plane[1]; }
121 Plane &Bottom() noexcept { return plane[2]; }
122 Plane &Top() noexcept { return plane[3]; }
123 Plane &Near() noexcept { return plane[4]; }
124 Plane &Far() noexcept { return plane[5]; }
125 const Plane &Left() const noexcept { return plane[0]; }
126 const Plane &Right() const noexcept { return plane[1]; }
127 const Plane &Bottom() const noexcept { return plane[2]; }
128 const Plane &Top() const noexcept { return plane[3]; }
129 const Plane &Near() const noexcept { return plane[4]; }
130 const Plane &Far() const noexcept { return plane[5]; }
132 /// create frustum from transposed MVP
133 Frustum(const glm::mat4 &mat)
143 void Normalize() noexcept {
144 for (Plane &p : plane) {
150 std::ostream &operator <<(std::ostream &, const Plane &);
151 std::ostream &operator <<(std::ostream &, const Frustum &);
153 bool CullTest(const AABB &box, const glm::mat4 &) noexcept;
154 bool CullTest(const AABB &box, const Frustum &) noexcept;