]> Dogcows Code - chaz/yoink/blob - src/moof/aabb.hh
3228b5a225a412927a0601b68c5a77bc608a5383
[chaz/yoink] / src / moof / aabb.hh
1
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
4 *
5 * Distributable under the terms and conditions of the 2-clause BSD license;
6 * see the file COPYING for a complete text of the license.
7 *
8 *****************************************************************************/
9
10 #ifndef _MOOF_AABB_HH_
11 #define _MOOF_AABB_HH_
12
13 /**
14 * \file aabb.hh
15 * Axis-aligned Bounding Box
16 */
17
18 #include <moof/cullable.hh>
19 #include <moof/drawable.hh>
20 #include <moof/math.hh>
21 #include <moof/plane.hh>
22 #include <moof/shape.hh>
23
24 #include <moof/frustum.hh> // FIXME: this file is quite broken
25 #include <moof/image.hh>
26 #include <moof/opengl.hh>
27
28
29 namespace moof {
30
31
32 // forward declarations
33 class script;
34
35 template <int D = 3>
36 struct aabb : public cullable, public drawable, public shape<D>
37 {
38 typedef moof::vector< scalar, fixed<D> > vector;
39
40 vector min;
41 vector max;
42
43 aabb() {}
44
45 aabb(const vector& a, const vector& b)
46 {
47 init(a, b);
48 }
49
50 aabb(scalar x1, scalar y1, scalar x2, scalar y2)
51 {
52 vector a(x1, y1);
53 vector b(x2, y2);
54
55 init(a, b);
56 }
57
58 aabb(scalar x1, scalar y1, scalar z1, scalar x2, scalar y2, scalar z2)
59 {
60 vector a(x1, y1, z1);
61 vector b(x2, y2, z2);
62
63 init(a, b);
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 vector center() const
125 {
126 return (min + max) / 2.0;
127 }
128
129 plane xy_plane() const
130 {
131 plane plane;
132 plane.normal = vector3(0.0, 0.0, 1.0);
133 plane.d = dot(-plane.normal, center());
134 return plane;
135 }
136
137 plane xz_plane() const
138 {
139 plane plane;
140 plane.normal = vector3(0.0, 1.0, 0.0);
141 plane.d = dot(-plane.normal, center());
142 return plane;
143 }
144
145 plane yz_plane() const
146 {
147 plane plane;
148 plane.normal = vector3(1.0, 0.0, 0.0);
149 plane.d = dot(-plane.normal, center());
150 return plane;
151 }
152
153 void get_corners(vector2 corners[4]) const
154 {
155 corners[0][0] = min[0]; corners[0][1] = min[1];
156 corners[1][0] = max[0]; corners[1][1] = min[1];
157 corners[2][0] = max[0]; corners[2][1] = max[1];
158 corners[3][0] = min[0]; corners[3][1] = max[1];
159 }
160
161 void get_corners(vector3 corners[8]) const
162 {
163 corners[0][0] = min[0];
164 corners[0][1] = min[1];
165 corners[0][2] = max[2];
166 corners[1][0] = max[0];
167 corners[1][1] = min[1];
168 corners[1][2] = max[2];
169 corners[2][0] = max[0];
170 corners[2][1] = max[1];
171 corners[2][2] = max[2];
172 corners[3][0] = min[0];
173 corners[3][1] = max[1];
174 corners[3][2] = max[2];
175 corners[4][0] = min[0];
176 corners[4][1] = min[1];
177 corners[4][2] = min[2];
178 corners[5][0] = max[0];
179 corners[5][1] = min[1];
180 corners[5][2] = min[2];
181 corners[6][0] = max[0];
182 corners[6][1] = max[1];
183 corners[6][2] = min[2];
184 corners[7][0] = min[0];
185 corners[7][1] = max[1];
186 corners[7][2] = min[2];
187 }
188
189 void enclose_vertices(const vector vertices[], unsigned count)
190 {
191 min.zero();
192 max.zero();
193 for (unsigned i = 1; i < count; ++i)
194 {
195 min.minimize(vertices[i]);
196 max.maximize(vertices[i]);
197 }
198 }
199
200 void draw(scalar alpha = 0.0) const
201 {
202 glRect(min[0], min[1], max[0], max[1]);
203 }
204
205 bool is_visible(const frustum& frustum) const
206 {
207 return true;
208 }
209 };
210
211
212 void import_aabb_class(script& script);
213
214 template <>
215 inline void aabb<3>::draw(scalar alpha) const
216 {
217 scalar vertices[] = {
218 min[0], min[1], min[2],
219 min[0], max[1], min[2],
220 max[0], max[1], min[2],
221 max[0], min[1], min[2],
222 min[0], max[1], max[2],
223 min[0], min[1], max[2],
224 max[0], min[1], max[2],
225 max[0], max[1], max[2]
226 };
227
228 GLubyte indices[] = {
229 0, 1, 2, 3,
230 1, 2, 7, 4,
231 3, 0, 5, 6,
232 2, 3, 6, 7,
233 5, 0, 1, 4,
234 4, 5, 6, 7
235 };
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 image::reset_binding();
243
244 glDrawElements(GL_QUADS, sizeof(indices), GL_UNSIGNED_BYTE, indices);
245
246 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
247 //glDisableClientState(GL_VERTEX_ARRAY);
248
249 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
250 }
251
252 template <>
253 inline bool aabb<3>::is_visible(const frustum& frustum) const
254 {
255 return frustum.contains(*this);
256 }
257
258 typedef aabb<2> aabb2;
259 typedef aabb2 rectangle;
260 typedef aabb<3> aabb3;
261
262
263 } // namespace moof
264
265 #endif // _MOOF_AABB_HH_
266
This page took 0.037858 seconds and 3 git commands to generate.