1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Patrick Boenzli |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION |
---|
17 | |
---|
18 | #include "obb_tree_node.h" |
---|
19 | #include "list.h" |
---|
20 | #include "obb.h" |
---|
21 | #include "obb_tree.h" |
---|
22 | #include "vector.h" |
---|
23 | #include "abstract_model.h" |
---|
24 | |
---|
25 | #include <math.h> |
---|
26 | |
---|
27 | |
---|
28 | #define WANT_STREAM |
---|
29 | #define WANT_MATH |
---|
30 | #define WANT_FSTREAM |
---|
31 | |
---|
32 | #include "stdincl.h" |
---|
33 | |
---|
34 | #include "include.h" |
---|
35 | #include "newmat.h" |
---|
36 | #include "newmatap.h" |
---|
37 | #include "newmatio.h" |
---|
38 | |
---|
39 | #include "lin_alg.h" |
---|
40 | |
---|
41 | |
---|
42 | |
---|
43 | |
---|
44 | using namespace std; |
---|
45 | |
---|
46 | OBBTree* OBBTreeNode::obbTree = NULL; |
---|
47 | |
---|
48 | float** OBBTreeNode::coMat = NULL; |
---|
49 | float** OBBTreeNode::eigvMat = NULL; |
---|
50 | float* OBBTreeNode::eigvlMat = NULL; |
---|
51 | int* OBBTreeNode::rotCount = NULL; |
---|
52 | |
---|
53 | /** |
---|
54 | \brief standard constructor |
---|
55 | */ |
---|
56 | OBBTreeNode::OBBTreeNode () |
---|
57 | { |
---|
58 | this->setClassID(CL_OBB_TREE_NODE, "OBBTreeNode"); |
---|
59 | this->nodeLeft = NULL; |
---|
60 | this->nodeRight = NULL; |
---|
61 | |
---|
62 | if(coMat == NULL) |
---|
63 | { |
---|
64 | coMat = new float*[4]; |
---|
65 | for(int i = 0; i < 4; i++) |
---|
66 | coMat[i] = new float[4]; |
---|
67 | } |
---|
68 | if(eigvMat == NULL) |
---|
69 | { |
---|
70 | eigvMat = new float*[4]; |
---|
71 | for(int i = 0; i < 4; i++) |
---|
72 | eigvMat[i] = new float[4]; |
---|
73 | } |
---|
74 | if( eigvlMat == NULL) |
---|
75 | { |
---|
76 | eigvlMat = new float[4]; |
---|
77 | } |
---|
78 | if( rotCount == NULL) |
---|
79 | rotCount = new int; |
---|
80 | |
---|
81 | this->sphereObj = gluNewQuadric(); |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | /** |
---|
86 | \brief standard deconstructor |
---|
87 | */ |
---|
88 | OBBTreeNode::~OBBTreeNode () |
---|
89 | { |
---|
90 | // delete what has to be deleted here |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | |
---|
95 | /** |
---|
96 | \brief creates a new BVTree or BVTree partition |
---|
97 | \param depth: how much more depth-steps to go: if == 1 don't go any deeper! |
---|
98 | \param verticesList: the list of vertices of the object - each vertices triple is interpreted as a triangle |
---|
99 | */ |
---|
100 | void OBBTreeNode::spawnBVTree(const int depth, sVec3D *verticesList, const int length) |
---|
101 | { |
---|
102 | PRINT(0)("\n"); |
---|
103 | this->treeIndex = this->obbTree->getID(); |
---|
104 | PRINTF(0)("OBB Depth: %i, tree index: %i, numVertices: %i\n", depth, treeIndex, length); |
---|
105 | this->depth = depth; |
---|
106 | |
---|
107 | |
---|
108 | this->bvElement = new OBB(); |
---|
109 | this->bvElement->vertices = verticesList; |
---|
110 | this->bvElement->numOfVertices = length; |
---|
111 | PRINTF(3)("Created OBBox\n"); |
---|
112 | this->calculateBoxCovariance(this->bvElement, verticesList, length); |
---|
113 | PRINTF(3)("Calculated attributes1\n"); |
---|
114 | this->calculateBoxEigenvectors(this->bvElement, verticesList, length); |
---|
115 | PRINTF(3)("Calculated attributes2\n"); |
---|
116 | this->calculateBoxAxis(this->bvElement, verticesList, length); |
---|
117 | PRINTF(3)("Calculated attributes3\n"); |
---|
118 | |
---|
119 | |
---|
120 | if( likely( this->depth > 0)) |
---|
121 | { |
---|
122 | this->forkBox(this->bvElement); |
---|
123 | |
---|
124 | |
---|
125 | if(this->tmpLen1 > 2) |
---|
126 | { |
---|
127 | OBBTreeNode* node1 = new OBBTreeNode(); |
---|
128 | this->nodeLeft = node1; |
---|
129 | this->nodeLeft->spawnBVTree(depth - 1, this->tmpVert1, this->tmpLen1); |
---|
130 | } |
---|
131 | else |
---|
132 | { |
---|
133 | PRINTF(0)("Aboarding tree walk: less than 3 vertices left\n"); |
---|
134 | } |
---|
135 | |
---|
136 | if( this->tmpLen2 > 2) |
---|
137 | { |
---|
138 | OBBTreeNode* node2 = new OBBTreeNode(); |
---|
139 | this->nodeRight = node2; |
---|
140 | this->nodeRight->spawnBVTree(depth - 1, this->tmpVert2, this->tmpLen2); |
---|
141 | } |
---|
142 | else |
---|
143 | { |
---|
144 | PRINTF(0)("Aboarding tree walk: less than 3 vertices left\n"); |
---|
145 | } |
---|
146 | |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | |
---|
152 | void OBBTreeNode::calculateBoxCovariance(OBB* box, sVec3D* verticesList, int length) |
---|
153 | { |
---|
154 | float facelet[length]; //!< surface area of the i'th triangle of the convex hull |
---|
155 | float face; //!< surface area of the entire convex hull |
---|
156 | Vector centroid[length]; //!< centroid of the i'th convex hull |
---|
157 | Vector center; //!< the center of the entire hull |
---|
158 | Vector p, q, r; //!< holder of the polygon data, much more conveniant to work with Vector than sVec3d |
---|
159 | Vector t1, t2; //!< temporary values |
---|
160 | float covariance[3][3]; //!< the covariance matrix |
---|
161 | |
---|
162 | this->numOfVertices = length; |
---|
163 | this->vertices = verticesList; |
---|
164 | |
---|
165 | |
---|
166 | |
---|
167 | |
---|
168 | /* fist compute all the convex hull face/facelets and centroids */ |
---|
169 | for(int i = 0; i < length; i+=3) /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/ |
---|
170 | { |
---|
171 | p = verticesList[i]; |
---|
172 | q = verticesList[i +1]; |
---|
173 | r = verticesList[i + 2]; |
---|
174 | |
---|
175 | t1 = p - q; t2 = p - r; |
---|
176 | |
---|
177 | /* finding the facelet surface via cross-product */ |
---|
178 | facelet[i] = 0.5f * fabs( t1.cross(t2).len() ); |
---|
179 | /* update the entire convex hull surface */ |
---|
180 | face += facelet[i]; |
---|
181 | |
---|
182 | /* calculate the cetroid of the hull triangles */ |
---|
183 | centroid[i] = (p + q + r) * 1/3; |
---|
184 | /* now calculate the centroid of the entire convex hull, weighted average of triangle centroids */ |
---|
185 | center += centroid[i] * facelet[i]; |
---|
186 | } |
---|
187 | /* take the average of the centroid sum */ |
---|
188 | center /= face; |
---|
189 | PRINTF(3)("-- Calculated Center\n"); |
---|
190 | |
---|
191 | |
---|
192 | /* now calculate the covariance matrix - if not written in three for-loops, it would compute faster: minor */ |
---|
193 | for(int j = 0; j < 3; ++j) |
---|
194 | { |
---|
195 | for(int k = 0; k < 3; ++k) |
---|
196 | { |
---|
197 | for(int i = 0; i < length; i+=3) |
---|
198 | { |
---|
199 | p = verticesList[i]; |
---|
200 | q = verticesList[i +1]; |
---|
201 | r = verticesList[i + 2]; |
---|
202 | |
---|
203 | covariance[j][k] = facelet[i] / (12.0f * face) * (9.0f * centroid[i][j] * centroid[i][k] + p[j]* p[k] + |
---|
204 | q[j] * q[k] + r[j]*r[k]) - center[j] * center[k]; |
---|
205 | } |
---|
206 | } |
---|
207 | } |
---|
208 | PRINTF(3)("-- Calculated Covariance\n"); |
---|
209 | |
---|
210 | |
---|
211 | PRINTF(0)("\nVertex Data:\n"); |
---|
212 | for(int i = 0; i < length; i++) |
---|
213 | { |
---|
214 | //PRINTF(0)("vertex %i: %f, %f, %f\n", i, verticesList[i][0], verticesList[i][1], verticesList[i][2]); |
---|
215 | PRINTF(0)("vertex %i: %f, %f, %f\n", i, box->vertices[i][0], box->vertices[i][1], box->vertices[i][2]); |
---|
216 | } |
---|
217 | |
---|
218 | // PRINTF(3)("\nCovariance Matrix:\n"); |
---|
219 | // for(int j = 0; j < 3; ++j) |
---|
220 | // { |
---|
221 | // PRINTF(3)(" |"); |
---|
222 | // for(int k = 0; k < 3; ++k) |
---|
223 | // { |
---|
224 | // PRINTF(3)(" \b%f ", covariance[j][k]); |
---|
225 | // } |
---|
226 | // PRINTF(3)(" |\n"); |
---|
227 | // } |
---|
228 | PRINTF(3)("center: %f, %f, %f\n", center.x, center.y, center.z); |
---|
229 | |
---|
230 | |
---|
231 | // for(int i = 0; i < 3; ++i) |
---|
232 | // { |
---|
233 | // box->covarianceMatrix[i][0] = covariance[i][0]; |
---|
234 | // box->covarianceMatrix[i][1] = covariance[i][1]; |
---|
235 | // box->covarianceMatrix[i][3] = covariance[i][2]; |
---|
236 | // } |
---|
237 | *box->center = center; |
---|
238 | PRINTF(3)("-- Written Result to obb\n"); |
---|
239 | } |
---|
240 | |
---|
241 | |
---|
242 | |
---|
243 | void OBBTreeNode::calculateBoxEigenvectors(OBB* box, sVec3D* verticesList, int length) |
---|
244 | { |
---|
245 | |
---|
246 | /* now getting spanning vectors of the sub-space: |
---|
247 | the eigenvectors of a symmertric matrix, such as the |
---|
248 | covarience matrix are mutually orthogonal. |
---|
249 | after normalizing them, they can be used as a the basis |
---|
250 | vectors |
---|
251 | */ |
---|
252 | Vector** axis = new Vector*[3]; //!< the references to the obb axis |
---|
253 | |
---|
254 | coMat[1][1] = box->covarianceMatrix[0][0]; coMat[1][2] = box->covarianceMatrix[0][1]; coMat[1][3] = box->covarianceMatrix[0][2]; |
---|
255 | coMat[2][1] = box->covarianceMatrix[1][0]; coMat[2][2] = box->covarianceMatrix[1][1]; coMat[2][3] = box->covarianceMatrix[1][2]; |
---|
256 | coMat[3][1] = box->covarianceMatrix[2][0]; coMat[3][2] = box->covarianceMatrix[2][1]; coMat[3][3] = box->covarianceMatrix[2][2]; |
---|
257 | |
---|
258 | /* new jacobi tests */ |
---|
259 | JacobI(coMat, 3, eigvlMat, eigvMat, rotCount); |
---|
260 | PRINTF(3)("-- Done Jacobi Decomposition\n"); |
---|
261 | |
---|
262 | |
---|
263 | // PRINTF(3)("Jacobi\n"); |
---|
264 | // for(int j = 1; j < 4; ++j) |
---|
265 | // { |
---|
266 | // PRINTF(3)(" |"); |
---|
267 | // for(int k = 1; k < 4; ++k) |
---|
268 | // { |
---|
269 | // PRINTF(3)(" \b%f ", eigvMat[j][k]); |
---|
270 | // } |
---|
271 | // PRINTF(3)(" |\n"); |
---|
272 | // } |
---|
273 | |
---|
274 | axis[0] = new Vector(eigvMat[1][1], eigvMat[2][1], eigvMat[3][1]); |
---|
275 | axis[1] = new Vector(eigvMat[1][2], eigvMat[2][2], eigvMat[3][2]); |
---|
276 | axis[2] = new Vector(eigvMat[1][3], eigvMat[2][3], eigvMat[3][3]); |
---|
277 | box->axis = axis; |
---|
278 | PRINTF(3)("-- Got Axis\n"); |
---|
279 | |
---|
280 | PRINTF(0)("eigenvector: %f, %f, %f\n", box->axis[0]->x, box->axis[0]->y, box->axis[0]->z); |
---|
281 | PRINTF(0)("eigenvector: %f, %f, %f\n", box->axis[1]->x, box->axis[1]->y, box->axis[1]->z); |
---|
282 | PRINTF(0)("eigenvector: %f, %f, %f\n", box->axis[2]->x, box->axis[2]->y, box->axis[2]->z); |
---|
283 | } |
---|
284 | |
---|
285 | |
---|
286 | void OBBTreeNode::calculateBoxAxis(OBB* box, sVec3D* verticesList, int length) |
---|
287 | { |
---|
288 | |
---|
289 | /* now get the axis length */ |
---|
290 | Line ax[3]; //!< the axis |
---|
291 | float* halfLength = new float[3]; //!< half length of the axis |
---|
292 | float tmpLength; //!< tmp save point for the length |
---|
293 | Plane p0(*box->axis[0], *box->center); //!< the axis planes |
---|
294 | Plane p1(*box->axis[1], *box->center); |
---|
295 | Plane p2(*box->axis[2], *box->center); |
---|
296 | |
---|
297 | halfLength[0] = -1.0f; |
---|
298 | for(int j = 0; j < length; ++j) |
---|
299 | { |
---|
300 | tmpLength = fabs(p0.distancePoint(vertices[j])); |
---|
301 | if( tmpLength > halfLength[0]) |
---|
302 | halfLength[0] = tmpLength; |
---|
303 | } |
---|
304 | |
---|
305 | halfLength[1] = -1.0f; |
---|
306 | for(int j = 0; j < length; ++j) |
---|
307 | { |
---|
308 | tmpLength = fabs(p1.distancePoint(vertices[j])); |
---|
309 | if( tmpLength > halfLength[1]) |
---|
310 | halfLength[1] = tmpLength; |
---|
311 | } |
---|
312 | |
---|
313 | halfLength[2] = -1.0f; |
---|
314 | for(int j = 0; j < length; ++j) |
---|
315 | { |
---|
316 | tmpLength = fabs(p2.distancePoint(vertices[j])); |
---|
317 | if( tmpLength > halfLength[2]) |
---|
318 | halfLength[2] = tmpLength; |
---|
319 | } |
---|
320 | |
---|
321 | box->halfLength = halfLength; |
---|
322 | PRINTF(3)("-- Written Axis to obb\n"); |
---|
323 | PRINTF(3)("-- Finished Calculating Attributes\n"); |
---|
324 | |
---|
325 | |
---|
326 | |
---|
327 | // PRINTF(3)("\nwe got length: \n"); |
---|
328 | for(int i = 0; i < 3; ++i) |
---|
329 | { |
---|
330 | //if( box->halfLength[i] == 0.0) |
---|
331 | PRINTF(0)("length[%i] = %f\n", i, box->halfLength[i]); |
---|
332 | } |
---|
333 | } |
---|
334 | |
---|
335 | |
---|
336 | |
---|
337 | /** |
---|
338 | \brief this separates an ob-box in the middle |
---|
339 | \param box: the box to separate |
---|
340 | |
---|
341 | this will separate the box into to smaller boxes. the separation is done along the middle of the longest axis |
---|
342 | */ |
---|
343 | void OBBTreeNode::forkBox(OBB* box) |
---|
344 | { |
---|
345 | /* get the longest axis of the box */ |
---|
346 | float aLength = -1.0f; //!< the length of the longest axis |
---|
347 | int axisIndex = 0; //!< this is the nr of the longest axis |
---|
348 | |
---|
349 | for(int i = 0; i < 3; ++i) |
---|
350 | { |
---|
351 | if( aLength < box->halfLength[i]) |
---|
352 | { |
---|
353 | aLength = box->halfLength[i]; |
---|
354 | axisIndex = i; |
---|
355 | } |
---|
356 | } |
---|
357 | |
---|
358 | PRINTF(0)("longest axis is: nr %i with a half-length of: %f\n", axisIndex, aLength); |
---|
359 | |
---|
360 | |
---|
361 | /* get the closest vertex near the center */ |
---|
362 | float dist = 999999.0f; //!< the smallest distance to each vertex |
---|
363 | float tmpDist; //!< temporary distance |
---|
364 | int vertexIndex; |
---|
365 | Plane middlePlane(*box->axis[axisIndex], *box->center); //!< the middle plane |
---|
366 | |
---|
367 | for(int i = 0; i < box->numOfVertices; ++i) |
---|
368 | { |
---|
369 | tmpDist = fabs(middlePlane.distancePoint(box->vertices[i])); |
---|
370 | if( tmpDist < dist) |
---|
371 | { |
---|
372 | dist = tmpDist; |
---|
373 | vertexIndex = i; |
---|
374 | } |
---|
375 | } |
---|
376 | |
---|
377 | // PRINTF(3)("\nthe clostest vertex is nr: %i, with a dist of: %f\n", vertexIndex ,dist); |
---|
378 | |
---|
379 | |
---|
380 | /* now definin the separation plane through this specified nearest point and partition |
---|
381 | the points depending on which side they are located |
---|
382 | */ |
---|
383 | tList<sVec3D> partition1; //!< the vertex partition 1 |
---|
384 | tList<sVec3D> partition2; //!< the vertex partition 2 |
---|
385 | |
---|
386 | |
---|
387 | this->separationPlane = new Plane(*box->axis[axisIndex], box->vertices[vertexIndex]); //!< separation plane |
---|
388 | this->sepPlaneCenter = &box->vertices[vertexIndex]; |
---|
389 | this->longestAxisIndex = axisIndex; |
---|
390 | |
---|
391 | for(int i = 0; i < box->numOfVertices; ++i) |
---|
392 | { |
---|
393 | if( this->separationPlane->distancePoint(box->vertices[i]) > 0.0f) |
---|
394 | partition1.add(&box->vertices[i]); |
---|
395 | else |
---|
396 | partition2.add(&box->vertices[i]); |
---|
397 | } |
---|
398 | partition1.add(&box->vertices[vertexIndex]); |
---|
399 | |
---|
400 | // PRINTF(3)("\npartition1: got %i vertices/ partition 2: got %i vertices\n", partition1.getSize(), partition2.getSize()); |
---|
401 | |
---|
402 | |
---|
403 | /* now comes the separation into two different sVec3D arrays */ |
---|
404 | tIterator<sVec3D>* iterator; //!< the iterator to go through the lists |
---|
405 | sVec3D* element; //!< the elements |
---|
406 | int index; //!< index storage place |
---|
407 | sVec3D* vertList1; //!< the vertex list 1 |
---|
408 | sVec3D* vertList2; //!< the vertex list 2 |
---|
409 | |
---|
410 | vertList1 = new sVec3D[partition1.getSize()]; |
---|
411 | vertList2 = new sVec3D[partition2.getSize()]; |
---|
412 | |
---|
413 | iterator = partition1.getIterator(); |
---|
414 | element = iterator->nextElement(); |
---|
415 | index = 0; |
---|
416 | while( element != NULL) |
---|
417 | { |
---|
418 | vertList1[index][0] = element[0][0]; |
---|
419 | vertList1[index][1] = element[0][1]; |
---|
420 | vertList1[index][2] = element[0][2]; |
---|
421 | ++index; |
---|
422 | element = iterator->nextElement(); |
---|
423 | } |
---|
424 | |
---|
425 | // PRINTF(0)("\npartition 1:\n"); |
---|
426 | // for(int i = 0; i < partition1.getSize(); ++i) |
---|
427 | // { |
---|
428 | // 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]); |
---|
429 | // } |
---|
430 | |
---|
431 | iterator = partition2.getIterator(); |
---|
432 | element = iterator->nextElement(); |
---|
433 | index = 0; |
---|
434 | while( element != NULL) |
---|
435 | { |
---|
436 | vertList2[index][0] = element[0][0]; |
---|
437 | vertList2[index][1] = element[0][1]; |
---|
438 | vertList2[index][2] = element[0][2]; |
---|
439 | ++index; |
---|
440 | element = iterator->nextElement(); |
---|
441 | } |
---|
442 | |
---|
443 | this->tmpVert1 = vertList1; |
---|
444 | this->tmpVert2 = vertList2; |
---|
445 | this->tmpLen1 = partition1.getSize(); |
---|
446 | this->tmpLen2 = partition2.getSize(); |
---|
447 | |
---|
448 | delete iterator; |
---|
449 | |
---|
450 | // PRINTF(0)("\npartition 2:\n"); |
---|
451 | // for(int i = 0; i < partition2.getSize(); ++i) |
---|
452 | // { |
---|
453 | // 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]); |
---|
454 | // } |
---|
455 | } |
---|
456 | |
---|
457 | |
---|
458 | |
---|
459 | |
---|
460 | void OBBTreeNode::collideWith(const BVTree &tree) |
---|
461 | {} |
---|
462 | |
---|
463 | |
---|
464 | |
---|
465 | |
---|
466 | void OBBTreeNode::drawBV(int depth, int drawMode) const |
---|
467 | { |
---|
468 | this->obbTree->getMaterial(treeIndex)->select(); |
---|
469 | |
---|
470 | /* draw the model itself, there is some problem concerning this: the vertices are drawn multiple times */ |
---|
471 | if( drawMode & DRAW_MODEL || drawMode & DRAW_ALL) |
---|
472 | { |
---|
473 | if( !(drawMode & DRAW_SINGLE && depth != 0)) |
---|
474 | { |
---|
475 | glBegin(GL_LINE_STRIP); |
---|
476 | for(int i = 0; i < this->bvElement->numOfVertices; ++i) |
---|
477 | { |
---|
478 | glPushMatrix(); |
---|
479 | glMatrixMode(GL_MODELVIEW); |
---|
480 | //glVertex3f(this->bvElement->vertices[i][0], this->bvElement->vertices[i][1], this->bvElement->vertices[i][2]); |
---|
481 | glTranslatef(this->bvElement->vertices[i][0], this->bvElement->vertices[i][1], this->bvElement->vertices[i][2]); |
---|
482 | gluSphere(this->sphereObj, 1, 10, 10); |
---|
483 | //PRINTF(0)("v(%f, %f, %f)\n", this->vertices[i][0], this->vertices[i][1], this->vertices[i][2]); |
---|
484 | glPopMatrix(); |
---|
485 | } |
---|
486 | glEnd(); |
---|
487 | } |
---|
488 | } |
---|
489 | |
---|
490 | |
---|
491 | /* draw world axes */ |
---|
492 | // glBegin(GL_LINES); |
---|
493 | // glColor3f(0.0, 0.4, 0.3); |
---|
494 | // glVertex3f(0.0, 0.0, 0.0); |
---|
495 | // glVertex3f(3.0, 0.0, 0.0); |
---|
496 | // |
---|
497 | // glVertex3f(0.0, 0.0, 0.0); |
---|
498 | // glVertex3f(0.0, 3.0, 0.0); |
---|
499 | // |
---|
500 | // glVertex3f(0.0, 0.0, 0.0); |
---|
501 | // glVertex3f(0.0, 0.0, 3.0); |
---|
502 | // glEnd(); |
---|
503 | |
---|
504 | |
---|
505 | if( drawMode & DRAW_BV_AXIS || drawMode & DRAW_ALL) |
---|
506 | { |
---|
507 | if( !(drawMode & DRAW_SINGLE && depth != 0)) |
---|
508 | { |
---|
509 | /* draw the obb axes */ |
---|
510 | glBegin(GL_LINES); |
---|
511 | glColor3f(0.0, 0.4, 0.3); |
---|
512 | glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z); |
---|
513 | glVertex3f(this->bvElement->center->x + this->bvElement->axis[0]->x * this->bvElement->halfLength[0], |
---|
514 | this->bvElement->center->y + this->bvElement->axis[0]->y * this->bvElement->halfLength[0], |
---|
515 | this->bvElement->center->z + this->bvElement->axis[0]->z * this->bvElement->halfLength[0]); |
---|
516 | |
---|
517 | glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z); |
---|
518 | glVertex3f(this->bvElement->center->x + this->bvElement->axis[1]->x * this->bvElement->halfLength[1], |
---|
519 | this->bvElement->center->y + this->bvElement->axis[1]->y * this->bvElement->halfLength[1], |
---|
520 | this->bvElement->center->z + this->bvElement->axis[1]->z * this->bvElement->halfLength[1]); |
---|
521 | |
---|
522 | glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z); |
---|
523 | glVertex3f(this->bvElement->center->x + this->bvElement->axis[2]->x * this->bvElement->halfLength[2], |
---|
524 | this->bvElement->center->y + this->bvElement->axis[2]->y * this->bvElement->halfLength[2], |
---|
525 | this->bvElement->center->z + this->bvElement->axis[2]->z * this->bvElement->halfLength[2]); |
---|
526 | glEnd(); |
---|
527 | } |
---|
528 | } |
---|
529 | |
---|
530 | |
---|
531 | if( drawMode & DRAW_BV_POLYGON || drawMode & DRAW_ALL) |
---|
532 | { |
---|
533 | if( !(drawMode & DRAW_SINGLE && depth != 0)) |
---|
534 | { |
---|
535 | Vector cen = *this->bvElement->center; |
---|
536 | Vector** axis = this->bvElement->axis; |
---|
537 | float* len = this->bvElement->halfLength; |
---|
538 | |
---|
539 | /* draw bounding box */ |
---|
540 | glBegin(GL_LINE_LOOP); |
---|
541 | glVertex3f(cen.x + axis[0]->x * len[0] + axis[1]->x * len[1] + axis[2]->x * len[2], |
---|
542 | cen.y + axis[0]->y * len[0] + axis[1]->y * len[1] + axis[2]->y * len[2], |
---|
543 | cen.z + axis[0]->z * len[0] + axis[1]->z * len[1] + axis[2]->z * len[2]); |
---|
544 | glVertex3f(cen.x + axis[0]->x * len[0] + axis[1]->x * len[1] - axis[2]->x * len[2], |
---|
545 | cen.y + axis[0]->y * len[0] + axis[1]->y * len[1] - axis[2]->y * len[2], |
---|
546 | cen.z + axis[0]->z * len[0] + axis[1]->z * len[1] - axis[2]->z * len[2]); |
---|
547 | glVertex3f(cen.x + axis[0]->x * len[0] - axis[1]->x * len[1] - axis[2]->x * len[2], |
---|
548 | cen.y + axis[0]->y * len[0] - axis[1]->y * len[1] - axis[2]->y * len[2], |
---|
549 | cen.z + axis[0]->z * len[0] - axis[1]->z * len[1] - axis[2]->z * len[2]); |
---|
550 | glVertex3f(cen.x + axis[0]->x * len[0] - axis[1]->x * len[1] + axis[2]->x * len[2], |
---|
551 | cen.y + axis[0]->y * len[0] - axis[1]->y * len[1] + axis[2]->y * len[2], |
---|
552 | cen.z + axis[0]->z * len[0] - axis[1]->z * len[1] + axis[2]->z * len[2]); |
---|
553 | glEnd(); |
---|
554 | |
---|
555 | glBegin(GL_LINE_LOOP); |
---|
556 | glVertex3f(cen.x + axis[0]->x * len[0] - axis[1]->x * len[1] + axis[2]->x * len[2], |
---|
557 | cen.y + axis[0]->y * len[0] - axis[1]->y * len[1] + axis[2]->y * len[2], |
---|
558 | cen.z + axis[0]->z * len[0] - axis[1]->z * len[1] + axis[2]->z * len[2]); |
---|
559 | glVertex3f(cen.x + axis[0]->x * len[0] - axis[1]->x * len[1] - axis[2]->x * len[2], |
---|
560 | cen.y + axis[0]->y * len[0] - axis[1]->y * len[1] - axis[2]->y * len[2], |
---|
561 | cen.z + axis[0]->z * len[0] - axis[1]->z * len[1] - axis[2]->z * len[2]); |
---|
562 | glVertex3f(cen.x - axis[0]->x * len[0] - axis[1]->x * len[1] - axis[2]->x * len[2], |
---|
563 | cen.y - axis[0]->y * len[0] - axis[1]->y * len[1] - axis[2]->y * len[2], |
---|
564 | cen.z - axis[0]->z * len[0] - axis[1]->z * len[1] - axis[2]->z * len[2]); |
---|
565 | glVertex3f(cen.x - axis[0]->x * len[0] - axis[1]->x * len[1] + axis[2]->x * len[2], |
---|
566 | cen.y - axis[0]->y * len[0] - axis[1]->y * len[1] + axis[2]->y * len[2], |
---|
567 | cen.z - axis[0]->z * len[0] - axis[1]->z * len[1] + axis[2]->z * len[2]); |
---|
568 | glEnd(); |
---|
569 | |
---|
570 | glBegin(GL_LINE_LOOP); |
---|
571 | glVertex3f(cen.x - axis[0]->x * len[0] - axis[1]->x * len[1] + axis[2]->x * len[2], |
---|
572 | cen.y - axis[0]->y * len[0] - axis[1]->y * len[1] + axis[2]->y * len[2], |
---|
573 | cen.z - axis[0]->z * len[0] - axis[1]->z * len[1] + axis[2]->z * len[2]); |
---|
574 | glVertex3f(cen.x - axis[0]->x * len[0] - axis[1]->x * len[1] - axis[2]->x * len[2], |
---|
575 | cen.y - axis[0]->y * len[0] - axis[1]->y * len[1] - axis[2]->y * len[2], |
---|
576 | cen.z - axis[0]->z * len[0] - axis[1]->z * len[1] - axis[2]->z * len[2]); |
---|
577 | glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] - axis[2]->x * len[2], |
---|
578 | cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] - axis[2]->y * len[2], |
---|
579 | cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] - axis[2]->z * len[2]); |
---|
580 | glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] + axis[2]->x * len[2], |
---|
581 | cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] + axis[2]->y * len[2], |
---|
582 | cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] + axis[2]->z * len[2]); |
---|
583 | glEnd(); |
---|
584 | |
---|
585 | glBegin(GL_LINE_LOOP); |
---|
586 | glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] - axis[2]->x * len[2], |
---|
587 | cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] - axis[2]->y * len[2], |
---|
588 | cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] - axis[2]->z * len[2]); |
---|
589 | glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] + axis[2]->x * len[2], |
---|
590 | cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] + axis[2]->y * len[2], |
---|
591 | cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] + axis[2]->z * len[2]); |
---|
592 | glVertex3f(cen.x + axis[0]->x * len[0] + axis[1]->x * len[1] + axis[2]->x * len[2], |
---|
593 | cen.y + axis[0]->y * len[0] + axis[1]->y * len[1] + axis[2]->y * len[2], |
---|
594 | cen.z + axis[0]->z * len[0] + axis[1]->z * len[1] + axis[2]->z * len[2]); |
---|
595 | glVertex3f(cen.x + axis[0]->x * len[0] + axis[1]->x * len[1] - axis[2]->x * len[2], |
---|
596 | cen.y + axis[0]->y * len[0] + axis[1]->y * len[1] - axis[2]->y * len[2], |
---|
597 | cen.z + axis[0]->z * len[0] + axis[1]->z * len[1] - axis[2]->z * len[2]); |
---|
598 | glEnd(); |
---|
599 | } |
---|
600 | |
---|
601 | } |
---|
602 | |
---|
603 | if( drawMode & DRAW_SEPARATING_PLANE || drawMode & DRAW_ALL) |
---|
604 | { |
---|
605 | if( !(drawMode & DRAW_SINGLE && depth != 0)) |
---|
606 | { |
---|
607 | /* now draw the separation plane */ |
---|
608 | Vector a1 = *this->bvElement->axis[(this->longestAxisIndex + 1)%3]; |
---|
609 | Vector a2 = *this->bvElement->axis[(this->longestAxisIndex + 2)%3]; |
---|
610 | Vector c = *this->bvElement->center; |
---|
611 | float l1 = this->bvElement->halfLength[(this->longestAxisIndex + 1)%3]; |
---|
612 | float l2 = this->bvElement->halfLength[(this->longestAxisIndex + 2)%3]; |
---|
613 | glBegin(GL_QUADS); |
---|
614 | 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); |
---|
615 | 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); |
---|
616 | 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); |
---|
617 | 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); |
---|
618 | glEnd(); |
---|
619 | } |
---|
620 | } |
---|
621 | |
---|
622 | if( this->nodeLeft != NULL && depth != 0 ) |
---|
623 | this->nodeLeft->drawBV(depth - 1, drawMode); |
---|
624 | if( this->nodeRight != NULL && depth != 0) |
---|
625 | this->nodeRight->drawBV(depth - 1, drawMode); |
---|
626 | |
---|
627 | } |
---|
628 | |
---|
629 | |
---|
630 | |
---|
631 | void OBBTreeNode::debug() |
---|
632 | { |
---|
633 | |
---|
634 | /* |
---|
635 | for(int i = 0; i < length; i++) |
---|
636 | { |
---|
637 | PRINTF(3)("vertex %i: %f, %f, %f\n", i, verticesList[i][0], verticesList[i][1], verticesList[i][2]); |
---|
638 | } |
---|
639 | */ |
---|
640 | } |
---|