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