1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Patrick Boenzli |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION |
---|
17 | |
---|
18 | #include "obb_tree_node.h" |
---|
19 | #include "list.h" |
---|
20 | #include "obb.h" |
---|
21 | #include "obb_tree.h" |
---|
22 | #include "vector.h" |
---|
23 | #include "abstract_model.h" |
---|
24 | #include "world_entity.h" |
---|
25 | |
---|
26 | #include <math.h> |
---|
27 | #include "color.h" |
---|
28 | |
---|
29 | #include "stdincl.h" |
---|
30 | #include "lin_alg.h" |
---|
31 | #include "glincl.h" |
---|
32 | |
---|
33 | |
---|
34 | |
---|
35 | using namespace std; |
---|
36 | |
---|
37 | OBBTree* OBBTreeNode::obbTree = NULL; |
---|
38 | |
---|
39 | float** OBBTreeNode::coMat = NULL; |
---|
40 | float** OBBTreeNode::eigvMat = NULL; |
---|
41 | float* OBBTreeNode::eigvlMat = NULL; |
---|
42 | int* OBBTreeNode::rotCount = NULL; |
---|
43 | GLUquadricObj* OBBTreeNode_sphereObj = NULL; |
---|
44 | |
---|
45 | /** |
---|
46 | * standard constructor |
---|
47 | */ |
---|
48 | OBBTreeNode::OBBTreeNode () |
---|
49 | { |
---|
50 | this->setClassID(CL_OBB_TREE_NODE, "OBBTreeNode"); |
---|
51 | this->nodeLeft = NULL; |
---|
52 | this->nodeRight = NULL; |
---|
53 | this->bvElement = NULL; |
---|
54 | |
---|
55 | if(OBBTreeNode::coMat == NULL) |
---|
56 | { |
---|
57 | OBBTreeNode::coMat = new float*[4]; |
---|
58 | for(int i = 0; i < 4; i++) |
---|
59 | OBBTreeNode::coMat[i] = new float[4]; |
---|
60 | } |
---|
61 | if(OBBTreeNode::eigvMat == NULL) |
---|
62 | { |
---|
63 | OBBTreeNode::eigvMat = new float*[4]; |
---|
64 | for(int i = 0; i < 4; i++) |
---|
65 | OBBTreeNode::eigvMat[i] = new float[4]; |
---|
66 | } |
---|
67 | if( OBBTreeNode::eigvlMat == NULL) |
---|
68 | { |
---|
69 | OBBTreeNode::eigvlMat = new float[4]; |
---|
70 | } |
---|
71 | if( OBBTreeNode::rotCount == NULL) |
---|
72 | OBBTreeNode::rotCount = new int; |
---|
73 | |
---|
74 | if (OBBTreeNode_sphereObj == NULL) |
---|
75 | OBBTreeNode_sphereObj = gluNewQuadric(); |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | /** |
---|
80 | * standard deconstructor |
---|
81 | */ |
---|
82 | OBBTreeNode::~OBBTreeNode () |
---|
83 | { |
---|
84 | if( this->nodeLeft) |
---|
85 | { |
---|
86 | delete this->nodeLeft; |
---|
87 | this->nodeLeft = NULL; |
---|
88 | } |
---|
89 | if( this->nodeRight) |
---|
90 | { |
---|
91 | delete this->nodeRight; |
---|
92 | this->nodeRight = NULL; |
---|
93 | } |
---|
94 | if( this->bvElement) |
---|
95 | delete this->bvElement; |
---|
96 | this->bvElement = NULL; |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | |
---|
101 | /** |
---|
102 | * creates a new BVTree or BVTree partition |
---|
103 | * @param depth: how much more depth-steps to go: if == 1 don't go any deeper! |
---|
104 | * @param verticesList: the list of vertices of the object - each vertices triple is interpreted as a triangle |
---|
105 | */ |
---|
106 | void OBBTreeNode::spawnBVTree(const int depth, sVec3D *verticesList, const int length) |
---|
107 | { |
---|
108 | PRINT(3)("\n"); |
---|
109 | this->treeIndex = this->obbTree->getID(); |
---|
110 | PRINTF(3)("OBB Depth: %i, tree index: %i, numVertices: %i\n", depth, treeIndex, length); |
---|
111 | this->depth = depth; |
---|
112 | |
---|
113 | |
---|
114 | this->bvElement = new OBB(); |
---|
115 | this->bvElement->vertices = verticesList; |
---|
116 | this->bvElement->numOfVertices = length; |
---|
117 | PRINTF(3)("Created OBBox\n"); |
---|
118 | this->calculateBoxCovariance(this->bvElement, verticesList, length); |
---|
119 | PRINTF(3)("Calculated attributes1\n"); |
---|
120 | this->calculateBoxEigenvectors(this->bvElement, verticesList, length); |
---|
121 | PRINTF(3)("Calculated attributes2\n"); |
---|
122 | this->calculateBoxAxis(this->bvElement, verticesList, length); |
---|
123 | PRINTF(3)("Calculated attributes3\n"); |
---|
124 | |
---|
125 | /* if this is the first node, the vertices data are the original ones of the model itself, so dont delete them in cleanup */ |
---|
126 | if( this->treeIndex == 1) |
---|
127 | this->bvElement->bOrigVertices = true; |
---|
128 | |
---|
129 | if( likely( this->depth > 0)) |
---|
130 | { |
---|
131 | this->forkBox(this->bvElement); |
---|
132 | |
---|
133 | |
---|
134 | if(this->tmpLen1 > 2) |
---|
135 | { |
---|
136 | OBBTreeNode* node1 = new OBBTreeNode(); |
---|
137 | this->nodeLeft = node1; |
---|
138 | this->nodeLeft->spawnBVTree(depth - 1, this->tmpVert1, this->tmpLen1); |
---|
139 | } |
---|
140 | else |
---|
141 | { |
---|
142 | PRINTF(3)("Aboarding tree walk: less than 3 vertices left\n"); |
---|
143 | } |
---|
144 | |
---|
145 | if( this->tmpLen2 > 2) |
---|
146 | { |
---|
147 | OBBTreeNode* node2 = new OBBTreeNode(); |
---|
148 | this->nodeRight = node2; |
---|
149 | this->nodeRight->spawnBVTree(depth - 1, this->tmpVert2, this->tmpLen2); |
---|
150 | } |
---|
151 | else |
---|
152 | { |
---|
153 | PRINTF(3)("Abording tree walk: less than 3 vertices left\n"); |
---|
154 | } |
---|
155 | |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | |
---|
161 | void OBBTreeNode::calculateBoxCovariance(OBB* box, sVec3D* verticesList, int length) |
---|
162 | { |
---|
163 | float facelet[length]; //!< surface area of the i'th triangle of the convex hull |
---|
164 | float face = 0.0f; //!< surface area of the entire convex hull |
---|
165 | Vector centroid[length]; //!< centroid of the i'th convex hull |
---|
166 | Vector center; //!< the center of the entire hull |
---|
167 | Vector p, q, r; //!< holder of the polygon data, much more conveniant to work with Vector than sVec3d |
---|
168 | Vector t1, t2; //!< temporary values |
---|
169 | float covariance[3][3]; //!< the covariance matrix |
---|
170 | int mode = 0; //!< mode = 0: vertex soup, no connections, mode = 1: 3 following verteces build a triangle |
---|
171 | |
---|
172 | this->numOfVertices = length; |
---|
173 | this->vertices = verticesList; |
---|
174 | |
---|
175 | |
---|
176 | if( likely(mode == 0)) |
---|
177 | { |
---|
178 | /* fist compute all the convex hull face/facelets and centroids */ |
---|
179 | for( int i = 0; i+3 < length ; i+=3) /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/ |
---|
180 | { |
---|
181 | p = verticesList[i]; |
---|
182 | q = verticesList[i + 1]; |
---|
183 | r = verticesList[i + 2]; |
---|
184 | |
---|
185 | t1 = p - q; t2 = p - r; |
---|
186 | |
---|
187 | /* finding the facelet surface via cross-product */ |
---|
188 | facelet[i] = 0.5f * fabs( t1.cross(t2).len() ); |
---|
189 | /* update the entire convex hull surface */ |
---|
190 | face += facelet[i]; |
---|
191 | |
---|
192 | /* calculate the cetroid of the hull triangles */ |
---|
193 | centroid[i] = (p + q + r) * 1/3; |
---|
194 | /* now calculate the centroid of the entire convex hull, weighted average of triangle centroids */ |
---|
195 | center += centroid[i] * facelet[i]; |
---|
196 | } |
---|
197 | /* take the average of the centroid sum */ |
---|
198 | center /= face; |
---|
199 | PRINTF(3)("-- Calculated Center\n"); |
---|
200 | |
---|
201 | |
---|
202 | /* now calculate the covariance matrix - if not written in three for-loops, it would compute faster: minor */ |
---|
203 | for( int j = 0; j < 3; ++j) |
---|
204 | { |
---|
205 | for( int k = 0; k < 3; ++k) |
---|
206 | { |
---|
207 | for( int i = 0; i + 3 < length; i+=3) |
---|
208 | { |
---|
209 | p = verticesList[i]; |
---|
210 | q = verticesList[i + 1]; |
---|
211 | r = verticesList[i + 2]; |
---|
212 | |
---|
213 | covariance[j][k] = facelet[i] / (12.0f * face) * (9.0f * centroid[i][j] * centroid[i][k] + p[j] * p[k] + |
---|
214 | q[j] * q[k] + r[j] * r[k]) - center[j] * center[k]; |
---|
215 | } |
---|
216 | } |
---|
217 | } |
---|
218 | PRINTF(3)("-- Calculated Covariance\n"); |
---|
219 | } |
---|
220 | else if( mode == 1) |
---|
221 | { |
---|
222 | for( int i = 0; i + 3 < length; i+=3) /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/ |
---|
223 | { |
---|
224 | p = verticesList[i]; |
---|
225 | q = verticesList[i + 1]; |
---|
226 | r = verticesList[i + 2]; |
---|
227 | |
---|
228 | centroid[i] = (p + q + r) / 3.0f; |
---|
229 | center += centroid[i]; |
---|
230 | } |
---|
231 | center /= length; |
---|
232 | |
---|
233 | for( int j = 0; j < 3; ++j) |
---|
234 | { |
---|
235 | for( int k = 0; k < 3; ++k) |
---|
236 | { |
---|
237 | for( int i = 0; i + 3 < length; i+=3) |
---|
238 | { |
---|
239 | p = verticesList[i]; |
---|
240 | q = verticesList[i +1]; |
---|
241 | r = verticesList[i + 2]; |
---|
242 | |
---|
243 | covariance[j][k] = p[j] * p[k] + q[j] * q[k] + r[j] + r[k]; |
---|
244 | } |
---|
245 | covariance[j][k] /= (3.0f * length); |
---|
246 | } |
---|
247 | } |
---|
248 | PRINTF(3)("-- Calculated Covariance\n"); |
---|
249 | } |
---|
250 | else if( mode == 2) |
---|
251 | { |
---|
252 | /* fist compute all the convex hull face/facelets and centroids */ |
---|
253 | for(int i = 0; i + 3 < length; i+=3) /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/ |
---|
254 | { |
---|
255 | p = verticesList[i]; |
---|
256 | q = verticesList[i + 1]; |
---|
257 | r = verticesList[i + 2]; |
---|
258 | |
---|
259 | t1 = p - q; t2 = p - r; |
---|
260 | |
---|
261 | /* finding the facelet surface via cross-product */ |
---|
262 | facelet[i] = 0.5f * fabs( t1.cross(t2).len() ); |
---|
263 | /* update the entire convex hull surface */ |
---|
264 | face += facelet[i]; |
---|
265 | |
---|
266 | /* calculate the cetroid of the hull triangles */ |
---|
267 | centroid[i] = (p + q + r) * 1/3; |
---|
268 | /* now calculate the centroid of the entire convex hull, weighted average of triangle centroids */ |
---|
269 | center += centroid[i] * facelet[i]; |
---|
270 | } |
---|
271 | /* take the average of the centroid sum */ |
---|
272 | center /= face; |
---|
273 | PRINTF(3)("-- Calculated Center\n"); |
---|
274 | |
---|
275 | for( int j = 0; j < 3; ++j) |
---|
276 | { |
---|
277 | for( int k = 0; k < 3; ++k) |
---|
278 | { |
---|
279 | for( int i = 0; i + 3 < length; i+=3) |
---|
280 | { |
---|
281 | p = verticesList[i]; |
---|
282 | q = verticesList[i +1]; |
---|
283 | r = verticesList[i + 2]; |
---|
284 | |
---|
285 | covariance[j][k] = p[j] * p[k] + q[j] * q[k] + r[j] + r[k]; |
---|
286 | } |
---|
287 | covariance[j][k] /= (3.0f * length); |
---|
288 | } |
---|
289 | } |
---|
290 | PRINTF(3)("-- Calculated Covariance\n"); |
---|
291 | } |
---|
292 | else |
---|
293 | { |
---|
294 | for( int i = 0; i < length; ++i) /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/ |
---|
295 | { |
---|
296 | center += verticesList[i]; |
---|
297 | } |
---|
298 | center /= length; |
---|
299 | |
---|
300 | for( int j = 0; j < 3; ++j) |
---|
301 | { |
---|
302 | for( int k = 0; k < 3; ++k) |
---|
303 | { |
---|
304 | for( int i = 0; i + 3 < length; i+=3) |
---|
305 | { |
---|
306 | p = verticesList[i]; |
---|
307 | q = verticesList[i +1]; |
---|
308 | r = verticesList[i + 2]; |
---|
309 | |
---|
310 | covariance[j][k] = p[j] * p[k] + q[j] * q[k] + r[j] + r[k]; |
---|
311 | } |
---|
312 | covariance[j][k] /= (3.0f * length); |
---|
313 | } |
---|
314 | } |
---|
315 | PRINTF(3)("-- Calculated Covariance\n"); |
---|
316 | } |
---|
317 | |
---|
318 | PRINTF(3)("\nVertex Data:\n"); |
---|
319 | for(int i = 0; i < length; i++) |
---|
320 | { |
---|
321 | PRINTF(3)("vertex %i: %f, %f, %f\n", i, box->vertices[i][0], box->vertices[i][1], box->vertices[i][2]); |
---|
322 | } |
---|
323 | |
---|
324 | |
---|
325 | PRINTF(3)("\nCovariance Matrix:\n"); |
---|
326 | for(int j = 0; j < 3; ++j) |
---|
327 | { |
---|
328 | PRINT(3)(" |"); |
---|
329 | for(int k = 0; k < 3; ++k) |
---|
330 | { |
---|
331 | PRINT(3)(" \b%f ", covariance[j][k]); |
---|
332 | } |
---|
333 | PRINT(3)(" |\n"); |
---|
334 | } |
---|
335 | |
---|
336 | PRINTF(3)("center: %f, %f, %f\n", center.x, center.y, center.z); |
---|
337 | |
---|
338 | |
---|
339 | for(int i = 0; i < 3; ++i) |
---|
340 | { |
---|
341 | box->covarianceMatrix[i][0] = covariance[i][0]; |
---|
342 | box->covarianceMatrix[i][1] = covariance[i][1]; |
---|
343 | box->covarianceMatrix[i][2] = covariance[i][2]; |
---|
344 | } |
---|
345 | *box->center = center; |
---|
346 | PRINTF(3)("-- Written Result to obb\n"); |
---|
347 | } |
---|
348 | |
---|
349 | |
---|
350 | |
---|
351 | void OBBTreeNode::calculateBoxEigenvectors(OBB* box, sVec3D* verticesList, int length) |
---|
352 | { |
---|
353 | |
---|
354 | /* now getting spanning vectors of the sub-space: |
---|
355 | the eigenvectors of a symmertric matrix, such as the |
---|
356 | covarience matrix are mutually orthogonal. |
---|
357 | after normalizing them, they can be used as a the basis |
---|
358 | vectors |
---|
359 | */ |
---|
360 | Vector* axis = new Vector[3]; //!< the references to the obb axis |
---|
361 | |
---|
362 | OBBTreeNode::coMat[0][0] = box->covarianceMatrix[0][0]; |
---|
363 | OBBTreeNode::coMat[0][1] = box->covarianceMatrix[0][1]; |
---|
364 | OBBTreeNode::coMat[0][2] = box->covarianceMatrix[0][2]; |
---|
365 | |
---|
366 | OBBTreeNode::coMat[1][0] = box->covarianceMatrix[1][0]; |
---|
367 | OBBTreeNode::coMat[1][1] = box->covarianceMatrix[1][1]; |
---|
368 | OBBTreeNode::coMat[1][2] = box->covarianceMatrix[1][2]; |
---|
369 | |
---|
370 | OBBTreeNode::coMat[2][0] = box->covarianceMatrix[2][0]; |
---|
371 | OBBTreeNode::coMat[2][1] = box->covarianceMatrix[2][1]; |
---|
372 | OBBTreeNode::coMat[2][2] = box->covarianceMatrix[2][2]; |
---|
373 | |
---|
374 | // OBBTreeNode::coMat[0][0] = box->covarianceMatrix[0][0]; |
---|
375 | // OBBTreeNode::coMat[0][1] = box->covarianceMatrix[0][1]; |
---|
376 | // OBBTreeNode::coMat[0][2] = box->covarianceMatrix[0][2]; |
---|
377 | // |
---|
378 | // OBBTreeNode::coMat[1][0] = box->covarianceMatrix[1][0]; |
---|
379 | // OBBTreeNode::coMat[1][1] = box->covarianceMatrix[1][1]; |
---|
380 | // OBBTreeNode::coMat[1][3] = box->covarianceMatrix[1][2]; |
---|
381 | // |
---|
382 | // OBBTreeNode::coMat[2][0] = box->covarianceMatrix[2][0]; |
---|
383 | // OBBTreeNode::coMat[2][1] = box->covarianceMatrix[2][1]; |
---|
384 | // OBBTreeNode::coMat[2][2] = box->covarianceMatrix[2][2]; |
---|
385 | |
---|
386 | |
---|
387 | /* new jacobi tests */ |
---|
388 | JacobI(OBBTreeNode::coMat, 3, OBBTreeNode::eigvlMat, OBBTreeNode::eigvMat, OBBTreeNode::rotCount); |
---|
389 | PRINTF(3)("-- Done Jacobi Decomposition\n"); |
---|
390 | |
---|
391 | |
---|
392 | // PRINTF(3)("Jacobi\n"); |
---|
393 | // for(int j = 1; j < 4; ++j) |
---|
394 | // { |
---|
395 | // PRINTF(3)(" |"); |
---|
396 | // for(int k = 1; k < 4; ++k) |
---|
397 | // { |
---|
398 | // PRINTF(3)(" \b%f ", OBBTreeNode::OBBTreeNode::eigvMat[j][k]); |
---|
399 | // } |
---|
400 | // PRINTF(3)(" |\n"); |
---|
401 | // } |
---|
402 | |
---|
403 | axis[0].x = OBBTreeNode::eigvMat[0][0]; axis[0].y = OBBTreeNode::eigvMat[1][0]; axis[0].z = OBBTreeNode::eigvMat[2][0]; |
---|
404 | axis[1].x = OBBTreeNode::eigvMat[0][1]; axis[1].y = OBBTreeNode::eigvMat[1][1]; axis[1].z = OBBTreeNode::eigvMat[2][1]; |
---|
405 | axis[2].x = OBBTreeNode::eigvMat[0][2]; axis[2].y = OBBTreeNode::eigvMat[1][2]; axis[2].z = OBBTreeNode::eigvMat[2][2]; |
---|
406 | axis[0].normalize(); |
---|
407 | axis[1].normalize(); |
---|
408 | axis[2].normalize(); |
---|
409 | box->axis = axis; |
---|
410 | |
---|
411 | PRINTF(3)("-- Got Axis\n"); |
---|
412 | |
---|
413 | PRINTF(3)("eigenvector: %f, %f, %f\n", box->axis[0].x, box->axis[0].y, box->axis[0].z); |
---|
414 | PRINTF(3)("eigenvector: %f, %f, %f\n", box->axis[1].x, box->axis[1].y, box->axis[1].z); |
---|
415 | PRINTF(3)("eigenvector: %f, %f, %f\n", box->axis[2].x, box->axis[2].y, box->axis[2].z); |
---|
416 | } |
---|
417 | |
---|
418 | |
---|
419 | void OBBTreeNode::calculateBoxAxis(OBB* box, sVec3D* verticesList, int length) |
---|
420 | { |
---|
421 | |
---|
422 | /* now get the axis length */ |
---|
423 | Line ax[3]; //!< the axis |
---|
424 | float* halfLength = new float[3]; //!< half length of the axis |
---|
425 | float tmpLength; //!< tmp save point for the length |
---|
426 | Plane p0(box->axis[0], *box->center); //!< the axis planes |
---|
427 | Plane p1(box->axis[1], *box->center); |
---|
428 | Plane p2(box->axis[2], *box->center); |
---|
429 | float maxLength[3]; |
---|
430 | float minLength[3]; |
---|
431 | |
---|
432 | |
---|
433 | /* get a bad bounding box */ |
---|
434 | halfLength[0] = -1.0f; |
---|
435 | for(int j = 0; j < length; ++j) |
---|
436 | { |
---|
437 | tmpLength = fabs(p0.distancePoint(vertices[j])); |
---|
438 | if( tmpLength > halfLength[0]) |
---|
439 | halfLength[0] = tmpLength; |
---|
440 | } |
---|
441 | |
---|
442 | halfLength[1] = -1.0f; |
---|
443 | for(int j = 0; j < length; ++j) |
---|
444 | { |
---|
445 | tmpLength = fabs(p1.distancePoint(vertices[j])); |
---|
446 | if( tmpLength > halfLength[1]) |
---|
447 | halfLength[1] = tmpLength; |
---|
448 | } |
---|
449 | |
---|
450 | halfLength[2] = -1.0f; |
---|
451 | for(int j = 0; j < length; ++j) |
---|
452 | { |
---|
453 | tmpLength = fabs(p2.distancePoint(vertices[j])); |
---|
454 | if( tmpLength > halfLength[2]) |
---|
455 | halfLength[2] = tmpLength; |
---|
456 | } |
---|
457 | |
---|
458 | |
---|
459 | |
---|
460 | /* get the maximal dimensions of the body in all directions */ |
---|
461 | maxLength[0] = p0.distancePoint(vertices[0]); |
---|
462 | minLength[0] = p0.distancePoint(vertices[0]); |
---|
463 | for(int j = 0; j < length; ++j) |
---|
464 | { |
---|
465 | tmpLength = p0.distancePoint(vertices[j]); |
---|
466 | if( tmpLength > maxLength[0]) |
---|
467 | maxLength[0] = tmpLength; |
---|
468 | else if( tmpLength < minLength[0]) |
---|
469 | minLength[0] = tmpLength; |
---|
470 | } |
---|
471 | |
---|
472 | maxLength[1] = p1.distancePoint(vertices[0]); |
---|
473 | minLength[1] = p1.distancePoint(vertices[0]); |
---|
474 | for(int j = 0; j < length; ++j) |
---|
475 | { |
---|
476 | tmpLength = p1.distancePoint(vertices[j]); |
---|
477 | if( tmpLength > maxLength[1]) |
---|
478 | maxLength[1] = tmpLength; |
---|
479 | else if( tmpLength < minLength[1]) |
---|
480 | minLength[1] = tmpLength; |
---|
481 | } |
---|
482 | |
---|
483 | maxLength[2] = p2.distancePoint(vertices[0]); |
---|
484 | minLength[2] = p2.distancePoint(vertices[0]); |
---|
485 | for(int j = 0; j < length; ++j) |
---|
486 | { |
---|
487 | tmpLength = p2.distancePoint(vertices[j]); |
---|
488 | if( tmpLength > maxLength[2]) |
---|
489 | maxLength[2] = tmpLength; |
---|
490 | else if( tmpLength < minLength[2]) |
---|
491 | minLength[2] = tmpLength; |
---|
492 | } |
---|
493 | |
---|
494 | |
---|
495 | /* calculate the real centre of the body by using the axis length */ |
---|
496 | float centerOffset[3]; |
---|
497 | float newHalfLength[3]; |
---|
498 | for(int i = 0; i < 3; ++i) |
---|
499 | { |
---|
500 | PRINTF(3)("max: %f, min: %f \n", maxLength[i], minLength[i]); |
---|
501 | centerOffset[i] = (maxLength[i] + minLength[i]) / 2.0f; // min length is negatie |
---|
502 | newHalfLength[i] = (maxLength[i] - minLength[i]) / 2.0f; // min length is negative |
---|
503 | *box->center += (box->axis[i] * centerOffset[i]); // update the new center vector |
---|
504 | halfLength[i] = newHalfLength[i]; |
---|
505 | } |
---|
506 | |
---|
507 | |
---|
508 | |
---|
509 | box->halfLength = halfLength; |
---|
510 | PRINTF(3)("-- Written Axis to obb\n"); |
---|
511 | PRINTF(3)("-- Finished Calculating Attributes\n"); |
---|
512 | |
---|
513 | } |
---|
514 | |
---|
515 | |
---|
516 | |
---|
517 | /** |
---|
518 | \brief this separates an ob-box in the middle |
---|
519 | * @param box: the box to separate |
---|
520 | |
---|
521 | this will separate the box into to smaller boxes. the separation is done along the middle of the longest axis |
---|
522 | */ |
---|
523 | void OBBTreeNode::forkBox(OBB* box) |
---|
524 | { |
---|
525 | /* get the longest axis of the box */ |
---|
526 | float aLength = -1.0f; //!< the length of the longest axis |
---|
527 | int axisIndex = 0; //!< this is the nr of the longest axis |
---|
528 | |
---|
529 | for(int i = 0; i < 3; ++i) |
---|
530 | { |
---|
531 | if( aLength < box->halfLength[i]) |
---|
532 | { |
---|
533 | aLength = box->halfLength[i]; |
---|
534 | axisIndex = i; |
---|
535 | } |
---|
536 | } |
---|
537 | |
---|
538 | PRINTF(3)("longest axis is: nr %i with a half-length of: %f\n", axisIndex, aLength); |
---|
539 | |
---|
540 | |
---|
541 | /* get the closest vertex near the center */ |
---|
542 | float dist = 999999.0f; //!< the smallest distance to each vertex |
---|
543 | float tmpDist; //!< temporary distance |
---|
544 | int vertexIndex; |
---|
545 | Plane middlePlane(box->axis[axisIndex], *box->center); //!< the middle plane |
---|
546 | |
---|
547 | vertexIndex = 0; |
---|
548 | for(int i = 0; i < box->numOfVertices; ++i) |
---|
549 | { |
---|
550 | tmpDist = fabs(middlePlane.distancePoint(box->vertices[i])); |
---|
551 | if( tmpDist < dist) |
---|
552 | { |
---|
553 | dist = tmpDist; |
---|
554 | vertexIndex = i; |
---|
555 | } |
---|
556 | } |
---|
557 | |
---|
558 | PRINTF(3)("\nthe clostest vertex is nr: %i, with a dist of: %f\n", vertexIndex ,dist); |
---|
559 | |
---|
560 | |
---|
561 | /* now definin the separation plane through this specified nearest point and partition |
---|
562 | the points depending on which side they are located |
---|
563 | */ |
---|
564 | tList<sVec3D> partition1; //!< the vertex partition 1 |
---|
565 | tList<sVec3D> partition2; //!< the vertex partition 2 |
---|
566 | |
---|
567 | |
---|
568 | PRINTF(3)("vertex index: %i, of %i\n", vertexIndex, box->numOfVertices); |
---|
569 | this->separationPlane = new Plane(box->axis[axisIndex], box->vertices[vertexIndex]); //!< separation plane |
---|
570 | this->sepPlaneCenter = &box->vertices[vertexIndex]; |
---|
571 | this->longestAxisIndex = axisIndex; |
---|
572 | |
---|
573 | for(int i = 0; i < box->numOfVertices; ++i) |
---|
574 | { |
---|
575 | if( i == vertexIndex) continue; |
---|
576 | tmpDist = this->separationPlane->distancePoint(box->vertices[i]); |
---|
577 | if( tmpDist > 0.0) |
---|
578 | partition1.add(&box->vertices[i]); /* positive numbers plus zero */ |
---|
579 | else |
---|
580 | partition2.add(&box->vertices[i]); /* negatice numbers */ |
---|
581 | } |
---|
582 | partition1.add(&box->vertices[vertexIndex]); |
---|
583 | partition2.add(&box->vertices[vertexIndex]); |
---|
584 | |
---|
585 | PRINTF(3)("\npartition1: got %i vertices/ partition 2: got %i vertices\n", partition1.getSize(), partition2.getSize()); |
---|
586 | |
---|
587 | |
---|
588 | /* now comes the separation into two different sVec3D arrays */ |
---|
589 | tIterator<sVec3D>* iterator; //!< the iterator to go through the lists |
---|
590 | sVec3D* element; //!< the elements |
---|
591 | int index; //!< index storage place |
---|
592 | sVec3D* vertList1; //!< the vertex list 1 |
---|
593 | sVec3D* vertList2; //!< the vertex list 2 |
---|
594 | |
---|
595 | vertList1 = new sVec3D[partition1.getSize()]; |
---|
596 | vertList2 = new sVec3D[partition2.getSize()]; |
---|
597 | |
---|
598 | iterator = partition1.getIterator(); |
---|
599 | element = iterator->firstElement(); |
---|
600 | index = 0; |
---|
601 | while( element != NULL) |
---|
602 | { |
---|
603 | vertList1[index][0] = element[0][0]; |
---|
604 | vertList1[index][1] = element[0][1]; |
---|
605 | vertList1[index][2] = element[0][2]; |
---|
606 | ++index; |
---|
607 | element = iterator->nextElement(); |
---|
608 | } |
---|
609 | |
---|
610 | // PRINTF(0)("\npartition 1:\n"); |
---|
611 | // for(int i = 0; i < partition1.getSize(); ++i) |
---|
612 | // { |
---|
613 | // PRINTF(0)("v[%i][0] = %f,\tv[%i][1] = %f,\tv[%i][1] = %f\n", i, vertList1[i][0], i, vertList1[i][1], i, vertList1[i][2]); |
---|
614 | // } |
---|
615 | |
---|
616 | iterator = partition2.getIterator(); |
---|
617 | element = iterator->firstElement(); |
---|
618 | index = 0; |
---|
619 | while( element != NULL) |
---|
620 | { |
---|
621 | vertList2[index][0] = element[0][0]; |
---|
622 | vertList2[index][1] = element[0][1]; |
---|
623 | vertList2[index][2] = element[0][2]; |
---|
624 | ++index; |
---|
625 | element = iterator->nextElement(); |
---|
626 | } |
---|
627 | |
---|
628 | this->tmpVert1 = vertList1; |
---|
629 | this->tmpVert2 = vertList2; |
---|
630 | this->tmpLen1 = partition1.getSize(); |
---|
631 | this->tmpLen2 = partition2.getSize(); |
---|
632 | |
---|
633 | delete iterator; |
---|
634 | |
---|
635 | // PRINTF(0)("\npartition 2:\n"); |
---|
636 | // for(int i = 0; i < partition2.getSize(); ++i) |
---|
637 | // { |
---|
638 | // PRINTF(0)("v[%i][0] = %f,\tv[%i][1] = %f,\tv[%i][1] = %f\n", i, vertList2[i][0], i, vertList2[i][1], i, vertList2[i][2]); |
---|
639 | // } |
---|
640 | } |
---|
641 | |
---|
642 | |
---|
643 | |
---|
644 | |
---|
645 | void OBBTreeNode::collideWith(BVTreeNode* treeNode, WorldEntity* nodeA, WorldEntity* nodeB) |
---|
646 | { |
---|
647 | PRINTF(3)("collideWith\n"); |
---|
648 | /* if the obb overlap, make subtests: check which node is realy overlaping */ |
---|
649 | PRINT(3)("Checking OBB %i vs %i: ", this->getIndex(), treeNode->getIndex()); |
---|
650 | if( unlikely(treeNode == NULL)) return; |
---|
651 | |
---|
652 | if( this->overlapTest(this->bvElement, ((OBBTreeNode*)treeNode)->bvElement, nodeA, nodeB)) |
---|
653 | { |
---|
654 | PRINTF(3)("collision @ lvl %i, object %s vs. %s, (%p, %p)\n", this->depth, nodeA->getClassName(), nodeB->getClassName(), this->nodeLeft, this->nodeRight); |
---|
655 | |
---|
656 | /* check if left node overlaps */ |
---|
657 | if( likely( this->nodeLeft != NULL)) |
---|
658 | { |
---|
659 | PRINT(3)("Checking OBB %i vs %i: ", this->nodeLeft->getIndex(), treeNode->getIndex()); |
---|
660 | if( this->overlapTest(this->nodeLeft->bvElement, ((OBBTreeNode*)treeNode)->bvElement, nodeA, nodeB)) |
---|
661 | { |
---|
662 | this->nodeLeft->collideWith(((OBBTreeNode*)treeNode)->nodeLeft, nodeA, nodeB); |
---|
663 | this->nodeLeft->collideWith(((OBBTreeNode*)treeNode)->nodeRight, nodeA, nodeB); |
---|
664 | } |
---|
665 | } |
---|
666 | /* check if right node overlaps */ |
---|
667 | if( likely( this->nodeRight != NULL)) |
---|
668 | { |
---|
669 | PRINT(3)("Checking OBB %i vs %i: ", this->nodeRight->getIndex(), treeNode->getIndex()); |
---|
670 | if(this->overlapTest(this->nodeRight->bvElement, ((OBBTreeNode*)treeNode)->bvElement, nodeA, nodeB)) |
---|
671 | { |
---|
672 | this->nodeRight->collideWith(((OBBTreeNode*)treeNode)->nodeLeft, nodeA, nodeB); |
---|
673 | this->nodeRight->collideWith(((OBBTreeNode*)treeNode)->nodeRight, nodeA, nodeB); |
---|
674 | } |
---|
675 | } |
---|
676 | |
---|
677 | /* so there is a collision and this is the last box in the tree (i.e. leaf) */ |
---|
678 | if( unlikely(this->nodeRight == NULL && this->nodeLeft == NULL)) |
---|
679 | { |
---|
680 | nodeA->collidesWith(nodeB, *((OBBTreeNode*)treeNode)->bvElement->center); |
---|
681 | |
---|
682 | nodeB->collidesWith(nodeA, *this->bvElement->center); |
---|
683 | } |
---|
684 | |
---|
685 | } |
---|
686 | } |
---|
687 | |
---|
688 | |
---|
689 | |
---|
690 | bool OBBTreeNode::overlapTest(OBB* boxA, OBB* boxB, WorldEntity* nodeA, WorldEntity* nodeB) |
---|
691 | { |
---|
692 | /* first check all axis */ |
---|
693 | Vector t; |
---|
694 | float rA = 0.0f; |
---|
695 | float rB = 0.0f; |
---|
696 | Vector l; |
---|
697 | Vector rotAxisA[3]; |
---|
698 | Vector rotAxisB[3]; |
---|
699 | |
---|
700 | rotAxisA[0] = nodeA->getAbsDir().apply(boxA->axis[0]); |
---|
701 | rotAxisA[1] = nodeA->getAbsDir().apply(boxA->axis[1]); |
---|
702 | rotAxisA[2] = nodeA->getAbsDir().apply(boxA->axis[2]); |
---|
703 | |
---|
704 | rotAxisB[0] = nodeB->getAbsDir().apply(boxB->axis[0]); |
---|
705 | rotAxisB[1] = nodeB->getAbsDir().apply(boxB->axis[1]); |
---|
706 | rotAxisB[2] = nodeB->getAbsDir().apply(boxB->axis[2]); |
---|
707 | |
---|
708 | t = nodeA->getAbsCoor() + nodeA->getAbsDir().apply(*boxA->center) - ( nodeB->getAbsCoor() + nodeB->getAbsDir().apply(*boxB->center)); |
---|
709 | |
---|
710 | // printf("\n"); |
---|
711 | // printf("(%f, %f, %f) -> (%f, %f, %f)\n", boxA->axis[0].x, boxA->axis[0].y, boxA->axis[0].z, rotAxisA[0].x, rotAxisA[0].y, rotAxisA[0].z); |
---|
712 | // printf("(%f, %f, %f) -> (%f, %f, %f)\n", boxA->axis[1].x, boxA->axis[1].y, boxA->axis[1].z, rotAxisA[1].x, rotAxisA[1].y, rotAxisA[1].z); |
---|
713 | // printf("(%f, %f, %f) -> (%f, %f, %f)\n", boxA->axis[2].x, boxA->axis[2].y, boxA->axis[2].z, rotAxisA[2].x, rotAxisA[2].y, rotAxisA[2].z); |
---|
714 | // |
---|
715 | // printf("(%f, %f, %f) -> (%f, %f, %f)\n", boxB->axis[0].x, boxB->axis[0].y, boxB->axis[0].z, rotAxisB[0].x, rotAxisB[0].y, rotAxisB[0].z); |
---|
716 | // printf("(%f, %f, %f) -> (%f, %f, %f)\n", boxB->axis[1].x, boxB->axis[1].y, boxB->axis[1].z, rotAxisB[1].x, rotAxisB[1].y, rotAxisB[1].z); |
---|
717 | // printf("(%f, %f, %f) -> (%f, %f, %f)\n", boxB->axis[2].x, boxB->axis[2].y, boxB->axis[2].z, rotAxisB[2].x, rotAxisB[2].y, rotAxisB[2].z); |
---|
718 | |
---|
719 | |
---|
720 | /* All 3 axis of the object A */ |
---|
721 | for( int j = 0; j < 3; ++j) |
---|
722 | { |
---|
723 | rA = 0.0f; |
---|
724 | rB = 0.0f; |
---|
725 | l = rotAxisA[j]; |
---|
726 | |
---|
727 | rA += fabs(boxA->halfLength[0] * rotAxisA[0].dot(l)); |
---|
728 | rA += fabs(boxA->halfLength[1] * rotAxisA[1].dot(l)); |
---|
729 | rA += fabs(boxA->halfLength[2] * rotAxisA[2].dot(l)); |
---|
730 | |
---|
731 | rB += fabs(boxB->halfLength[0] * rotAxisB[0].dot(l)); |
---|
732 | rB += fabs(boxB->halfLength[1] * rotAxisB[1].dot(l)); |
---|
733 | rB += fabs(boxB->halfLength[2] * rotAxisB[2].dot(l)); |
---|
734 | |
---|
735 | PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB); |
---|
736 | |
---|
737 | if( (rA + rB) < fabs(t.dot(l))) |
---|
738 | { |
---|
739 | PRINT(3)("keine Kollision\n"); |
---|
740 | return false; |
---|
741 | } |
---|
742 | } |
---|
743 | |
---|
744 | /* All 3 axis of the object B */ |
---|
745 | for( int j = 0; j < 3; ++j) |
---|
746 | { |
---|
747 | rA = 0.0f; |
---|
748 | rB = 0.0f; |
---|
749 | l = rotAxisB[j]; |
---|
750 | |
---|
751 | rA += fabs(boxA->halfLength[0] * rotAxisA[0].dot(l)); |
---|
752 | rA += fabs(boxA->halfLength[1] * rotAxisA[1].dot(l)); |
---|
753 | rA += fabs(boxA->halfLength[2] * rotAxisA[2].dot(l)); |
---|
754 | |
---|
755 | rB += fabs(boxB->halfLength[0] * rotAxisB[0].dot(l)); |
---|
756 | rB += fabs(boxB->halfLength[1] * rotAxisB[1].dot(l)); |
---|
757 | rB += fabs(boxB->halfLength[2] * rotAxisB[2].dot(l)); |
---|
758 | |
---|
759 | PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB); |
---|
760 | |
---|
761 | if( (rA + rB) < fabs(t.dot(l))) |
---|
762 | { |
---|
763 | PRINT(3)("keine Kollision\n"); |
---|
764 | return false; |
---|
765 | } |
---|
766 | } |
---|
767 | |
---|
768 | |
---|
769 | /* Now check for all face cross products */ |
---|
770 | |
---|
771 | for( int j = 0; j < 3; ++j) |
---|
772 | { |
---|
773 | for(int k = 0; k < 3; ++k ) |
---|
774 | { |
---|
775 | rA = 0.0f; |
---|
776 | rB = 0.0f; |
---|
777 | l = rotAxisA[j].cross(rotAxisB[k]); |
---|
778 | |
---|
779 | rA += fabs(boxA->halfLength[0] * rotAxisA[0].dot(l)); |
---|
780 | rA += fabs(boxA->halfLength[1] * rotAxisA[1].dot(l)); |
---|
781 | rA += fabs(boxA->halfLength[2] * rotAxisA[2].dot(l)); |
---|
782 | |
---|
783 | rB += fabs(boxB->halfLength[0] * rotAxisB[0].dot(l)); |
---|
784 | rB += fabs(boxB->halfLength[1] * rotAxisB[1].dot(l)); |
---|
785 | rB += fabs(boxB->halfLength[2] * rotAxisB[2].dot(l)); |
---|
786 | |
---|
787 | PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB); |
---|
788 | |
---|
789 | if( (rA + rB) < fabs(t.dot(l))) |
---|
790 | { |
---|
791 | PRINT(3)("keine Kollision\n"); |
---|
792 | return false; |
---|
793 | } |
---|
794 | } |
---|
795 | } |
---|
796 | |
---|
797 | |
---|
798 | boxA->bCollided = true; /* use this ONLY(!!!!) for drawing operations */ |
---|
799 | boxB->bCollided = true; |
---|
800 | PRINT(3)("Kollision!\n"); |
---|
801 | return true; |
---|
802 | } |
---|
803 | |
---|
804 | |
---|
805 | |
---|
806 | |
---|
807 | |
---|
808 | void OBBTreeNode::drawBV(int depth, int drawMode, const Vector& color, bool top) const |
---|
809 | { |
---|
810 | |
---|
811 | /* draw the model itself, there is some problem concerning this: the vertices are drawn multiple times */ |
---|
812 | if( drawMode & DRAW_MODEL || drawMode & DRAW_ALL) |
---|
813 | { |
---|
814 | if( !(drawMode & DRAW_SINGLE && depth != 0)) |
---|
815 | { |
---|
816 | if( drawMode & DRAW_POINTS) |
---|
817 | glBegin(GL_POINTS); |
---|
818 | for(int i = 0; i < this->bvElement->numOfVertices; ++i) |
---|
819 | { |
---|
820 | if( drawMode & DRAW_POINTS) |
---|
821 | glVertex3f(this->bvElement->vertices[i][0], this->bvElement->vertices[i][1], this->bvElement->vertices[i][2]); |
---|
822 | else |
---|
823 | { |
---|
824 | glPushMatrix(); |
---|
825 | glTranslatef(this->bvElement->vertices[i][0], this->bvElement->vertices[i][1], this->bvElement->vertices[i][2]); |
---|
826 | gluSphere(OBBTreeNode_sphereObj, 0.1, 10, 10); |
---|
827 | glPopMatrix(); |
---|
828 | } |
---|
829 | } |
---|
830 | if( drawMode & DRAW_POINTS) |
---|
831 | glEnd(); |
---|
832 | } |
---|
833 | } |
---|
834 | |
---|
835 | if (top) |
---|
836 | { |
---|
837 | glPushAttrib(GL_ENABLE_BIT); |
---|
838 | glDisable(GL_LIGHTING); |
---|
839 | glDisable(GL_TEXTURE_2D); |
---|
840 | glDisable(GL_BLEND); |
---|
841 | } |
---|
842 | glColor3f(color.x, color.y, color.z); |
---|
843 | |
---|
844 | |
---|
845 | /* draw world axes */ |
---|
846 | if( drawMode & DRAW_BV_AXIS) |
---|
847 | { |
---|
848 | glBegin(GL_LINES); |
---|
849 | glColor3f(1.0, 0.0, 0.0); |
---|
850 | glVertex3f(0.0, 0.0, 0.0); |
---|
851 | glVertex3f(3.0, 0.0, 0.0); |
---|
852 | |
---|
853 | glColor3f(0.0, 1.0, 0.0); |
---|
854 | glVertex3f(0.0, 0.0, 0.0); |
---|
855 | glVertex3f(0.0, 3.0, 0.0); |
---|
856 | |
---|
857 | glColor3f(0.0, 0.0, 1.0); |
---|
858 | glVertex3f(0.0, 0.0, 0.0); |
---|
859 | glVertex3f(0.0, 0.0, 3.0); |
---|
860 | glEnd(); |
---|
861 | } |
---|
862 | |
---|
863 | |
---|
864 | if( drawMode & DRAW_BV_AXIS || drawMode & DRAW_ALL) |
---|
865 | { |
---|
866 | if( !(drawMode & DRAW_SINGLE && depth != 0)) |
---|
867 | { |
---|
868 | /* draw the obb axes */ |
---|
869 | glBegin(GL_LINES); |
---|
870 | glColor3f(0.0, 0.4, 0.3); |
---|
871 | glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z); |
---|
872 | glVertex3f(this->bvElement->center->x + this->bvElement->axis[0].x * this->bvElement->halfLength[0], |
---|
873 | this->bvElement->center->y + this->bvElement->axis[0].y * this->bvElement->halfLength[0], |
---|
874 | this->bvElement->center->z + this->bvElement->axis[0].z * this->bvElement->halfLength[0]); |
---|
875 | |
---|
876 | glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z); |
---|
877 | glVertex3f(this->bvElement->center->x + this->bvElement->axis[1].x * this->bvElement->halfLength[1], |
---|
878 | this->bvElement->center->y + this->bvElement->axis[1].y * this->bvElement->halfLength[1], |
---|
879 | this->bvElement->center->z + this->bvElement->axis[1].z * this->bvElement->halfLength[1]); |
---|
880 | |
---|
881 | glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z); |
---|
882 | glVertex3f(this->bvElement->center->x + this->bvElement->axis[2].x * this->bvElement->halfLength[2], |
---|
883 | this->bvElement->center->y + this->bvElement->axis[2].y * this->bvElement->halfLength[2], |
---|
884 | this->bvElement->center->z + this->bvElement->axis[2].z * this->bvElement->halfLength[2]); |
---|
885 | glEnd(); |
---|
886 | } |
---|
887 | } |
---|
888 | |
---|
889 | |
---|
890 | /* DRAW POLYGONS */ |
---|
891 | if( drawMode & DRAW_BV_POLYGON || drawMode & DRAW_ALL || drawMode & DRAW_BV_BLENDED) |
---|
892 | { |
---|
893 | if(this->nodeLeft == NULL || this->nodeRight == NULL) |
---|
894 | depth = 0; |
---|
895 | if( !(drawMode & DRAW_SINGLE && depth != 0)) |
---|
896 | { |
---|
897 | Vector cen = *this->bvElement->center; |
---|
898 | Vector* axis = this->bvElement->axis; |
---|
899 | float* len = this->bvElement->halfLength; |
---|
900 | |
---|
901 | if( this->bvElement->bCollided) |
---|
902 | { |
---|
903 | glColor3f(1.0, 1.0, 1.0); // COLLISION COLOR |
---|
904 | } |
---|
905 | else if( drawMode & DRAW_BV_BLENDED) |
---|
906 | { |
---|
907 | glColor4f(color.x, color.y, color.z, .5); |
---|
908 | } |
---|
909 | |
---|
910 | |
---|
911 | |
---|
912 | /* draw bounding box */ |
---|
913 | if( drawMode & DRAW_BV_BLENDED) |
---|
914 | glBegin(GL_QUADS); |
---|
915 | else |
---|
916 | glBegin(GL_LINE_LOOP); |
---|
917 | glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], |
---|
918 | cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], |
---|
919 | cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); |
---|
920 | glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], |
---|
921 | cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], |
---|
922 | cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); |
---|
923 | glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], |
---|
924 | cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], |
---|
925 | cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); |
---|
926 | glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], |
---|
927 | cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], |
---|
928 | cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); |
---|
929 | glEnd(); |
---|
930 | |
---|
931 | if( drawMode & DRAW_BV_BLENDED) |
---|
932 | glBegin(GL_QUADS); |
---|
933 | else |
---|
934 | glBegin(GL_LINE_LOOP); |
---|
935 | glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], |
---|
936 | cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], |
---|
937 | cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); |
---|
938 | glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], |
---|
939 | cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], |
---|
940 | cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); |
---|
941 | glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], |
---|
942 | cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], |
---|
943 | cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); |
---|
944 | glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], |
---|
945 | cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], |
---|
946 | cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); |
---|
947 | glEnd(); |
---|
948 | |
---|
949 | if( drawMode & DRAW_BV_BLENDED) |
---|
950 | glBegin(GL_QUADS); |
---|
951 | else |
---|
952 | glBegin(GL_LINE_LOOP); |
---|
953 | glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], |
---|
954 | cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], |
---|
955 | cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); |
---|
956 | glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], |
---|
957 | cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], |
---|
958 | cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); |
---|
959 | glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], |
---|
960 | cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], |
---|
961 | cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); |
---|
962 | glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], |
---|
963 | cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], |
---|
964 | cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); |
---|
965 | glEnd(); |
---|
966 | |
---|
967 | if( drawMode & DRAW_BV_BLENDED) |
---|
968 | glBegin(GL_QUADS); |
---|
969 | else |
---|
970 | glBegin(GL_LINE_LOOP); |
---|
971 | glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], |
---|
972 | cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], |
---|
973 | cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); |
---|
974 | glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], |
---|
975 | cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], |
---|
976 | cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); |
---|
977 | glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], |
---|
978 | cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], |
---|
979 | cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); |
---|
980 | glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], |
---|
981 | cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], |
---|
982 | cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); |
---|
983 | glEnd(); |
---|
984 | |
---|
985 | |
---|
986 | if( drawMode & DRAW_BV_BLENDED) |
---|
987 | { |
---|
988 | glBegin(GL_QUADS); |
---|
989 | glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], |
---|
990 | cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], |
---|
991 | cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); |
---|
992 | glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], |
---|
993 | cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], |
---|
994 | cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); |
---|
995 | glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], |
---|
996 | cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], |
---|
997 | cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); |
---|
998 | glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], |
---|
999 | cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], |
---|
1000 | cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); |
---|
1001 | glEnd(); |
---|
1002 | |
---|
1003 | glBegin(GL_QUADS); |
---|
1004 | glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], |
---|
1005 | cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], |
---|
1006 | cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); |
---|
1007 | glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], |
---|
1008 | cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], |
---|
1009 | cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); |
---|
1010 | glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], |
---|
1011 | cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], |
---|
1012 | cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); |
---|
1013 | glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], |
---|
1014 | cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], |
---|
1015 | cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); |
---|
1016 | glEnd(); |
---|
1017 | } |
---|
1018 | |
---|
1019 | |
---|
1020 | if( drawMode & DRAW_BV_BLENDED) |
---|
1021 | glColor3f(color.x, color.y, color.z); |
---|
1022 | } |
---|
1023 | |
---|
1024 | } |
---|
1025 | |
---|
1026 | /* DRAW SEPARATING PLANE */ |
---|
1027 | if( drawMode & DRAW_SEPARATING_PLANE || drawMode & DRAW_ALL) |
---|
1028 | { |
---|
1029 | if( !(drawMode & DRAW_SINGLE && depth != 0)) |
---|
1030 | { |
---|
1031 | if( drawMode & DRAW_BV_BLENDED) |
---|
1032 | glColor4f(color.x, color.y, color.z, .6); |
---|
1033 | |
---|
1034 | /* now draw the separation plane */ |
---|
1035 | Vector a1 = this->bvElement->axis[(this->longestAxisIndex + 1)%3]; |
---|
1036 | Vector a2 = this->bvElement->axis[(this->longestAxisIndex + 2)%3]; |
---|
1037 | Vector c = *this->bvElement->center; |
---|
1038 | float l1 = this->bvElement->halfLength[(this->longestAxisIndex + 1)%3]; |
---|
1039 | float l2 = this->bvElement->halfLength[(this->longestAxisIndex + 2)%3]; |
---|
1040 | glBegin(GL_QUADS); |
---|
1041 | glVertex3f(c.x + a1.x * l1 + a2.x * l2, c.y + a1.y * l1+ a2.y * l2, c.z + a1.z * l1 + a2.z * l2); |
---|
1042 | glVertex3f(c.x - a1.x * l1 + a2.x * l2, c.y - a1.y * l1+ a2.y * l2, c.z - a1.z * l1 + a2.z * l2); |
---|
1043 | glVertex3f(c.x - a1.x * l1 - a2.x * l2, c.y - a1.y * l1- a2.y * l2, c.z - a1.z * l1 - a2.z * l2); |
---|
1044 | glVertex3f(c.x + a1.x * l1 - a2.x * l2, c.y + a1.y * l1- a2.y * l2, c.z + a1.z * l1 - a2.z * l2); |
---|
1045 | glEnd(); |
---|
1046 | |
---|
1047 | if( drawMode & DRAW_BV_BLENDED) |
---|
1048 | glColor4f(color.x, color.y, color.z, 1.0); |
---|
1049 | |
---|
1050 | } |
---|
1051 | } |
---|
1052 | |
---|
1053 | |
---|
1054 | |
---|
1055 | if (depth > 0) |
---|
1056 | { |
---|
1057 | Vector childColor = Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(20,0,.0)); |
---|
1058 | if( this->nodeLeft != NULL) |
---|
1059 | this->nodeLeft->drawBV(depth - 1, drawMode, childColor, false); |
---|
1060 | if( this->nodeRight != NULL) |
---|
1061 | this->nodeRight->drawBV(depth - 1, drawMode, childColor, false); |
---|
1062 | } |
---|
1063 | this->bvElement->bCollided = false; |
---|
1064 | |
---|
1065 | if (top) |
---|
1066 | glPopAttrib(); |
---|
1067 | } |
---|
1068 | |
---|
1069 | |
---|
1070 | |
---|
1071 | void OBBTreeNode::debug() const |
---|
1072 | { |
---|
1073 | |
---|
1074 | /* |
---|
1075 | for(int i = 0; i < length; i++) |
---|
1076 | { |
---|
1077 | PRINTF(3)("vertex %i: %f, %f, %f\n", i, verticesList[i][0], verticesList[i][1], verticesList[i][2]); |
---|
1078 | } |
---|
1079 | */ |
---|
1080 | } |
---|