lmgl 1.0.0
A lightweight OpenGL graphics engine library written in C++
Loading...
Searching...
No Matches
frustum.hpp
Go to the documentation of this file.
1
12#pragma once
13
14#include <array>
15#include <glm/glm.hpp>
16
17namespace lmgl {
18
19namespace scene {
20
27struct AABB {
29 glm::vec3 min;
30
32 glm::vec3 max;
33
39 AABB() : min(glm::vec3(0.0f)), max(glm::vec3(0.0f)) {}
40
49 AABB(const glm::vec3 &min, const glm::vec3 &max) : min(min), max(max) {}
50
58 inline glm::vec3 get_center() const { return (min + max) * 0.5f; }
59
67 inline glm::vec3 get_extents() const { return (max - min) * 0.5f; }
68
77 AABB transform(const glm::mat4 &matrix) const;
78
86 void expand(const glm::vec3 &point);
87
95 void merge(const AABB &other);
96};
97
107 glm::vec3 center;
108
110 float radius;
111
117 BoundingSphere() : center(glm::vec3(0.0f)), radius(0.0f) {}
118
127 BoundingSphere(const glm::vec3 &center, float radius) : center(center), radius(radius) {}
128
137 static BoundingSphere from_aabb(const AABB &aabb);
138
147 BoundingSphere transform(const glm::mat4 &matrix) const;
148};
149
157struct Plane {
159 glm::vec3 normal;
160
162 float distance;
163
169 Plane() : normal(glm::vec3(0.0f, 1.0f, 0.0f)), distance(0.0f) {}
170
179 Plane(const glm::vec3 &normal, float distance) : normal(normal), distance(distance) {}
180
190 Plane(const glm::vec3 &point, const glm::vec3 &normal)
191 : normal(glm::normalize(normal)), distance(glm::dot(this->normal, point)) {}
192
201 inline float distance_to_point(const glm::vec3 &point) const { return glm::dot(normal, point) - distance; }
202
208 void normalize();
209};
210
218class Frustum {
219 public:
231 enum PlaneIndex { Left = 0, Right, Bottom, Top, Near, Far };
232
240 void update(const glm::mat4 &view_projection);
241
250 bool contains_point(const glm::vec3 &point) const;
251
260 bool contains_sphere(const BoundingSphere &sphere) const;
261
270 bool contains_aabb(const AABB &aabb) const;
271
280 inline const Plane &get_plane(PlaneIndex index) const { return m_planes[index]; }
281
282 private:
284 std::array<Plane, 6> m_planes;
285};
286
287} // namespace scene
288
289} // namespace lmgl
Frustum structure.
Definition frustum.hpp:218
bool contains_sphere(const BoundingSphere &sphere) const
Check if a bounding sphere is inside the frustum.
Definition frustum.cpp:102
const Plane & get_plane(PlaneIndex index) const
Get a specific frustum plane.
Definition frustum.hpp:280
bool contains_aabb(const AABB &aabb) const
Check if an Axis-Aligned Bounding Box (AABB) is inside the frustum.
Definition frustum.cpp:112
void update(const glm::mat4 &view_projection)
Update the frustum planes from a view-projection matrix.
Definition frustum.cpp:63
bool contains_point(const glm::vec3 &point) const
Check if a point is inside the frustum.
Definition frustum.cpp:93
PlaneIndex
Enumeration for the frustum planes.
Definition frustum.hpp:231
Namespace for scene-related classes and functions.
Definition camera.cpp:7
Forward declarations for Assimp Material structure.
Definition model_loader.cpp:12
Axis-Aligned Bounding Box (AABB) structure.
Definition frustum.hpp:27
AABB transform(const glm::mat4 &matrix) const
Transform the AABB with a matrix.
Definition frustum.cpp:11
glm::vec3 max
Maximum corner point of the AABB.
Definition frustum.hpp:32
glm::vec3 min
Minimum corner point of the AABB.
Definition frustum.hpp:29
void expand(const glm::vec3 &point)
Expand the AABB to include a point.
Definition frustum.cpp:25
glm::vec3 get_extents() const
Get the extents of the AABB.
Definition frustum.hpp:67
AABB()
Default constructor initializing an empty AABB.
Definition frustum.hpp:39
void merge(const AABB &other)
Merge the AABB with another AABB.
Definition frustum.cpp:30
glm::vec3 get_center() const
Get the center point of the AABB.
Definition frustum.hpp:58
AABB(const glm::vec3 &min, const glm::vec3 &max)
Parameterized constructor.
Definition frustum.hpp:49
Bounding Sphere structure.
Definition frustum.hpp:105
BoundingSphere(const glm::vec3 &center, float radius)
Parameterized constructor.
Definition frustum.hpp:127
static BoundingSphere from_aabb(const AABB &aabb)
Create a bounding sphere from an AABB.
Definition frustum.cpp:37
BoundingSphere()
Default constructor initializing an empty bounding sphere.
Definition frustum.hpp:117
glm::vec3 center
Center point of the bounding sphere.
Definition frustum.hpp:107
BoundingSphere transform(const glm::mat4 &matrix) const
Transform the bounding sphere with a matrix.
Definition frustum.cpp:43
float radius
Radius of the bounding sphere.
Definition frustum.hpp:110
Plane structure.
Definition frustum.hpp:157
Plane(const glm::vec3 &point, const glm::vec3 &normal)
Construct a plane from a point and a normal vector.
Definition frustum.hpp:190
Plane(const glm::vec3 &normal, float distance)
Parameterized constructor.
Definition frustum.hpp:179
void normalize()
Normalize the plane equation.
Definition frustum.cpp:55
Plane()
Default constructor initializing a horizontal plane at the origin.
Definition frustum.hpp:169
float distance_to_point(const glm::vec3 &point) const
Calculate the distance from a point to the plane.
Definition frustum.hpp:201
glm::vec3 normal
Normal vector of the plane.
Definition frustum.hpp:159
float distance
Distance from the origin to the plane along the normal.
Definition frustum.hpp:162