]>
Dogcows Code - chaz/yoink/blob - src/Moof/Aabb.hh
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_
15 #include <Moof/Cullable.hh>
16 #include <Moof/Drawable.hh>
17 #include <Moof/Math.hh>
18 #include <Moof/Plane.hh>
19 #include <Moof/Shape.hh>
21 #include <Moof/Frustum.hh> // FIXME this file is quite broken
22 #include <Moof/OpenGL.hh>
23 #include <Moof/Texture.hh>
33 * Axis-aligned Bounding Box
37 struct Aabb
: public Cullable
, public Drawable
, public Shape
<D
>
39 typedef cml::vector
< Scalar
, cml::fixed
<D
> > Vector
;
47 Aabb(const Vector
& a
, const Vector
& b
)
52 Aabb(Scalar x1
, Scalar y1
, Scalar x2
, Scalar y2
)
60 Aabb(Scalar x1
, Scalar y1
, Scalar z1
, Scalar x2
, Scalar y2
, Scalar z2
)
69 void init(const Vector2
& a
, const Vector2
& b
)
93 void init(const Vector3
& a
, const Vector3
& b
)
128 Vector
getCenter() const
130 return (min
+ max
) / 2.0;
134 Plane
getPlaneXY() const
137 plane
.normal
= Vector3(0.0, 0.0, 1.0);
138 plane
.d
= cml::dot(-plane
.normal
, getCenter());
142 Plane
getPlaneXZ() const
145 plane
.normal
= Vector3(0.0, 1.0, 0.0);
146 plane
.d
= cml::dot(-plane
.normal
, getCenter());
150 Plane
getPlaneYZ() const
153 plane
.normal
= Vector3(1.0, 0.0, 0.0);
154 plane
.d
= cml::dot(-plane
.normal
, getCenter());
159 void getCorners(Vector2 corners
[4]) const
161 corners
[0][0] = min
[0]; corners
[0][1] = min
[1];
162 corners
[1][0] = max
[0]; corners
[1][1] = min
[1];
163 corners
[2][0] = max
[0]; corners
[2][1] = max
[1];
164 corners
[3][0] = min
[0]; corners
[3][1] = max
[1];
167 void getCorners(Vector3 corners
[8]) const
169 corners
[0][0] = min
[0];
170 corners
[0][1] = min
[1];
171 corners
[0][2] = max
[2];
172 corners
[1][0] = max
[0];
173 corners
[1][1] = min
[1];
174 corners
[1][2] = max
[2];
175 corners
[2][0] = max
[0];
176 corners
[2][1] = max
[1];
177 corners
[2][2] = max
[2];
178 corners
[3][0] = min
[0];
179 corners
[3][1] = max
[1];
180 corners
[3][2] = max
[2];
181 corners
[4][0] = min
[0];
182 corners
[4][1] = min
[1];
183 corners
[4][2] = min
[2];
184 corners
[5][0] = max
[0];
185 corners
[5][1] = min
[1];
186 corners
[5][2] = min
[2];
187 corners
[6][0] = max
[0];
188 corners
[6][1] = max
[1];
189 corners
[6][2] = min
[2];
190 corners
[7][0] = min
[0];
191 corners
[7][1] = max
[1];
192 corners
[7][2] = min
[2];
196 void encloseVertices(const Vector vertices
[], unsigned count
)
201 for (unsigned i
= 1; i
< count
; ++i
)
203 min
.minimize(vertices
[i
]);
204 max
.maximize(vertices
[i
]);
209 void draw(Scalar alpha
= 0.0) const
211 glRect(min
[0], min
[1], max
[0], max
[1]);
214 bool isVisible(const Frustum
& frustum
) const
221 void importAabbClass(Script
& script
);
224 inline void Aabb
<3>::draw(Scalar alpha
) const
226 Scalar vertices
[] = {min
[0], min
[1], min
[2],
227 min
[0], max
[1], min
[2],
228 max
[0], max
[1], min
[2],
229 max
[0], min
[1], min
[2],
230 min
[0], max
[1], max
[2],
231 min
[0], min
[1], max
[2],
232 max
[0], min
[1], max
[2],
233 max
[0], max
[1], max
[2]};
235 GLubyte indices
[] = {0, 1, 2, 3,
242 glEnableClientState(GL_VERTEX_ARRAY
);
243 glDisableClientState(GL_TEXTURE_COORD_ARRAY
);
244 glVertexPointer(3, GL_SCALAR
, 0, vertices
);
246 glPolygonMode(GL_FRONT_AND_BACK
, GL_LINE
);
247 Texture::resetBind();
249 glDrawElements(GL_QUADS
, sizeof(indices
), GL_UNSIGNED_BYTE
,
252 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
253 //glDisableClientState(GL_VERTEX_ARRAY);
255 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
259 inline bool Aabb
<3>::isVisible(const Frustum
& frustum
) const
261 return frustum
.contains(*this);
265 typedef Aabb
<2> Aabb2
;
266 typedef Aabb2 Rectangle
;
267 typedef Aabb
<3> Aabb3
;
272 #endif // _MOOF_AABB_HH_
This page took 0.050363 seconds and 4 git commands to generate.