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