X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=src%2Fmoof%2Ffrustum.cc;fp=src%2Fmoof%2Ffrustum.cc;h=aaeceb094fb4ecacc2e2c2fbf1d49835eb345c74;hb=831f04d4bc19a390415ac0bbac4331c7a65509bc;hp=0000000000000000000000000000000000000000;hpb=299af4f2047e767e5d79501c26444473bda64c64;p=chaz%2Fyoink diff --git a/src/moof/frustum.cc b/src/moof/frustum.cc new file mode 100644 index 0000000..aaeceb0 --- /dev/null +++ b/src/moof/frustum.cc @@ -0,0 +1,94 @@ + +/*] Copyright (c) 2009-2010, Charles McGarvey [************************** +**] All rights reserved. +* +* vi:ts=4 sw=4 tw=75 +* +* 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 +{ + vector3 corners[8]; + int nTotalInside = 0; + + aabb.get_corners(corners); + + for (int i = 0; i < 6; ++i) + { + int nInside = 8; + + for (int j = 0; j < 8; ++j) + { + if (planes_[i].intersects(corners[j]) == plane::negative) + { + --nInside; + } + } + + if (nInside == 0) return outside; + else if (nInside == 8) ++nTotalInside; + } + + if (nTotalInside == 6) return inside; + else return intersecting; +} + + +frustum::collision frustum::contains(const sphere<3>& 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 +