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