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