5namespace Vkm::Engine::Math {
9inline constexpr float BOUNDS_EPSILON_SQ = 1e-8f;
24 const glm::vec3 extent = max - min;
25 return glm::dot(extent, extent) > BOUNDS_EPSILON_SQ;
28 glm::vec3 center() const noexcept {
return (min + max) * 0.5f; }
30 glm::vec3 halfExtent() const noexcept {
return (max - min) * 0.5f; }
40inline AABB transform(
const glm::mat4& matrix,
const AABB& local) {
42 world.min = glm::vec3(matrix[3]);
43 world.max = glm::vec3(matrix[3]);
45 for (
int j = 0; j < 3; ++j) {
46 const glm::vec3 col(matrix[j]);
47 const glm::vec3 a = col * local.min[j];
48 const glm::vec3 b = col * local.max[j];
49 world.min += glm::min(a, b);
50 world.max += glm::max(a, b);
59 glm::vec3 origin{0.0f};
73inline Ray rayThroughNdc(
const glm::mat4& invViewProj,
const glm::vec2& ndc) {
74 glm::vec4 nearPoint = invViewProj * glm::vec4(ndc, -1.0f, 1.0f);
75 glm::vec4 farPoint = invViewProj * glm::vec4(ndc, 1.0f, 1.0f);
76 nearPoint /= nearPoint.w;
77 farPoint /= farPoint.w;
78 return Ray{glm::vec3(nearPoint), glm::normalize(glm::vec3(farPoint - nearPoint))};
91inline bool rayIntersectsAABB(
92 const glm::vec3& origin,
93 const glm::vec3& invDir,
97 const glm::vec3 t0 = (box.min - origin) * invDir;
98 const glm::vec3 t1 = (box.max - origin) * invDir;
100 const glm::vec3 tMinV = glm::min(t0, t1);
101 const glm::vec3 tMaxV = glm::max(t0, t1);
103 float tMin = glm::max(glm::max(tMinV.x, tMinV.y), tMinV.z);
104 float tMax = glm::min(glm::min(tMaxV.x, tMaxV.y), tMaxV.z);
108 tHit = tMin > 0.0f ? tMin : tMax;
109 return tMax >= tMin && tMax >= 0.0f;
An axis-aligned box.
Definition bounds.h:14
bool valid() const noexcept
True when the box has non-degenerate extent.
Definition bounds.h:23
A half-line in world space.
Definition bounds.h:58
glm::vec3 direction
Unit length.
Definition bounds.h:60