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