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