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