[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 */ |
---|
[5929] | 171 | centroid[i] = (p + q + r) / 3.0f; |
---|
[5706] | 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 | |
---|
[5929] | 195 | covariance[j][k] = facelet[i] * (9.0f * centroid[i][j] * centroid[i][k] + p[j] * p[k] + |
---|
| 196 | q[j] * q[k] + r[j] * r[k]); |
---|
[4648] | 197 | } |
---|
[5929] | 198 | covariance[j][k] = covariance[j][k] / (12.0f * face) - center[j] * center[k]; |
---|
[4648] | 199 | } |
---|
| 200 | } |
---|
[5923] | 201 | for(int i = 0; i < 3; ++i) |
---|
| 202 | { |
---|
| 203 | box.covarianceMatrix[i][0] = covariance[i][0]; |
---|
| 204 | box.covarianceMatrix[i][1] = covariance[i][1]; |
---|
| 205 | box.covarianceMatrix[i][2] = covariance[i][2]; |
---|
| 206 | } |
---|
| 207 | box.center = center; |
---|
[4648] | 208 | |
---|
[5922] | 209 | |
---|
| 210 | std::vector<int> vertIndexVector; //!< vertex indexes list |
---|
| 211 | int vertIndex; //!< index to vertex |
---|
| 212 | bool vertexFound; //!< vertex found flag |
---|
| 213 | Vector arithCenter; //!< aritmetical center |
---|
| 214 | |
---|
| 215 | /* calculate the arithmetical center of the box */ |
---|
| 216 | |
---|
| 217 | /* go thourgh all vertices, add only the used vertices indexes */ |
---|
| 218 | for( int i = 0; i < length; ++i) |
---|
| 219 | { |
---|
| 220 | for(int j = 0; j < 3; ++j) |
---|
| 221 | { |
---|
| 222 | vertIndex = modelInf.pTriangles[triangleIndexes[i]].indexToVertices[j]; |
---|
| 223 | |
---|
| 224 | vertexFound = false; |
---|
| 225 | for( int i = 0; i < vertIndexVector.size(); i++) |
---|
| 226 | { |
---|
| 227 | if( vertIndexVector[i] == vertIndex) |
---|
| 228 | vertexFound = true; |
---|
| 229 | } |
---|
| 230 | if( !vertexFound) |
---|
| 231 | vertIndexVector.push_back(vertIndex); |
---|
| 232 | } |
---|
| 233 | } |
---|
| 234 | /* now realy calculate the center */ |
---|
| 235 | for( int i = 0; i < vertIndexVector.size(); ++i) |
---|
| 236 | { |
---|
| 237 | tmpVec = (sVec3D*)(&modelInf.pVertices[vertIndexVector[i]]); |
---|
| 238 | arithCenter += *tmpVec; |
---|
| 239 | } |
---|
| 240 | box.arithCenter = arithCenter / vertIndexVector.size(); |
---|
| 241 | |
---|
| 242 | |
---|
| 243 | |
---|
[5923] | 244 | /* debug output section*/ |
---|
[5706] | 245 | PRINTF(3)("\nOBB Covariance Matrix:\n"); |
---|
[5825] | 246 | for(int j = 0; j < 3; ++j) |
---|
| 247 | { |
---|
[5882] | 248 | PRINT(3)("\t\t"); |
---|
[5825] | 249 | for(int k = 0; k < 3; ++k) |
---|
| 250 | { |
---|
[5929] | 251 | PRINT(3)("%11.4f\t", covariance[j][k]); |
---|
[5825] | 252 | } |
---|
[5882] | 253 | PRINT(3)("\n"); |
---|
[5825] | 254 | } |
---|
[5929] | 255 | PRINTF(3)("\nWeighteed OBB Center:\n\t\t%11.4f\t %11.4f\t %11.4f\n", center.x, center.y, center.z); |
---|
| 256 | 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); |
---|
[4588] | 257 | |
---|
[5825] | 258 | /* write back the covariance matrix data to the object oriented bouning box */ |
---|
[4631] | 259 | } |
---|
[4557] | 260 | |
---|
[4631] | 261 | |
---|
[5867] | 262 | |
---|
| 263 | /** |
---|
| 264 | * calculate the eigenvectors for the object oriented box |
---|
| 265 | * @param box: reference to the box |
---|
| 266 | * @param modelInf: the model info structure of the model |
---|
| 267 | * @param tirangleIndexes: an array with the indexes of the triangles inside this |
---|
| 268 | * @param length: the length of the indexes array |
---|
| 269 | */ |
---|
| 270 | void OBBTreeNode::calculateBoxEigenvectors(OBB& box, const modelInfo& modelInf, |
---|
[5825] | 271 | const int* triangleIndexes, unsigned int length) |
---|
| 272 | { |
---|
[4631] | 273 | |
---|
[5825] | 274 | Vector axis[3]; //!< the references to the obb axis |
---|
| 275 | Matrix covMat( box.covarianceMatrix ); //!< covariance matrix (in the matrix dataform) |
---|
| 276 | |
---|
| 277 | /* |
---|
| 278 | now getting spanning vectors of the sub-space: |
---|
[4617] | 279 | the eigenvectors of a symmertric matrix, such as the |
---|
| 280 | covarience matrix are mutually orthogonal. |
---|
| 281 | after normalizing them, they can be used as a the basis |
---|
| 282 | vectors |
---|
[4557] | 283 | */ |
---|
[4588] | 284 | |
---|
[5825] | 285 | /* calculate the axis */ |
---|
[5674] | 286 | covMat.getEigenVectors(axis[0], axis[1], axis[2] ); |
---|
[5712] | 287 | box.axis[0] = axis[0]; |
---|
| 288 | box.axis[1] = axis[1]; |
---|
| 289 | box.axis[2] = axis[2]; |
---|
[4660] | 290 | |
---|
[5882] | 291 | PRINTF(3)("Eigenvectors:\n"); |
---|
| 292 | 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); |
---|
| 293 | 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); |
---|
| 294 | 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] | 295 | } |
---|
[4588] | 296 | |
---|
[4626] | 297 | |
---|
[5867] | 298 | |
---|
| 299 | |
---|
| 300 | /** |
---|
| 301 | * calculate the eigenvectors for the object oriented box |
---|
| 302 | * @param box: reference to the box |
---|
| 303 | * @param modelInf: the model info structure of the model |
---|
| 304 | * @param tirangleIndexes: an array with the indexes of the triangles inside this |
---|
| 305 | * @param length: the length of the indexes array |
---|
| 306 | */ |
---|
[5868] | 307 | void OBBTreeNode::calculateBoxAxis(OBB& box, const modelInfo& modelInf, const int* triangleIndexes, unsigned int length) |
---|
[5686] | 308 | { |
---|
[5684] | 309 | |
---|
[5882] | 310 | PRINTF(3)("Calculate Box Axis\n"); |
---|
[4576] | 311 | /* now get the axis length */ |
---|
[4578] | 312 | Line ax[3]; //!< the axis |
---|
[5699] | 313 | float halfLength[3]; //!< half length of the axis |
---|
[4578] | 314 | float tmpLength; //!< tmp save point for the length |
---|
[5882] | 315 | Plane p0(box.axis[0], box.center); //!< the axis planes |
---|
| 316 | Plane p1(box.axis[1], box.center); //!< the axis planes |
---|
| 317 | Plane p2(box.axis[2], box.center); //!< the axis planes |
---|
| 318 | float maxLength[3]; //!< maximal lenth of the axis |
---|
| 319 | float minLength[3]; //!< minimal length of the axis |
---|
| 320 | const sVec3D* tmpVec; //!< variable taking tmp vectors |
---|
[4588] | 321 | |
---|
[5868] | 322 | /* |
---|
[5869] | 323 | this step is split up in three: first there will be made a bounding box which is |
---|
| 324 | very badly centered. In the section step there will be calculated a new center |
---|
[5868] | 325 | and a better bounding box. |
---|
| 326 | */ |
---|
[4658] | 327 | |
---|
[5868] | 328 | /* get a bad bounding box axis */ |
---|
[5920] | 329 | // halfLength[0] = -1.0f; |
---|
| 330 | // for( int j = 0; j < length; ++j) |
---|
| 331 | // { |
---|
| 332 | // for( int i = 0; i < 3; ++i) |
---|
| 333 | // { |
---|
| 334 | // tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[j]].indexToVertices[i]]); |
---|
| 335 | // tmpLength = fabs(p0.distancePoint(*tmpVec)); |
---|
| 336 | // if( tmpLength > halfLength[0]) |
---|
| 337 | // halfLength[0] = tmpLength; |
---|
| 338 | // } |
---|
| 339 | // } |
---|
| 340 | // |
---|
| 341 | // halfLength[1] = -1.0f; |
---|
| 342 | // for( int j = 0; j < length; ++j) |
---|
| 343 | // { |
---|
| 344 | // for( int i = 0; i < 3; ++i) |
---|
| 345 | // { |
---|
| 346 | // tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[j]].indexToVertices[i]]); |
---|
| 347 | // tmpLength = fabs(p0.distancePoint(*tmpVec)); |
---|
| 348 | // if( tmpLength > halfLength[1]) |
---|
| 349 | // halfLength[1] = tmpLength; |
---|
| 350 | // } |
---|
| 351 | // } |
---|
| 352 | // |
---|
| 353 | // halfLength[2] = -1.0f; |
---|
| 354 | // for( int j = 0; j < length; ++j) |
---|
| 355 | // { |
---|
| 356 | // for( int i = 0; i < 3; ++i) |
---|
| 357 | // { |
---|
| 358 | // tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[j]].indexToVertices[i]]); |
---|
| 359 | // tmpLength = fabs(p0.distancePoint(*tmpVec)); |
---|
| 360 | // if( tmpLength > halfLength[2]) |
---|
| 361 | // halfLength[2] = tmpLength; |
---|
| 362 | // } |
---|
| 363 | // } |
---|
[4658] | 364 | |
---|
| 365 | |
---|
| 366 | |
---|
| 367 | /* get the maximal dimensions of the body in all directions */ |
---|
[5868] | 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[0] = p0.distancePoint(*tmpVec); |
---|
| 371 | minLength[0] = p0.distancePoint(*tmpVec); |
---|
| 372 | for( int j = 0; j < length; ++j) |
---|
[5825] | 373 | { |
---|
[5868] | 374 | for( int i = 0; i < 3; ++i) |
---|
| 375 | { |
---|
| 376 | tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[j]].indexToVertices[i]]); |
---|
| 377 | tmpLength = p0.distancePoint(*tmpVec); |
---|
| 378 | if( tmpLength > maxLength[0]) |
---|
| 379 | maxLength[0] = tmpLength; |
---|
| 380 | else if( tmpLength < minLength[0]) |
---|
| 381 | minLength[0] = tmpLength; |
---|
| 382 | } |
---|
[5825] | 383 | } |
---|
[4578] | 384 | |
---|
[5868] | 385 | /* for the initialisation the value just has to be inside of the polygon soup -> first vertices (rand) */ |
---|
| 386 | tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[0]].indexToVertices[0]]); |
---|
| 387 | maxLength[1] = p1.distancePoint(*tmpVec); |
---|
| 388 | minLength[1] = p1.distancePoint(*tmpVec); |
---|
| 389 | for( int j = 0; j < length; ++j) |
---|
[5825] | 390 | { |
---|
[5868] | 391 | for( int i = 0; i < 3; ++i) |
---|
| 392 | { |
---|
| 393 | tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[j]].indexToVertices[i]]); |
---|
[5925] | 394 | tmpLength = p1.distancePoint(*tmpVec); |
---|
[5868] | 395 | if( tmpLength > maxLength[1]) |
---|
| 396 | maxLength[1] = tmpLength; |
---|
| 397 | else if( tmpLength < minLength[1]) |
---|
| 398 | minLength[1] = tmpLength; |
---|
| 399 | } |
---|
[5825] | 400 | } |
---|
[4585] | 401 | |
---|
[5868] | 402 | /* for the initialisation the value just has to be inside of the polygon soup -> first vertices (rand) */ |
---|
| 403 | tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[0]].indexToVertices[0]]); |
---|
| 404 | maxLength[2] = p2.distancePoint(*tmpVec); |
---|
| 405 | minLength[2] = p2.distancePoint(*tmpVec); |
---|
| 406 | for( int j = 0; j < length; ++j) |
---|
[5825] | 407 | { |
---|
[5868] | 408 | for( int i = 0; i < 3; ++i) |
---|
| 409 | { |
---|
| 410 | tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[j]].indexToVertices[i]]); |
---|
[5925] | 411 | tmpLength = p2.distancePoint(*tmpVec); |
---|
[5868] | 412 | if( tmpLength > maxLength[2]) |
---|
| 413 | maxLength[2] = tmpLength; |
---|
| 414 | else if( tmpLength < minLength[2]) |
---|
| 415 | minLength[2] = tmpLength; |
---|
| 416 | } |
---|
[5825] | 417 | } |
---|
[4585] | 418 | |
---|
[4660] | 419 | |
---|
[5825] | 420 | /* calculate the real centre of the body by using the axis length */ |
---|
[5920] | 421 | float centerOffset[3]; |
---|
| 422 | float newHalfLength[3]; |
---|
| 423 | |
---|
[5922] | 424 | for( int i = 0; i < 3; ++i) |
---|
[5825] | 425 | { |
---|
| 426 | centerOffset[i] = (maxLength[i] + minLength[i]) / 2.0f; // min length is negatie |
---|
| 427 | newHalfLength[i] = (maxLength[i] - minLength[i]) / 2.0f; // min length is negative |
---|
[5882] | 428 | box.center += (box.axis[i] * centerOffset[i]); // update the new center vector |
---|
[5825] | 429 | halfLength[i] = newHalfLength[i]; |
---|
| 430 | } |
---|
[5882] | 431 | PRINTF(3)("\n"); |
---|
[5920] | 432 | PRINT(3)("\tAxis Length x: %f (max: %11.2f, \tmin: %11.2f)\n", halfLength[0], maxLength[0], minLength[0]); |
---|
| 433 | PRINT(3)("\tAxis Length x: %f (max: %11.2f, \tmin: %11.2f)\n", halfLength[1], maxLength[1], minLength[1]); |
---|
| 434 | PRINT(3)("\tAxis Length x: %f (max: %11.2f, \tmin: %11.2f)\n", halfLength[2], maxLength[2], minLength[2]); |
---|
[4660] | 435 | |
---|
| 436 | |
---|
[5712] | 437 | box.halfLength[0] = halfLength[0]; |
---|
| 438 | box.halfLength[1] = halfLength[1]; |
---|
| 439 | box.halfLength[2] = halfLength[2]; |
---|
[4542] | 440 | } |
---|
| 441 | |
---|
| 442 | |
---|
[4609] | 443 | |
---|
| 444 | /** |
---|
[5869] | 445 | * this separates an ob-box in the middle |
---|
| 446 | * @param box: the box to separate |
---|
| 447 | * |
---|
| 448 | * this will separate the box into to smaller boxes. the separation is done along the middle of the longest axis |
---|
[4609] | 449 | */ |
---|
[5712] | 450 | void OBBTreeNode::forkBox(OBB& box) |
---|
[4557] | 451 | { |
---|
[5869] | 452 | |
---|
[5882] | 453 | PRINTF(3)("Fork Box\n"); |
---|
| 454 | PRINTF(4)("Calculating the longest Axis\n"); |
---|
[4557] | 455 | /* get the longest axis of the box */ |
---|
[5882] | 456 | float longestAxis = -1.0f; //!< the length of the longest axis |
---|
| 457 | int longestAxisIndex = 0; //!< this is the nr of the longest axis |
---|
[4609] | 458 | |
---|
[5882] | 459 | |
---|
| 460 | /* now get the longest axis of the three exiting */ |
---|
| 461 | for( int i = 0; i < 3; ++i) |
---|
[4609] | 462 | { |
---|
[5882] | 463 | if( longestAxis < box.halfLength[i]) |
---|
[4557] | 464 | { |
---|
[5882] | 465 | longestAxis = box.halfLength[i]; |
---|
| 466 | longestAxisIndex = i; |
---|
[4557] | 467 | } |
---|
[4609] | 468 | } |
---|
[5882] | 469 | PRINTF(3)("\nLongest Axis is: Nr %i with a half-length of:%11.2f\n", longestAxisIndex, longestAxis); |
---|
[4588] | 470 | |
---|
[4609] | 471 | |
---|
[5882] | 472 | PRINTF(4)("Separating along the longest axis\n"); |
---|
[4557] | 473 | /* get the closest vertex near the center */ |
---|
[4611] | 474 | float dist = 999999.0f; //!< the smallest distance to each vertex |
---|
[5882] | 475 | float tmpDist; //!< variable to save diverse distances temporarily |
---|
| 476 | int vertexIndex; //!< index of the vertex near the center |
---|
| 477 | Plane middlePlane(box.axis[longestAxisIndex], box.center); //!< the middle plane |
---|
| 478 | const sVec3D* tmpVec; //!< temp simple 3D vector |
---|
[4588] | 479 | |
---|
[4609] | 480 | |
---|
[4611] | 481 | /* now definin the separation plane through this specified nearest point and partition |
---|
[4617] | 482 | the points depending on which side they are located |
---|
[4611] | 483 | */ |
---|
[5882] | 484 | std::list<int> partition1; //!< the vertex partition 1 |
---|
| 485 | std::list<int> partition2; //!< the vertex partition 2 |
---|
[5927] | 486 | float* triangleCenter = new float[3]; //!< the center of the triangle |
---|
[5882] | 487 | const float* a; //!< triangle edge a |
---|
| 488 | const float* b; //!< triangle edge b |
---|
| 489 | const float* c; //!< triangle edge c |
---|
[4611] | 490 | |
---|
[5920] | 491 | |
---|
[5922] | 492 | /* find the center of the box */ |
---|
[5920] | 493 | |
---|
[5882] | 494 | this->separationPlane = Plane(box.axis[longestAxisIndex], box.center); |
---|
| 495 | this->sepPlaneCenter[0] = box.center.x; |
---|
| 496 | this->sepPlaneCenter[1] = box.center.y; |
---|
| 497 | this->sepPlaneCenter[2] = box.center.z; |
---|
| 498 | this->longestAxisIndex = longestAxisIndex; |
---|
[4710] | 499 | |
---|
[5882] | 500 | for( int i = 0; i < box.triangleIndexesLength; ++i) |
---|
| 501 | { |
---|
| 502 | /* first calculate the middle of the triangle */ |
---|
| 503 | a = &box.modelInf->pVertices[box.modelInf->pTriangles[box.triangleIndexes[i]].indexToVertices[0]]; |
---|
| 504 | b = &box.modelInf->pVertices[box.modelInf->pTriangles[box.triangleIndexes[i]].indexToVertices[1]]; |
---|
| 505 | c = &box.modelInf->pVertices[box.modelInf->pTriangles[box.triangleIndexes[i]].indexToVertices[2]]; |
---|
[5869] | 506 | |
---|
[5927] | 507 | triangleCenter[0] = (a[0] + b[0] + c[0]) / 3.0f; |
---|
| 508 | triangleCenter[1] = (a[1] + b[1] + c[1]) / 3.0f; |
---|
| 509 | triangleCenter[2] = (a[2] + b[2] + c[2]) / 3.0f; |
---|
| 510 | tmpDist = this->separationPlane.distancePoint(*((sVec3D*)triangleCenter)); |
---|
[5869] | 511 | |
---|
[5882] | 512 | if( tmpDist > 0.0f) |
---|
[5927] | 513 | partition1.push_back(box.triangleIndexes[i]); /* positive numbers plus zero */ |
---|
[4612] | 514 | else |
---|
[5927] | 515 | partition2.push_back(box.triangleIndexes[i]); /* negatice numbers */ |
---|
[4612] | 516 | } |
---|
[5882] | 517 | PRINTF(3)("\nPartition1: got \t%i Vertices \nPartition2: got \t%i Vertices\n", partition1.size(), partition2.size()); |
---|
[4611] | 518 | |
---|
[4612] | 519 | |
---|
[4613] | 520 | /* now comes the separation into two different sVec3D arrays */ |
---|
| 521 | int index; //!< index storage place |
---|
[5882] | 522 | int* triangleIndexList1; //!< the vertex list 1 |
---|
| 523 | int* triangleIndexList2; //!< the vertex list 2 |
---|
| 524 | std::list<int>::iterator element; //!< the list iterator |
---|
[4613] | 525 | |
---|
[5882] | 526 | triangleIndexList1 = new int[partition1.size()]; |
---|
| 527 | triangleIndexList2 = new int[partition2.size()]; |
---|
[4613] | 528 | |
---|
[5882] | 529 | for( element = partition1.begin(), index = 0; element != partition1.end(); element++, index++) |
---|
| 530 | triangleIndexList1[index] = (*element); |
---|
[5869] | 531 | |
---|
[5882] | 532 | for( element = partition2.begin(), index = 0; element != partition2.end(); element++, index++) |
---|
| 533 | triangleIndexList2[index] = (*element); |
---|
[5869] | 534 | |
---|
[5882] | 535 | if( this->triangleIndexList1!= NULL) |
---|
| 536 | delete[] this->triangleIndexList1; |
---|
| 537 | this->triangleIndexList1 = triangleIndexList1; |
---|
| 538 | this->triangleIndexLength1 = partition1.size(); |
---|
[4613] | 539 | |
---|
[5882] | 540 | if( this->triangleIndexList2 != NULL) |
---|
| 541 | delete[] this->triangleIndexList2; |
---|
| 542 | this->triangleIndexList2 = triangleIndexList2; |
---|
| 543 | this->triangleIndexLength2 = partition2.size(); |
---|
[4557] | 544 | } |
---|
| 545 | |
---|
| 546 | |
---|
[4626] | 547 | |
---|
| 548 | |
---|
[5718] | 549 | void OBBTreeNode::collideWith(const BVTreeNode& treeNode, const WorldEntity& nodeA, const WorldEntity& nodeB) const |
---|
[4695] | 550 | { |
---|
[4705] | 551 | PRINTF(3)("collideWith\n"); |
---|
[4695] | 552 | /* if the obb overlap, make subtests: check which node is realy overlaping */ |
---|
[5718] | 553 | PRINTF(3)("Checking OBB %i vs %i: ", this->getIndex(), treeNode.getIndex()); |
---|
[5825] | 554 | // if( unlikely(treeNode == NULL)) return; |
---|
[5042] | 555 | |
---|
[5718] | 556 | |
---|
| 557 | if( this->overlapTest(*this->bvElement, *(((const OBBTreeNode*)&treeNode)->bvElement), nodeA, nodeB)) |
---|
[4695] | 558 | { |
---|
[5718] | 559 | PRINTF(3)("collision @ lvl %i, object %s vs. %s, (%p, %p)\n", this->depth, nodeA.getClassName(), nodeB.getClassName(), this->nodeLeft, this->nodeRight); |
---|
[5038] | 560 | |
---|
[4695] | 561 | /* check if left node overlaps */ |
---|
[4704] | 562 | if( likely( this->nodeLeft != NULL)) |
---|
| 563 | { |
---|
[5718] | 564 | PRINTF(3)("Checking OBB %i vs %i: ", this->nodeLeft->getIndex(), treeNode.getIndex()); |
---|
| 565 | if( this->overlapTest(*this->nodeLeft->bvElement, *(((const OBBTreeNode*)&treeNode)->bvElement), nodeA, nodeB)) |
---|
[4704] | 566 | { |
---|
[5718] | 567 | this->nodeLeft->collideWith(*(((const OBBTreeNode*)&treeNode)->nodeLeft), nodeA, nodeB); |
---|
| 568 | this->nodeLeft->collideWith(*(((const OBBTreeNode*)&treeNode)->nodeRight), nodeA, nodeB); |
---|
[4704] | 569 | } |
---|
| 570 | } |
---|
[4695] | 571 | /* check if right node overlaps */ |
---|
[4704] | 572 | if( likely( this->nodeRight != NULL)) |
---|
| 573 | { |
---|
[5718] | 574 | PRINTF(3)("Checking OBB %i vs %i: ", this->nodeRight->getIndex(), treeNode.getIndex()); |
---|
| 575 | if(this->overlapTest(*this->nodeRight->bvElement, *(((const OBBTreeNode*)&treeNode)->bvElement), nodeA, nodeB)) |
---|
[4704] | 576 | { |
---|
[5718] | 577 | this->nodeRight->collideWith(*(((const OBBTreeNode*)&treeNode)->nodeLeft), nodeA, nodeB); |
---|
| 578 | this->nodeRight->collideWith(*(((const OBBTreeNode*)&treeNode)->nodeRight), nodeA, nodeB); |
---|
[4704] | 579 | } |
---|
[5044] | 580 | } |
---|
[5028] | 581 | |
---|
[5044] | 582 | /* so there is a collision and this is the last box in the tree (i.e. leaf) */ |
---|
[5718] | 583 | /* FIXME: If we would choose || insead of && there would also be asymmetrical cases supported */ |
---|
[5044] | 584 | if( unlikely(this->nodeRight == NULL && this->nodeLeft == NULL)) |
---|
| 585 | { |
---|
[5718] | 586 | nodeA.collidesWith(nodeB, (((const OBBTreeNode*)&treeNode)->bvElement->center)); |
---|
[5046] | 587 | |
---|
[5718] | 588 | nodeB.collidesWith(nodeA, this->bvElement->center); |
---|
[4704] | 589 | } |
---|
[5044] | 590 | |
---|
[4695] | 591 | } |
---|
| 592 | } |
---|
[4542] | 593 | |
---|
| 594 | |
---|
[4626] | 595 | |
---|
[5718] | 596 | bool OBBTreeNode::overlapTest(const OBB& boxA, const OBB& boxB, const WorldEntity& nodeA, const WorldEntity& nodeB) const |
---|
[4695] | 597 | { |
---|
[5825] | 598 | // if( boxB == NULL || boxA == NULL) |
---|
| 599 | // return false; |
---|
[5711] | 600 | |
---|
[4696] | 601 | /* first check all axis */ |
---|
[4708] | 602 | Vector t; |
---|
[4700] | 603 | float rA = 0.0f; |
---|
| 604 | float rB = 0.0f; |
---|
| 605 | Vector l; |
---|
[4708] | 606 | Vector rotAxisA[3]; |
---|
| 607 | Vector rotAxisB[3]; |
---|
[4626] | 608 | |
---|
[5718] | 609 | rotAxisA[0] = nodeA.getAbsDir().apply(boxA.axis[0]); |
---|
| 610 | rotAxisA[1] = nodeA.getAbsDir().apply(boxA.axis[1]); |
---|
| 611 | rotAxisA[2] = nodeA.getAbsDir().apply(boxA.axis[2]); |
---|
[4708] | 612 | |
---|
[5718] | 613 | rotAxisB[0] = nodeB.getAbsDir().apply(boxB.axis[0]); |
---|
| 614 | rotAxisB[1] = nodeB.getAbsDir().apply(boxB.axis[1]); |
---|
| 615 | rotAxisB[2] = nodeB.getAbsDir().apply(boxB.axis[2]); |
---|
[4708] | 616 | |
---|
| 617 | |
---|
[5718] | 618 | t = nodeA.getAbsCoor() + nodeA.getAbsDir().apply(boxA.center) - ( nodeB.getAbsCoor() + nodeB.getAbsDir().apply(boxB.center)); |
---|
[5706] | 619 | |
---|
[5825] | 620 | // printf("\n"); |
---|
| 621 | // 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); |
---|
| 622 | // 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); |
---|
| 623 | // 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); |
---|
| 624 | // |
---|
| 625 | // 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); |
---|
| 626 | // 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); |
---|
| 627 | // 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] | 628 | |
---|
| 629 | |
---|
[4703] | 630 | /* All 3 axis of the object A */ |
---|
[4701] | 631 | for( int j = 0; j < 3; ++j) |
---|
[4705] | 632 | { |
---|
| 633 | rA = 0.0f; |
---|
| 634 | rB = 0.0f; |
---|
[4708] | 635 | l = rotAxisA[j]; |
---|
[4705] | 636 | |
---|
[5718] | 637 | rA += fabs(boxA.halfLength[0] * rotAxisA[0].dot(l)); |
---|
| 638 | rA += fabs(boxA.halfLength[1] * rotAxisA[1].dot(l)); |
---|
| 639 | rA += fabs(boxA.halfLength[2] * rotAxisA[2].dot(l)); |
---|
[4705] | 640 | |
---|
[5718] | 641 | rB += fabs(boxB.halfLength[0] * rotAxisB[0].dot(l)); |
---|
| 642 | rB += fabs(boxB.halfLength[1] * rotAxisB[1].dot(l)); |
---|
| 643 | rB += fabs(boxB.halfLength[2] * rotAxisB[2].dot(l)); |
---|
[4705] | 644 | |
---|
| 645 | PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB); |
---|
| 646 | |
---|
| 647 | if( (rA + rB) < fabs(t.dot(l))) |
---|
[4700] | 648 | { |
---|
[5713] | 649 | PRINTF(3)("no Collision\n"); |
---|
[4705] | 650 | return false; |
---|
| 651 | } |
---|
| 652 | } |
---|
[4700] | 653 | |
---|
[4705] | 654 | /* All 3 axis of the object B */ |
---|
| 655 | for( int j = 0; j < 3; ++j) |
---|
| 656 | { |
---|
| 657 | rA = 0.0f; |
---|
| 658 | rB = 0.0f; |
---|
[4708] | 659 | l = rotAxisB[j]; |
---|
[4701] | 660 | |
---|
[5718] | 661 | rA += fabs(boxA.halfLength[0] * rotAxisA[0].dot(l)); |
---|
| 662 | rA += fabs(boxA.halfLength[1] * rotAxisA[1].dot(l)); |
---|
| 663 | rA += fabs(boxA.halfLength[2] * rotAxisA[2].dot(l)); |
---|
[4700] | 664 | |
---|
[5718] | 665 | rB += fabs(boxB.halfLength[0] * rotAxisB[0].dot(l)); |
---|
| 666 | rB += fabs(boxB.halfLength[1] * rotAxisB[1].dot(l)); |
---|
| 667 | rB += fabs(boxB.halfLength[2] * rotAxisB[2].dot(l)); |
---|
[4703] | 668 | |
---|
[4705] | 669 | PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB); |
---|
| 670 | |
---|
| 671 | if( (rA + rB) < fabs(t.dot(l))) |
---|
| 672 | { |
---|
[5713] | 673 | PRINTF(3)("no Collision\n"); |
---|
[4705] | 674 | return false; |
---|
[4701] | 675 | } |
---|
[4705] | 676 | } |
---|
[4700] | 677 | |
---|
[4705] | 678 | |
---|
| 679 | /* Now check for all face cross products */ |
---|
| 680 | |
---|
| 681 | for( int j = 0; j < 3; ++j) |
---|
| 682 | { |
---|
| 683 | for(int k = 0; k < 3; ++k ) |
---|
[4701] | 684 | { |
---|
| 685 | rA = 0.0f; |
---|
| 686 | rB = 0.0f; |
---|
[4708] | 687 | l = rotAxisA[j].cross(rotAxisB[k]); |
---|
[4701] | 688 | |
---|
[5718] | 689 | rA += fabs(boxA.halfLength[0] * rotAxisA[0].dot(l)); |
---|
| 690 | rA += fabs(boxA.halfLength[1] * rotAxisA[1].dot(l)); |
---|
| 691 | rA += fabs(boxA.halfLength[2] * rotAxisA[2].dot(l)); |
---|
[4701] | 692 | |
---|
[5718] | 693 | rB += fabs(boxB.halfLength[0] * rotAxisB[0].dot(l)); |
---|
| 694 | rB += fabs(boxB.halfLength[1] * rotAxisB[1].dot(l)); |
---|
| 695 | rB += fabs(boxB.halfLength[2] * rotAxisB[2].dot(l)); |
---|
[4701] | 696 | |
---|
[4703] | 697 | PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB); |
---|
| 698 | |
---|
[4701] | 699 | if( (rA + rB) < fabs(t.dot(l))) |
---|
| 700 | { |
---|
[5713] | 701 | PRINTF(3)("keine Kollision\n"); |
---|
[4701] | 702 | return false; |
---|
| 703 | } |
---|
[4703] | 704 | } |
---|
[4705] | 705 | } |
---|
[4701] | 706 | |
---|
[5718] | 707 | /* FIXME: there is no collision mark set now */ |
---|
[5825] | 708 | // boxA.bCollided = true; /* use this ONLY(!!!!) for drawing operations */ |
---|
| 709 | // boxB.bCollided = true; |
---|
[4701] | 710 | |
---|
[5718] | 711 | |
---|
[5713] | 712 | PRINTF(3)("Kollision!\n"); |
---|
[4705] | 713 | return true; |
---|
[4695] | 714 | } |
---|
| 715 | |
---|
| 716 | |
---|
[4696] | 717 | |
---|
[4708] | 718 | |
---|
| 719 | |
---|
[5481] | 720 | void OBBTreeNode::drawBV(int depth, int drawMode, const Vector& color, bool top) const |
---|
[4553] | 721 | { |
---|
[4635] | 722 | |
---|
| 723 | /* draw the model itself, there is some problem concerning this: the vertices are drawn multiple times */ |
---|
| 724 | if( drawMode & DRAW_MODEL || drawMode & DRAW_ALL) |
---|
| 725 | { |
---|
[4638] | 726 | if( !(drawMode & DRAW_SINGLE && depth != 0)) |
---|
[4622] | 727 | { |
---|
[4712] | 728 | if( drawMode & DRAW_POINTS) |
---|
| 729 | glBegin(GL_POINTS); |
---|
[5882] | 730 | for( int i = 0; i < this->bvElement->modelInf->numVertices; i+=3) |
---|
[4638] | 731 | { |
---|
[4712] | 732 | if( drawMode & DRAW_POINTS) |
---|
[5882] | 733 | glVertex3f(this->bvElement->modelInf->pVertices[i], this->bvElement->modelInf->pVertices[i+1], this->bvElement->modelInf->pVertices[i+2]); |
---|
[4712] | 734 | else |
---|
| 735 | { |
---|
| 736 | glPushMatrix(); |
---|
[5882] | 737 | glVertex3f(this->bvElement->modelInf->pVertices[i], this->bvElement->modelInf->pVertices[i+1], this->bvElement->modelInf->pVertices[i+2]); |
---|
[5430] | 738 | gluSphere(OBBTreeNode_sphereObj, 0.1, 10, 10); |
---|
[4712] | 739 | glPopMatrix(); |
---|
| 740 | } |
---|
[4638] | 741 | } |
---|
[4712] | 742 | if( drawMode & DRAW_POINTS) |
---|
| 743 | glEnd(); |
---|
[4622] | 744 | } |
---|
[4635] | 745 | } |
---|
[4542] | 746 | |
---|
[5481] | 747 | if (top) |
---|
| 748 | { |
---|
| 749 | glPushAttrib(GL_ENABLE_BIT); |
---|
| 750 | glDisable(GL_LIGHTING); |
---|
| 751 | glDisable(GL_TEXTURE_2D); |
---|
| 752 | } |
---|
| 753 | glColor3f(color.x, color.y, color.z); |
---|
[4542] | 754 | |
---|
[5481] | 755 | |
---|
[4589] | 756 | /* draw world axes */ |
---|
[4676] | 757 | if( drawMode & DRAW_BV_AXIS) |
---|
| 758 | { |
---|
| 759 | glBegin(GL_LINES); |
---|
[5481] | 760 | glColor3f(1.0, 0.0, 0.0); |
---|
[4676] | 761 | glVertex3f(0.0, 0.0, 0.0); |
---|
| 762 | glVertex3f(3.0, 0.0, 0.0); |
---|
[4589] | 763 | |
---|
[5481] | 764 | glColor3f(0.0, 1.0, 0.0); |
---|
[4676] | 765 | glVertex3f(0.0, 0.0, 0.0); |
---|
| 766 | glVertex3f(0.0, 3.0, 0.0); |
---|
[4589] | 767 | |
---|
[5481] | 768 | glColor3f(0.0, 0.0, 1.0); |
---|
[4676] | 769 | glVertex3f(0.0, 0.0, 0.0); |
---|
| 770 | glVertex3f(0.0, 0.0, 3.0); |
---|
| 771 | glEnd(); |
---|
| 772 | } |
---|
[4674] | 773 | |
---|
[4688] | 774 | |
---|
[4635] | 775 | if( drawMode & DRAW_BV_AXIS || drawMode & DRAW_ALL) |
---|
| 776 | { |
---|
[4636] | 777 | if( !(drawMode & DRAW_SINGLE && depth != 0)) |
---|
[4635] | 778 | { |
---|
| 779 | /* draw the obb axes */ |
---|
| 780 | glBegin(GL_LINES); |
---|
| 781 | glColor3f(0.0, 0.4, 0.3); |
---|
[5706] | 782 | glVertex3f(this->bvElement->center.x, this->bvElement->center.y, this->bvElement->center.z); |
---|
| 783 | glVertex3f(this->bvElement->center.x + this->bvElement->axis[0].x * this->bvElement->halfLength[0], |
---|
| 784 | this->bvElement->center.y + this->bvElement->axis[0].y * this->bvElement->halfLength[0], |
---|
| 785 | this->bvElement->center.z + this->bvElement->axis[0].z * this->bvElement->halfLength[0]); |
---|
[4589] | 786 | |
---|
[5706] | 787 | glVertex3f(this->bvElement->center.x, this->bvElement->center.y, this->bvElement->center.z); |
---|
| 788 | glVertex3f(this->bvElement->center.x + this->bvElement->axis[1].x * this->bvElement->halfLength[1], |
---|
| 789 | this->bvElement->center.y + this->bvElement->axis[1].y * this->bvElement->halfLength[1], |
---|
| 790 | this->bvElement->center.z + this->bvElement->axis[1].z * this->bvElement->halfLength[1]); |
---|
[4588] | 791 | |
---|
[5706] | 792 | glVertex3f(this->bvElement->center.x, this->bvElement->center.y, this->bvElement->center.z); |
---|
| 793 | glVertex3f(this->bvElement->center.x + this->bvElement->axis[2].x * this->bvElement->halfLength[2], |
---|
| 794 | this->bvElement->center.y + this->bvElement->axis[2].y * this->bvElement->halfLength[2], |
---|
| 795 | this->bvElement->center.z + this->bvElement->axis[2].z * this->bvElement->halfLength[2]); |
---|
[4635] | 796 | glEnd(); |
---|
| 797 | } |
---|
| 798 | } |
---|
[4581] | 799 | |
---|
[4588] | 800 | |
---|
[4674] | 801 | /* DRAW POLYGONS */ |
---|
[4673] | 802 | if( drawMode & DRAW_BV_POLYGON || drawMode & DRAW_ALL || drawMode & DRAW_BV_BLENDED) |
---|
[4635] | 803 | { |
---|
[5487] | 804 | if (top) |
---|
| 805 | { |
---|
| 806 | glEnable(GL_BLEND); |
---|
| 807 | glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
---|
| 808 | } |
---|
| 809 | |
---|
[4711] | 810 | if(this->nodeLeft == NULL || this->nodeRight == NULL) |
---|
[4710] | 811 | depth = 0; |
---|
[4636] | 812 | if( !(drawMode & DRAW_SINGLE && depth != 0)) |
---|
[4635] | 813 | { |
---|
[5825] | 814 | Vector cen = this->bvElement->center; |
---|
| 815 | Vector* axis = this->bvElement->axis; |
---|
| 816 | float* len = this->bvElement->halfLength; |
---|
[4588] | 817 | |
---|
[5825] | 818 | if( this->bvElement->bCollided) |
---|
| 819 | { |
---|
| 820 | glColor4f(1.0, 1.0, 1.0, .5); // COLLISION COLOR |
---|
| 821 | } |
---|
| 822 | else if( drawMode & DRAW_BV_BLENDED) |
---|
| 823 | { |
---|
| 824 | glColor4f(color.x, color.y, color.z, .5); |
---|
| 825 | } |
---|
[4670] | 826 | |
---|
[5825] | 827 | /* draw bounding box */ |
---|
| 828 | if( drawMode & DRAW_BV_BLENDED) |
---|
| 829 | glBegin(GL_QUADS); |
---|
| 830 | else |
---|
| 831 | glBegin(GL_LINE_LOOP); |
---|
| 832 | glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 833 | cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 834 | cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); |
---|
[4671] | 835 | glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 836 | cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 837 | cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); |
---|
| 838 | glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 839 | cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 840 | cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); |
---|
[5825] | 841 | glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 842 | cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 843 | cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); |
---|
| 844 | glEnd(); |
---|
| 845 | |
---|
| 846 | if( drawMode & DRAW_BV_BLENDED) |
---|
| 847 | glBegin(GL_QUADS); |
---|
| 848 | else |
---|
| 849 | glBegin(GL_LINE_LOOP); |
---|
| 850 | glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 851 | cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 852 | cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); |
---|
| 853 | glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 854 | cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 855 | cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); |
---|
[4671] | 856 | glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 857 | cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 858 | cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); |
---|
[5825] | 859 | glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 860 | cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 861 | cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); |
---|
[4671] | 862 | glEnd(); |
---|
| 863 | |
---|
[5825] | 864 | if( drawMode & DRAW_BV_BLENDED) |
---|
| 865 | glBegin(GL_QUADS); |
---|
| 866 | else |
---|
| 867 | glBegin(GL_LINE_LOOP); |
---|
| 868 | glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 869 | cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 870 | cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); |
---|
| 871 | glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 872 | cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 873 | cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); |
---|
| 874 | glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 875 | cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 876 | cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); |
---|
[4671] | 877 | glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 878 | cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 879 | cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); |
---|
[5825] | 880 | glEnd(); |
---|
| 881 | |
---|
| 882 | if( drawMode & DRAW_BV_BLENDED) |
---|
| 883 | glBegin(GL_QUADS); |
---|
| 884 | else |
---|
| 885 | glBegin(GL_LINE_LOOP); |
---|
| 886 | glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 887 | cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 888 | cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); |
---|
| 889 | glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 890 | cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 891 | cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); |
---|
[4671] | 892 | glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 893 | cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 894 | cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); |
---|
[5825] | 895 | glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 896 | cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 897 | cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); |
---|
[4671] | 898 | glEnd(); |
---|
| 899 | |
---|
| 900 | |
---|
[5825] | 901 | if( drawMode & DRAW_BV_BLENDED) |
---|
| 902 | { |
---|
| 903 | glBegin(GL_QUADS); |
---|
| 904 | glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 905 | cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 906 | cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); |
---|
| 907 | glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 908 | cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 909 | cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); |
---|
| 910 | glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 911 | cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 912 | cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); |
---|
| 913 | glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 914 | cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 915 | cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); |
---|
| 916 | glEnd(); |
---|
| 917 | |
---|
| 918 | glBegin(GL_QUADS); |
---|
| 919 | glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 920 | cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 921 | cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); |
---|
| 922 | glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 923 | cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 924 | cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); |
---|
| 925 | glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 926 | cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 927 | cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); |
---|
| 928 | glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 929 | cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 930 | cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); |
---|
| 931 | glEnd(); |
---|
| 932 | } |
---|
| 933 | |
---|
| 934 | |
---|
| 935 | if( drawMode & DRAW_BV_BLENDED) |
---|
| 936 | glColor3f(color.x, color.y, color.z); |
---|
[4635] | 937 | } |
---|
[4636] | 938 | |
---|
[4635] | 939 | } |
---|
[4588] | 940 | |
---|
[4674] | 941 | /* DRAW SEPARATING PLANE */ |
---|
[4635] | 942 | if( drawMode & DRAW_SEPARATING_PLANE || drawMode & DRAW_ALL) |
---|
[4632] | 943 | { |
---|
[4636] | 944 | if( !(drawMode & DRAW_SINGLE && depth != 0)) |
---|
[4635] | 945 | { |
---|
[4671] | 946 | if( drawMode & DRAW_BV_BLENDED) |
---|
[5481] | 947 | glColor4f(color.x, color.y, color.z, .6); |
---|
[4671] | 948 | |
---|
[5825] | 949 | /* now draw the separation plane */ |
---|
| 950 | Vector a1 = this->bvElement->axis[(this->longestAxisIndex + 1)%3]; |
---|
| 951 | Vector a2 = this->bvElement->axis[(this->longestAxisIndex + 2)%3]; |
---|
| 952 | Vector c = this->bvElement->center; |
---|
| 953 | float l1 = this->bvElement->halfLength[(this->longestAxisIndex + 1)%3]; |
---|
| 954 | float l2 = this->bvElement->halfLength[(this->longestAxisIndex + 2)%3]; |
---|
| 955 | glBegin(GL_QUADS); |
---|
| 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 | 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); |
---|
| 960 | glEnd(); |
---|
[4671] | 961 | |
---|
[5825] | 962 | if( drawMode & DRAW_BV_BLENDED) |
---|
| 963 | glColor4f(color.x, color.y, color.z, 1.0); |
---|
[4671] | 964 | |
---|
[4635] | 965 | } |
---|
[4632] | 966 | } |
---|
[4588] | 967 | |
---|
[4702] | 968 | |
---|
| 969 | |
---|
[5481] | 970 | if (depth > 0) |
---|
| 971 | { |
---|
| 972 | if( this->nodeLeft != NULL) |
---|
[5494] | 973 | this->nodeLeft->drawBV(depth - 1, drawMode, Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(15.0,0.0,0.0)), false); |
---|
[5481] | 974 | if( this->nodeRight != NULL) |
---|
[5494] | 975 | this->nodeRight->drawBV(depth - 1, drawMode, Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(30.0,0.0,0.0)), false); |
---|
[5481] | 976 | } |
---|
| 977 | this->bvElement->bCollided = false; |
---|
[4588] | 978 | |
---|
[5481] | 979 | if (top) |
---|
| 980 | glPopAttrib(); |
---|
[4557] | 981 | } |
---|
[4542] | 982 | |
---|
| 983 | |
---|
[4568] | 984 | |
---|
[4746] | 985 | void OBBTreeNode::debug() const |
---|
[4568] | 986 | { |
---|
[5825] | 987 | PRINT(0)("========OBBTreeNode::debug()=====\n"); |
---|
| 988 | PRINT(0)(" Current depth: %i", this->depth); |
---|
| 989 | PRINT(0)(" "); |
---|
| 990 | PRINT(0)("=================================\n"); |
---|
[4617] | 991 | } |
---|