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