]> Dogcows Code - chaz/yoink/blob - src/Moof/Frustum.cc
reformatting
[chaz/yoink] / src / Moof / Frustum.cc
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
9 *
10 **************************************************************************/
11
12 #include <Moof/Aabb.hh>
13 #include <Moof/Frustum.hh>
14 #include <Moof/Sphere.hh>
15
16
17 namespace Mf {
18
19
20 void Frustum::init(const Matrix4& modelview, const Matrix4& projection)
21 {
22 Scalar planes[6][4];
23
24 cml::extract_frustum_planes(modelview, projection, planes,
25 cml::z_clip_neg_one);
26
27 mPlanes[0] = Plane(planes[0][0], planes[0][1],
28 planes[0][2], planes[0][3]);
29 mPlanes[1] = Plane(planes[1][0], planes[1][1],
30 planes[1][2], planes[1][3]);
31 mPlanes[2] = Plane(planes[2][0], planes[2][1],
32 planes[2][2], planes[2][3]);
33 mPlanes[3] = Plane(planes[3][0], planes[3][1],
34 planes[3][2], planes[3][3]);
35 mPlanes[4] = Plane(planes[4][0], planes[4][1],
36 planes[4][2], planes[4][3]);
37 mPlanes[5] = Plane(planes[5][0], planes[5][1],
38 planes[5][2], planes[5][3]);
39 }
40
41 void Frustum::init(const Matrix4& modelview, Scalar fovy, Scalar aspect,
42 Scalar abutting, Scalar distant)
43 {
44 Matrix4 projection;
45
46 cml::matrix_perspective_yfov_RH(projection, fovy, aspect, abutting,
47 distant, cml::z_clip_neg_one);
48
49 init(modelview, projection);
50 }
51
52 Frustum::Collision Frustum::contains(const Aabb<3>& aabb) const
53 {
54 Vector3 corners[8];
55 int nTotalInside = 0;
56
57 aabb.getCorners(corners);
58
59 for (int i = 0; i < 6; ++i)
60 {
61 int nInside = 8;
62
63 for (int j = 0; j < 8; ++j)
64 {
65 if (mPlanes[i].intersects(corners[j]) == Plane::NEGATIVE)
66 {
67 --nInside;
68 }
69 }
70
71 if (nInside == 0) return OUTSIDE;
72 else if (nInside == 8) ++nTotalInside;
73 }
74
75 if (nTotalInside == 6) return INSIDE;
76 else return INTERSECT;
77 }
78
79
80 Frustum::Collision Frustum::contains(const Sphere<3>& sphere) const
81 {
82 for (int i = 0; i < 6; ++i)
83 {
84 Plane::Halfspace halfspace = mPlanes[i].intersects(sphere);
85
86 if (halfspace == Plane::NEGATIVE) return OUTSIDE;
87 else if (halfspace == Plane::INTERSECT) return INTERSECT;
88 }
89
90 return INSIDE;
91 }
92
93
94 } // namespace Mf
95
This page took 0.041786 seconds and 5 git commands to generate.