]> Dogcows Code - chaz/yoink/blob - src/moof/aabb.hh
bugfix: win32 packaging script temp directories
[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 /**
16 * \file aabb.hh
17 * Axis-aligned Bounding Box
18 */
19
20 #include <moof/cullable.hh>
21 #include <moof/drawable.hh>
22 #include <moof/math.hh>
23 #include <moof/plane.hh>
24 #include <moof/shape.hh>
25
26 #include <moof/frustum.hh> // FIXME: this file is quite broken
27 #include <moof/opengl.hh>
28 #include <moof/texture.hh>
29
30
31 namespace moof {
32
33
34 class script;
35
36
37 template <int D = 3>
38 struct aabb : public cullable, public drawable, public shape<D>
39 {
40 typedef moof::vector< scalar, fixed<D> > vector;
41
42 vector min;
43 vector max;
44
45
46 aabb() {}
47
48 aabb(const vector& a, const vector& b)
49 {
50 init(a, b);
51 }
52
53 aabb(scalar x1, scalar y1, scalar x2, scalar y2)
54 {
55 vector a(x1, y1);
56 vector b(x2, y2);
57
58 init(a, b);
59 }
60
61 aabb(scalar x1, scalar y1, scalar z1, scalar x2, scalar y2, scalar z2)
62 {
63 vector a(x1, y1, z1);
64 vector b(x2, y2, z2);
65
66 init(a, b);
67 }
68
69
70 void init(const vector2& a, const vector2& b)
71 {
72 if (a[0] < b[0])
73 {
74 min[0] = a[0];
75 max[0] = b[0];
76 }
77 else
78 {
79 min[0] = b[0];
80 max[0] = a[0];
81 }
82 if (a[1] < b[1])
83 {
84 min[1] = a[1];
85 max[1] = b[1];
86 }
87 else
88 {
89 min[1] = b[1];
90 max[1] = a[1];
91 }
92 }
93
94 void init(const vector3& a, const vector3& b)
95 {
96 if (a[0] < b[0])
97 {
98 min[0] = a[0];
99 max[0] = b[0];
100 }
101 else
102 {
103 min[0] = b[0];
104 max[0] = a[0];
105 }
106 if (a[1] < b[1])
107 {
108 min[1] = a[1];
109 max[1] = b[1];
110 }
111 else
112 {
113 min[1] = b[1];
114 max[1] = a[1];
115 }
116 if (a[2] < b[2])
117 {
118 min[2] = a[2];
119 max[2] = b[2];
120 }
121 else
122 {
123 min[2] = b[2];
124 max[2] = a[2];
125 }
126 }
127
128
129 vector center() const
130 {
131 return (min + max) / 2.0;
132 }
133
134
135 plane xy_plane() const
136 {
137 plane plane;
138 plane.normal = vector3(0.0, 0.0, 1.0);
139 plane.d = dot(-plane.normal, center());
140 return plane;
141 }
142
143 plane xz_plane() const
144 {
145 plane plane;
146 plane.normal = vector3(0.0, 1.0, 0.0);
147 plane.d = dot(-plane.normal, center());
148 return plane;
149 }
150
151 plane yz_plane() const
152 {
153 plane plane;
154 plane.normal = vector3(1.0, 0.0, 0.0);
155 plane.d = dot(-plane.normal, center());
156 return plane;
157 }
158
159
160 void get_corners(vector2 corners[4]) const
161 {
162 corners[0][0] = min[0]; corners[0][1] = min[1];
163 corners[1][0] = max[0]; corners[1][1] = min[1];
164 corners[2][0] = max[0]; corners[2][1] = max[1];
165 corners[3][0] = min[0]; corners[3][1] = max[1];
166 }
167
168 void get_corners(vector3 corners[8]) const
169 {
170 corners[0][0] = min[0];
171 corners[0][1] = min[1];
172 corners[0][2] = max[2];
173 corners[1][0] = max[0];
174 corners[1][1] = min[1];
175 corners[1][2] = max[2];
176 corners[2][0] = max[0];
177 corners[2][1] = max[1];
178 corners[2][2] = max[2];
179 corners[3][0] = min[0];
180 corners[3][1] = max[1];
181 corners[3][2] = max[2];
182 corners[4][0] = min[0];
183 corners[4][1] = min[1];
184 corners[4][2] = min[2];
185 corners[5][0] = max[0];
186 corners[5][1] = min[1];
187 corners[5][2] = min[2];
188 corners[6][0] = max[0];
189 corners[6][1] = max[1];
190 corners[6][2] = min[2];
191 corners[7][0] = min[0];
192 corners[7][1] = max[1];
193 corners[7][2] = min[2];
194 }
195
196
197 void enclose_vertices(const vector vertices[], unsigned count)
198 {
199 min.zero();
200 max.zero();
201
202 for (unsigned i = 1; i < count; ++i)
203 {
204 min.minimize(vertices[i]);
205 max.maximize(vertices[i]);
206 }
207 }
208
209
210 void draw(scalar alpha = 0.0) const
211 {
212 glRect(min[0], min[1], max[0], max[1]);
213 }
214
215 bool is_visible(const frustum& frustum) const
216 {
217 return true;
218 }
219 };
220
221
222 void import_aabb_class(script& script);
223
224 template <>
225 inline void aabb<3>::draw(scalar alpha) const
226 {
227 scalar vertices[] = {min[0], min[1], min[2],
228 min[0], max[1], min[2],
229 max[0], max[1], min[2],
230 max[0], min[1], min[2],
231 min[0], max[1], max[2],
232 min[0], min[1], max[2],
233 max[0], min[1], max[2],
234 max[0], max[1], max[2]};
235
236 GLubyte indices[] = {0, 1, 2, 3,
237 1, 2, 7, 4,
238 3, 0, 5, 6,
239 2, 3, 6, 7,
240 5, 0, 1, 4,
241 4, 5, 6, 7};
242
243 glEnableClientState(GL_VERTEX_ARRAY);
244 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
245 glVertexPointer(3, GL_SCALAR, 0, vertices);
246
247 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
248 texture::reset_binding();
249
250 glDrawElements(GL_QUADS, sizeof(indices), GL_UNSIGNED_BYTE,
251 indices);
252
253 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
254 //glDisableClientState(GL_VERTEX_ARRAY);
255
256 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
257 }
258
259 template <>
260 inline bool aabb<3>::is_visible(const frustum& frustum) const
261 {
262 return frustum.contains(*this);
263 }
264
265
266 typedef aabb<2> aabb2;
267 typedef aabb2 rectangle;
268 typedef aabb<3> aabb3;
269
270
271 } // namespace moof
272
273 #endif // _MOOF_AABB_HH_
274
This page took 0.040395 seconds and 4 git commands to generate.