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