Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/collision_detection/obb_tree_node.cc @ 4696

Last change on this file since 4696 was 4696, checked in by patrick, 19 years ago

orxonox/trunk: started calculating the prjection axis

File size: 29.9 KB
Line 
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#include "stdincl.h"
28
29#include "lin_alg.h"
30
31
32
33
34using namespace std;
35
36OBBTree*  OBBTreeNode::obbTree = NULL;
37
38float**  OBBTreeNode::coMat = NULL;
39float**  OBBTreeNode::eigvMat = NULL;
40float*   OBBTreeNode::eigvlMat = NULL;
41int*     OBBTreeNode::rotCount = NULL;
42
43/**
44   \brief standard constructor
45 */
46OBBTreeNode::OBBTreeNode ()
47{
48  this->setClassID(CL_OBB_TREE_NODE, "OBBTreeNode");
49  this->nodeLeft = NULL;
50  this->nodeRight = NULL;
51
52  if(coMat == NULL)
53  {
54    coMat = new float*[4];
55    for(int i = 0; i < 4; i++)
56      coMat[i] = new float[4];
57  }
58  if(eigvMat == NULL)
59  {
60    eigvMat = new float*[4];
61    for(int i = 0; i < 4; i++)
62      eigvMat[i] = new float[4];
63  }
64  if( eigvlMat == NULL)
65  {
66    eigvlMat = new float[4];
67  }
68  if( rotCount == NULL)
69    rotCount = new int;
70
71  this->sphereObj = gluNewQuadric();
72}
73
74
75/**
76   \brief standard deconstructor
77 */
78OBBTreeNode::~OBBTreeNode ()
79{
80  // delete what has to be deleted here
81}
82
83
84
85/**
86   \brief creates a new BVTree or BVTree partition
87   \param depth: how much more depth-steps to go: if == 1 don't go any deeper!
88   \param verticesList: the list of vertices of the object - each vertices triple is interpreted as a triangle
89 */
90void OBBTreeNode::spawnBVTree(const int depth, sVec3D *verticesList, const int length)
91{
92  PRINT(0)("\n");
93  this->treeIndex = this->obbTree->getID();
94  PRINTF(0)("OBB Depth: %i, tree index: %i, numVertices: %i\n", depth, treeIndex, length);
95  this->depth = depth;
96
97
98  this->bvElement = new OBB();
99  this->bvElement->vertices = verticesList;
100  this->bvElement->numOfVertices = length;
101  PRINTF(3)("Created OBBox\n");
102  this->calculateBoxCovariance(this->bvElement, verticesList, length);
103  PRINTF(3)("Calculated attributes1\n");
104  this->calculateBoxEigenvectors(this->bvElement, verticesList, length);
105  PRINTF(3)("Calculated attributes2\n");
106  this->calculateBoxAxis(this->bvElement, verticesList, length);
107  PRINTF(3)("Calculated attributes3\n");
108
109
110  if( likely( this->depth > 0))
111  {
112    this->forkBox(this->bvElement);
113
114
115    if(this->tmpLen1 > 0)
116    {
117      OBBTreeNode* node1 = new OBBTreeNode();
118      this->nodeLeft = node1;
119      this->nodeLeft->spawnBVTree(depth - 1, this->tmpVert1, this->tmpLen1);
120    }
121    else
122    {
123      PRINTF(0)("Aboarding tree walk: less than 3 vertices left\n");
124    }
125
126    if( this->tmpLen2 > 0)
127    {
128      OBBTreeNode* node2 = new OBBTreeNode();
129      this->nodeRight = node2;
130      this->nodeRight->spawnBVTree(depth - 1, this->tmpVert2, this->tmpLen2);
131    }
132    else
133    {
134      PRINTF(0)("Aboarding tree walk: less than 3 vertices left\n");
135    }
136
137  }
138}
139
140
141
142void OBBTreeNode::calculateBoxCovariance(OBB* box, sVec3D* verticesList, int length)
143{
144  float     facelet[length];                         //!< surface area of the i'th triangle of the convex hull
145  float     face;                                    //!< surface area of the entire convex hull
146  Vector    centroid[length];                        //!< centroid of the i'th convex hull
147  Vector    center;                                  //!< the center of the entire hull
148  Vector    p, q, r;                                 //!< holder of the polygon data, much more conveniant to work with Vector than sVec3d
149  Vector    t1, t2;                                  //!< temporary values
150  float     covariance[3][3];                        //!< the covariance matrix
151  int       mode = 0;                                //!< mode = 0: vertex soup, no connections, mode = 1: 3 following verteces build a triangle
152
153  this->numOfVertices = length;
154  this->vertices = verticesList;
155
156
157  if( likely(mode == 0))
158  {
159    /* fist compute all the convex hull face/facelets and centroids */
160    for(int i = 0; i < length; i+=3)          /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/
161    {
162      p = verticesList[i];
163      q = verticesList[i + 1];
164      r = verticesList[i + 2];
165
166      t1 = p - q; t2 = p - r;
167
168      /* finding the facelet surface via cross-product */
169      facelet[i] = 0.5f * fabs( t1.cross(t2).len() );
170      /* update the entire convex hull surface */
171      face += facelet[i];
172
173      /* calculate the cetroid of the hull triangles */
174      centroid[i] = (p + q + r) * 1/3;
175      /* now calculate the centroid of the entire convex hull, weighted average of triangle centroids */
176      center += centroid[i] * facelet[i];
177    }
178    /* take the average of the centroid sum */
179    center /= face;
180    PRINTF(3)("-- Calculated Center\n");
181
182
183    /* now calculate the covariance matrix - if not written in three for-loops, it would compute faster: minor */
184    for(int j = 0; j < 3; ++j)
185    {
186      for(int k = 0; k < 3; ++k)
187      {
188        for(int i = 0; i < length; i+=3)
189        {
190          p = verticesList[i];
191          q = verticesList[i + 1];
192          r = verticesList[i + 2];
193
194          covariance[j][k] = facelet[i] / (12.0f * face) * (9.0f * centroid[i][j] * centroid[i][k] + p[j] * p[k] +
195              q[j] * q[k] + r[j] * r[k]) - center[j] * center[k];
196        }
197      }
198    }
199    PRINTF(3)("-- Calculated Covariance\n");
200  }
201  else if( mode == 1)
202  {
203    for( int i = 0; i < length; i+=3)          /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/
204    {
205      p = verticesList[i];
206      q = verticesList[i + 1];
207      r = verticesList[i + 2];
208
209      centroid[i] = (p + q + r) / 3.0f;
210      center += centroid[i];
211    }
212    center /= length;
213
214    for( int j = 0; j < 3; ++j)
215    {
216      for( int k = 0; k < 3; ++k)
217      {
218        for( int i = 0; i < length; i+=3)
219        {
220          p = verticesList[i];
221          q = verticesList[i +1];
222          r = verticesList[i + 2];
223
224          covariance[j][k] = p[j] * p[k] + q[j] * q[k] + r[j] + r[k];
225        }
226        covariance[j][k] /= (3.0f * length);
227      }
228    }
229    PRINTF(3)("-- Calculated Covariance\n");
230  }
231  else if( mode == 2)
232  {
233    /* fist compute all the convex hull face/facelets and centroids */
234    for(int i = 0; i < length; i+=3)          /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/
235    {
236      p = verticesList[i];
237      q = verticesList[i + 1];
238      r = verticesList[i + 2];
239
240      t1 = p - q; t2 = p - r;
241
242      /* finding the facelet surface via cross-product */
243      facelet[i] = 0.5f * fabs( t1.cross(t2).len() );
244      /* update the entire convex hull surface */
245      face += facelet[i];
246
247      /* calculate the cetroid of the hull triangles */
248      centroid[i] = (p + q + r) * 1/3;
249      /* now calculate the centroid of the entire convex hull, weighted average of triangle centroids */
250      center += centroid[i] * facelet[i];
251    }
252    /* take the average of the centroid sum */
253    center /= face;
254    PRINTF(3)("-- Calculated Center\n");
255
256    for( int j = 0; j < 3; ++j)
257    {
258      for( int k = 0; k < 3; ++k)
259      {
260        for( int i = 0; i < length; i+=3)
261        {
262          p = verticesList[i];
263          q = verticesList[i +1];
264          r = verticesList[i + 2];
265
266          covariance[j][k] = p[j] * p[k] + q[j] * q[k] + r[j] + r[k];
267        }
268        covariance[j][k] /= (3.0f * length);
269      }
270    }
271    PRINTF(3)("-- Calculated Covariance\n");
272  }
273  else
274  {
275    for( int i = 0; i < length; ++i)          /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/
276    {
277      center += verticesList[i];
278    }
279    center /= length;
280
281    for( int j = 0; j < 3; ++j)
282    {
283      for( int k = 0; k < 3; ++k)
284      {
285        for( int i = 0; i < length; i+=3)
286        {
287          p = verticesList[i];
288          q = verticesList[i +1];
289          r = verticesList[i + 2];
290
291          covariance[j][k] = p[j] * p[k] + q[j] * q[k] + r[j] + r[k];
292        }
293        covariance[j][k] /= (3.0f * length);
294      }
295    }
296    PRINTF(3)("-- Calculated Covariance\n");
297  }
298
299  PRINTF(3)("\nVertex Data:\n");
300  for(int i = 0; i < length; i++)
301  {
302    PRINTF(3)("vertex %i: %f, %f, %f\n", i, box->vertices[i][0], box->vertices[i][1], box->vertices[i][2]);
303  }
304
305
306  PRINTF(3)("\nCovariance Matrix:\n");
307  for(int j = 0; j < 3; ++j)
308  {
309    PRINT(3)(" |");
310    for(int k = 0; k < 3; ++k)
311    {
312      PRINT(3)(" \b%f ", covariance[j][k]);
313    }
314    PRINT(3)(" |\n");
315  }
316
317  PRINTF(3)("center: %f, %f, %f\n", center.x, center.y, center.z);
318
319
320  for(int i = 0; i < 3; ++i)
321  {
322    box->covarianceMatrix[i][0] = covariance[i][0];
323    box->covarianceMatrix[i][1] = covariance[i][1];
324    box->covarianceMatrix[i][2] = covariance[i][2];
325  }
326  *box->center = center;
327  PRINTF(3)("-- Written Result to obb\n");
328}
329
330
331
332void OBBTreeNode::calculateBoxEigenvectors(OBB* box, sVec3D* verticesList, int length)
333{
334
335  /* now getting spanning vectors of the sub-space:
336  the eigenvectors of a symmertric matrix, such as the
337  covarience matrix are mutually orthogonal.
338  after normalizing them, they can be used as a the basis
339  vectors
340  */
341  Vector*              axis = new Vector[3];                //!< the references to the obb axis
342
343  coMat[1][1] = box->covarianceMatrix[0][0]; coMat[1][2] = box->covarianceMatrix[0][1]; coMat[1][3] = box->covarianceMatrix[0][2];
344  coMat[2][1] = box->covarianceMatrix[1][0]; coMat[2][2] = box->covarianceMatrix[1][1]; coMat[2][3] = box->covarianceMatrix[1][2];
345  coMat[3][1] = box->covarianceMatrix[2][0]; coMat[3][2] = box->covarianceMatrix[2][1]; coMat[3][3] = box->covarianceMatrix[2][2];
346
347  /* new jacobi tests */
348  JacobI(coMat, 3, eigvlMat, eigvMat, rotCount);
349  PRINTF(3)("-- Done Jacobi Decomposition\n");
350
351
352//   PRINTF(3)("Jacobi\n");
353//   for(int j = 1; j < 4; ++j)
354//   {
355//     PRINTF(3)(" |");
356//     for(int k = 1; k < 4; ++k)
357//     {
358//       PRINTF(3)(" \b%f ", eigvMat[j][k]);
359//     }
360//     PRINTF(3)(" |\n");
361//   }
362
363  axis[0].x = eigvMat[1][1]; axis[0].y = eigvMat[2][1]; axis[0].z = eigvMat[3][1];
364  axis[1].x = eigvMat[1][2]; axis[1].y = eigvMat[2][2]; axis[1].z = eigvMat[3][2];
365  axis[2].x = eigvMat[1][3]; axis[2].y = eigvMat[2][3]; axis[2].z = eigvMat[3][3];
366  box->axis = axis;
367
368  PRINTF(3)("-- Got Axis\n");
369
370  PRINTF(3)("eigenvector: %f, %f, %f\n", box->axis[0].x, box->axis[0].y, box->axis[0].z);
371  PRINTF(3)("eigenvector: %f, %f, %f\n", box->axis[1].x, box->axis[1].y, box->axis[1].z);
372  PRINTF(3)("eigenvector: %f, %f, %f\n", box->axis[2].x, box->axis[2].y, box->axis[2].z);
373}
374
375
376void OBBTreeNode::calculateBoxAxis(OBB* box, sVec3D* verticesList, int length)
377{
378
379  /* now get the axis length */
380  Line                ax[3];                                 //!< the axis
381  float*              halfLength = new float[3];             //!< half length of the axis
382  float               tmpLength;                             //!< tmp save point for the length
383  Plane               p0(box->axis[0], *box->center);       //!< the axis planes
384  Plane               p1(box->axis[1], *box->center);
385  Plane               p2(box->axis[2], *box->center);
386  float               maxLength[3];
387  float               minLength[3];
388
389
390  /* get a bad bounding box */
391  halfLength[0] = -1.0f;
392  for(int j = 0; j < length; ++j)
393    {
394      tmpLength = fabs(p0.distancePoint(vertices[j]));
395      if( tmpLength > halfLength[0])
396        halfLength[0] = tmpLength;
397    }
398
399  halfLength[1] = -1.0f;
400  for(int j = 0; j < length; ++j)
401    {
402      tmpLength = fabs(p1.distancePoint(vertices[j]));
403      if( tmpLength > halfLength[1])
404        halfLength[1] = tmpLength;
405    }
406
407  halfLength[2] = -1.0f;
408  for(int j = 0; j < length; ++j)
409    {
410      tmpLength = fabs(p2.distancePoint(vertices[j]));
411      if( tmpLength > halfLength[2])
412        halfLength[2] = tmpLength;
413    }
414
415
416
417  /* get the maximal dimensions of the body in all directions */
418   maxLength[0] = 0.0f;
419   minLength[0] = 0.0f;
420   for(int j = 0; j < length; ++j)
421   {
422     tmpLength = p0.distancePoint(vertices[j]);
423     if( tmpLength > maxLength[0])
424       maxLength[0] = tmpLength;
425     else if( tmpLength < minLength[0])
426       minLength[0] = tmpLength;
427   }
428
429   maxLength[1] = 0.0f;
430   minLength[1] = 0.0f;
431   for(int j = 0; j < length; ++j)
432   {
433     tmpLength = p1.distancePoint(vertices[j]);
434     if( tmpLength > maxLength[1])
435       maxLength[1] = tmpLength;
436     else if( tmpLength < minLength[1])
437       minLength[1] = tmpLength;
438   }
439
440   maxLength[2] = 0.0f;
441   minLength[2] = 0.0f;
442   for(int j = 0; j < length; ++j)
443   {
444     tmpLength = p2.distancePoint(vertices[j]);
445     if( tmpLength > maxLength[2])
446       maxLength[2] = tmpLength;
447     else if( tmpLength < minLength[2])
448       minLength[2] = tmpLength;
449   }
450
451
452   /* calculate the real centre of the body by using the axis length */
453   float centerOffset[3];
454   float newHalfLength[3];
455   for(int i = 0; i < 3; ++i)
456     {
457       PRINTF(3)("max: %f, min: %f \n", maxLength[i], minLength[i]);
458       centerOffset[i] = (maxLength[i] + minLength[i]) / 2.0f; // min length is negatie
459       newHalfLength[i] = (maxLength[i] - minLength[i]) / 2.0f; // min length is negative
460       *box->center +=  (box->axis[i] * centerOffset[i]);            // update the new center vector
461       halfLength[i] = newHalfLength[i];
462     }
463
464
465
466  box->halfLength = halfLength;
467  PRINTF(3)("-- Written Axis to obb\n");
468  PRINTF(3)("-- Finished Calculating Attributes\n");
469
470
471
472//   PRINTF(3)("\nwe got length: \n");
473  for(int i = 0; i < 3; ++i)
474  {
475    //if( box->halfLength[i] == 0.0)
476    PRINTF(3)("length[%i] = %f\n", i, box->halfLength[i]);
477  }
478}
479
480
481
482/**
483  \brief this separates an ob-box in the middle
484  \param box: the box to separate
485
486  this will separate the box into to smaller boxes. the separation is done along the middle of the longest axis
487 */
488void OBBTreeNode::forkBox(OBB* box)
489{
490  /* get the longest axis of the box */
491  float               aLength = -1.0f;                     //!< the length of the longest axis
492  int                 axisIndex = 0;                       //!< this is the nr of the longest axis
493
494  for(int i = 0; i < 3; ++i)
495  {
496    if( aLength < box->halfLength[i])
497    {
498      aLength = box->halfLength[i];
499      axisIndex = i;
500    }
501  }
502
503   PRINTF(3)("longest axis is: nr %i with a half-length of: %f\n", axisIndex, aLength);
504
505
506  /* get the closest vertex near the center */
507  float               dist = 999999.0f;                    //!< the smallest distance to each vertex
508  float               tmpDist;                             //!< temporary distance
509  int                 vertexIndex;
510  Plane               middlePlane(box->axis[axisIndex], *box->center); //!< the middle plane
511
512  vertexIndex = 0;
513  for(int i = 0; i < box->numOfVertices; ++i)
514  {
515    tmpDist = fabs(middlePlane.distancePoint(box->vertices[i]));
516    if( tmpDist < dist)
517    {
518      dist = tmpDist;
519      vertexIndex = i;
520    }
521  }
522
523//   PRINTF(3)("\nthe clostest vertex is nr: %i, with a dist of: %f\n", vertexIndex ,dist);
524
525
526  /* now definin the separation plane through this specified nearest point and partition
527  the points depending on which side they are located
528  */
529  tList<sVec3D>      partition1;                           //!< the vertex partition 1
530  tList<sVec3D>      partition2;                           //!< the vertex partition 2
531
532  PRINTF(3)("vertex index: %i, of %i\n", vertexIndex, box->numOfVertices);
533  this->separationPlane = new Plane(box->axis[axisIndex], box->vertices[vertexIndex]);  //!< separation plane
534  this->sepPlaneCenter = &box->vertices[vertexIndex];
535  this->longestAxisIndex = axisIndex;
536
537  for(int i = 0; i < box->numOfVertices; ++i)
538  {
539    if( this->separationPlane->distancePoint(box->vertices[i]) > 0.0f)
540      partition1.add(&box->vertices[i]);
541    else
542      partition2.add(&box->vertices[i]);
543  }
544  partition1.add(&box->vertices[vertexIndex]);
545
546//   PRINTF(3)("\npartition1: got %i vertices/ partition 2: got %i vertices\n", partition1.getSize(), partition2.getSize());
547
548
549  /* now comes the separation into two different sVec3D arrays */
550  tIterator<sVec3D>* iterator;                             //!< the iterator to go through the lists
551  sVec3D*            element;                              //!< the elements
552  int                index;                                //!< index storage place
553  sVec3D*            vertList1;                            //!< the vertex list 1
554  sVec3D*            vertList2;                            //!< the vertex list 2
555
556  vertList1 = new sVec3D[partition1.getSize()];
557  vertList2 = new sVec3D[partition2.getSize()];
558
559  iterator = partition1.getIterator();
560  element = iterator->nextElement();
561  index = 0;
562  while( element != NULL)
563  {
564    vertList1[index][0] = element[0][0];
565    vertList1[index][1] = element[0][1];
566    vertList1[index][2] = element[0][2];
567    ++index;
568    element = iterator->nextElement();
569  }
570
571//   PRINTF(0)("\npartition 1:\n");
572//   for(int i = 0; i < partition1.getSize(); ++i)
573//   {
574//     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]);
575//   }
576
577  iterator = partition2.getIterator();
578  element = iterator->nextElement();
579  index = 0;
580  while( element != NULL)
581  {
582    vertList2[index][0] = element[0][0];
583    vertList2[index][1] = element[0][1];
584    vertList2[index][2] = element[0][2];
585    ++index;
586    element = iterator->nextElement();
587  }
588
589  this->tmpVert1 = vertList1;
590  this->tmpVert2 = vertList2;
591  this->tmpLen1 = partition1.getSize();
592  this->tmpLen2 = partition2.getSize();
593
594  delete iterator;
595
596//   PRINTF(0)("\npartition 2:\n");
597//   for(int i = 0; i < partition2.getSize(); ++i)
598//   {
599//     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]);
600//   }
601}
602
603
604
605
606void OBBTreeNode::collideWith(BVTreeNode* treeNode)
607{
608  PRINTF(0)("collideWith");
609  /* if the obb overlap, make subtests: check which node is realy overlaping  */
610  if( this->overlapTest(this->bvElement, ((OBBTreeNode*)treeNode)->bvElement))
611  {
612    /* check if left node overlaps */
613    if( unlikely( this->nodeLeft != NULL))
614      if( this->overlapTest(this->nodeLeft->bvElement, ((OBBTreeNode*)treeNode)->bvElement))
615        this->nodeLeft->collideWith(((OBBTreeNode*)treeNode)->nodeLeft);
616    /* check if right node overlaps */
617    if( unlikely( this->nodeRight != NULL))
618      if(this->overlapTest(this->nodeRight->bvElement, ((OBBTreeNode*)treeNode)->bvElement))
619        this->nodeLeft->collideWith(((OBBTreeNode*)treeNode)->nodeRight);
620  }
621}
622
623
624
625bool OBBTreeNode::overlapTest(OBB* boxA, OBB* boxB)
626{
627  /* first check all axis */
628  float r = 0.0f;
629  Vector l = boxA->axis[0];
630  for(int i = 0; i < 3; ++i)
631  {
632    r += boxA->halfLength[i] * boxA->axis[i].dot(l);
633  }
634
635  printf("r = %f\n", r);
636  /* now check all orthogonals from the axis */
637}
638
639
640
641void OBBTreeNode::drawBV(int depth, int drawMode) const
642{
643  this->obbTree->getMaterial(treeIndex)->select();
644
645  /* draw the model itself, there is some problem concerning this: the vertices are drawn multiple times */
646  if( drawMode & DRAW_MODEL || drawMode & DRAW_ALL)
647  {
648    if( !(drawMode & DRAW_SINGLE && depth != 0))
649    {
650      for(int i = 0; i < this->bvElement->numOfVertices; ++i)
651      {
652        glPushMatrix();
653        //glMatrixMode(GL_MODELVIEW);
654        //glVertex3f(this->bvElement->vertices[i][0], this->bvElement->vertices[i][1], this->bvElement->vertices[i][2]);
655        glTranslatef(this->bvElement->vertices[i][0], this->bvElement->vertices[i][1], this->bvElement->vertices[i][2]);
656        gluSphere(this->sphereObj, 0.1, 10, 10);
657        //PRINTF(0)("v(%f, %f, %f)\n", this->bvElement->vertices[i][0], this->bvElement->vertices[i][1], this->bvElement->vertices[i][2]);
658        glPopMatrix();
659      }
660    }
661  }
662
663
664  /* draw world axes */
665  if( drawMode & DRAW_BV_AXIS)
666  {
667    glBegin(GL_LINES);
668    glColor3f(0.0, 0.4, 0.3);
669    glVertex3f(0.0, 0.0, 0.0);
670    glVertex3f(3.0, 0.0, 0.0);
671
672    glVertex3f(0.0, 0.0, 0.0);
673    glVertex3f(0.0, 3.0, 0.0);
674
675    glVertex3f(0.0, 0.0, 0.0);
676    glVertex3f(0.0, 0.0, 3.0);
677    glEnd();
678  }
679
680
681  if( drawMode & DRAW_BV_AXIS || drawMode & DRAW_ALL)
682  {
683    if( !(drawMode & DRAW_SINGLE && depth != 0))
684    {
685      /* draw the obb axes */
686      glBegin(GL_LINES);
687      glColor3f(0.0, 0.4, 0.3);
688      glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z);
689      glVertex3f(this->bvElement->center->x + this->bvElement->axis[0].x * this->bvElement->halfLength[0],
690                 this->bvElement->center->y + this->bvElement->axis[0].y * this->bvElement->halfLength[0],
691                 this->bvElement->center->z + this->bvElement->axis[0].z * this->bvElement->halfLength[0]);
692
693      glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z);
694      glVertex3f(this->bvElement->center->x + this->bvElement->axis[1].x * this->bvElement->halfLength[1],
695                 this->bvElement->center->y + this->bvElement->axis[1].y * this->bvElement->halfLength[1],
696                 this->bvElement->center->z + this->bvElement->axis[1].z * this->bvElement->halfLength[1]);
697
698      glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z);
699      glVertex3f(this->bvElement->center->x + this->bvElement->axis[2].x * this->bvElement->halfLength[2],
700                 this->bvElement->center->y + this->bvElement->axis[2].y * this->bvElement->halfLength[2],
701                 this->bvElement->center->z + this->bvElement->axis[2].z * this->bvElement->halfLength[2]);
702      glEnd();
703    }
704  }
705
706
707  /* DRAW POLYGONS */
708  if( drawMode & DRAW_BV_POLYGON || drawMode & DRAW_ALL || drawMode & DRAW_BV_BLENDED)
709  {
710    if( !(drawMode & DRAW_SINGLE && depth != 0))
711    {
712    Vector cen = *this->bvElement->center;
713    Vector* axis = this->bvElement->axis;
714    float* len = this->bvElement->halfLength;
715
716    if( drawMode & DRAW_BV_BLENDED)
717      this->obbTree->getTransparentMaterial(treeIndex)->select();
718
719    /* draw bounding box */
720    if( drawMode & DRAW_BV_BLENDED)
721      glBegin(GL_QUADS);
722    else
723      glBegin(GL_LINE_LOOP);
724    glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
725               cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
726               cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
727    glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
728               cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
729               cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
730    glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
731               cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
732               cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
733    glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
734               cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
735               cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
736    glEnd();
737
738    if( drawMode & DRAW_BV_BLENDED)
739      glBegin(GL_QUADS);
740    else
741      glBegin(GL_LINE_LOOP);
742    glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
743               cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
744               cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
745    glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
746               cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
747               cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
748    glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
749               cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
750               cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
751    glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
752               cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
753               cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
754    glEnd();
755
756    if( drawMode & DRAW_BV_BLENDED)
757      glBegin(GL_QUADS);
758    else
759      glBegin(GL_LINE_LOOP);
760    glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
761               cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
762               cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
763    glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
764               cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
765               cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
766    glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
767               cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
768               cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
769    glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
770               cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
771               cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
772    glEnd();
773
774    if( drawMode & DRAW_BV_BLENDED)
775      glBegin(GL_QUADS);
776    else
777      glBegin(GL_LINE_LOOP);
778    glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
779               cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
780               cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
781    glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
782               cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
783               cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
784    glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
785               cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
786               cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
787    glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
788               cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
789               cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
790    glEnd();
791
792
793    if( drawMode & DRAW_BV_BLENDED)
794    {
795      glBegin(GL_QUADS);
796      glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
797                 cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
798                 cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
799      glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2],
800                 cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2],
801                 cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]);
802      glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
803                 cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
804                 cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
805      glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2],
806                 cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2],
807                 cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]);
808      glEnd();
809
810      glBegin(GL_QUADS);
811      glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
812                 cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
813                 cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
814      glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2],
815                 cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2],
816                 cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]);
817      glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
818                 cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
819                 cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
820      glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2],
821                 cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2],
822                 cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]);
823      glEnd();
824    }
825
826
827    if( drawMode & DRAW_BV_BLENDED)
828      this->obbTree->getMaterial(treeIndex)->select();
829    }
830
831  }
832
833  /* DRAW SEPARATING PLANE */
834  if( drawMode & DRAW_SEPARATING_PLANE || drawMode & DRAW_ALL)
835  {
836    if( !(drawMode & DRAW_SINGLE && depth != 0))
837    {
838      if( drawMode & DRAW_BV_BLENDED)
839        this->obbTree->getTransparentMaterial(treeIndex)->select();
840
841    /* now draw the separation plane */
842    Vector a1 = this->bvElement->axis[(this->longestAxisIndex + 1)%3];
843    Vector a2 = this->bvElement->axis[(this->longestAxisIndex + 2)%3];
844    Vector c = *this->bvElement->center;
845    float l1 = this->bvElement->halfLength[(this->longestAxisIndex + 1)%3];
846    float l2 = this->bvElement->halfLength[(this->longestAxisIndex + 2)%3];
847    glBegin(GL_QUADS);
848    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);
849    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);
850    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);
851    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);
852    glEnd();
853
854    if( drawMode & DRAW_BV_BLENDED)
855      this->obbTree->getMaterial(treeIndex)->select();
856
857    }
858  }
859
860  if( this->nodeLeft != NULL && depth != 0 )
861    this->nodeLeft->drawBV(depth - 1, drawMode);
862  if( this->nodeRight != NULL && depth != 0)
863    this->nodeRight->drawBV(depth - 1, drawMode);
864
865}
866
867
868
869void OBBTreeNode::debug(void) const
870{
871
872  /*
873  for(int i = 0; i < length; i++)
874  {
875  PRINTF(3)("vertex %i: %f, %f, %f\n", i, verticesList[i][0], verticesList[i][1], verticesList[i][2]);
876}
877  */
878}
Note: See TracBrowser for help on using the repository browser.