]> Dogcows Code - chaz/yoink/blobdiff - src/moof/frustum.cc
the massive refactoring effort
[chaz/yoink] / src / moof / frustum.cc
diff --git a/src/moof/frustum.cc b/src/moof/frustum.cc
new file mode 100644 (file)
index 0000000..aaeceb0
--- /dev/null
@@ -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
+
This page took 0.026762 seconds and 4 git commands to generate.