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