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