[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" |
---|
[4541] | 24 | |
---|
[4543] | 25 | #include <math.h> |
---|
| 26 | |
---|
[4572] | 27 | |
---|
| 28 | #define WANT_STREAM |
---|
| 29 | #define WANT_MATH |
---|
| 30 | #define WANT_FSTREAM |
---|
| 31 | |
---|
[4638] | 32 | #include "stdincl.h" |
---|
[4572] | 33 | |
---|
| 34 | #include "include.h" |
---|
| 35 | #include "newmat.h" |
---|
| 36 | #include "newmatap.h" |
---|
| 37 | #include "newmatio.h" |
---|
| 38 | |
---|
[4627] | 39 | #include "lin_alg.h" |
---|
[4572] | 40 | |
---|
| 41 | |
---|
| 42 | |
---|
[4627] | 43 | |
---|
[4541] | 44 | using namespace std; |
---|
| 45 | |
---|
[4622] | 46 | OBBTree* OBBTreeNode::obbTree = NULL; |
---|
[4541] | 47 | |
---|
[4630] | 48 | float** OBBTreeNode::coMat = NULL; |
---|
| 49 | float** OBBTreeNode::eigvMat = NULL; |
---|
| 50 | float* OBBTreeNode::eigvlMat = NULL; |
---|
| 51 | int* OBBTreeNode::rotCount = NULL; |
---|
| 52 | |
---|
[4541] | 53 | /** |
---|
| 54 | \brief standard constructor |
---|
[4617] | 55 | */ |
---|
[4588] | 56 | OBBTreeNode::OBBTreeNode () |
---|
[4541] | 57 | { |
---|
[4617] | 58 | this->setClassID(CL_OBB_TREE_NODE, "OBBTreeNode"); |
---|
[4618] | 59 | this->nodeLeft = NULL; |
---|
| 60 | this->nodeRight = NULL; |
---|
[4630] | 61 | |
---|
| 62 | if(coMat == NULL) |
---|
| 63 | { |
---|
| 64 | coMat = new float*[4]; |
---|
| 65 | for(int i = 0; i < 4; i++) |
---|
| 66 | coMat[i] = new float[4]; |
---|
| 67 | } |
---|
| 68 | if(eigvMat == NULL) |
---|
| 69 | { |
---|
| 70 | eigvMat = new float*[4]; |
---|
| 71 | for(int i = 0; i < 4; i++) |
---|
| 72 | eigvMat[i] = new float[4]; |
---|
| 73 | } |
---|
| 74 | if( eigvlMat == NULL) |
---|
| 75 | { |
---|
| 76 | eigvlMat = new float[4]; |
---|
| 77 | } |
---|
| 78 | if( rotCount == NULL) |
---|
| 79 | rotCount = new int; |
---|
[4638] | 80 | |
---|
| 81 | this->sphereObj = gluNewQuadric(); |
---|
[4541] | 82 | } |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | \brief standard deconstructor |
---|
[4617] | 87 | */ |
---|
[4588] | 88 | OBBTreeNode::~OBBTreeNode () |
---|
[4541] | 89 | { |
---|
| 90 | // delete what has to be deleted here |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | |
---|
[4542] | 94 | |
---|
| 95 | /** |
---|
| 96 | \brief creates a new BVTree or BVTree partition |
---|
[4614] | 97 | \param depth: how much more depth-steps to go: if == 1 don't go any deeper! |
---|
[4542] | 98 | \param verticesList: the list of vertices of the object - each vertices triple is interpreted as a triangle |
---|
[4617] | 99 | */ |
---|
[4544] | 100 | void OBBTreeNode::spawnBVTree(const int depth, sVec3D *verticesList, const int length) |
---|
[4542] | 101 | { |
---|
[4638] | 102 | PRINT(0)("\n"); |
---|
| 103 | this->treeIndex = this->obbTree->getID(); |
---|
| 104 | PRINTF(0)("OBB Depth: %i, tree index: %i, numVertices: %i\n", depth, treeIndex, length); |
---|
[4614] | 105 | this->depth = depth; |
---|
| 106 | |
---|
[4638] | 107 | |
---|
[4630] | 108 | this->bvElement = new OBB(); |
---|
[4638] | 109 | this->bvElement->vertices = verticesList; |
---|
| 110 | this->bvElement->numOfVertices = length; |
---|
| 111 | PRINTF(3)("Created OBBox\n"); |
---|
[4632] | 112 | this->calculateBoxCovariance(this->bvElement, verticesList, length); |
---|
[4638] | 113 | PRINTF(3)("Calculated attributes1\n"); |
---|
[4632] | 114 | this->calculateBoxEigenvectors(this->bvElement, verticesList, length); |
---|
[4638] | 115 | PRINTF(3)("Calculated attributes2\n"); |
---|
[4632] | 116 | this->calculateBoxAxis(this->bvElement, verticesList, length); |
---|
[4638] | 117 | PRINTF(3)("Calculated attributes3\n"); |
---|
[4617] | 118 | |
---|
[4632] | 119 | |
---|
[4614] | 120 | if( likely( this->depth > 0)) |
---|
| 121 | { |
---|
| 122 | this->forkBox(this->bvElement); |
---|
[4626] | 123 | |
---|
[4630] | 124 | |
---|
[4638] | 125 | if(this->tmpLen1 > 2) |
---|
| 126 | { |
---|
| 127 | OBBTreeNode* node1 = new OBBTreeNode(); |
---|
| 128 | this->nodeLeft = node1; |
---|
| 129 | this->nodeLeft->spawnBVTree(depth - 1, this->tmpVert1, this->tmpLen1); |
---|
| 130 | } |
---|
| 131 | else |
---|
| 132 | { |
---|
| 133 | PRINTF(0)("Aboarding tree walk: less than 3 vertices left\n"); |
---|
| 134 | } |
---|
[4630] | 135 | |
---|
[4638] | 136 | if( this->tmpLen2 > 2) |
---|
| 137 | { |
---|
| 138 | OBBTreeNode* node2 = new OBBTreeNode(); |
---|
| 139 | this->nodeRight = node2; |
---|
| 140 | this->nodeRight->spawnBVTree(depth - 1, this->tmpVert2, this->tmpLen2); |
---|
| 141 | } |
---|
| 142 | else |
---|
| 143 | { |
---|
| 144 | PRINTF(0)("Aboarding tree walk: less than 3 vertices left\n"); |
---|
| 145 | } |
---|
[4630] | 146 | |
---|
[4614] | 147 | } |
---|
[4557] | 148 | } |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | |
---|
[4632] | 152 | void OBBTreeNode::calculateBoxCovariance(OBB* box, sVec3D* verticesList, int length) |
---|
[4557] | 153 | { |
---|
[4543] | 154 | float facelet[length]; //!< surface area of the i'th triangle of the convex hull |
---|
| 155 | float face; //!< surface area of the entire convex hull |
---|
[4588] | 156 | Vector centroid[length]; //!< centroid of the i'th convex hull |
---|
[4557] | 157 | Vector center; //!< the center of the entire hull |
---|
[4544] | 158 | Vector p, q, r; //!< holder of the polygon data, much more conveniant to work with Vector than sVec3d |
---|
[4545] | 159 | Vector t1, t2; //!< temporary values |
---|
[4628] | 160 | float covariance[3][3]; //!< the covariance matrix |
---|
[4588] | 161 | |
---|
[4553] | 162 | this->numOfVertices = length; |
---|
| 163 | this->vertices = verticesList; |
---|
| 164 | |
---|
[4562] | 165 | |
---|
[4638] | 166 | |
---|
| 167 | |
---|
[4545] | 168 | /* fist compute all the convex hull face/facelets and centroids */ |
---|
| 169 | for(int i = 0; i < length; i+=3) /* FIX-ME-QUICK: hops of 3, array indiscontinuity*/ |
---|
[4617] | 170 | { |
---|
| 171 | p = verticesList[i]; |
---|
| 172 | q = verticesList[i +1]; |
---|
| 173 | r = verticesList[i + 2]; |
---|
[4588] | 174 | |
---|
[4617] | 175 | t1 = p - q; t2 = p - r; |
---|
[4588] | 176 | |
---|
[4617] | 177 | /* finding the facelet surface via cross-product */ |
---|
| 178 | facelet[i] = 0.5f * fabs( t1.cross(t2).len() ); |
---|
| 179 | /* update the entire convex hull surface */ |
---|
| 180 | face += facelet[i]; |
---|
[4545] | 181 | |
---|
[4617] | 182 | /* calculate the cetroid of the hull triangles */ |
---|
| 183 | centroid[i] = (p + q + r) * 1/3; |
---|
| 184 | /* now calculate the centroid of the entire convex hull, weighted average of triangle centroids */ |
---|
| 185 | center += centroid[i] * facelet[i]; |
---|
| 186 | } |
---|
[4545] | 187 | /* take the average of the centroid sum */ |
---|
[4557] | 188 | center /= face; |
---|
[4638] | 189 | PRINTF(3)("-- Calculated Center\n"); |
---|
[4562] | 190 | |
---|
| 191 | |
---|
[4545] | 192 | /* now calculate the covariance matrix - if not written in three for-loops, it would compute faster: minor */ |
---|
| 193 | for(int j = 0; j < 3; ++j) |
---|
[4617] | 194 | { |
---|
| 195 | for(int k = 0; k < 3; ++k) |
---|
[4545] | 196 | { |
---|
[4617] | 197 | for(int i = 0; i < length; i+=3) |
---|
| 198 | { |
---|
| 199 | p = verticesList[i]; |
---|
| 200 | q = verticesList[i +1]; |
---|
| 201 | r = verticesList[i + 2]; |
---|
[4544] | 202 | |
---|
[4617] | 203 | covariance[j][k] = facelet[i] / (12.0f * face) * (9.0f * centroid[i][j] * centroid[i][k] + p[j]* p[k] + |
---|
| 204 | q[j] * q[k] + r[j]*r[k]) - center[j] * center[k]; |
---|
| 205 | } |
---|
[4545] | 206 | } |
---|
[4617] | 207 | } |
---|
[4638] | 208 | PRINTF(3)("-- Calculated Covariance\n"); |
---|
[4562] | 209 | |
---|
[4633] | 210 | |
---|
[4638] | 211 | PRINTF(0)("\nVertex Data:\n"); |
---|
| 212 | for(int i = 0; i < length; i++) |
---|
| 213 | { |
---|
| 214 | //PRINTF(0)("vertex %i: %f, %f, %f\n", i, verticesList[i][0], verticesList[i][1], verticesList[i][2]); |
---|
| 215 | PRINTF(0)("vertex %i: %f, %f, %f\n", i, box->vertices[i][0], box->vertices[i][1], box->vertices[i][2]); |
---|
| 216 | } |
---|
[4588] | 217 | |
---|
[4638] | 218 | // PRINTF(3)("\nCovariance Matrix:\n"); |
---|
[4629] | 219 | // for(int j = 0; j < 3; ++j) |
---|
| 220 | // { |
---|
[4638] | 221 | // PRINTF(3)(" |"); |
---|
[4629] | 222 | // for(int k = 0; k < 3; ++k) |
---|
| 223 | // { |
---|
[4638] | 224 | // PRINTF(3)(" \b%f ", covariance[j][k]); |
---|
[4629] | 225 | // } |
---|
[4638] | 226 | // PRINTF(3)(" |\n"); |
---|
[4629] | 227 | // } |
---|
[4638] | 228 | PRINTF(3)("center: %f, %f, %f\n", center.x, center.y, center.z); |
---|
[4553] | 229 | |
---|
[4588] | 230 | |
---|
[4633] | 231 | // for(int i = 0; i < 3; ++i) |
---|
| 232 | // { |
---|
| 233 | // box->covarianceMatrix[i][0] = covariance[i][0]; |
---|
| 234 | // box->covarianceMatrix[i][1] = covariance[i][1]; |
---|
| 235 | // box->covarianceMatrix[i][3] = covariance[i][2]; |
---|
| 236 | // } |
---|
[4560] | 237 | *box->center = center; |
---|
[4638] | 238 | PRINTF(3)("-- Written Result to obb\n"); |
---|
[4631] | 239 | } |
---|
[4557] | 240 | |
---|
[4631] | 241 | |
---|
| 242 | |
---|
[4632] | 243 | void OBBTreeNode::calculateBoxEigenvectors(OBB* box, sVec3D* verticesList, int length) |
---|
[4631] | 244 | { |
---|
| 245 | |
---|
[4557] | 246 | /* now getting spanning vectors of the sub-space: |
---|
[4617] | 247 | the eigenvectors of a symmertric matrix, such as the |
---|
| 248 | covarience matrix are mutually orthogonal. |
---|
| 249 | after normalizing them, they can be used as a the basis |
---|
| 250 | vectors |
---|
[4557] | 251 | */ |
---|
[4576] | 252 | Vector** axis = new Vector*[3]; //!< the references to the obb axis |
---|
[4588] | 253 | |
---|
[4631] | 254 | coMat[1][1] = box->covarianceMatrix[0][0]; coMat[1][2] = box->covarianceMatrix[0][1]; coMat[1][3] = box->covarianceMatrix[0][2]; |
---|
| 255 | coMat[2][1] = box->covarianceMatrix[1][0]; coMat[2][2] = box->covarianceMatrix[1][1]; coMat[2][3] = box->covarianceMatrix[1][2]; |
---|
| 256 | coMat[3][1] = box->covarianceMatrix[2][0]; coMat[3][2] = box->covarianceMatrix[2][1]; coMat[3][3] = box->covarianceMatrix[2][2]; |
---|
[4627] | 257 | |
---|
[4630] | 258 | /* new jacobi tests */ |
---|
| 259 | JacobI(coMat, 3, eigvlMat, eigvMat, rotCount); |
---|
[4638] | 260 | PRINTF(3)("-- Done Jacobi Decomposition\n"); |
---|
[4628] | 261 | |
---|
[4627] | 262 | |
---|
[4638] | 263 | // PRINTF(3)("Jacobi\n"); |
---|
[4629] | 264 | // for(int j = 1; j < 4; ++j) |
---|
| 265 | // { |
---|
[4638] | 266 | // PRINTF(3)(" |"); |
---|
[4629] | 267 | // for(int k = 1; k < 4; ++k) |
---|
| 268 | // { |
---|
[4638] | 269 | // PRINTF(3)(" \b%f ", eigvMat[j][k]); |
---|
[4629] | 270 | // } |
---|
[4638] | 271 | // PRINTF(3)(" |\n"); |
---|
[4629] | 272 | // } |
---|
| 273 | |
---|
[4630] | 274 | axis[0] = new Vector(eigvMat[1][1], eigvMat[2][1], eigvMat[3][1]); |
---|
| 275 | axis[1] = new Vector(eigvMat[1][2], eigvMat[2][2], eigvMat[3][2]); |
---|
| 276 | axis[2] = new Vector(eigvMat[1][3], eigvMat[2][3], eigvMat[3][3]); |
---|
[4576] | 277 | box->axis = axis; |
---|
[4638] | 278 | PRINTF(3)("-- Got Axis\n"); |
---|
[4588] | 279 | |
---|
[4638] | 280 | PRINTF(0)("eigenvector: %f, %f, %f\n", box->axis[0]->x, box->axis[0]->y, box->axis[0]->z); |
---|
| 281 | PRINTF(0)("eigenvector: %f, %f, %f\n", box->axis[1]->x, box->axis[1]->y, box->axis[1]->z); |
---|
| 282 | PRINTF(0)("eigenvector: %f, %f, %f\n", box->axis[2]->x, box->axis[2]->y, box->axis[2]->z); |
---|
[4632] | 283 | } |
---|
[4588] | 284 | |
---|
[4626] | 285 | |
---|
[4632] | 286 | void OBBTreeNode::calculateBoxAxis(OBB* box, sVec3D* verticesList, int length) |
---|
[4631] | 287 | { |
---|
[4630] | 288 | |
---|
[4576] | 289 | /* now get the axis length */ |
---|
[4578] | 290 | Line ax[3]; //!< the axis |
---|
| 291 | float* halfLength = new float[3]; //!< half length of the axis |
---|
| 292 | float tmpLength; //!< tmp save point for the length |
---|
[4609] | 293 | Plane p0(*box->axis[0], *box->center); //!< the axis planes |
---|
| 294 | Plane p1(*box->axis[1], *box->center); |
---|
| 295 | Plane p2(*box->axis[2], *box->center); |
---|
[4588] | 296 | |
---|
[4589] | 297 | halfLength[0] = -1.0f; |
---|
[4585] | 298 | for(int j = 0; j < length; ++j) |
---|
[4578] | 299 | { |
---|
[4589] | 300 | tmpLength = fabs(p0.distancePoint(vertices[j])); |
---|
[4585] | 301 | if( tmpLength > halfLength[0]) |
---|
| 302 | halfLength[0] = tmpLength; |
---|
[4578] | 303 | } |
---|
| 304 | |
---|
[4589] | 305 | halfLength[1] = -1.0f; |
---|
[4585] | 306 | for(int j = 0; j < length; ++j) |
---|
| 307 | { |
---|
[4589] | 308 | tmpLength = fabs(p1.distancePoint(vertices[j])); |
---|
[4585] | 309 | if( tmpLength > halfLength[1]) |
---|
| 310 | halfLength[1] = tmpLength; |
---|
| 311 | } |
---|
| 312 | |
---|
[4589] | 313 | halfLength[2] = -1.0f; |
---|
[4585] | 314 | for(int j = 0; j < length; ++j) |
---|
| 315 | { |
---|
[4589] | 316 | tmpLength = fabs(p2.distancePoint(vertices[j])); |
---|
[4585] | 317 | if( tmpLength > halfLength[2]) |
---|
| 318 | halfLength[2] = tmpLength; |
---|
| 319 | } |
---|
| 320 | |
---|
[4586] | 321 | box->halfLength = halfLength; |
---|
[4638] | 322 | PRINTF(3)("-- Written Axis to obb\n"); |
---|
| 323 | PRINTF(3)("-- Finished Calculating Attributes\n"); |
---|
[4585] | 324 | |
---|
[4638] | 325 | |
---|
| 326 | |
---|
| 327 | // PRINTF(3)("\nwe got length: \n"); |
---|
| 328 | for(int i = 0; i < 3; ++i) |
---|
| 329 | { |
---|
| 330 | //if( box->halfLength[i] == 0.0) |
---|
| 331 | PRINTF(0)("length[%i] = %f\n", i, box->halfLength[i]); |
---|
| 332 | } |
---|
[4542] | 333 | } |
---|
| 334 | |
---|
| 335 | |
---|
[4609] | 336 | |
---|
| 337 | /** |
---|
| 338 | \brief this separates an ob-box in the middle |
---|
| 339 | \param box: the box to separate |
---|
| 340 | |
---|
| 341 | this will separate the box into to smaller boxes. the separation is done along the middle of the longest axis |
---|
| 342 | */ |
---|
[4557] | 343 | void OBBTreeNode::forkBox(OBB* box) |
---|
| 344 | { |
---|
| 345 | /* get the longest axis of the box */ |
---|
[4609] | 346 | float aLength = -1.0f; //!< the length of the longest axis |
---|
| 347 | int axisIndex = 0; //!< this is the nr of the longest axis |
---|
| 348 | |
---|
[4557] | 349 | for(int i = 0; i < 3; ++i) |
---|
[4609] | 350 | { |
---|
| 351 | if( aLength < box->halfLength[i]) |
---|
[4557] | 352 | { |
---|
[4609] | 353 | aLength = box->halfLength[i]; |
---|
| 354 | axisIndex = i; |
---|
[4557] | 355 | } |
---|
[4609] | 356 | } |
---|
[4588] | 357 | |
---|
[4638] | 358 | PRINTF(0)("longest axis is: nr %i with a half-length of: %f\n", axisIndex, aLength); |
---|
[4609] | 359 | |
---|
| 360 | |
---|
[4557] | 361 | /* get the closest vertex near the center */ |
---|
[4611] | 362 | float dist = 999999.0f; //!< the smallest distance to each vertex |
---|
[4609] | 363 | float tmpDist; //!< temporary distance |
---|
| 364 | int vertexIndex; |
---|
[4611] | 365 | Plane middlePlane(*box->axis[axisIndex], *box->center); //!< the middle plane |
---|
[4588] | 366 | |
---|
[4609] | 367 | for(int i = 0; i < box->numOfVertices; ++i) |
---|
| 368 | { |
---|
[4611] | 369 | tmpDist = fabs(middlePlane.distancePoint(box->vertices[i])); |
---|
| 370 | if( tmpDist < dist) |
---|
| 371 | { |
---|
[4609] | 372 | dist = tmpDist; |
---|
[4611] | 373 | vertexIndex = i; |
---|
| 374 | } |
---|
[4609] | 375 | } |
---|
| 376 | |
---|
[4638] | 377 | // PRINTF(3)("\nthe clostest vertex is nr: %i, with a dist of: %f\n", vertexIndex ,dist); |
---|
[4609] | 378 | |
---|
| 379 | |
---|
[4611] | 380 | /* now definin the separation plane through this specified nearest point and partition |
---|
[4617] | 381 | the points depending on which side they are located |
---|
[4611] | 382 | */ |
---|
| 383 | tList<sVec3D> partition1; //!< the vertex partition 1 |
---|
| 384 | tList<sVec3D> partition2; //!< the vertex partition 2 |
---|
| 385 | |
---|
[4632] | 386 | |
---|
| 387 | this->separationPlane = new Plane(*box->axis[axisIndex], box->vertices[vertexIndex]); //!< separation plane |
---|
| 388 | this->sepPlaneCenter = &box->vertices[vertexIndex]; |
---|
| 389 | this->longestAxisIndex = axisIndex; |
---|
| 390 | |
---|
[4612] | 391 | for(int i = 0; i < box->numOfVertices; ++i) |
---|
| 392 | { |
---|
[4632] | 393 | if( this->separationPlane->distancePoint(box->vertices[i]) > 0.0f) |
---|
[4612] | 394 | partition1.add(&box->vertices[i]); |
---|
| 395 | else |
---|
| 396 | partition2.add(&box->vertices[i]); |
---|
| 397 | } |
---|
[4613] | 398 | partition1.add(&box->vertices[vertexIndex]); |
---|
[4611] | 399 | |
---|
[4638] | 400 | // PRINTF(3)("\npartition1: got %i vertices/ partition 2: got %i vertices\n", partition1.getSize(), partition2.getSize()); |
---|
[4612] | 401 | |
---|
[4613] | 402 | |
---|
| 403 | /* now comes the separation into two different sVec3D arrays */ |
---|
| 404 | tIterator<sVec3D>* iterator; //!< the iterator to go through the lists |
---|
| 405 | sVec3D* element; //!< the elements |
---|
| 406 | int index; //!< index storage place |
---|
| 407 | sVec3D* vertList1; //!< the vertex list 1 |
---|
| 408 | sVec3D* vertList2; //!< the vertex list 2 |
---|
| 409 | |
---|
| 410 | vertList1 = new sVec3D[partition1.getSize()]; |
---|
| 411 | vertList2 = new sVec3D[partition2.getSize()]; |
---|
| 412 | |
---|
| 413 | iterator = partition1.getIterator(); |
---|
| 414 | element = iterator->nextElement(); |
---|
| 415 | index = 0; |
---|
| 416 | while( element != NULL) |
---|
| 417 | { |
---|
| 418 | vertList1[index][0] = element[0][0]; |
---|
| 419 | vertList1[index][1] = element[0][1]; |
---|
| 420 | vertList1[index][2] = element[0][2]; |
---|
| 421 | ++index; |
---|
| 422 | element = iterator->nextElement(); |
---|
| 423 | } |
---|
| 424 | |
---|
[4638] | 425 | // PRINTF(0)("\npartition 1:\n"); |
---|
[4626] | 426 | // for(int i = 0; i < partition1.getSize(); ++i) |
---|
| 427 | // { |
---|
[4638] | 428 | // 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] | 429 | // } |
---|
[4613] | 430 | |
---|
| 431 | iterator = partition2.getIterator(); |
---|
| 432 | element = iterator->nextElement(); |
---|
| 433 | index = 0; |
---|
| 434 | while( element != NULL) |
---|
| 435 | { |
---|
| 436 | vertList2[index][0] = element[0][0]; |
---|
| 437 | vertList2[index][1] = element[0][1]; |
---|
| 438 | vertList2[index][2] = element[0][2]; |
---|
| 439 | ++index; |
---|
| 440 | element = iterator->nextElement(); |
---|
| 441 | } |
---|
| 442 | |
---|
[4630] | 443 | this->tmpVert1 = vertList1; |
---|
| 444 | this->tmpVert2 = vertList2; |
---|
| 445 | this->tmpLen1 = partition1.getSize(); |
---|
| 446 | this->tmpLen2 = partition2.getSize(); |
---|
| 447 | |
---|
[4638] | 448 | delete iterator; |
---|
| 449 | |
---|
| 450 | // PRINTF(0)("\npartition 2:\n"); |
---|
[4626] | 451 | // for(int i = 0; i < partition2.getSize(); ++i) |
---|
| 452 | // { |
---|
[4638] | 453 | // 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] | 454 | // } |
---|
[4557] | 455 | } |
---|
| 456 | |
---|
| 457 | |
---|
[4626] | 458 | |
---|
| 459 | |
---|
[4542] | 460 | void OBBTreeNode::collideWith(const BVTree &tree) |
---|
| 461 | {} |
---|
| 462 | |
---|
| 463 | |
---|
[4626] | 464 | |
---|
| 465 | |
---|
[4635] | 466 | void OBBTreeNode::drawBV(int depth, int drawMode) const |
---|
[4553] | 467 | { |
---|
[4638] | 468 | this->obbTree->getMaterial(treeIndex)->select(); |
---|
[4635] | 469 | |
---|
| 470 | /* draw the model itself, there is some problem concerning this: the vertices are drawn multiple times */ |
---|
| 471 | if( drawMode & DRAW_MODEL || drawMode & DRAW_ALL) |
---|
| 472 | { |
---|
[4638] | 473 | if( !(drawMode & DRAW_SINGLE && depth != 0)) |
---|
[4622] | 474 | { |
---|
[4638] | 475 | glBegin(GL_LINE_STRIP); |
---|
| 476 | for(int i = 0; i < this->bvElement->numOfVertices; ++i) |
---|
| 477 | { |
---|
| 478 | glPushMatrix(); |
---|
| 479 | glMatrixMode(GL_MODELVIEW); |
---|
| 480 | //glVertex3f(this->bvElement->vertices[i][0], this->bvElement->vertices[i][1], this->bvElement->vertices[i][2]); |
---|
| 481 | glTranslatef(this->bvElement->vertices[i][0], this->bvElement->vertices[i][1], this->bvElement->vertices[i][2]); |
---|
| 482 | gluSphere(this->sphereObj, 1, 10, 10); |
---|
| 483 | //PRINTF(0)("v(%f, %f, %f)\n", this->vertices[i][0], this->vertices[i][1], this->vertices[i][2]); |
---|
| 484 | glPopMatrix(); |
---|
| 485 | } |
---|
| 486 | glEnd(); |
---|
[4622] | 487 | } |
---|
[4635] | 488 | } |
---|
[4542] | 489 | |
---|
| 490 | |
---|
[4589] | 491 | /* draw world axes */ |
---|
[4618] | 492 | // glBegin(GL_LINES); |
---|
| 493 | // glColor3f(0.0, 0.4, 0.3); |
---|
| 494 | // glVertex3f(0.0, 0.0, 0.0); |
---|
| 495 | // glVertex3f(3.0, 0.0, 0.0); |
---|
| 496 | // |
---|
| 497 | // glVertex3f(0.0, 0.0, 0.0); |
---|
| 498 | // glVertex3f(0.0, 3.0, 0.0); |
---|
| 499 | // |
---|
| 500 | // glVertex3f(0.0, 0.0, 0.0); |
---|
| 501 | // glVertex3f(0.0, 0.0, 3.0); |
---|
| 502 | // glEnd(); |
---|
[4589] | 503 | |
---|
| 504 | |
---|
[4635] | 505 | if( drawMode & DRAW_BV_AXIS || drawMode & DRAW_ALL) |
---|
| 506 | { |
---|
[4636] | 507 | if( !(drawMode & DRAW_SINGLE && depth != 0)) |
---|
[4635] | 508 | { |
---|
| 509 | /* draw the obb axes */ |
---|
| 510 | glBegin(GL_LINES); |
---|
| 511 | glColor3f(0.0, 0.4, 0.3); |
---|
| 512 | glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z); |
---|
| 513 | glVertex3f(this->bvElement->center->x + this->bvElement->axis[0]->x * this->bvElement->halfLength[0], |
---|
| 514 | this->bvElement->center->y + this->bvElement->axis[0]->y * this->bvElement->halfLength[0], |
---|
| 515 | this->bvElement->center->z + this->bvElement->axis[0]->z * this->bvElement->halfLength[0]); |
---|
[4589] | 516 | |
---|
[4635] | 517 | glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z); |
---|
| 518 | glVertex3f(this->bvElement->center->x + this->bvElement->axis[1]->x * this->bvElement->halfLength[1], |
---|
| 519 | this->bvElement->center->y + this->bvElement->axis[1]->y * this->bvElement->halfLength[1], |
---|
| 520 | this->bvElement->center->z + this->bvElement->axis[1]->z * this->bvElement->halfLength[1]); |
---|
[4588] | 521 | |
---|
[4635] | 522 | glVertex3f(this->bvElement->center->x, this->bvElement->center->y, this->bvElement->center->z); |
---|
| 523 | glVertex3f(this->bvElement->center->x + this->bvElement->axis[2]->x * this->bvElement->halfLength[2], |
---|
| 524 | this->bvElement->center->y + this->bvElement->axis[2]->y * this->bvElement->halfLength[2], |
---|
| 525 | this->bvElement->center->z + this->bvElement->axis[2]->z * this->bvElement->halfLength[2]); |
---|
| 526 | glEnd(); |
---|
| 527 | } |
---|
| 528 | } |
---|
[4581] | 529 | |
---|
[4588] | 530 | |
---|
[4635] | 531 | if( drawMode & DRAW_BV_POLYGON || drawMode & DRAW_ALL) |
---|
| 532 | { |
---|
[4636] | 533 | if( !(drawMode & DRAW_SINGLE && depth != 0)) |
---|
[4635] | 534 | { |
---|
[4636] | 535 | Vector cen = *this->bvElement->center; |
---|
| 536 | Vector** axis = this->bvElement->axis; |
---|
| 537 | float* len = this->bvElement->halfLength; |
---|
[4588] | 538 | |
---|
[4636] | 539 | /* draw bounding box */ |
---|
| 540 | glBegin(GL_LINE_LOOP); |
---|
| 541 | glVertex3f(cen.x + axis[0]->x * len[0] + axis[1]->x * len[1] + axis[2]->x * len[2], |
---|
| 542 | cen.y + axis[0]->y * len[0] + axis[1]->y * len[1] + axis[2]->y * len[2], |
---|
| 543 | cen.z + axis[0]->z * len[0] + axis[1]->z * len[1] + axis[2]->z * len[2]); |
---|
| 544 | glVertex3f(cen.x + axis[0]->x * len[0] + axis[1]->x * len[1] - axis[2]->x * len[2], |
---|
| 545 | cen.y + axis[0]->y * len[0] + axis[1]->y * len[1] - axis[2]->y * len[2], |
---|
| 546 | cen.z + axis[0]->z * len[0] + axis[1]->z * len[1] - axis[2]->z * len[2]); |
---|
| 547 | glVertex3f(cen.x + axis[0]->x * len[0] - axis[1]->x * len[1] - axis[2]->x * len[2], |
---|
| 548 | cen.y + axis[0]->y * len[0] - axis[1]->y * len[1] - axis[2]->y * len[2], |
---|
| 549 | cen.z + axis[0]->z * len[0] - axis[1]->z * len[1] - axis[2]->z * len[2]); |
---|
| 550 | glVertex3f(cen.x + axis[0]->x * len[0] - axis[1]->x * len[1] + axis[2]->x * len[2], |
---|
| 551 | cen.y + axis[0]->y * len[0] - axis[1]->y * len[1] + axis[2]->y * len[2], |
---|
| 552 | cen.z + axis[0]->z * len[0] - axis[1]->z * len[1] + axis[2]->z * len[2]); |
---|
| 553 | glEnd(); |
---|
[4588] | 554 | |
---|
[4636] | 555 | glBegin(GL_LINE_LOOP); |
---|
| 556 | glVertex3f(cen.x + axis[0]->x * len[0] - axis[1]->x * len[1] + axis[2]->x * len[2], |
---|
| 557 | cen.y + axis[0]->y * len[0] - axis[1]->y * len[1] + axis[2]->y * len[2], |
---|
| 558 | cen.z + axis[0]->z * len[0] - axis[1]->z * len[1] + axis[2]->z * len[2]); |
---|
| 559 | glVertex3f(cen.x + axis[0]->x * len[0] - axis[1]->x * len[1] - axis[2]->x * len[2], |
---|
| 560 | cen.y + axis[0]->y * len[0] - axis[1]->y * len[1] - axis[2]->y * len[2], |
---|
| 561 | cen.z + axis[0]->z * len[0] - axis[1]->z * len[1] - axis[2]->z * len[2]); |
---|
| 562 | glVertex3f(cen.x - axis[0]->x * len[0] - axis[1]->x * len[1] - axis[2]->x * len[2], |
---|
| 563 | cen.y - axis[0]->y * len[0] - axis[1]->y * len[1] - axis[2]->y * len[2], |
---|
| 564 | cen.z - axis[0]->z * len[0] - axis[1]->z * len[1] - axis[2]->z * len[2]); |
---|
| 565 | glVertex3f(cen.x - axis[0]->x * len[0] - axis[1]->x * len[1] + axis[2]->x * len[2], |
---|
| 566 | cen.y - axis[0]->y * len[0] - axis[1]->y * len[1] + axis[2]->y * len[2], |
---|
| 567 | cen.z - axis[0]->z * len[0] - axis[1]->z * len[1] + axis[2]->z * len[2]); |
---|
| 568 | glEnd(); |
---|
[4588] | 569 | |
---|
[4636] | 570 | glBegin(GL_LINE_LOOP); |
---|
| 571 | glVertex3f(cen.x - axis[0]->x * len[0] - axis[1]->x * len[1] + axis[2]->x * len[2], |
---|
| 572 | cen.y - axis[0]->y * len[0] - axis[1]->y * len[1] + axis[2]->y * len[2], |
---|
| 573 | cen.z - axis[0]->z * len[0] - axis[1]->z * len[1] + axis[2]->z * len[2]); |
---|
| 574 | glVertex3f(cen.x - axis[0]->x * len[0] - axis[1]->x * len[1] - axis[2]->x * len[2], |
---|
| 575 | cen.y - axis[0]->y * len[0] - axis[1]->y * len[1] - axis[2]->y * len[2], |
---|
| 576 | cen.z - axis[0]->z * len[0] - axis[1]->z * len[1] - axis[2]->z * len[2]); |
---|
| 577 | glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] - axis[2]->x * len[2], |
---|
| 578 | cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] - axis[2]->y * len[2], |
---|
| 579 | cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] - axis[2]->z * len[2]); |
---|
| 580 | glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] + axis[2]->x * len[2], |
---|
| 581 | cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] + axis[2]->y * len[2], |
---|
| 582 | cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] + axis[2]->z * len[2]); |
---|
| 583 | glEnd(); |
---|
[4588] | 584 | |
---|
[4636] | 585 | glBegin(GL_LINE_LOOP); |
---|
| 586 | glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] - axis[2]->x * len[2], |
---|
| 587 | cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] - axis[2]->y * len[2], |
---|
| 588 | cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] - axis[2]->z * len[2]); |
---|
| 589 | glVertex3f(cen.x - axis[0]->x * len[0] + axis[1]->x * len[1] + axis[2]->x * len[2], |
---|
| 590 | cen.y - axis[0]->y * len[0] + axis[1]->y * len[1] + axis[2]->y * len[2], |
---|
| 591 | cen.z - axis[0]->z * len[0] + axis[1]->z * len[1] + axis[2]->z * len[2]); |
---|
| 592 | glVertex3f(cen.x + axis[0]->x * len[0] + axis[1]->x * len[1] + axis[2]->x * len[2], |
---|
| 593 | cen.y + axis[0]->y * len[0] + axis[1]->y * len[1] + axis[2]->y * len[2], |
---|
| 594 | cen.z + axis[0]->z * len[0] + axis[1]->z * len[1] + axis[2]->z * len[2]); |
---|
| 595 | glVertex3f(cen.x + axis[0]->x * len[0] + axis[1]->x * len[1] - axis[2]->x * len[2], |
---|
| 596 | cen.y + axis[0]->y * len[0] + axis[1]->y * len[1] - axis[2]->y * len[2], |
---|
| 597 | cen.z + axis[0]->z * len[0] + axis[1]->z * len[1] - axis[2]->z * len[2]); |
---|
| 598 | glEnd(); |
---|
[4635] | 599 | } |
---|
[4636] | 600 | |
---|
[4635] | 601 | } |
---|
[4588] | 602 | |
---|
[4635] | 603 | if( drawMode & DRAW_SEPARATING_PLANE || drawMode & DRAW_ALL) |
---|
[4632] | 604 | { |
---|
[4636] | 605 | if( !(drawMode & DRAW_SINGLE && depth != 0)) |
---|
[4635] | 606 | { |
---|
[4636] | 607 | /* now draw the separation plane */ |
---|
| 608 | Vector a1 = *this->bvElement->axis[(this->longestAxisIndex + 1)%3]; |
---|
| 609 | Vector a2 = *this->bvElement->axis[(this->longestAxisIndex + 2)%3]; |
---|
| 610 | Vector c = *this->bvElement->center; |
---|
| 611 | float l1 = this->bvElement->halfLength[(this->longestAxisIndex + 1)%3]; |
---|
| 612 | float l2 = this->bvElement->halfLength[(this->longestAxisIndex + 2)%3]; |
---|
| 613 | glBegin(GL_QUADS); |
---|
| 614 | 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); |
---|
| 615 | 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); |
---|
| 616 | 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); |
---|
| 617 | 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); |
---|
| 618 | glEnd(); |
---|
[4635] | 619 | } |
---|
[4632] | 620 | } |
---|
[4588] | 621 | |
---|
[4622] | 622 | if( this->nodeLeft != NULL && depth != 0 ) |
---|
[4635] | 623 | this->nodeLeft->drawBV(depth - 1, drawMode); |
---|
[4618] | 624 | if( this->nodeRight != NULL && depth != 0) |
---|
[4635] | 625 | this->nodeRight->drawBV(depth - 1, drawMode); |
---|
[4588] | 626 | |
---|
[4557] | 627 | } |
---|
[4542] | 628 | |
---|
| 629 | |
---|
[4568] | 630 | |
---|
| 631 | void OBBTreeNode::debug() |
---|
| 632 | { |
---|
| 633 | |
---|
| 634 | /* |
---|
| 635 | for(int i = 0; i < length; i++) |
---|
[4617] | 636 | { |
---|
[4638] | 637 | PRINTF(3)("vertex %i: %f, %f, %f\n", i, verticesList[i][0], verticesList[i][1], verticesList[i][2]); |
---|
[4617] | 638 | } |
---|
[4568] | 639 | */ |
---|
| 640 | } |
---|