Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5688 was 5688, checked in by patrick, 18 years ago

orxonox/trunk: the const collision detection war - strike

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