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