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