4#include <glm/gtc/matrix_access.hpp>
6#include "core/math/bounds.h"
8namespace Vkm::Engine::Math {
18 glm::vec3 absNormals[6];
28inline Frustum extractFrustum(
const glm::mat4& viewProjection) {
30 const glm::vec4 row0 = glm::row(viewProjection, 0);
31 const glm::vec4 row1 = glm::row(viewProjection, 1);
32 const glm::vec4 row2 = glm::row(viewProjection, 2);
33 const glm::vec4 row3 = glm::row(viewProjection, 3);
34 glm::vec4 planes[6] = {row3 + row0, row3 - row0, row3 + row1, row3 - row1, row3 + row2, row3 - row2};
35 for (
int i = 0; i < 6; ++i) {
36 const glm::vec3 n = glm::vec3(planes[i]);
37 const float length = glm::length(n);
39 const float invLen = 1.0f / length;
40 frustum.
normals[i] = n * invLen;
41 frustum.
d[i] = planes[i].w * invLen;
43 frustum.
normals[i] = glm::vec3(0.0f);
46 frustum.absNormals[i] = glm::abs(frustum.
normals[i]);
58inline bool frustumIntersectsAABB(
const Frustum& f,
const AABB& bounds) {
59 const glm::vec3 center = bounds.center();
60 const glm::vec3 halfExtent = bounds.halfExtent();
61 for (
int i = 0; i < 6; ++i) {
62 const float dist = glm::dot(f.normals[i], center) + f.d[i];
63 const float radius = glm::dot(f.absNormals[i], halfExtent);
64 if (dist + radius < 0.0f)
return false;
An axis-aligned box.
Definition bounds.h:14
View frustum for culling: six planes from a view-projection matrix.
Definition frustum.h:16
glm::vec3 normals[6]
Normalized.
Definition frustum.h:17
float d[6]
The constant in ax+by+cz+d=0.
Definition frustum.h:19