/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * Copyright 2018 Danny Robson */ #ifndef CRUFT_UTIL_GEOM_FRUSTUM_HPP #define CRUFT_UTIL_GEOM_FRUSTUM_HPP #include "../matrix.hpp" #include "plane.hpp" #include "aabb.hpp" #include #include namespace util::geom { /// a viewing frustrum comprised of 4 axis planes, a near plane, and a far /// plane. it may describe something other than a perspective projection /// (eg, an orthographic projection) template struct frustum { explicit frustum (const matrix<4,4,ValueT>&); std::array, 6> planes; enum { X_NEG, X_POS, Y_NEG, Y_POS, Z_NEG, Z_POS, LEFT = X_NEG, RIGHT = X_POS, TOP = Y_POS, BOTTOM = Y_NEG, NEAR = Z_POS, FAR = Z_NEG }; }; using frustum3f = frustum; /// tests whether a frustum and an aabb overlap /// /// XXX: there may be occasional false positives. template bool intersects (const frustum &f, const aabb &b) { for (const auto &p: f.planes) { // find the distance to the point, if it's positive it's inside // the frustum. const auto d = max ( p.coefficients * b.lo.template redim (1), p.coefficients * b.hi.template redim (1) ); if (sum (d) < 0) return false; } return true; } }; #endif