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