]>
Dogcows Code - chaz/yoink/blob - src/moof/aabb.hh
3228b5a225a412927a0601b68c5a77bc608a5383
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
5 * Distributable under the terms and conditions of the 2-clause BSD license;
6 * see the file COPYING for a complete text of the license.
8 *****************************************************************************/
10 #ifndef _MOOF_AABB_HH_
11 #define _MOOF_AABB_HH_
15 * Axis-aligned Bounding Box
18 #include <moof/cullable.hh>
19 #include <moof/drawable.hh>
20 #include <moof/math.hh>
21 #include <moof/plane.hh>
22 #include <moof/shape.hh>
24 #include <moof/frustum.hh> // FIXME: this file is quite broken
25 #include <moof/image.hh>
26 #include <moof/opengl.hh>
32 // forward declarations
36 struct aabb
: public cullable
, public drawable
, public shape
<D
>
38 typedef moof::vector
< scalar
, fixed
<D
> > vector
;
45 aabb(const vector
& a
, const vector
& b
)
50 aabb(scalar x1
, scalar y1
, scalar x2
, scalar y2
)
58 aabb(scalar x1
, scalar y1
, scalar z1
, scalar x2
, scalar y2
, scalar z2
)
66 void init(const vector2
& a
, const vector2
& b
)
90 void init(const vector3
& a
, const vector3
& b
)
124 vector
center() const
126 return (min
+ max
) / 2.0;
129 plane
xy_plane() const
132 plane
.normal
= vector3(0.0, 0.0, 1.0);
133 plane
.d
= dot(-plane
.normal
, center());
137 plane
xz_plane() const
140 plane
.normal
= vector3(0.0, 1.0, 0.0);
141 plane
.d
= dot(-plane
.normal
, center());
145 plane
yz_plane() const
148 plane
.normal
= vector3(1.0, 0.0, 0.0);
149 plane
.d
= dot(-plane
.normal
, center());
153 void get_corners(vector2 corners
[4]) const
155 corners
[0][0] = min
[0]; corners
[0][1] = min
[1];
156 corners
[1][0] = max
[0]; corners
[1][1] = min
[1];
157 corners
[2][0] = max
[0]; corners
[2][1] = max
[1];
158 corners
[3][0] = min
[0]; corners
[3][1] = max
[1];
161 void get_corners(vector3 corners
[8]) const
163 corners
[0][0] = min
[0];
164 corners
[0][1] = min
[1];
165 corners
[0][2] = max
[2];
166 corners
[1][0] = max
[0];
167 corners
[1][1] = min
[1];
168 corners
[1][2] = max
[2];
169 corners
[2][0] = max
[0];
170 corners
[2][1] = max
[1];
171 corners
[2][2] = max
[2];
172 corners
[3][0] = min
[0];
173 corners
[3][1] = max
[1];
174 corners
[3][2] = max
[2];
175 corners
[4][0] = min
[0];
176 corners
[4][1] = min
[1];
177 corners
[4][2] = min
[2];
178 corners
[5][0] = max
[0];
179 corners
[5][1] = min
[1];
180 corners
[5][2] = min
[2];
181 corners
[6][0] = max
[0];
182 corners
[6][1] = max
[1];
183 corners
[6][2] = min
[2];
184 corners
[7][0] = min
[0];
185 corners
[7][1] = max
[1];
186 corners
[7][2] = min
[2];
189 void enclose_vertices(const vector vertices
[], unsigned count
)
193 for (unsigned i
= 1; i
< count
; ++i
)
195 min
.minimize(vertices
[i
]);
196 max
.maximize(vertices
[i
]);
200 void draw(scalar alpha
= 0.0) const
202 glRect(min
[0], min
[1], max
[0], max
[1]);
205 bool is_visible(const frustum
& frustum
) const
212 void import_aabb_class(script
& script
);
215 inline void aabb
<3>::draw(scalar alpha
) const
217 scalar vertices
[] = {
218 min
[0], min
[1], min
[2],
219 min
[0], max
[1], min
[2],
220 max
[0], max
[1], min
[2],
221 max
[0], min
[1], min
[2],
222 min
[0], max
[1], max
[2],
223 min
[0], min
[1], max
[2],
224 max
[0], min
[1], max
[2],
225 max
[0], max
[1], max
[2]
228 GLubyte indices
[] = {
237 glEnableClientState(GL_VERTEX_ARRAY
);
238 glDisableClientState(GL_TEXTURE_COORD_ARRAY
);
239 glVertexPointer(3, GL_SCALAR
, 0, vertices
);
241 glPolygonMode(GL_FRONT_AND_BACK
, GL_LINE
);
242 image::reset_binding();
244 glDrawElements(GL_QUADS
, sizeof(indices
), GL_UNSIGNED_BYTE
, indices
);
246 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
247 //glDisableClientState(GL_VERTEX_ARRAY);
249 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
253 inline bool aabb
<3>::is_visible(const frustum
& frustum
) const
255 return frustum
.contains(*this);
258 typedef aabb
<2> aabb2
;
259 typedef aabb2 rectangle
;
260 typedef aabb
<3> aabb3
;
265 #endif // _MOOF_AABB_HH_
This page took 0.041993 seconds and 3 git commands to generate.