]>
Dogcows Code - chaz/yoink/blob - src/Moof/Aabb.hh
8fcdbc3787114aca620ba3bdf832698d119f66f3
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>
30 * Axis-aligned Bounding Box
34 struct Aabb
: public Cullable
, public Drawable
, public Shape
<D
>
36 typedef cml::vector
< Scalar
, cml::fixed
<D
> > Vector
;
44 Aabb(const Vector
& a
, const Vector
& b
)
49 Aabb(Scalar x1
, Scalar y1
, Scalar x2
, Scalar y2
)
57 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
)
125 Vector
getCenter() const
127 return (min
+ max
) / 2.0;
131 Plane
getPlaneXY() const
134 plane
.normal
= Vector3(0.0, 0.0, 1.0);
135 plane
.d
= cml::dot(-plane
.normal
, getCenter());
139 Plane
getPlaneXZ() const
142 plane
.normal
= Vector3(0.0, 1.0, 0.0);
143 plane
.d
= cml::dot(-plane
.normal
, getCenter());
147 Plane
getPlaneYZ() const
150 plane
.normal
= Vector3(1.0, 0.0, 0.0);
151 plane
.d
= cml::dot(-plane
.normal
, getCenter());
156 void getCorners(Vector2 corners
[4]) const
158 corners
[0][0] = min
[0]; corners
[0][1] = min
[1];
159 corners
[1][0] = max
[0]; corners
[1][1] = min
[1];
160 corners
[2][0] = max
[0]; corners
[2][1] = max
[1];
161 corners
[3][0] = min
[0]; corners
[3][1] = max
[1];
164 void getCorners(Vector3 corners
[8]) const
166 corners
[0][0] = min
[0];
167 corners
[0][1] = min
[1];
168 corners
[0][2] = max
[2];
169 corners
[1][0] = max
[0];
170 corners
[1][1] = min
[1];
171 corners
[1][2] = max
[2];
172 corners
[2][0] = max
[0];
173 corners
[2][1] = max
[1];
174 corners
[2][2] = max
[2];
175 corners
[3][0] = min
[0];
176 corners
[3][1] = max
[1];
177 corners
[3][2] = max
[2];
178 corners
[4][0] = min
[0];
179 corners
[4][1] = min
[1];
180 corners
[4][2] = min
[2];
181 corners
[5][0] = max
[0];
182 corners
[5][1] = min
[1];
183 corners
[5][2] = min
[2];
184 corners
[6][0] = max
[0];
185 corners
[6][1] = max
[1];
186 corners
[6][2] = min
[2];
187 corners
[7][0] = min
[0];
188 corners
[7][1] = max
[1];
189 corners
[7][2] = min
[2];
193 void encloseVertices(const Vector vertices
[], unsigned count
)
198 for (unsigned i
= 1; i
< count
; ++i
)
200 min
.minimize(vertices
[i
]);
201 max
.maximize(vertices
[i
]);
206 void draw(Scalar alpha
= 0.0) const
208 glRect(min
[0], min
[1], max
[0], max
[1]);
211 bool isVisible(const Frustum
& frustum
) const
219 inline void Aabb
<3>::draw(Scalar alpha
) const
221 Scalar vertices
[] = {min
[0], min
[1], min
[2],
222 min
[0], max
[1], min
[2],
223 max
[0], max
[1], min
[2],
224 max
[0], min
[1], min
[2],
225 min
[0], max
[1], max
[2],
226 min
[0], min
[1], max
[2],
227 max
[0], min
[1], max
[2],
228 max
[0], max
[1], max
[2]};
230 GLubyte indices
[] = {0, 1, 2, 3,
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 Texture::resetBind();
244 glDrawElements(GL_QUADS
, sizeof(indices
), GL_UNSIGNED_BYTE
,
247 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
248 //glDisableClientState(GL_VERTEX_ARRAY);
250 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
254 inline bool Aabb
<3>::isVisible(const Frustum
& frustum
) const
256 return frustum
.contains(*this);
260 typedef Aabb
<2> Aabb2
;
261 typedef Aabb2 Rectangle
;
262 typedef Aabb
<3> Aabb3
;
267 #endif // _MOOF_AABB_HH_
This page took 0.048861 seconds and 3 git commands to generate.