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