]> Dogcows Code - chaz/yoink/blob - src/moof/frustum.cc
begin cleaning up resource management
[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 "aabb.hh"
13 #include "frustum.hh"
14 #include "sphere.hh"
15
16
17 namespace moof {
18
19
20 void frustum::init(const matrix4& modelview, const matrix4& projection)
21 {
22 scalar planes[6][4];
23
24 extract_frustum_planes(modelview, projection, planes, z_clip_neg_one);
25
26 planes_[0] = plane(planes[0][0], planes[0][1],
27 planes[0][2], planes[0][3]);
28 planes_[1] = plane(planes[1][0], planes[1][1],
29 planes[1][2], planes[1][3]);
30 planes_[2] = plane(planes[2][0], planes[2][1],
31 planes[2][2], planes[2][3]);
32 planes_[3] = plane(planes[3][0], planes[3][1],
33 planes[3][2], planes[3][3]);
34 planes_[4] = plane(planes[4][0], planes[4][1],
35 planes[4][2], planes[4][3]);
36 planes_[5] = plane(planes[5][0], planes[5][1],
37 planes[5][2], planes[5][3]);
38 }
39
40 void frustum::init(const matrix4& modelview, scalar fovy, scalar aspect,
41 scalar abutting, scalar distant)
42 {
43 matrix4 projection;
44
45 matrix_perspective_yfov_RH(projection, fovy, aspect, abutting,
46 distant, z_clip_neg_one);
47
48 init(modelview, projection);
49 }
50
51 frustum::collision frustum::contains(const aabb<3>& aabb) const
52 {
53 vector3 corners[8];
54 int nTotalInside = 0;
55
56 aabb.get_corners(corners);
57
58 for (int i = 0; i < 6; ++i)
59 {
60 int nInside = 8;
61
62 for (int j = 0; j < 8; ++j)
63 {
64 if (planes_[i].intersects(corners[j]) == plane::negative)
65 {
66 --nInside;
67 }
68 }
69
70 if (nInside == 0) return outside;
71 else if (nInside == 8) ++nTotalInside;
72 }
73
74 if (nTotalInside == 6) return inside;
75 else return intersecting;
76 }
77
78
79 frustum::collision frustum::contains(const sphere<3>& sphere) const
80 {
81 for (int i = 0; i < 6; ++i)
82 {
83 plane::halfspace halfspace = planes_[i].intersects(sphere);
84
85 if (halfspace == plane::negative) return outside;
86 else if (halfspace == plane::intersecting) return intersecting;
87 }
88
89 return inside;
90 }
91
92
93 } // namespace moof
94
This page took 0.036624 seconds and 4 git commands to generate.