/*] Copyright (c) 2009-2011, Charles McGarvey [***************************** **] All rights reserved. * * Distributable under the terms and conditions of the 2-clause BSD license; * see the file COPYING for a complete text of the license. * *****************************************************************************/ #include "aabb.hh" #include "frustum.hh" #include "sphere.hh" namespace moof { void frustum::init(const matrix4& modelview, const matrix4& projection) { scalar planes[6][4]; extract_frustum_planes(modelview, projection, planes, z_clip_neg_one); planes_[0] = plane(planes[0][0], planes[0][1], planes[0][2], planes[0][3]); planes_[1] = plane(planes[1][0], planes[1][1], planes[1][2], planes[1][3]); planes_[2] = plane(planes[2][0], planes[2][1], planes[2][2], planes[2][3]); planes_[3] = plane(planes[3][0], planes[3][1], planes[3][2], planes[3][3]); planes_[4] = plane(planes[4][0], planes[4][1], planes[4][2], planes[4][3]); planes_[5] = plane(planes[5][0], planes[5][1], planes[5][2], planes[5][3]); } void frustum::init(const matrix4& modelview, scalar fovy, scalar aspect, scalar abutting, scalar distant) { matrix4 projection; matrix_perspective_yfov_RH(projection, fovy, aspect, abutting, distant, z_clip_neg_one); init(modelview, projection); } frustum::collision frustum::contains(const aabb<3>& aabb) const { int total = 0; vector3 corners[8]; aabb.get_corners(corners); for (int i = 0; i < 6; ++i) { int num = 8; for (int j = 0; j < 8; ++j) { if (planes_[i].intersects(corners[j]) == plane::negative) --num; } if (num == 0) return outside; else if (num == 8) ++total; } if (total == 6) return inside; return intersecting; } frustum::collision frustum::contains(const sphere3& sphere) const { for (int i = 0; i < 6; ++i) { plane::halfspace halfspace = planes_[i].intersects(sphere); if (halfspace == plane::negative) return outside; else if (halfspace == plane::intersecting) return intersecting; } return inside; } } // namespace moof