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