pleascach/util/geo.hpp
2024-02-24 09:18:31 -05:00

34 lines
791 B
C++

#pragma once
#include <glm/glm.hpp>
#include <glm/gtc/matrix_access.hpp>
#include <array>
/* extracts frustum from projection matrix */
static std::array<glm::vec4, 6> frustum(const glm::mat4& mat) {
/* Left, Right, Top, Bottom, Back, Front */
std::array<glm::vec4, 6> ret;
for (size_t i = 0; i < 3; i++)
for (size_t j = 0; j < 4; j++)
ret[i * 2][j] = mat[j].w + mat[j][i],
ret[i * 2 + 1][j] = mat[j].w - mat[j][i];
return ret;
}
static bool box_in_frustum(const std::array<glm::vec4, 6>& frustum, const glm::vec3 box_verts[8]) {
for (const auto& plane : frustum) {
for (size_t i = 0; i < 8; i++) {
const auto& vert = box_verts[i];
if (glm::dot(glm::vec4(vert, 1.0), plane) > 0)
goto double_continue;
}
return false;
double_continue:;
}
return true;
}