1 #ifndef BLANK_GEOMETRY_PRIMITIVE_HPP_
2 #define BLANK_GEOMETRY_PRIMITIVE_HPP_
15 void Adjust() noexcept {
16 if (max.x < min.x) std::swap(max.x, min.x);
17 if (max.y < min.y) std::swap(max.y, min.y);
18 if (max.z < min.z) std::swap(max.z, min.z);
21 glm::vec3 Center() const noexcept {
22 return min + (max - min) * 0.5f;
25 /// return distance between origin and farthest vertex
26 float OriginRadius() const noexcept {
27 glm::vec3 high(glm::max(abs(min), abs(max)));
32 std::ostream &operator <<(std::ostream &, const AABB &);
34 // TODO: this should really use setters/getters for dir and inv_dir so
35 // manipulating code doesn't "forget" to call Update()
42 void Update() noexcept {
47 std::ostream &operator <<(std::ostream &, const Ray &);
49 /// axis aligned boolean ray/box intersection test
50 /// if true, dist constains distance from ray's origin to intersection point
54 float &dist) noexcept;
56 /// detailed oriented ray/box intersection test
61 float *dist = nullptr,
62 glm::vec3 *normal = nullptr) noexcept;
64 /// matrices may translate and rotate, but must not scale/shear/etc
65 /// (basically the first three columns must have unit length)
72 glm::vec3 &normal) noexcept;
79 float &A() noexcept { return normal.x; }
80 float &B() noexcept { return normal.y; }
81 float &C() noexcept { return normal.z; }
82 float &D() noexcept { return dist; }
83 float A() const noexcept { return normal.x; }
84 float B() const noexcept { return normal.y; }
85 float C() const noexcept { return normal.z; }
86 float D() const noexcept { return dist; }
88 Plane(const glm::vec3 &n, float d)
89 : normal(n), dist(d) { }
90 Plane(const glm::vec4 &abcd)
91 : normal(abcd), dist(abcd.w) { }
93 void Normalize() noexcept {
94 const float l = length(normal);
100 std::ostream &operator <<(std::ostream &, const Plane &);
104 Plane &Left() noexcept { return plane[0]; }
105 Plane &Right() noexcept { return plane[1]; }
106 Plane &Bottom() noexcept { return plane[2]; }
107 Plane &Top() noexcept { return plane[3]; }
108 Plane &Near() noexcept { return plane[4]; }
109 Plane &Far() noexcept { return plane[5]; }
110 const Plane &Left() const noexcept { return plane[0]; }
111 const Plane &Right() const noexcept { return plane[1]; }
112 const Plane &Bottom() const noexcept { return plane[2]; }
113 const Plane &Top() const noexcept { return plane[3]; }
114 const Plane &Near() const noexcept { return plane[4]; }
115 const Plane &Far() const noexcept { return plane[5]; }
117 /// create frustum from transposed MVP
118 Frustum(const glm::mat4 &mat)
128 void Normalize() noexcept {
129 for (Plane &p : plane) {
135 std::ostream &operator <<(std::ostream &, const Plane &);
136 std::ostream &operator <<(std::ostream &, const Frustum &);
138 bool CullTest(const AABB &box, const glm::mat4 &) noexcept;
139 bool CullTest(const AABB &box, const Frustum &) noexcept;