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