]> Dogcows Code - chaz/yoink/blob - src/Moof/Aabb.hh
game loop tweaks; shapes hierarchy defined
[chaz/yoink] / src / Moof / Aabb.hh
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 #ifndef _MOOF_AABB_HH_
13 #define _MOOF_AABB_HH_
14
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>
20
21 #include <Moof/Frustum.hh> // FIXME this file is quite broken
22 #include <Moof/OpenGL.hh>
23 #include <Moof/Texture.hh>
24
25
26 namespace Mf {
27
28
29 /**
30 * Axis-aligned Bounding Box
31 */
32
33 template <int D = 3>
34 struct Aabb : public Cullable, public Drawable, public Shape<D>
35 {
36 typedef cml::vector< Scalar, cml::fixed<D> > Vector;
37
38 Vector min;
39 Vector max;
40
41
42 Aabb() {}
43
44 Aabb(const Vector& a, const Vector& b)
45 {
46 init(a, b);
47 }
48
49 Aabb(Scalar x1, Scalar y1, Scalar x2, Scalar y2)
50 {
51 Vector a(x1, y1);
52 Vector b(x2, y2);
53
54 init(a, b);
55 }
56
57 Aabb(Scalar x1, Scalar y1, Scalar z1, Scalar x2, Scalar y2, Scalar z2)
58 {
59 Vector a(x1, y1, z1);
60 Vector b(x2, y2, z2);
61
62 init(a, b);
63 }
64
65
66 void init(const Vector2& a, const Vector2& b)
67 {
68 if (a[0] < b[0])
69 {
70 min[0] = a[0];
71 max[0] = b[0];
72 }
73 else
74 {
75 min[0] = b[0];
76 max[0] = a[0];
77 }
78 if (a[1] < b[1])
79 {
80 min[1] = a[1];
81 max[1] = b[1];
82 }
83 else
84 {
85 min[1] = b[1];
86 max[1] = a[1];
87 }
88 }
89
90 void init(const Vector3& a, const Vector3& b)
91 {
92 if (a[0] < b[0])
93 {
94 min[0] = a[0];
95 max[0] = b[0];
96 }
97 else
98 {
99 min[0] = b[0];
100 max[0] = a[0];
101 }
102 if (a[1] < b[1])
103 {
104 min[1] = a[1];
105 max[1] = b[1];
106 }
107 else
108 {
109 min[1] = b[1];
110 max[1] = a[1];
111 }
112 if (a[2] < b[2])
113 {
114 min[2] = a[2];
115 max[2] = b[2];
116 }
117 else
118 {
119 min[2] = b[2];
120 max[2] = a[2];
121 }
122 }
123
124
125 Vector getCenter() const
126 {
127 return (min + max) / 2.0;
128 }
129
130
131 Plane getPlaneXY() const
132 {
133 Plane plane;
134 plane.normal = Vector3(0.0, 0.0, 1.0);
135 plane.d = cml::dot(-plane.normal, getCenter());
136 return plane;
137 }
138
139 Plane getPlaneXZ() const
140 {
141 Plane plane;
142 plane.normal = Vector3(0.0, 1.0, 0.0);
143 plane.d = cml::dot(-plane.normal, getCenter());
144 return plane;
145 }
146
147 Plane getPlaneYZ() const
148 {
149 Plane plane;
150 plane.normal = Vector3(1.0, 0.0, 0.0);
151 plane.d = cml::dot(-plane.normal, getCenter());
152 return plane;
153 }
154
155
156 void getCorners(Vector2 corners[4]) const
157 {
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];
162 }
163
164 void getCorners(Vector3 corners[8]) const
165 {
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];
190 }
191
192
193 void encloseVertices(const Vector vertices[], unsigned count)
194 {
195 min.zero();
196 max.zero();
197
198 for (unsigned i = 1; i < count; ++i)
199 {
200 min.minimize(vertices[i]);
201 max.maximize(vertices[i]);
202 }
203 }
204
205
206 void draw(Scalar alpha = 0.0) const
207 {
208 glRect(min[0], min[1], max[0], max[1]);
209 }
210
211 bool isVisible(const Frustum& frustum) const
212 {
213 return true;
214 }
215 };
216
217
218 template <>
219 inline void Aabb<3>::draw(Scalar alpha) const
220 {
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]};
229
230 GLubyte indices[] = {0, 1, 2, 3,
231 1, 2, 7, 4,
232 3, 0, 5, 6,
233 2, 3, 6, 7,
234 5, 0, 1, 4,
235 4, 5, 6, 7};
236
237 glEnableClientState(GL_VERTEX_ARRAY);
238 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
239 glVertexPointer(3, GL_SCALAR, 0, vertices);
240
241 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
242 Texture::resetBind();
243
244 glDrawElements(GL_QUADS, sizeof(indices), GL_UNSIGNED_BYTE,
245 indices);
246
247 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
248 //glDisableClientState(GL_VERTEX_ARRAY);
249
250 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
251 }
252
253 template <>
254 inline bool Aabb<3>::isVisible(const Frustum& frustum) const
255 {
256 return frustum.contains(*this);
257 }
258
259
260 typedef Aabb<2> Aabb2;
261 typedef Aabb2 Rectangle;
262 typedef Aabb<3> Aabb3;
263
264
265 } // namespace Mf
266
267 #endif // _MOOF_AABB_HH_
268
This page took 0.045439 seconds and 5 git commands to generate.