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