Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7143 was 6617, checked in by bensch, 18 years ago

trunk: split Rotation/Line/Quaternion/Plane(Rectangle) into seperate files

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