Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/collision_detection/src/lib/collision_detection/obb_tree_node.cc @ 5704

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

collision_detection: removed some more unused variables

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