]>
Dogcows Code - chaz/yoink/blob - src/moof/aabb.hh
b2fc4b0d23835ce44d1d7a6bb48afbe06bc6f3db
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
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.
10 **************************************************************************/
12 #ifndef _MOOF_AABB_HH_
13 #define _MOOF_AABB_HH_
17 * Axis-aligned Bounding Box
20 #include <moof/cullable.hh>
21 #include <moof/drawable.hh>
22 #include <moof/math.hh>
23 #include <moof/plane.hh>
24 #include <moof/shape.hh>
26 #include <moof/frustum.hh> // FIXME: this file is quite broken
27 #include <moof/image.hh>
28 #include <moof/opengl.hh>
38 struct aabb
: public cullable
, public drawable
, public shape
<D
>
40 typedef moof::vector
< scalar
, fixed
<D
> > vector
;
48 aabb(const vector
& a
, const vector
& b
)
53 aabb(scalar x1
, scalar y1
, scalar x2
, scalar y2
)
61 aabb(scalar x1
, scalar y1
, scalar z1
, scalar x2
, scalar y2
, scalar z2
)
70 void init(const vector2
& a
, const vector2
& b
)
94 void init(const vector3
& a
, const vector3
& b
)
129 vector
center() const
131 return (min
+ max
) / 2.0;
135 plane
xy_plane() const
138 plane
.normal
= vector3(0.0, 0.0, 1.0);
139 plane
.d
= dot(-plane
.normal
, center());
143 plane
xz_plane() const
146 plane
.normal
= vector3(0.0, 1.0, 0.0);
147 plane
.d
= dot(-plane
.normal
, center());
151 plane
yz_plane() const
154 plane
.normal
= vector3(1.0, 0.0, 0.0);
155 plane
.d
= dot(-plane
.normal
, center());
160 void get_corners(vector2 corners
[4]) const
162 corners
[0][0] = min
[0]; corners
[0][1] = min
[1];
163 corners
[1][0] = max
[0]; corners
[1][1] = min
[1];
164 corners
[2][0] = max
[0]; corners
[2][1] = max
[1];
165 corners
[3][0] = min
[0]; corners
[3][1] = max
[1];
168 void get_corners(vector3 corners
[8]) const
170 corners
[0][0] = min
[0];
171 corners
[0][1] = min
[1];
172 corners
[0][2] = max
[2];
173 corners
[1][0] = max
[0];
174 corners
[1][1] = min
[1];
175 corners
[1][2] = max
[2];
176 corners
[2][0] = max
[0];
177 corners
[2][1] = max
[1];
178 corners
[2][2] = max
[2];
179 corners
[3][0] = min
[0];
180 corners
[3][1] = max
[1];
181 corners
[3][2] = max
[2];
182 corners
[4][0] = min
[0];
183 corners
[4][1] = min
[1];
184 corners
[4][2] = min
[2];
185 corners
[5][0] = max
[0];
186 corners
[5][1] = min
[1];
187 corners
[5][2] = min
[2];
188 corners
[6][0] = max
[0];
189 corners
[6][1] = max
[1];
190 corners
[6][2] = min
[2];
191 corners
[7][0] = min
[0];
192 corners
[7][1] = max
[1];
193 corners
[7][2] = min
[2];
197 void enclose_vertices(const vector vertices
[], unsigned count
)
202 for (unsigned i
= 1; i
< count
; ++i
)
204 min
.minimize(vertices
[i
]);
205 max
.maximize(vertices
[i
]);
210 void draw(scalar alpha
= 0.0) const
212 glRect(min
[0], min
[1], max
[0], max
[1]);
215 bool is_visible(const frustum
& frustum
) const
222 void import_aabb_class(script
& script
);
225 inline void aabb
<3>::draw(scalar alpha
) const
227 scalar vertices
[] = {min
[0], min
[1], min
[2],
228 min
[0], max
[1], min
[2],
229 max
[0], max
[1], min
[2],
230 max
[0], min
[1], min
[2],
231 min
[0], max
[1], max
[2],
232 min
[0], min
[1], max
[2],
233 max
[0], min
[1], max
[2],
234 max
[0], max
[1], max
[2]};
236 GLubyte indices
[] = {0, 1, 2, 3,
243 glEnableClientState(GL_VERTEX_ARRAY
);
244 glDisableClientState(GL_TEXTURE_COORD_ARRAY
);
245 glVertexPointer(3, GL_SCALAR
, 0, vertices
);
247 glPolygonMode(GL_FRONT_AND_BACK
, GL_LINE
);
248 image::reset_binding();
250 glDrawElements(GL_QUADS
, sizeof(indices
), GL_UNSIGNED_BYTE
,
253 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
254 //glDisableClientState(GL_VERTEX_ARRAY);
256 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
260 inline bool aabb
<3>::is_visible(const frustum
& frustum
) const
262 return frustum
.contains(*this);
266 typedef aabb
<2> aabb2
;
267 typedef aabb2 rectangle
;
268 typedef aabb
<3> aabb3
;
273 #endif // _MOOF_AABB_HH_
This page took 0.043873 seconds and 4 git commands to generate.