]> Dogcows Code - chaz/yoink/blob - src/Moof/Aabb.hh
83926b3fa47789e19a85ed9124c9bef61bd89fca
[chaz/yoink] / src / Moof / Aabb.hh
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #ifndef _MOOF_AABB_HH_
30 #define _MOOF_AABB_HH_
31
32 #include <Moof/Cullable.hh>
33 #include <Moof/Drawable.hh>
34 #include <Moof/Math.hh>
35 #include <Moof/Plane.hh>
36 #include <Moof/Shape.hh>
37
38 #include <Moof/Frustum.hh>
39 #include <Moof/OpenGL.hh>
40 #include <Moof/Texture.hh>
41
42
43 namespace Mf {
44
45
46 /**
47 * Axis-aligned Bounding Box
48 */
49
50 template <int D = 3>
51 struct Aabb : public Cullable, public Drawable, public Shape<D>
52 {
53 typedef cml::vector< Scalar, cml::fixed<D> > Vector;
54
55 Vector min;
56 Vector max;
57
58 Aabb() {}
59
60 Aabb(const Vector& a, const Vector& b)
61 {
62 init(a, b);
63 }
64
65 Aabb(Scalar ax, Scalar ay, Scalar az,
66 Scalar bx, Scalar by, Scalar bz)
67 {
68 Vector a(ax, ay, az);
69 Vector b(bx, by, bz);
70
71 init(a, b);
72 }
73
74 void init(const Vector& a, const Vector& b)
75 {
76 if (a[0] < b[0])
77 {
78 min[0] = a[0];
79 max[0] = b[0];
80 }
81 else
82 {
83 min[0] = b[0];
84 max[0] = a[0];
85 }
86 if (a[1] < b[1])
87 {
88 min[1] = a[1];
89 max[1] = b[1];
90 }
91 else
92 {
93 min[1] = b[1];
94 max[1] = a[1];
95 }
96 if (a[2] < b[2])
97 {
98 min[2] = a[2];
99 max[2] = b[2];
100 }
101 else
102 {
103 min[2] = b[2];
104 max[2] = a[2];
105 }
106 }
107
108 Vector getCenter() const
109 {
110 return Vector((min[0] + max[0]) / 2.0,
111 (min[1] + max[1]) / 2.0,
112 (min[2] + max[2]) / 2.0);
113 }
114
115 //void getOctant(Aabb& octant, int num) const;
116
117 Plane getPlaneXY() const
118 {
119 Plane plane;
120 plane.normal = Vector(0.0, 0.0, 1.0);
121 plane.d = cml::dot(-plane.normal, getCenter());
122 return plane;
123 }
124
125 Plane getPlaneXZ() const
126 {
127 Plane plane;
128 plane.normal = Vector(0.0, 1.0, 0.0);
129 plane.d = cml::dot(-plane.normal, getCenter());
130 return plane;
131 }
132
133 Plane getPlaneYZ() const
134 {
135 Plane plane;
136 plane.normal = Vector(1.0, 0.0, 0.0);
137 plane.d = cml::dot(-plane.normal, getCenter());
138 return plane;
139 }
140
141 /*
142 void getCorners(Vector3 corners[8]) const;
143
144 void encloseVertices(const Vector3 vertices[], unsigned count);
145
146 void draw(Scalar alpha = 0.0) const;
147 bool isVisible(const Frustum& frustum) const;
148 */
149
150
151 void getCorners(Vector corners[8]) const
152 {
153 corners[0][0] = min[0]; corners[0][1] = min[1]; corners[0][2] = max[2];
154 corners[1][0] = max[0]; corners[1][1] = min[1]; corners[1][2] = max[2];
155 corners[2][0] = max[0]; corners[2][1] = max[1]; corners[2][2] = max[2];
156 corners[3][0] = min[0]; corners[3][1] = max[1]; corners[3][2] = max[2];
157 corners[4][0] = min[0]; corners[4][1] = min[1]; corners[4][2] = min[2];
158 corners[5][0] = max[0]; corners[5][1] = min[1]; corners[5][2] = min[2];
159 corners[6][0] = max[0]; corners[6][1] = max[1]; corners[6][2] = min[2];
160 corners[7][0] = min[0]; corners[7][1] = max[1]; corners[7][2] = min[2];
161 }
162
163
164 void encloseVertices(const Vector vertices[], unsigned count)
165 {
166 min.zero();
167 max.zero();
168
169 for (unsigned i = 1; i < count; ++i)
170 {
171 min.minimize(vertices[i]);
172 max.maximize(vertices[i]);
173 }
174 }
175
176
177 void draw(Scalar alpha = 0.0) const
178 {
179 Scalar vertices[] = {min[0], min[1], min[2],
180 min[0], max[1], min[2],
181 max[0], max[1], min[2],
182 max[0], min[1], min[2],
183 min[0], max[1], max[2],
184 min[0], min[1], max[2],
185 max[0], min[1], max[2],
186 max[0], max[1], max[2]};
187
188 GLubyte indices[] = {0, 1, 2, 3,
189 1, 2, 7, 4,
190 3, 0, 5, 6,
191 2, 3, 6, 7,
192 5, 0, 1, 4,
193 4, 5, 6, 7};
194
195 glEnableClientState(GL_VERTEX_ARRAY);
196 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
197 glVertexPointer(3, GL_SCALAR, 0, vertices);
198
199 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
200 Texture::resetBind();
201
202 glDrawElements(GL_QUADS, sizeof(indices), GL_UNSIGNED_BYTE, 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
221 /** vim: set ts=4 sw=4 tw=80: *************************************************/
222
This page took 0.040081 seconds and 3 git commands to generate.