| 1 | /* | 
|---|
| 2 |    orxonox - the future of 3D-vertical-scrollers | 
|---|
| 3 |  | 
|---|
| 4 |    Copyright (C) 2004 orx | 
|---|
| 5 |  | 
|---|
| 6 |    This program is free software; you can redistribute it and/or modify | 
|---|
| 7 |    it under the terms of the GNU General Public License as published by | 
|---|
| 8 |    the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 9 |    any later version. | 
|---|
| 10 |  | 
|---|
| 11 | ### File Specific: | 
|---|
| 12 |    main-programmer: Patrick Boenzli | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | #define DEBUG_SPECIAL_MODULE 3/* DEBUG_MODULE_COLLISION_DETECTION*/ | 
|---|
| 16 |  | 
|---|
| 17 | #include "obb_tree_node.h" | 
|---|
| 18 | #include "obb_tree.h" | 
|---|
| 19 | #include "obb.h" | 
|---|
| 20 |  | 
|---|
| 21 | #include "matrix.h" | 
|---|
| 22 | #include "model.h" | 
|---|
| 23 | #include "world_entity.h" | 
|---|
| 24 | #include "plane.h" | 
|---|
| 25 |  | 
|---|
| 26 | #include "color.h" | 
|---|
| 27 | #include "glincl.h" | 
|---|
| 28 |  | 
|---|
| 29 | #include <list> | 
|---|
| 30 | #include <vector> | 
|---|
| 31 | #include "debug.h" | 
|---|
| 32 |  | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 | using namespace std; | 
|---|
| 36 |  | 
|---|
| 37 |  | 
|---|
| 38 | GLUquadricObj* OBBTreeNode_sphereObj = NULL; | 
|---|
| 39 |  | 
|---|
| 40 |  | 
|---|
| 41 | /** | 
|---|
| 42 |  *  standard constructor | 
|---|
| 43 |  * @param tree: reference to the obb tree | 
|---|
| 44 |  * @param depth: the depth of the obb tree to generate | 
|---|
| 45 |  */ | 
|---|
| 46 | OBBTreeNode::OBBTreeNode (const OBBTree& tree, OBBTreeNode* prev, int depth) | 
|---|
| 47 |     : BVTreeNode() | 
|---|
| 48 | { | 
|---|
| 49 |   this->setClassID(CL_OBB_TREE_NODE, "OBBTreeNode"); | 
|---|
| 50 |  | 
|---|
| 51 |   this->obbTree = &tree; | 
|---|
| 52 |   this->nodePrev = prev; | 
|---|
| 53 |   this->depth = depth; | 
|---|
| 54 |   this->nextID = 0; | 
|---|
| 55 |  | 
|---|
| 56 |   this->nodeLeft = NULL; | 
|---|
| 57 |   this->nodeRight = NULL; | 
|---|
| 58 |   this->bvElement = NULL; | 
|---|
| 59 |  | 
|---|
| 60 |   this->triangleIndexList1 = NULL; | 
|---|
| 61 |   this->triangleIndexList2 = NULL; | 
|---|
| 62 |  | 
|---|
| 63 |   this->modelInf = NULL; | 
|---|
| 64 |   this->triangleIndexes = NULL; | 
|---|
| 65 |  | 
|---|
| 66 |   if( OBBTreeNode_sphereObj == NULL) | 
|---|
| 67 |     OBBTreeNode_sphereObj = gluNewQuadric(); | 
|---|
| 68 |  | 
|---|
| 69 |   this->owner = NULL; | 
|---|
| 70 |  | 
|---|
| 71 |   /* debug ids */ | 
|---|
| 72 |   if( this->nodePrev) | 
|---|
| 73 |     this->treeIndex = 100 * this->depth + this->nodePrev->getID(); | 
|---|
| 74 |   else | 
|---|
| 75 |     this->treeIndex = 0; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 |  | 
|---|
| 79 | /** | 
|---|
| 80 |  *  standard deconstructor | 
|---|
| 81 |  */ | 
|---|
| 82 | OBBTreeNode::~OBBTreeNode () | 
|---|
| 83 | { | 
|---|
| 84 |   if( this->nodeLeft) | 
|---|
| 85 |     delete this->nodeLeft; | 
|---|
| 86 |   if( this->nodeRight) | 
|---|
| 87 |     delete this->nodeRight; | 
|---|
| 88 |  | 
|---|
| 89 |   if( this->bvElement) | 
|---|
| 90 |     delete this->bvElement; | 
|---|
| 91 |  | 
|---|
| 92 | //   if( this->triangleIndexList1 != NULL) | 
|---|
| 93 | //     delete [] this->triangleIndexList1; | 
|---|
| 94 | //   if( this->triangleIndexList2 != NULL) | 
|---|
| 95 | //     delete [] this->triangleIndexList2; | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 |  | 
|---|
| 99 | /** | 
|---|
| 100 |  *  creates a new BVTree or BVTree partition | 
|---|
| 101 |  * @param depth: how much more depth-steps to go: if == 1 don't go any deeper! | 
|---|
| 102 |  * @param modInfo: model informations from the abstrac model | 
|---|
| 103 |  * | 
|---|
| 104 |  * this function creates the Bounding Volume tree from a modelInfo struct and bases its calculations | 
|---|
| 105 |  * on the triangle informations (triangle soup not polygon soup) | 
|---|
| 106 |  */ | 
|---|
| 107 | void OBBTreeNode::spawnBVTree(const modelInfo& modelInf, const int* triangleIndexes, int length) | 
|---|
| 108 | { | 
|---|
| 109 |   PRINTF(3)("\n==============================Creating OBB Tree Node==================\n"); | 
|---|
| 110 |   PRINT(3)(" OBB Tree Infos: \n"); | 
|---|
| 111 |   PRINT(3)("\tDepth: %i \n\tTree Index: %i \n\tNumber of Triangles: %i\n", depth, this->treeIndex, length); | 
|---|
| 112 |   this->depth = depth; | 
|---|
| 113 |  | 
|---|
| 114 |   this->bvElement = new OBB(); | 
|---|
| 115 |   this->bvElement->modelInf = &modelInf; | 
|---|
| 116 |   this->bvElement->triangleIndexes = triangleIndexes; | 
|---|
| 117 |   this->bvElement->triangleIndexesLength = length; | 
|---|
| 118 |  | 
|---|
| 119 |   // full debug output | 
|---|
| 120 |  | 
|---|
| 121 |   //indexes debug | 
|---|
| 122 |   for( int i = 0; i < length; ++i) | 
|---|
| 123 |   { | 
|---|
| 124 |     PRINTF(2)("triangle[%i], index: %i\n", i, triangleIndexes[i]); | 
|---|
| 125 |   } | 
|---|
| 126 |  | 
|---|
| 127 |   // vertex coordinates | 
|---|
| 128 |   for( int i = 0; i < length; ++i) | 
|---|
| 129 |   { | 
|---|
| 130 |     PRINTF(2)("triangle[%i]\n", i); | 
|---|
| 131 |     for(int j = 0; j < 3; ++j) | 
|---|
| 132 |     { | 
|---|
| 133 |       PRINTF(2)("  vertex[%i]: %f, %f, %f\n", j, (&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[j]])[0], | 
|---|
| 134 |       (&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[j]])[1], | 
|---|
| 135 |       (&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[j]])[2]); | 
|---|
| 136 |     } | 
|---|
| 137 |   } | 
|---|
| 138 |  | 
|---|
| 139 |  | 
|---|
| 140 |  | 
|---|
| 141 |   /* create the bounding boxes in three steps */ | 
|---|
| 142 |   this->calculateBoxCovariance(*this->bvElement, modelInf, triangleIndexes, length); | 
|---|
| 143 |   this->calculateBoxEigenvectors(*this->bvElement, modelInf, triangleIndexes, length); | 
|---|
| 144 | //   this->calculateBoxAxis(*this->bvElement, modelInf, triangleIndexes, length); | 
|---|
| 145 | //   this->calculateBoxAxis(*this->bvElement, modelInf, triangleIndexes, length); | 
|---|
| 146 |   this->calculateBoxAxis(*this->bvElement, modelInf, triangleIndexes, length); | 
|---|
| 147 |  | 
|---|
| 148 |   /* do we need to descent further in the obb tree?*/ | 
|---|
| 149 |   if( likely( this->depth > 0)) | 
|---|
| 150 |   { | 
|---|
| 151 |     this->forkBox(*this->bvElement); | 
|---|
| 152 |  | 
|---|
| 153 |     if( this->triangleIndexLength1 >= 3) | 
|---|
| 154 |     { | 
|---|
| 155 |       this->nodeLeft = new OBBTreeNode(*this->obbTree, this, depth - 1); | 
|---|
| 156 |       this->nodeLeft->spawnBVTree(modelInf, this->triangleIndexList1, this->triangleIndexLength1); | 
|---|
| 157 |     } | 
|---|
| 158 |     if( this->triangleIndexLength2 >= 3) | 
|---|
| 159 |     { | 
|---|
| 160 |       this->nodeRight = new OBBTreeNode(*this->obbTree, this, depth - 1); | 
|---|
| 161 |       this->nodeRight->spawnBVTree(modelInf, this->triangleIndexList2, this->triangleIndexLength2); | 
|---|
| 162 |     } | 
|---|
| 163 |   } | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 |  | 
|---|
| 167 |  | 
|---|
| 168 | /** | 
|---|
| 169 |  *  calculate the box covariance matrix | 
|---|
| 170 |  * @param box: reference to the box | 
|---|
| 171 |  * @param modelInf: the model info structure of the model | 
|---|
| 172 |  * @param tirangleIndexes: an array with the indexes of the triangles inside this | 
|---|
| 173 |  * @param length: the length of the indexes array | 
|---|
| 174 |  */ | 
|---|
| 175 | void OBBTreeNode::calculateBoxCovariance(OBB& box, const modelInfo& modelInf, const int* triangleIndexes, int length) | 
|---|
| 176 | { | 
|---|
| 177 |   float     facelet[length];                         //!< surface area of the i'th triangle of the convex hull | 
|---|
| 178 |   float     face = 0.0f;                             //!< surface area of the entire convex hull | 
|---|
| 179 |   Vector    centroid[length];                        //!< centroid of the i'th convex hull | 
|---|
| 180 |   Vector    center;                                  //!< the center of the entire hull | 
|---|
| 181 |   Vector    p, q, r;                                 //!< holder of the polygon data, much more conveniant to work with Vector than sVec3d | 
|---|
| 182 |   Vector    t1, t2;                                  //!< temporary values | 
|---|
| 183 |   float     covariance[3][3] = {0,0,0, 0,0,0, 0,0,0};//!< the covariance matrix | 
|---|
| 184 |   const float*   tmpVec = NULL;                           //!< a temp saving place for sVec3Ds | 
|---|
| 185 |  | 
|---|
| 186 |   /* fist compute all the convex hull face/facelets and centroids */ | 
|---|
| 187 |   for( int i = 0; i < length ; ++i) | 
|---|
| 188 |   { | 
|---|
| 189 |     p = &modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[0]]; | 
|---|
| 190 |     q = &modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[1]]; | 
|---|
| 191 |     r = &modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[2]]; | 
|---|
| 192 |  | 
|---|
| 193 |     /* finding the facelet surface via cross-product */ | 
|---|
| 194 |     t1 = p - q; | 
|---|
| 195 |     t2 = p - r; | 
|---|
| 196 |     facelet[i] = 0.5f * /*fabs*/( t1.cross(t2).len() ); | 
|---|
| 197 |     /* update the entire convex hull surface */ | 
|---|
| 198 |     face += facelet[i]; | 
|---|
| 199 |  | 
|---|
| 200 |     /* calculate the cetroid of the hull triangles */ | 
|---|
| 201 |     centroid[i] = (p + q + r) / 3.0f; | 
|---|
| 202 |     /* now calculate the centroid of the entire convex hull, weighted average of triangle centroids */ | 
|---|
| 203 |     center += centroid[i] * facelet[i]; | 
|---|
| 204 |     /* the arithmetical center */ | 
|---|
| 205 |   } | 
|---|
| 206 |   /* take the average of the centroid sum */ | 
|---|
| 207 |   center /= face; | 
|---|
| 208 |  | 
|---|
| 209 |  | 
|---|
| 210 |   /* now calculate the covariance matrix - if not written in three for-loops, | 
|---|
| 211 |      it would compute faster: minor */ | 
|---|
| 212 |   for( int j = 0; j < 3; ++j) | 
|---|
| 213 |   { | 
|---|
| 214 |     for( int k = 0; k < 3; ++k) | 
|---|
| 215 |     { | 
|---|
| 216 |       for( int i = 0; i < length; ++i) | 
|---|
| 217 |       { | 
|---|
| 218 |         p = (&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[0]]); | 
|---|
| 219 |         q = (&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[1]]); | 
|---|
| 220 |         r = (&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[2]]); | 
|---|
| 221 |  | 
|---|
| 222 |         covariance[j][k] = facelet[i] * (9.0f * centroid[i][j] * centroid[i][k] + p[j] * p[k] + | 
|---|
| 223 |                            q[j] * q[k] + r[j] * r[k]); | 
|---|
| 224 |       } | 
|---|
| 225 |       covariance[j][k] = covariance[j][k] / (12.0f * face) - center[j] * center[k]; | 
|---|
| 226 |     } | 
|---|
| 227 |   } | 
|---|
| 228 |   for( int i = 0; i < 3; ++i) | 
|---|
| 229 |   { | 
|---|
| 230 |     box.covarianceMatrix[i][0] = covariance[i][0]; | 
|---|
| 231 |     box.covarianceMatrix[i][1] = covariance[i][1]; | 
|---|
| 232 |     box.covarianceMatrix[i][2] = covariance[i][2]; | 
|---|
| 233 |   } | 
|---|
| 234 |   //box.center = center; | 
|---|
| 235 |   //FIXME setting center to 0,0,0 | 
|---|
| 236 |   box.center = Vector(); | 
|---|
| 237 |  | 
|---|
| 238 |  | 
|---|
| 239 |   /* debug output section*/ | 
|---|
| 240 |   PRINTF(3)("\nOBB Covariance Matrix:\n"); | 
|---|
| 241 |   for(int j = 0; j < 3; ++j) | 
|---|
| 242 |   { | 
|---|
| 243 |     PRINT(3)("\t\t"); | 
|---|
| 244 |     for(int k = 0; k < 3; ++k) | 
|---|
| 245 |     { | 
|---|
| 246 |       PRINT(3)("%11.4f\t", covariance[j][k]); | 
|---|
| 247 |     } | 
|---|
| 248 |     PRINT(3)("\n"); | 
|---|
| 249 |   } | 
|---|
| 250 |   PRINTF(3)("\nWeighteed OBB Center:\n\t\t%11.4f\t %11.4f\t %11.4f\n", center.x, center.y, center.z); | 
|---|
| 251 | //   PRINTF(3)("\nArithmetical OBB Center:\n\t\t%11.4f\t %11.4f\t %11.4f\n", box.arithCenter.x, box.arithCenter.y, box.arithCenter.z); | 
|---|
| 252 |  | 
|---|
| 253 |   /* write back the covariance matrix data to the object oriented bouning box */ | 
|---|
| 254 | } | 
|---|
| 255 |  | 
|---|
| 256 |  | 
|---|
| 257 |  | 
|---|
| 258 | /** | 
|---|
| 259 |  *  calculate the eigenvectors for the object oriented box | 
|---|
| 260 |  * @param box: reference to the box | 
|---|
| 261 |  * @param modelInf: the model info structure of the model | 
|---|
| 262 |  * @param tirangleIndexes: an array with the indexes of the triangles inside this | 
|---|
| 263 |  * @param length: the length of the indexes array | 
|---|
| 264 |  */ | 
|---|
| 265 | void OBBTreeNode::calculateBoxEigenvectors(OBB& box, const modelInfo& modelInf, | 
|---|
| 266 |     const int* triangleIndexes, int length) | 
|---|
| 267 | { | 
|---|
| 268 |  | 
|---|
| 269 |   Vector         axis[3];                            //!< the references to the obb axis | 
|---|
| 270 |   Matrix         covMat(  box.covarianceMatrix  );   //!< covariance matrix (in the matrix dataform) | 
|---|
| 271 |  | 
|---|
| 272 |   /* | 
|---|
| 273 |   now getting spanning vectors of the sub-space: | 
|---|
| 274 |   the eigenvectors of a symmertric matrix, such as the | 
|---|
| 275 |   covarience matrix are mutually orthogonal. | 
|---|
| 276 |   after normalizing them, they can be used as a the basis | 
|---|
| 277 |   vectors | 
|---|
| 278 |   */ | 
|---|
| 279 |  | 
|---|
| 280 |   /* calculate the axis */ | 
|---|
| 281 |   covMat.getEigenVectors(axis[0], axis[1], axis[2] ); | 
|---|
| 282 |   box.axis[0] = axis[0]; | 
|---|
| 283 |   box.axis[1] = axis[1]; | 
|---|
| 284 |   box.axis[2] = axis[2]; | 
|---|
| 285 |  | 
|---|
| 286 |   box.axis[0] = Vector(1,0,0); | 
|---|
| 287 |   box.axis[1] = Vector(0,1,0); | 
|---|
| 288 |   box.axis[2] = Vector(0,0,1); | 
|---|
| 289 |  | 
|---|
| 290 |   PRINTF(3)("Eigenvectors:\n"); | 
|---|
| 291 |   PRINT(3)("\t\t%11.2f \t%11.2f \t%11.2f\n", box.axis[0].x, box.axis[0].y, box.axis[0].z); | 
|---|
| 292 |   PRINT(3)("\t\t%11.2f \t%11.2f \t%11.2f\n", box.axis[1].x, box.axis[1].y, box.axis[1].z); | 
|---|
| 293 |   PRINT(3)("\t\t%11.2f \t%11.2f \t%11.2f\n", box.axis[2].x, box.axis[2].y, box.axis[2].z); | 
|---|
| 294 | } | 
|---|
| 295 |  | 
|---|
| 296 |  | 
|---|
| 297 |  | 
|---|
| 298 |  | 
|---|
| 299 | /** | 
|---|
| 300 |  *  calculate the eigenvectors for the object oriented box | 
|---|
| 301 |  * @param box: reference to the box | 
|---|
| 302 |  * @param modelInf: the model info structure of the model | 
|---|
| 303 |  * @param tirangleIndexes: an array with the indexes of the triangles inside this | 
|---|
| 304 |  * @param length: the length of the indexes array | 
|---|
| 305 |  */ | 
|---|
| 306 | void OBBTreeNode::calculateBoxAxis(OBB& box, const modelInfo& modelInf, const int* triangleIndexes, int length) | 
|---|
| 307 | { | 
|---|
| 308 |  | 
|---|
| 309 |   PRINTF(3)("Calculate Box Axis\n"); | 
|---|
| 310 |   /* now get the axis length */ | 
|---|
| 311 |   float               halfLength[3];                         //!< half length of the axis | 
|---|
| 312 |   float               tmpLength;                             //!< tmp save point for the length | 
|---|
| 313 |   Plane               p0(box.axis[0], box.center);           //!< the axis planes | 
|---|
| 314 |   Plane               p1(box.axis[1], box.center);           //!< the axis planes | 
|---|
| 315 |   Plane               p2(box.axis[2], box.center);           //!< the axis planes | 
|---|
| 316 |   float               maxLength[3];                          //!< maximal lenth of the axis | 
|---|
| 317 |   float               minLength[3];                          //!< minimal length of the axis | 
|---|
| 318 |   const float*        tmpVec;                                //!< variable taking tmp vectors | 
|---|
| 319 |   float               centerOffset[3]; | 
|---|
| 320 |  | 
|---|
| 321 |  | 
|---|
| 322 |  | 
|---|
| 323 |   /* get the maximal dimensions of the body in all directions */ | 
|---|
| 324 |   /* for the initialisation the value just has to be inside of the polygon soup -> first vertices (rand) */ | 
|---|
| 325 |   for( int k = 0; k  < 3; k++) | 
|---|
| 326 |   { | 
|---|
| 327 |     tmpVec = (&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[0]].indexToVertices[0]]); | 
|---|
| 328 |     Plane* p; | 
|---|
| 329 |     if( k == 0) | 
|---|
| 330 |       p = &p0; | 
|---|
| 331 |     else if( k == 1) | 
|---|
| 332 |       p = &p1; | 
|---|
| 333 |     else | 
|---|
| 334 |       p = &p2; | 
|---|
| 335 |     maxLength[k] = p->distancePoint(tmpVec); | 
|---|
| 336 |     minLength[k] = p->distancePoint(tmpVec); | 
|---|
| 337 |  | 
|---|
| 338 |  | 
|---|
| 339 |     for( int j = 0; j < length; ++j) { | 
|---|
| 340 |       for( int i = 0; i < 3; ++i) { | 
|---|
| 341 |         tmpVec = &modelInf.pVertices[modelInf.pTriangles[triangleIndexes[j]].indexToVertices[i]]; | 
|---|
| 342 |         tmpLength = p->distancePoint(tmpVec); | 
|---|
| 343 |         if( tmpLength > maxLength[k]) | 
|---|
| 344 |           maxLength[k] = tmpLength; | 
|---|
| 345 |         else if( tmpLength < minLength[k]) | 
|---|
| 346 |           minLength[k] = tmpLength; | 
|---|
| 347 |       } | 
|---|
| 348 |     } | 
|---|
| 349 |   } | 
|---|
| 350 |  | 
|---|
| 351 |  | 
|---|
| 352 | //   tmpVec = (&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[0]].indexToVertices[0]]); | 
|---|
| 353 | //   maxLength[1] = p1.distancePoint(tmpVec); | 
|---|
| 354 | //   minLength[1] = p1.distancePoint(tmpVec); | 
|---|
| 355 | //   for( int j = 0; j < length; ++j) | 
|---|
| 356 | //   { | 
|---|
| 357 | //     for( int i = 0; i < 3; ++i) | 
|---|
| 358 | //     { | 
|---|
| 359 | //       tmpVec = (&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[j]].indexToVertices[i]]); | 
|---|
| 360 | //       tmpLength = p1.distancePoint(tmpVec); | 
|---|
| 361 | //       if( tmpLength > maxLength[1]) | 
|---|
| 362 | //         maxLength[1] = tmpLength; | 
|---|
| 363 | //       else if( tmpLength < minLength[1]) | 
|---|
| 364 | //         minLength[1] = tmpLength; | 
|---|
| 365 | //     } | 
|---|
| 366 | //   } | 
|---|
| 367 | // | 
|---|
| 368 | // | 
|---|
| 369 | //   tmpVec = (&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[0]].indexToVertices[0]]); | 
|---|
| 370 | //   maxLength[2] = p2.distancePoint(tmpVec); | 
|---|
| 371 | //   minLength[2] = p2.distancePoint(tmpVec); | 
|---|
| 372 | //   for( int j = 0; j < length; ++j) | 
|---|
| 373 | //   { | 
|---|
| 374 | //     for( int i = 0; i < 3; ++i) | 
|---|
| 375 | //     { | 
|---|
| 376 | //       tmpVec = (&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[j]].indexToVertices[i]]); | 
|---|
| 377 | //       tmpLength = p2.distancePoint(tmpVec); | 
|---|
| 378 | //       if( tmpLength > maxLength[2]) | 
|---|
| 379 | //         maxLength[2] = tmpLength; | 
|---|
| 380 | //       else if( tmpLength < minLength[2]) | 
|---|
| 381 | //         minLength[2] = tmpLength; | 
|---|
| 382 | //     } | 
|---|
| 383 | //   } | 
|---|
| 384 |  | 
|---|
| 385 |  | 
|---|
| 386 |   /* calculate the real centre of the body by using the axis length */ | 
|---|
| 387 |  | 
|---|
| 388 |  | 
|---|
| 389 |   for( int i = 0; i < 3; ++i) | 
|---|
| 390 |   { | 
|---|
| 391 |     centerOffset[i] = (fabs(maxLength[i]) - fabs(minLength[i])) / 2.0f;       // min length is negatie | 
|---|
| 392 |     box.halfLength[i] = (maxLength[i] - minLength[i]) / 2.0f;                 // min length is negative | 
|---|
| 393 |   } | 
|---|
| 394 |  | 
|---|
| 395 |   // FIXME: += anstatt -= ????????? verwirr | 
|---|
| 396 |   box.center.x += centerOffset[0]; | 
|---|
| 397 |   box.center.y += centerOffset[1]; | 
|---|
| 398 |   box.center.z += centerOffset[2]; | 
|---|
| 399 |  | 
|---|
| 400 |   PRINTF(3)("\n"); | 
|---|
| 401 |   PRINT(3)("\tAxis halflength x: %11.2f (max: %11.2f, \tmin: %11.2f), offset: %11.2f\n", box.halfLength[0], maxLength[0], minLength[0], centerOffset[0]); | 
|---|
| 402 |   PRINT(3)("\tAxis halflength y: %11.2f (max: %11.2f, \tmin: %11.2f), offset: %11.2f\n", box.halfLength[1], maxLength[1], minLength[1], centerOffset[1] ); | 
|---|
| 403 |   PRINT(3)("\tAxis halflength z: %11.2f (max: %11.2f, \tmin: %11.2f), offset: %11.2f\n", box.halfLength[2], maxLength[2], minLength[2], centerOffset[2]); | 
|---|
| 404 | } | 
|---|
| 405 |  | 
|---|
| 406 |  | 
|---|
| 407 |  | 
|---|
| 408 | /** | 
|---|
| 409 |  *  this separates an ob-box in the middle | 
|---|
| 410 |  * @param box: the box to separate | 
|---|
| 411 |  * | 
|---|
| 412 |  * this will separate the box into to smaller boxes. the separation is done along the middle of the longest axis | 
|---|
| 413 |  */ | 
|---|
| 414 | void OBBTreeNode::forkBox(OBB& box) | 
|---|
| 415 | { | 
|---|
| 416 |  | 
|---|
| 417 |   PRINTF(3)("Fork Box\n"); | 
|---|
| 418 |   PRINTF(4)("Calculating the longest Axis\n"); | 
|---|
| 419 |   /* get the longest axis of the box */ | 
|---|
| 420 |   float               longestAxis = -1.0f;                 //!< the length of the longest axis | 
|---|
| 421 |   int                 longestAxisIndex = 0;                //!< this is the nr of the longest axis | 
|---|
| 422 |  | 
|---|
| 423 |  | 
|---|
| 424 |   /* now get the longest axis of the three exiting */ | 
|---|
| 425 |   for( int i = 0; i < 3; ++i) | 
|---|
| 426 |   { | 
|---|
| 427 |     if( longestAxis < box.halfLength[i]) | 
|---|
| 428 |     { | 
|---|
| 429 |       longestAxis = box.halfLength[i]; | 
|---|
| 430 |       longestAxisIndex = i; | 
|---|
| 431 |     } | 
|---|
| 432 |   } | 
|---|
| 433 |   PRINTF(3)("\nLongest Axis is: Nr %i with a half-length of:%11.2f\n", longestAxisIndex, longestAxis); | 
|---|
| 434 |  | 
|---|
| 435 |  | 
|---|
| 436 |   PRINTF(4)("Separating along the longest axis\n"); | 
|---|
| 437 |   /* get the closest vertex near the center */ | 
|---|
| 438 |   float               dist = 999999.0f;                    //!< the smallest distance to each vertex | 
|---|
| 439 |   float               tmpDist;                             //!< variable to save diverse distances temporarily | 
|---|
| 440 |   int                 vertexIndex;                         //!< index of the vertex near the center | 
|---|
| 441 |   Plane               middlePlane(box.axis[longestAxisIndex], box.center); //!< the middle plane | 
|---|
| 442 |   const sVec3D*       tmpVec;                              //!< temp simple 3D vector | 
|---|
| 443 |  | 
|---|
| 444 |  | 
|---|
| 445 |   /* now definin the separation plane through this specified nearest point and partition | 
|---|
| 446 |   the points depending on which side they are located | 
|---|
| 447 |   */ | 
|---|
| 448 |   std::list<int>           partition1;                           //!< the vertex partition 1 | 
|---|
| 449 |   std::list<int>           partition2;                           //!< the vertex partition 2 | 
|---|
| 450 |   float*                   triangleCenter = new float[3];        //!< the center of the triangle | 
|---|
| 451 |   const float*             a;                                    //!< triangle  edge a | 
|---|
| 452 |   const float*             b;                                    //!< triangle  edge b | 
|---|
| 453 |   const float*             c;                                    //!< triangle  edge c | 
|---|
| 454 |  | 
|---|
| 455 |  | 
|---|
| 456 |   /* find the center of the box */ | 
|---|
| 457 |   this->separationPlane = Plane(box.axis[longestAxisIndex], box.center); | 
|---|
| 458 |   this->sepPlaneCenter[0] = box.center.x; | 
|---|
| 459 |   this->sepPlaneCenter[1] = box.center.y; | 
|---|
| 460 |   this->sepPlaneCenter[2] = box.center.z; | 
|---|
| 461 |   this->longestAxisIndex = longestAxisIndex; | 
|---|
| 462 |  | 
|---|
| 463 |   for( int i = 0; i < box.triangleIndexesLength; ++i) | 
|---|
| 464 |   { | 
|---|
| 465 |     /* first calculate the middle of the triangle */ | 
|---|
| 466 |     a = &box.modelInf->pVertices[box.modelInf->pTriangles[box.triangleIndexes[i]].indexToVertices[0]]; | 
|---|
| 467 |     b = &box.modelInf->pVertices[box.modelInf->pTriangles[box.triangleIndexes[i]].indexToVertices[1]]; | 
|---|
| 468 |     c = &box.modelInf->pVertices[box.modelInf->pTriangles[box.triangleIndexes[i]].indexToVertices[2]]; | 
|---|
| 469 |  | 
|---|
| 470 |     triangleCenter[0] = (a[0] + b[0] + c[0]) / 3.0f; | 
|---|
| 471 |     triangleCenter[1] = (a[1] + b[1] + c[1]) / 3.0f; | 
|---|
| 472 |     triangleCenter[2] = (a[2] + b[2] + c[2]) / 3.0f; | 
|---|
| 473 |     tmpDist = this->separationPlane.distancePoint(*((sVec3D*)triangleCenter)); | 
|---|
| 474 |  | 
|---|
| 475 |     if( tmpDist > 0.0f) | 
|---|
| 476 |       partition1.push_back(box.triangleIndexes[i]); /* positive numbers plus zero */ | 
|---|
| 477 |     else if( tmpDist < 0.0f) | 
|---|
| 478 |       partition2.push_back(box.triangleIndexes[i]); /* negatice numbers */ | 
|---|
| 479 |     else { | 
|---|
| 480 |       partition1.push_back(box.triangleIndexes[i]); /* 0.0f? unprobable... */ | 
|---|
| 481 |       partition2.push_back(box.triangleIndexes[i]); | 
|---|
| 482 |     } | 
|---|
| 483 |   } | 
|---|
| 484 |   PRINTF(3)("\nPartition1: got \t%i Vertices \nPartition2: got \t%i Vertices\n", partition1.size(), partition2.size()); | 
|---|
| 485 |  | 
|---|
| 486 |  | 
|---|
| 487 |   /* now comes the separation into two different sVec3D arrays */ | 
|---|
| 488 |   int                index;                                //!< index storage place | 
|---|
| 489 |   int*               triangleIndexList1;                   //!< the vertex list 1 | 
|---|
| 490 |   int*               triangleIndexList2;                   //!< the vertex list 2 | 
|---|
| 491 |   std::list<int>::iterator element;                        //!< the list iterator | 
|---|
| 492 |  | 
|---|
| 493 |   triangleIndexList1 = new int[partition1.size()]; | 
|---|
| 494 |   triangleIndexList2 = new int[partition2.size()]; | 
|---|
| 495 |  | 
|---|
| 496 |   for( element = partition1.begin(), index = 0; element != partition1.end(); element++, index++) | 
|---|
| 497 |     triangleIndexList1[index] = (*element); | 
|---|
| 498 |  | 
|---|
| 499 |   for( element = partition2.begin(), index = 0; element != partition2.end(); element++, index++) | 
|---|
| 500 |     triangleIndexList2[index] = (*element); | 
|---|
| 501 |  | 
|---|
| 502 |   if( this->triangleIndexList1!= NULL) | 
|---|
| 503 |     delete[] this->triangleIndexList1; | 
|---|
| 504 |   this->triangleIndexList1 = triangleIndexList1; | 
|---|
| 505 |   this->triangleIndexLength1 = partition1.size(); | 
|---|
| 506 |  | 
|---|
| 507 |   if( this->triangleIndexList2 != NULL) | 
|---|
| 508 |     delete[] this->triangleIndexList2; | 
|---|
| 509 |   this->triangleIndexList2 = triangleIndexList2; | 
|---|
| 510 |   this->triangleIndexLength2 = partition2.size(); | 
|---|
| 511 | } | 
|---|
| 512 |  | 
|---|
| 513 |  | 
|---|
| 514 |  | 
|---|
| 515 |  | 
|---|
| 516 | void OBBTreeNode::collideWith(BVTreeNode* treeNode, WorldEntity* nodeA, WorldEntity* nodeB) | 
|---|
| 517 | { | 
|---|
| 518 |   if( unlikely(treeNode == NULL)) | 
|---|
| 519 |     return; | 
|---|
| 520 |  | 
|---|
| 521 |   PRINTF(3)("collideWith\n"); | 
|---|
| 522 |   /* if the obb overlap, make subtests: check which node is realy overlaping  */ | 
|---|
| 523 |   PRINTF(3)("Checking OBB %i vs %i: ", this->getIndex(), treeNode->getIndex()); | 
|---|
| 524 |   //   if( unlikely(treeNode == NULL)) return; | 
|---|
| 525 |  | 
|---|
| 526 |  | 
|---|
| 527 |   if( this->overlapTest(*this->bvElement, *(((const OBBTreeNode*)&treeNode)->bvElement), nodeA, nodeB)) | 
|---|
| 528 |   { | 
|---|
| 529 |     PRINTF(3)("collision @ lvl %i, object %s vs. %s, (%p, %p)\n", this->depth, nodeA->getClassName(), nodeB->getClassName(), this->nodeLeft, this->nodeRight); | 
|---|
| 530 |  | 
|---|
| 531 |     /* check if left node overlaps */ | 
|---|
| 532 |     if( likely( this->nodeLeft != NULL)) | 
|---|
| 533 |     { | 
|---|
| 534 |       PRINTF(3)("Checking OBB %i vs %i: ", this->nodeLeft->getIndex(), treeNode->getIndex()); | 
|---|
| 535 |       if( this->overlapTest(*this->nodeLeft->bvElement, *(((const OBBTreeNode*)&treeNode)->bvElement), nodeA, nodeB)) | 
|---|
| 536 |       { | 
|---|
| 537 |         this->nodeLeft->collideWith((((const OBBTreeNode*)treeNode)->nodeLeft), nodeA, nodeB); | 
|---|
| 538 |         this->nodeLeft->collideWith((((const OBBTreeNode*)treeNode)->nodeRight), nodeA, nodeB); | 
|---|
| 539 |       } | 
|---|
| 540 |     } | 
|---|
| 541 |     /* check if right node overlaps */ | 
|---|
| 542 |     if( likely( this->nodeRight != NULL)) | 
|---|
| 543 |     { | 
|---|
| 544 |       PRINTF(3)("Checking OBB %i vs %i: ", this->nodeRight->getIndex(), treeNode->getIndex()); | 
|---|
| 545 |       if(this->overlapTest(*this->nodeRight->bvElement, *(((const OBBTreeNode*)&treeNode)->bvElement), nodeA, nodeB)) | 
|---|
| 546 |       { | 
|---|
| 547 |         this->nodeRight->collideWith((((const OBBTreeNode*)treeNode)->nodeLeft), nodeA, nodeB); | 
|---|
| 548 |         this->nodeRight->collideWith((((const OBBTreeNode*)treeNode)->nodeRight), nodeA, nodeB); | 
|---|
| 549 |       } | 
|---|
| 550 |     } | 
|---|
| 551 |  | 
|---|
| 552 |     /* so there is a collision and this is the last box in the tree (i.e. leaf) */ | 
|---|
| 553 |     /* FIXME: If we would choose || insead of && there would also be asymmetrical cases supported */ | 
|---|
| 554 |     if( unlikely(this->nodeRight == NULL && this->nodeLeft == NULL)) | 
|---|
| 555 |     { | 
|---|
| 556 |       nodeA->collidesWith(nodeB, (((const OBBTreeNode*)&treeNode)->bvElement->center)); | 
|---|
| 557 |  | 
|---|
| 558 |       nodeB->collidesWith(nodeA, this->bvElement->center); | 
|---|
| 559 |     } | 
|---|
| 560 |  | 
|---|
| 561 |   } | 
|---|
| 562 | } | 
|---|
| 563 |  | 
|---|
| 564 |  | 
|---|
| 565 |  | 
|---|
| 566 | bool OBBTreeNode::overlapTest(OBB& boxA, OBB& boxB, WorldEntity* nodeA, WorldEntity* nodeB) | 
|---|
| 567 | { | 
|---|
| 568 |   //HACK remove this again | 
|---|
| 569 |   this->owner = nodeA; | 
|---|
| 570 |   //   if( boxB == NULL || boxA == NULL) | 
|---|
| 571 |   //     return false; | 
|---|
| 572 |  | 
|---|
| 573 |   /* first check all axis */ | 
|---|
| 574 |   Vector t; | 
|---|
| 575 |   float rA = 0.0f; | 
|---|
| 576 |   float rB = 0.0f; | 
|---|
| 577 |   Vector l; | 
|---|
| 578 |   Vector rotAxisA[3]; | 
|---|
| 579 |   Vector rotAxisB[3]; | 
|---|
| 580 |  | 
|---|
| 581 |   rotAxisA[0] =  nodeA->getAbsDir().apply(boxA.axis[0]); | 
|---|
| 582 |   rotAxisA[1] =  nodeA->getAbsDir().apply(boxA.axis[1]); | 
|---|
| 583 |   rotAxisA[2] =  nodeA->getAbsDir().apply(boxA.axis[2]); | 
|---|
| 584 |  | 
|---|
| 585 |   rotAxisB[0] =  nodeB->getAbsDir().apply(boxB.axis[0]); | 
|---|
| 586 |   rotAxisB[1] =  nodeB->getAbsDir().apply(boxB.axis[1]); | 
|---|
| 587 |   rotAxisB[2] =  nodeB->getAbsDir().apply(boxB.axis[2]); | 
|---|
| 588 |  | 
|---|
| 589 |  | 
|---|
| 590 |   t = nodeA->getAbsCoor() + nodeA->getAbsDir().apply(boxA.center) - ( nodeB->getAbsCoor() + nodeB->getAbsDir().apply(boxB.center)); | 
|---|
| 591 |  | 
|---|
| 592 |   //   printf("\n"); | 
|---|
| 593 |   //   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); | 
|---|
| 594 |   //   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); | 
|---|
| 595 |   //   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); | 
|---|
| 596 |   // | 
|---|
| 597 |   //   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); | 
|---|
| 598 |   //   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); | 
|---|
| 599 |   //   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); | 
|---|
| 600 |  | 
|---|
| 601 |  | 
|---|
| 602 |   /* All 3 axis of the object A */ | 
|---|
| 603 |   for( int j = 0; j < 3; ++j) | 
|---|
| 604 |   { | 
|---|
| 605 |     rA = 0.0f; | 
|---|
| 606 |     rB = 0.0f; | 
|---|
| 607 |     l = rotAxisA[j]; | 
|---|
| 608 |  | 
|---|
| 609 |     rA += fabs(boxA.halfLength[0] * rotAxisA[0].dot(l)); | 
|---|
| 610 |     rA += fabs(boxA.halfLength[1] * rotAxisA[1].dot(l)); | 
|---|
| 611 |     rA += fabs(boxA.halfLength[2] * rotAxisA[2].dot(l)); | 
|---|
| 612 |  | 
|---|
| 613 |     rB += fabs(boxB.halfLength[0] * rotAxisB[0].dot(l)); | 
|---|
| 614 |     rB += fabs(boxB.halfLength[1] * rotAxisB[1].dot(l)); | 
|---|
| 615 |     rB += fabs(boxB.halfLength[2] * rotAxisB[2].dot(l)); | 
|---|
| 616 |  | 
|---|
| 617 |     PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB); | 
|---|
| 618 |  | 
|---|
| 619 |     if( (rA + rB) < fabs(t.dot(l))) | 
|---|
| 620 |     { | 
|---|
| 621 |       PRINTF(3)("no Collision\n"); | 
|---|
| 622 |       return false; | 
|---|
| 623 |     } | 
|---|
| 624 |   } | 
|---|
| 625 |  | 
|---|
| 626 |   /* All 3 axis of the object B */ | 
|---|
| 627 |   for( int j = 0; j < 3; ++j) | 
|---|
| 628 |   { | 
|---|
| 629 |     rA = 0.0f; | 
|---|
| 630 |     rB = 0.0f; | 
|---|
| 631 |     l = rotAxisB[j]; | 
|---|
| 632 |  | 
|---|
| 633 |     rA += fabs(boxA.halfLength[0] * rotAxisA[0].dot(l)); | 
|---|
| 634 |     rA += fabs(boxA.halfLength[1] * rotAxisA[1].dot(l)); | 
|---|
| 635 |     rA += fabs(boxA.halfLength[2] * rotAxisA[2].dot(l)); | 
|---|
| 636 |  | 
|---|
| 637 |     rB += fabs(boxB.halfLength[0] * rotAxisB[0].dot(l)); | 
|---|
| 638 |     rB += fabs(boxB.halfLength[1] * rotAxisB[1].dot(l)); | 
|---|
| 639 |     rB += fabs(boxB.halfLength[2] * rotAxisB[2].dot(l)); | 
|---|
| 640 |  | 
|---|
| 641 |     PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB); | 
|---|
| 642 |  | 
|---|
| 643 |     if( (rA + rB) < fabs(t.dot(l))) | 
|---|
| 644 |     { | 
|---|
| 645 |       PRINTF(3)("no Collision\n"); | 
|---|
| 646 |       return false; | 
|---|
| 647 |     } | 
|---|
| 648 |   } | 
|---|
| 649 |  | 
|---|
| 650 |  | 
|---|
| 651 |   /* Now check for all face cross products */ | 
|---|
| 652 |  | 
|---|
| 653 |   for( int j = 0; j < 3; ++j) | 
|---|
| 654 |   { | 
|---|
| 655 |     for(int k = 0; k < 3; ++k ) | 
|---|
| 656 |     { | 
|---|
| 657 |       rA = 0.0f; | 
|---|
| 658 |       rB = 0.0f; | 
|---|
| 659 |       l = rotAxisA[j].cross(rotAxisB[k]); | 
|---|
| 660 |  | 
|---|
| 661 |       rA += fabs(boxA.halfLength[0] * rotAxisA[0].dot(l)); | 
|---|
| 662 |       rA += fabs(boxA.halfLength[1] * rotAxisA[1].dot(l)); | 
|---|
| 663 |       rA += fabs(boxA.halfLength[2] * rotAxisA[2].dot(l)); | 
|---|
| 664 |  | 
|---|
| 665 |       rB += fabs(boxB.halfLength[0] * rotAxisB[0].dot(l)); | 
|---|
| 666 |       rB += fabs(boxB.halfLength[1] * rotAxisB[1].dot(l)); | 
|---|
| 667 |       rB += fabs(boxB.halfLength[2] * rotAxisB[2].dot(l)); | 
|---|
| 668 |  | 
|---|
| 669 |       PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB); | 
|---|
| 670 |  | 
|---|
| 671 |       if( (rA + rB) < fabs(t.dot(l))) | 
|---|
| 672 |       { | 
|---|
| 673 |         PRINTF(3)("keine Kollision\n"); | 
|---|
| 674 |         return false; | 
|---|
| 675 |       } | 
|---|
| 676 |     } | 
|---|
| 677 |   } | 
|---|
| 678 |  | 
|---|
| 679 |   /* FIXME: there is no collision mark set now */ | 
|---|
| 680 |      boxA.bCollided = true; /* use this ONLY(!!!!) for drawing operations */ | 
|---|
| 681 |      boxB.bCollided = true; | 
|---|
| 682 |  | 
|---|
| 683 |  | 
|---|
| 684 |   PRINTF(3)("Kollision!\n"); | 
|---|
| 685 |   return true; | 
|---|
| 686 | } | 
|---|
| 687 |  | 
|---|
| 688 |  | 
|---|
| 689 |  | 
|---|
| 690 |  | 
|---|
| 691 |  | 
|---|
| 692 |  | 
|---|
| 693 |  | 
|---|
| 694 |  | 
|---|
| 695 |  | 
|---|
| 696 |  | 
|---|
| 697 | /** | 
|---|
| 698 |  * | 
|---|
| 699 |  * draw the BV tree - debug mode | 
|---|
| 700 |  */ | 
|---|
| 701 | void OBBTreeNode::drawBV(int depth, int drawMode, const Vector& color,  bool top) const | 
|---|
| 702 | { | 
|---|
| 703 |   /* this function can be used to draw the triangles and/or the points only  */ | 
|---|
| 704 |   if( 1 /*drawMode & DRAW_MODEL || drawMode & DRAW_ALL*/) | 
|---|
| 705 |   { | 
|---|
| 706 |     if( depth == 0/*!(drawMode & DRAW_SINGLE && depth != 0)*/) | 
|---|
| 707 |     { | 
|---|
| 708 |       if( 1 /*drawMode & DRAW_POINTS*/) | 
|---|
| 709 |       { | 
|---|
| 710 |         glBegin(GL_POINTS); | 
|---|
| 711 |         glColor3f(0.3, 0.8, 0.54); | 
|---|
| 712 |         for( int i = 0; i < this->bvElement->modelInf->numVertices*3; i+=3) | 
|---|
| 713 |           glVertex3f(this->bvElement->modelInf->pVertices[i], | 
|---|
| 714 |                      this->bvElement->modelInf->pVertices[i+1], | 
|---|
| 715 |                      this->bvElement->modelInf->pVertices[i+2]); | 
|---|
| 716 |         glEnd(); | 
|---|
| 717 |       } | 
|---|
| 718 |     } | 
|---|
| 719 |   } | 
|---|
| 720 |  | 
|---|
| 721 |   if (top) | 
|---|
| 722 |   { | 
|---|
| 723 |     glPushAttrib(GL_ENABLE_BIT); | 
|---|
| 724 |     glDisable(GL_LIGHTING); | 
|---|
| 725 |     glDisable(GL_TEXTURE_2D); | 
|---|
| 726 |   } | 
|---|
| 727 |   glColor3f(color.x, color.y, color.z); | 
|---|
| 728 |  | 
|---|
| 729 |  | 
|---|
| 730 |   /* draw world axes */ | 
|---|
| 731 |   if( 1 /*drawMode & DRAW_BV_AXIS*/) | 
|---|
| 732 |   { | 
|---|
| 733 |     glBegin(GL_LINES); | 
|---|
| 734 |     glColor3f(1.0, 0.0, 0.0); | 
|---|
| 735 |     glVertex3f(0.0, 0.0, 0.0); | 
|---|
| 736 |     glVertex3f(3.0, 0.0, 0.0); | 
|---|
| 737 |  | 
|---|
| 738 |     glColor3f(0.0, 1.0, 0.0); | 
|---|
| 739 |     glVertex3f(0.0, 0.0, 0.0); | 
|---|
| 740 |     glVertex3f(0.0, 3.0, 0.0); | 
|---|
| 741 |  | 
|---|
| 742 |     glColor3f(0.0, 0.0, 1.0); | 
|---|
| 743 |     glVertex3f(0.0, 0.0, 0.0); | 
|---|
| 744 |     glVertex3f(0.0, 0.0, 3.0); | 
|---|
| 745 |     glEnd(); | 
|---|
| 746 |   } | 
|---|
| 747 |  | 
|---|
| 748 |  | 
|---|
| 749 |   if( 1/*drawMode & DRAW_BV_AXIS || drawMode & DRAW_ALL*/) | 
|---|
| 750 |   { | 
|---|
| 751 |     if( 1/*drawMode & DRAW_SINGLE && depth != 0*/) | 
|---|
| 752 |     { | 
|---|
| 753 |       /* draw the obb axes */ | 
|---|
| 754 |       glBegin(GL_LINES); | 
|---|
| 755 |       glColor3f(1.0, 0.0, 0.0); | 
|---|
| 756 |       glVertex3f(this->bvElement->center.x, this->bvElement->center.y, this->bvElement->center.z); | 
|---|
| 757 |       glVertex3f(this->bvElement->center.x + this->bvElement->axis[0].x * this->bvElement->halfLength[0], | 
|---|
| 758 |                  this->bvElement->center.y + this->bvElement->axis[0].y * this->bvElement->halfLength[0], | 
|---|
| 759 |                  this->bvElement->center.z + this->bvElement->axis[0].z * this->bvElement->halfLength[0]); | 
|---|
| 760 |  | 
|---|
| 761 |       glColor3f(0.0, 1.0, 0.0); | 
|---|
| 762 |       glVertex3f(this->bvElement->center.x, this->bvElement->center.y, this->bvElement->center.z); | 
|---|
| 763 |       glVertex3f(this->bvElement->center.x + this->bvElement->axis[1].x * this->bvElement->halfLength[1], | 
|---|
| 764 |                  this->bvElement->center.y + this->bvElement->axis[1].y * this->bvElement->halfLength[1], | 
|---|
| 765 |                  this->bvElement->center.z + this->bvElement->axis[1].z * this->bvElement->halfLength[1]); | 
|---|
| 766 |  | 
|---|
| 767 |       glColor3f(0.0, 0.0, 1.0); | 
|---|
| 768 |       glVertex3f(this->bvElement->center.x, this->bvElement->center.y, this->bvElement->center.z); | 
|---|
| 769 |       glVertex3f(this->bvElement->center.x + this->bvElement->axis[2].x * this->bvElement->halfLength[2], | 
|---|
| 770 |                  this->bvElement->center.y + this->bvElement->axis[2].y * this->bvElement->halfLength[2], | 
|---|
| 771 |                  this->bvElement->center.z + this->bvElement->axis[2].z * this->bvElement->halfLength[2]); | 
|---|
| 772 |       glEnd(); | 
|---|
| 773 |     } | 
|---|
| 774 |   } | 
|---|
| 775 |  | 
|---|
| 776 |  | 
|---|
| 777 |   /* DRAW POLYGONS */ | 
|---|
| 778 |   if( drawMode & DRAW_BV_POLYGON || drawMode & DRAW_ALL || drawMode & DRAW_BV_BLENDED) | 
|---|
| 779 |   { | 
|---|
| 780 |     if (top) | 
|---|
| 781 |     { | 
|---|
| 782 |       glEnable(GL_BLEND); | 
|---|
| 783 |       glBlendFunc(GL_SRC_ALPHA, GL_ONE); | 
|---|
| 784 |     } | 
|---|
| 785 |  | 
|---|
| 786 |     if( this->nodeLeft == NULL && this->nodeRight == NULL) | 
|---|
| 787 |       depth = 0; | 
|---|
| 788 |  | 
|---|
| 789 |     if( depth == 0 /*!(drawMode & DRAW_SINGLE && depth != 0)*/) | 
|---|
| 790 |     { | 
|---|
| 791 |  | 
|---|
| 792 |  | 
|---|
| 793 |       Vector cen = this->bvElement->center; | 
|---|
| 794 |       Vector* axis = this->bvElement->axis; | 
|---|
| 795 |       float* len = this->bvElement->halfLength; | 
|---|
| 796 |  | 
|---|
| 797 |       if( this->bvElement->bCollided) | 
|---|
| 798 |       { | 
|---|
| 799 |         glColor4f(1.0, 1.0, 1.0, .5); // COLLISION COLOR | 
|---|
| 800 |       } | 
|---|
| 801 |       else if( drawMode & DRAW_BV_BLENDED) | 
|---|
| 802 |       { | 
|---|
| 803 |         glColor4f(color.x, color.y, color.z, .5); | 
|---|
| 804 |       } | 
|---|
| 805 |  | 
|---|
| 806 |       // debug out | 
|---|
| 807 |       if( this->obbTree->getOwner() != NULL) | 
|---|
| 808 |       { | 
|---|
| 809 |         PRINTF(4)("debug poly draw: depth: %i, mode: %i, entity-name: %s, class: %s\n", depth, drawMode, this->obbTree->getOwner()->getName(), this->obbTree->getOwner()->getClassName()); | 
|---|
| 810 |       } | 
|---|
| 811 |       else | 
|---|
| 812 |         PRINTF(4)("debug poly draw: depth: %i, mode: %i\n", depth, drawMode); | 
|---|
| 813 |  | 
|---|
| 814 |  | 
|---|
| 815 |       /* draw bounding box */ | 
|---|
| 816 |       if( drawMode & DRAW_BV_BLENDED) | 
|---|
| 817 |         glBegin(GL_QUADS); | 
|---|
| 818 |       else | 
|---|
| 819 |         glBegin(GL_LINE_LOOP); | 
|---|
| 820 |       glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], | 
|---|
| 821 |                  cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], | 
|---|
| 822 |                  cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); | 
|---|
| 823 |       glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], | 
|---|
| 824 |                  cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], | 
|---|
| 825 |                  cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); | 
|---|
| 826 |       glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], | 
|---|
| 827 |                  cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], | 
|---|
| 828 |                  cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); | 
|---|
| 829 |       glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], | 
|---|
| 830 |                  cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], | 
|---|
| 831 |                  cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); | 
|---|
| 832 |       glEnd(); | 
|---|
| 833 |  | 
|---|
| 834 |       if( drawMode & DRAW_BV_BLENDED) | 
|---|
| 835 |         glBegin(GL_QUADS); | 
|---|
| 836 |       else | 
|---|
| 837 |         glBegin(GL_LINE_LOOP); | 
|---|
| 838 |       glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], | 
|---|
| 839 |                  cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], | 
|---|
| 840 |                  cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); | 
|---|
| 841 |       glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], | 
|---|
| 842 |                  cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], | 
|---|
| 843 |                  cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); | 
|---|
| 844 |       glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], | 
|---|
| 845 |                  cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], | 
|---|
| 846 |                  cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); | 
|---|
| 847 |       glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], | 
|---|
| 848 |                  cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], | 
|---|
| 849 |                  cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); | 
|---|
| 850 |       glEnd(); | 
|---|
| 851 |  | 
|---|
| 852 |       if( drawMode & DRAW_BV_BLENDED) | 
|---|
| 853 |         glBegin(GL_QUADS); | 
|---|
| 854 |       else | 
|---|
| 855 |         glBegin(GL_LINE_LOOP); | 
|---|
| 856 |       glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], | 
|---|
| 857 |                  cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], | 
|---|
| 858 |                  cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); | 
|---|
| 859 |       glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], | 
|---|
| 860 |                  cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], | 
|---|
| 861 |                  cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); | 
|---|
| 862 |       glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], | 
|---|
| 863 |                  cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], | 
|---|
| 864 |                  cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); | 
|---|
| 865 |       glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], | 
|---|
| 866 |                  cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], | 
|---|
| 867 |                  cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); | 
|---|
| 868 |       glEnd(); | 
|---|
| 869 |  | 
|---|
| 870 |       if( drawMode & DRAW_BV_BLENDED) | 
|---|
| 871 |         glBegin(GL_QUADS); | 
|---|
| 872 |       else | 
|---|
| 873 |         glBegin(GL_LINE_LOOP); | 
|---|
| 874 |       glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], | 
|---|
| 875 |                  cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], | 
|---|
| 876 |                  cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); | 
|---|
| 877 |       glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], | 
|---|
| 878 |                  cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], | 
|---|
| 879 |                  cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); | 
|---|
| 880 |       glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], | 
|---|
| 881 |                  cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], | 
|---|
| 882 |                  cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); | 
|---|
| 883 |       glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], | 
|---|
| 884 |                  cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], | 
|---|
| 885 |                  cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); | 
|---|
| 886 |       glEnd(); | 
|---|
| 887 |  | 
|---|
| 888 |  | 
|---|
| 889 |       if( drawMode & DRAW_BV_BLENDED) | 
|---|
| 890 |       { | 
|---|
| 891 |         glBegin(GL_QUADS); | 
|---|
| 892 |         glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], | 
|---|
| 893 |                    cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], | 
|---|
| 894 |                    cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); | 
|---|
| 895 |         glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], | 
|---|
| 896 |                    cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], | 
|---|
| 897 |                    cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); | 
|---|
| 898 |         glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], | 
|---|
| 899 |                    cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], | 
|---|
| 900 |                    cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); | 
|---|
| 901 |         glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], | 
|---|
| 902 |                    cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], | 
|---|
| 903 |                    cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); | 
|---|
| 904 |         glEnd(); | 
|---|
| 905 |  | 
|---|
| 906 |         glBegin(GL_QUADS); | 
|---|
| 907 |         glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], | 
|---|
| 908 |                    cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], | 
|---|
| 909 |                    cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); | 
|---|
| 910 |         glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], | 
|---|
| 911 |                    cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], | 
|---|
| 912 |                    cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); | 
|---|
| 913 |         glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], | 
|---|
| 914 |                    cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], | 
|---|
| 915 |                    cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); | 
|---|
| 916 |         glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], | 
|---|
| 917 |                    cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], | 
|---|
| 918 |                    cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); | 
|---|
| 919 |         glEnd(); | 
|---|
| 920 |       } | 
|---|
| 921 |  | 
|---|
| 922 |       if( drawMode & DRAW_BV_BLENDED) | 
|---|
| 923 |         glColor3f(color.x, color.y, color.z); | 
|---|
| 924 |     } | 
|---|
| 925 |   } | 
|---|
| 926 |  | 
|---|
| 927 |   /* DRAW SEPARATING PLANE */ | 
|---|
| 928 |   if( drawMode & DRAW_SEPARATING_PLANE || drawMode & DRAW_ALL) | 
|---|
| 929 |   { | 
|---|
| 930 |     if( !(drawMode & DRAW_SINGLE && depth != 0)) | 
|---|
| 931 |     { | 
|---|
| 932 |       if( drawMode & DRAW_BV_BLENDED) | 
|---|
| 933 |         glColor4f(color.x, color.y, color.z, .6); | 
|---|
| 934 |  | 
|---|
| 935 |       /* now draw the separation plane */ | 
|---|
| 936 |       Vector a1 = this->bvElement->axis[(this->longestAxisIndex + 1)%3]; | 
|---|
| 937 |       Vector a2 = this->bvElement->axis[(this->longestAxisIndex + 2)%3]; | 
|---|
| 938 |       Vector c = this->bvElement->center; | 
|---|
| 939 |       float l1 = this->bvElement->halfLength[(this->longestAxisIndex + 1)%3]; | 
|---|
| 940 |       float l2 = this->bvElement->halfLength[(this->longestAxisIndex + 2)%3]; | 
|---|
| 941 |       glBegin(GL_QUADS); | 
|---|
| 942 |       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); | 
|---|
| 943 |       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); | 
|---|
| 944 |       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); | 
|---|
| 945 |       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); | 
|---|
| 946 |       glEnd(); | 
|---|
| 947 |  | 
|---|
| 948 |       if( drawMode & DRAW_BV_BLENDED) | 
|---|
| 949 |         glColor4f(color.x, color.y, color.z, 1.0); | 
|---|
| 950 |  | 
|---|
| 951 |     } | 
|---|
| 952 |   } | 
|---|
| 953 |  | 
|---|
| 954 |  | 
|---|
| 955 |  | 
|---|
| 956 |   if (depth > 0) | 
|---|
| 957 |   { | 
|---|
| 958 |     if( this->nodeLeft != NULL) | 
|---|
| 959 |       this->nodeLeft->drawBV(depth - 1, drawMode, Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(15.0,0.0,0.0)), false); | 
|---|
| 960 |     if( this->nodeRight != NULL) | 
|---|
| 961 |       this->nodeRight->drawBV(depth - 1, drawMode, Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(30.0,0.0,0.0)), false); | 
|---|
| 962 |   } | 
|---|
| 963 |   this->bvElement->bCollided = false; | 
|---|
| 964 |  | 
|---|
| 965 |   if (top) | 
|---|
| 966 |     glPopAttrib(); | 
|---|
| 967 | } | 
|---|
| 968 |  | 
|---|
| 969 |  | 
|---|
| 970 |  | 
|---|
| 971 | void OBBTreeNode::debug() const | 
|---|
| 972 | { | 
|---|
| 973 |   PRINT(0)("========OBBTreeNode::debug()=====\n"); | 
|---|
| 974 |   PRINT(0)(" Current depth: %i", this->depth); | 
|---|
| 975 |   PRINT(0)(" "); | 
|---|
| 976 |   PRINT(0)("=================================\n"); | 
|---|
| 977 | } | 
|---|