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