]> Dogcows Code - chaz/yoink/blob - src/Moof/Aabb.hh
reformatting
[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 Aabb() {}
42
43 Aabb(const Vector& a, const Vector& b)
44 {
45 init(a, b);
46 }
47
48 Aabb(Scalar ax, Scalar ay, Scalar az,
49 Scalar bx, Scalar by, Scalar bz)
50 {
51 Vector a(ax, ay, az);
52 Vector b(bx, by, bz);
53
54 init(a, b);
55 }
56
57 void init(const Vector& a, const Vector& b)
58 {
59 if (a[0] < b[0])
60 {
61 min[0] = a[0];
62 max[0] = b[0];
63 }
64 else
65 {
66 min[0] = b[0];
67 max[0] = a[0];
68 }
69 if (a[1] < b[1])
70 {
71 min[1] = a[1];
72 max[1] = b[1];
73 }
74 else
75 {
76 min[1] = b[1];
77 max[1] = a[1];
78 }
79 if (a[2] < b[2])
80 {
81 min[2] = a[2];
82 max[2] = b[2];
83 }
84 else
85 {
86 min[2] = b[2];
87 max[2] = a[2];
88 }
89 }
90
91 Vector getCenter() const
92 {
93 return Vector((min[0] + max[0]) / 2.0,
94 (min[1] + max[1]) / 2.0,
95 (min[2] + max[2]) / 2.0);
96 }
97
98 //void getOctant(Aabb& octant, int num) const;
99
100 Plane getPlaneXY() const
101 {
102 Plane plane;
103 plane.normal = Vector(0.0, 0.0, 1.0);
104 plane.d = cml::dot(-plane.normal, getCenter());
105 return plane;
106 }
107
108 Plane getPlaneXZ() const
109 {
110 Plane plane;
111 plane.normal = Vector(0.0, 1.0, 0.0);
112 plane.d = cml::dot(-plane.normal, getCenter());
113 return plane;
114 }
115
116 Plane getPlaneYZ() const
117 {
118 Plane plane;
119 plane.normal = Vector(1.0, 0.0, 0.0);
120 plane.d = cml::dot(-plane.normal, getCenter());
121 return plane;
122 }
123
124 /*
125 void getCorners(Vector3 corners[8]) const;
126
127 void encloseVertices(const Vector3 vertices[], unsigned count);
128
129 void draw(Scalar alpha = 0.0) const;
130 bool isVisible(const Frustum& frustum) const;
131 */
132
133
134 void getCorners(Vector corners[8]) const
135 {
136 corners[0][0] = min[0];
137 corners[0][1] = min[1];
138 corners[0][2] = max[2];
139 corners[1][0] = max[0];
140 corners[1][1] = min[1];
141 corners[1][2] = max[2];
142 corners[2][0] = max[0];
143 corners[2][1] = max[1];
144 corners[2][2] = max[2];
145 corners[3][0] = min[0];
146 corners[3][1] = max[1];
147 corners[3][2] = max[2];
148 corners[4][0] = min[0];
149 corners[4][1] = min[1];
150 corners[4][2] = min[2];
151 corners[5][0] = max[0];
152 corners[5][1] = min[1];
153 corners[5][2] = min[2];
154 corners[6][0] = max[0];
155 corners[6][1] = max[1];
156 corners[6][2] = min[2];
157 corners[7][0] = min[0];
158 corners[7][1] = max[1];
159 corners[7][2] = min[2];
160 }
161
162
163 void encloseVertices(const Vector vertices[], unsigned count)
164 {
165 min.zero();
166 max.zero();
167
168 for (unsigned i = 1; i < count; ++i)
169 {
170 min.minimize(vertices[i]);
171 max.maximize(vertices[i]);
172 }
173 }
174
175
176 void draw(Scalar alpha = 0.0) const
177 {
178 Scalar vertices[] = {min[0], min[1], min[2],
179 min[0], max[1], min[2],
180 max[0], max[1], min[2],
181 max[0], min[1], min[2],
182 min[0], max[1], max[2],
183 min[0], min[1], max[2],
184 max[0], min[1], max[2],
185 max[0], max[1], max[2]};
186
187 GLubyte indices[] = {0, 1, 2, 3,
188 1, 2, 7, 4,
189 3, 0, 5, 6,
190 2, 3, 6, 7,
191 5, 0, 1, 4,
192 4, 5, 6, 7};
193
194 glEnableClientState(GL_VERTEX_ARRAY);
195 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
196 glVertexPointer(3, GL_SCALAR, 0, vertices);
197
198 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
199 Texture::resetBind();
200
201 glDrawElements(GL_QUADS, sizeof(indices), GL_UNSIGNED_BYTE,
202 indices);
203
204 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
205 //glDisableClientState(GL_VERTEX_ARRAY);
206
207 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
208 }
209
210 bool isVisible(const Frustum& frustum) const
211 {
212 return frustum.contains(*this);
213 }
214 };
215
216
217 } // namespace Mf
218
219 #endif // _MOOF_AABB_HH_
220
This page took 0.040473 seconds and 4 git commands to generate.