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