Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

collision_detection: cleanup and recode of some small parts.

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