[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 | |
---|
[5713] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION_DETECTION |
---|
[4541] | 17 | |
---|
| 18 | #include "obb_tree_node.h" |
---|
[5882] | 19 | #include "obb_tree.h" |
---|
[4542] | 20 | #include "obb.h" |
---|
[5882] | 21 | |
---|
[5674] | 22 | #include "matrix.h" |
---|
[4550] | 23 | #include "abstract_model.h" |
---|
[5028] | 24 | #include "world_entity.h" |
---|
[4541] | 25 | |
---|
[5481] | 26 | #include "color.h" |
---|
[5882] | 27 | #include "glincl.h" |
---|
[4543] | 28 | |
---|
[5882] | 29 | #include <list> |
---|
[5511] | 30 | #include "debug.h" |
---|
[4572] | 31 | |
---|
| 32 | |
---|
| 33 | |
---|
[4541] | 34 | using namespace std; |
---|
| 35 | |
---|
[5882] | 36 | |
---|
[5430] | 37 | GLUquadricObj* OBBTreeNode_sphereObj = NULL; |
---|
[4630] | 38 | |
---|
[5882] | 39 | |
---|
[4541] | 40 | /** |
---|
[4836] | 41 | * standard constructor |
---|
[5882] | 42 | * @param tree: reference to the obb tree |
---|
| 43 | * @param depth: the depth of the obb tree to generate |
---|
[4617] | 44 | */ |
---|
[5713] | 45 | OBBTreeNode::OBBTreeNode (const OBBTree& tree, unsigned int depth) |
---|
[5825] | 46 | : BVTreeNode() |
---|
[4541] | 47 | { |
---|
[4617] | 48 | this->setClassID(CL_OBB_TREE_NODE, "OBBTreeNode"); |
---|
[5713] | 49 | |
---|
| 50 | this->obbTree = &tree; |
---|
| 51 | this->depth = depth; |
---|
| 52 | |
---|
[4618] | 53 | this->nodeLeft = NULL; |
---|
| 54 | this->nodeRight = NULL; |
---|
[4814] | 55 | this->bvElement = NULL; |
---|
[5713] | 56 | |
---|
[5882] | 57 | this->triangleIndexList1 = NULL; |
---|
| 58 | this->triangleIndexList2 = NULL; |
---|
[4630] | 59 | |
---|
[5882] | 60 | this->modelInf = NULL; |
---|
| 61 | this->triangleIndexes = NULL; |
---|
[4638] | 62 | |
---|
[5693] | 63 | if( OBBTreeNode_sphereObj == NULL) |
---|
[5430] | 64 | OBBTreeNode_sphereObj = gluNewQuadric(); |
---|
[4541] | 65 | } |
---|
| 66 | |
---|
[5716] | 67 | |
---|
[4541] | 68 | /** |
---|
[4836] | 69 | * standard deconstructor |
---|
[4617] | 70 | */ |
---|
[4588] | 71 | OBBTreeNode::~OBBTreeNode () |
---|
[4541] | 72 | { |
---|
[4814] | 73 | if( this->nodeLeft) |
---|
| 74 | delete this->nodeLeft; |
---|
| 75 | if( this->nodeRight) |
---|
| 76 | delete this->nodeRight; |
---|
[5882] | 77 | |
---|
[4814] | 78 | if( this->bvElement) |
---|
| 79 | delete this->bvElement; |
---|
[5699] | 80 | |
---|
[5882] | 81 | if( this->triangleIndexList1 != NULL) |
---|
| 82 | delete [] this->triangleIndexList1; |
---|
| 83 | if( this->triangleIndexList2 != NULL) |
---|
| 84 | delete [] this->triangleIndexList2; |
---|
[4541] | 85 | } |
---|
| 86 | |
---|
| 87 | |
---|
[5684] | 88 | /** |
---|
| 89 | * creates a new BVTree or BVTree partition |
---|
| 90 | * @param depth: how much more depth-steps to go: if == 1 don't go any deeper! |
---|
| 91 | * @param modInfo: model informations from the abstrac model |
---|
[5689] | 92 | * |
---|
[5684] | 93 | * this function creates the Bounding Volume tree from a modelInfo struct and bases its calculations |
---|
| 94 | * on the triangle informations (triangle soup not polygon soup) |
---|
| 95 | */ |
---|
[5716] | 96 | void OBBTreeNode::spawnBVTree(const modelInfo& modelInf, const int* triangleIndexes, unsigned int length) |
---|
[5684] | 97 | { |
---|
[5882] | 98 | PRINTF(3)("\n==============================Creating OBB Tree Node==================\n"); |
---|
| 99 | PRINT(3)(" OBB Tree Infos: \n"); |
---|
| 100 | PRINT(3)("\tDepth: %i \n\tTree Index: %i \n\tNumber of Vertices: %i\n", depth, treeIndex, length); |
---|
[5684] | 101 | this->depth = depth; |
---|
[4542] | 102 | |
---|
[5711] | 103 | this->bvElement = new OBB(); |
---|
[5702] | 104 | this->bvElement->modelInf = &modelInf; |
---|
| 105 | this->bvElement->triangleIndexes = triangleIndexes; |
---|
[5870] | 106 | this->bvElement->triangleIndexesLength = length; |
---|
[5711] | 107 | |
---|
[5705] | 108 | /* create the boxes in three steps */ |
---|
[5712] | 109 | this->calculateBoxCovariance(*this->bvElement, modelInf, triangleIndexes, length); |
---|
| 110 | this->calculateBoxEigenvectors(*this->bvElement, modelInf, triangleIndexes, length); |
---|
| 111 | this->calculateBoxAxis(*this->bvElement, modelInf, triangleIndexes, length); |
---|
[5684] | 112 | |
---|
[5705] | 113 | /* do we need to descent further in the obb tree?*/ |
---|
[5684] | 114 | if( likely( this->depth > 0)) |
---|
| 115 | { |
---|
[5882] | 116 | this->forkBox(*this->bvElement); |
---|
[5684] | 117 | |
---|
[5882] | 118 | if( this->triangleIndexLength1 >= 3) |
---|
| 119 | { |
---|
| 120 | this->nodeLeft = new OBBTreeNode(*this->obbTree, depth - 1); |
---|
| 121 | this->nodeLeft->spawnBVTree(modelInf, this->triangleIndexList1, this->triangleIndexLength1); |
---|
| 122 | } |
---|
| 123 | if( this->triangleIndexLength2 >= 3) |
---|
| 124 | { |
---|
| 125 | this->nodeRight = new OBBTreeNode(*this->obbTree, depth - 1); |
---|
| 126 | this->nodeRight->spawnBVTree(modelInf, this->triangleIndexList2, this->triangleIndexLength2); |
---|
| 127 | } |
---|
[5684] | 128 | } |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | |
---|
[5882] | 132 | |
---|
[4542] | 133 | /** |
---|
[5882] | 134 | * calculate the box covariance matrix |
---|
| 135 | * @param box: reference to the box |
---|
| 136 | * @param modelInf: the model info structure of the model |
---|
| 137 | * @param tirangleIndexes: an array with the indexes of the triangles inside this |
---|
| 138 | * @param length: the length of the indexes array |
---|
[4617] | 139 | */ |
---|
[5867] | 140 | void OBBTreeNode::calculateBoxCovariance(OBB& box, const modelInfo& modelInf, const int* triangleIndexes, unsigned int length) |
---|
[4557] | 141 | { |
---|
[4543] | 142 | float facelet[length]; //!< surface area of the i'th triangle of the convex hull |
---|
[5428] | 143 | float face = 0.0f; //!< surface area of the entire convex hull |
---|
[4588] | 144 | Vector centroid[length]; //!< centroid of the i'th convex hull |
---|
[4557] | 145 | Vector center; //!< the center of the entire hull |
---|
[4544] | 146 | Vector p, q, r; //!< holder of the polygon data, much more conveniant to work with Vector than sVec3d |
---|
[4545] | 147 | Vector t1, t2; //!< temporary values |
---|
[5692] | 148 | float covariance[3][3] = {0,0,0, 0,0,0, 0,0,0};//!< the covariance matrix |
---|
[5710] | 149 | sVec3D* tmpVec = NULL; //!< a temp saving place for sVec3Ds |
---|
[4588] | 150 | |
---|
[4553] | 151 | |
---|
[5706] | 152 | /* fist compute all the convex hull face/facelets and centroids */ |
---|
[5710] | 153 | for( int i = 0; i < length ; ++i) |
---|
[4648] | 154 | { |
---|
[5713] | 155 | tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[0]]); |
---|
| 156 | p = *tmpVec; |
---|
| 157 | tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[1]]); |
---|
| 158 | q = *tmpVec; |
---|
| 159 | tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[2]]); |
---|
| 160 | r = *tmpVec; |
---|
[4638] | 161 | |
---|
[5713] | 162 | /* finding the facelet surface via cross-product */ |
---|
[5825] | 163 | t1 = p - q; |
---|
| 164 | t2 = p - r; |
---|
[5706] | 165 | facelet[i] = 0.5f * fabs( t1.cross(t2).len() ); |
---|
| 166 | /* update the entire convex hull surface */ |
---|
| 167 | face += facelet[i]; |
---|
[4648] | 168 | |
---|
[5706] | 169 | /* calculate the cetroid of the hull triangles */ |
---|
| 170 | centroid[i] = (p + q + r) * 1/3; |
---|
| 171 | /* now calculate the centroid of the entire convex hull, weighted average of triangle centroids */ |
---|
| 172 | center += centroid[i] * facelet[i]; |
---|
[4648] | 173 | } |
---|
[5706] | 174 | /* take the average of the centroid sum */ |
---|
| 175 | center /= face; |
---|
[4588] | 176 | |
---|
| 177 | |
---|
[5711] | 178 | /* now calculate the covariance matrix - if not written in three for-loops, |
---|
[5706] | 179 | it would compute faster: minor */ |
---|
| 180 | for( int j = 0; j < 3; ++j) |
---|
[4648] | 181 | { |
---|
[5706] | 182 | for( int k = 0; k < 3; ++k) |
---|
[4648] | 183 | { |
---|
[5706] | 184 | for( int i = 0; i + 3 < length; i+=3) |
---|
[4648] | 185 | { |
---|
[5713] | 186 | tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[0]]); |
---|
| 187 | p = *tmpVec; |
---|
| 188 | tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[1]]); |
---|
| 189 | q = *tmpVec; |
---|
| 190 | tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[i]].indexToVertices[2]]); |
---|
| 191 | r = *tmpVec; |
---|
[4648] | 192 | |
---|
[5706] | 193 | covariance[j][k] = facelet[i] / (12.0f * face) * (9.0f * centroid[i][j] * centroid[i][k] + p[j] * p[k] + |
---|
[5825] | 194 | q[j] * q[k] + r[j] * r[k]) - center[j] * center[k]; |
---|
[4648] | 195 | } |
---|
| 196 | } |
---|
| 197 | } |
---|
| 198 | |
---|
[5706] | 199 | PRINTF(3)("\nOBB Covariance Matrix:\n"); |
---|
[5825] | 200 | for(int j = 0; j < 3; ++j) |
---|
| 201 | { |
---|
[5882] | 202 | PRINT(3)("\t\t"); |
---|
[5825] | 203 | for(int k = 0; k < 3; ++k) |
---|
| 204 | { |
---|
[5882] | 205 | PRINT(3)("%11.2f\t", covariance[j][k]); |
---|
[5825] | 206 | } |
---|
[5882] | 207 | PRINT(3)("\n"); |
---|
[5825] | 208 | } |
---|
[5882] | 209 | PRINTF(3)("\nOBB Center:\n\t\t%11.2f\t %11.2f\t %11.2f\n", center.x, center.y, center.z); |
---|
[4588] | 210 | |
---|
[5825] | 211 | /* write back the covariance matrix data to the object oriented bouning box */ |
---|
[4674] | 212 | for(int i = 0; i < 3; ++i) |
---|
| 213 | { |
---|
[5712] | 214 | box.covarianceMatrix[i][0] = covariance[i][0]; |
---|
| 215 | box.covarianceMatrix[i][1] = covariance[i][1]; |
---|
| 216 | box.covarianceMatrix[i][2] = covariance[i][2]; |
---|
[4674] | 217 | } |
---|
[5712] | 218 | box.center = center; |
---|
[4631] | 219 | } |
---|
[4557] | 220 | |
---|
[4631] | 221 | |
---|
[5867] | 222 | |
---|
| 223 | /** |
---|
| 224 | * calculate the eigenvectors for the object oriented box |
---|
| 225 | * @param box: reference to the box |
---|
| 226 | * @param modelInf: the model info structure of the model |
---|
| 227 | * @param tirangleIndexes: an array with the indexes of the triangles inside this |
---|
| 228 | * @param length: the length of the indexes array |
---|
| 229 | */ |
---|
| 230 | void OBBTreeNode::calculateBoxEigenvectors(OBB& box, const modelInfo& modelInf, |
---|
[5825] | 231 | const int* triangleIndexes, unsigned int length) |
---|
| 232 | { |
---|
[4631] | 233 | |
---|
[5825] | 234 | Vector axis[3]; //!< the references to the obb axis |
---|
| 235 | Matrix covMat( box.covarianceMatrix ); //!< covariance matrix (in the matrix dataform) |
---|
| 236 | |
---|
| 237 | /* |
---|
| 238 | now getting spanning vectors of the sub-space: |
---|
[4617] | 239 | the eigenvectors of a symmertric matrix, such as the |
---|
| 240 | covarience matrix are mutually orthogonal. |
---|
| 241 | after normalizing them, they can be used as a the basis |
---|
| 242 | vectors |
---|
[4557] | 243 | */ |
---|
[4588] | 244 | |
---|
[5825] | 245 | /* calculate the axis */ |
---|
[5674] | 246 | covMat.getEigenVectors(axis[0], axis[1], axis[2] ); |
---|
[5712] | 247 | box.axis[0] = axis[0]; |
---|
| 248 | box.axis[1] = axis[1]; |
---|
| 249 | box.axis[2] = axis[2]; |
---|
[4660] | 250 | |
---|
[5882] | 251 | PRINTF(3)("Eigenvectors:\n"); |
---|
| 252 | PRINT(3)("\t\t%11.2f \t%11.2f \t%11.2f\n", box.axis[0].x, box.axis[0].y, box.axis[0].z); |
---|
| 253 | PRINT(3)("\t\t%11.2f \t%11.2f \t%11.2f\n", box.axis[1].x, box.axis[1].y, box.axis[1].z); |
---|
| 254 | PRINT(3)("\t\t%11.2f \t%11.2f \t%11.2f\n", box.axis[2].x, box.axis[2].y, box.axis[2].z); |
---|
[4632] | 255 | } |
---|
[4588] | 256 | |
---|
[4626] | 257 | |
---|
[5867] | 258 | |
---|
| 259 | |
---|
| 260 | /** |
---|
| 261 | * calculate the eigenvectors for the object oriented box |
---|
| 262 | * @param box: reference to the box |
---|
| 263 | * @param modelInf: the model info structure of the model |
---|
| 264 | * @param tirangleIndexes: an array with the indexes of the triangles inside this |
---|
| 265 | * @param length: the length of the indexes array |
---|
| 266 | */ |
---|
[5868] | 267 | void OBBTreeNode::calculateBoxAxis(OBB& box, const modelInfo& modelInf, const int* triangleIndexes, unsigned int length) |
---|
[5686] | 268 | { |
---|
[5684] | 269 | |
---|
[5867] | 270 | sVec3D* verticesList; |
---|
[5684] | 271 | |
---|
[5686] | 272 | |
---|
[5882] | 273 | PRINTF(3)("Calculate Box Axis\n"); |
---|
[4576] | 274 | /* now get the axis length */ |
---|
[4578] | 275 | Line ax[3]; //!< the axis |
---|
[5699] | 276 | float halfLength[3]; //!< half length of the axis |
---|
[4578] | 277 | float tmpLength; //!< tmp save point for the length |
---|
[5882] | 278 | Plane p0(box.axis[0], box.center); //!< the axis planes |
---|
| 279 | Plane p1(box.axis[1], box.center); //!< the axis planes |
---|
| 280 | Plane p2(box.axis[2], box.center); //!< the axis planes |
---|
| 281 | float maxLength[3]; //!< maximal lenth of the axis |
---|
| 282 | float minLength[3]; //!< minimal length of the axis |
---|
| 283 | const sVec3D* tmpVec; //!< variable taking tmp vectors |
---|
[4588] | 284 | |
---|
[5868] | 285 | /* |
---|
[5869] | 286 | this step is split up in three: first there will be made a bounding box which is |
---|
| 287 | very badly centered. In the section step there will be calculated a new center |
---|
[5868] | 288 | and a better bounding box. |
---|
| 289 | */ |
---|
[4658] | 290 | |
---|
[5868] | 291 | /* get a bad bounding box axis */ |
---|
[4589] | 292 | halfLength[0] = -1.0f; |
---|
[5868] | 293 | for( int j = 0; j < length; ++j) |
---|
[5825] | 294 | { |
---|
[5868] | 295 | for( int i = 0; i < 3; ++i) |
---|
| 296 | { |
---|
| 297 | tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[j]].indexToVertices[i]]); |
---|
| 298 | tmpLength = fabs(p0.distancePoint(*tmpVec)); |
---|
| 299 | if( tmpLength > halfLength[0]) |
---|
| 300 | halfLength[0] = tmpLength; |
---|
| 301 | } |
---|
[5825] | 302 | } |
---|
[4658] | 303 | |
---|
| 304 | halfLength[1] = -1.0f; |
---|
[5868] | 305 | for( int j = 0; j < length; ++j) |
---|
[5825] | 306 | { |
---|
[5868] | 307 | for( int i = 0; i < 3; ++i) |
---|
| 308 | { |
---|
| 309 | tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[j]].indexToVertices[i]]); |
---|
| 310 | tmpLength = fabs(p0.distancePoint(*tmpVec)); |
---|
| 311 | if( tmpLength > halfLength[1]) |
---|
| 312 | halfLength[1] = tmpLength; |
---|
| 313 | } |
---|
[5825] | 314 | } |
---|
[4658] | 315 | |
---|
| 316 | halfLength[2] = -1.0f; |
---|
[5868] | 317 | for( int j = 0; j < length; ++j) |
---|
[5825] | 318 | { |
---|
[5868] | 319 | for( int i = 0; i < 3; ++i) |
---|
| 320 | { |
---|
| 321 | tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[j]].indexToVertices[i]]); |
---|
| 322 | tmpLength = fabs(p0.distancePoint(*tmpVec)); |
---|
| 323 | if( tmpLength > halfLength[2]) |
---|
| 324 | halfLength[2] = tmpLength; |
---|
| 325 | } |
---|
[5825] | 326 | } |
---|
[4658] | 327 | |
---|
| 328 | |
---|
| 329 | |
---|
| 330 | /* get the maximal dimensions of the body in all directions */ |
---|
[5868] | 331 | /* for the initialisation the value just has to be inside of the polygon soup -> first vertices (rand) */ |
---|
| 332 | tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[0]].indexToVertices[0]]); |
---|
| 333 | maxLength[0] = p0.distancePoint(*tmpVec); |
---|
| 334 | minLength[0] = p0.distancePoint(*tmpVec); |
---|
| 335 | for( int j = 0; j < length; ++j) |
---|
[5825] | 336 | { |
---|
[5868] | 337 | for( int i = 0; i < 3; ++i) |
---|
| 338 | { |
---|
| 339 | tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[j]].indexToVertices[i]]); |
---|
| 340 | tmpLength = p0.distancePoint(*tmpVec); |
---|
| 341 | if( tmpLength > maxLength[0]) |
---|
| 342 | maxLength[0] = tmpLength; |
---|
| 343 | else if( tmpLength < minLength[0]) |
---|
| 344 | minLength[0] = tmpLength; |
---|
| 345 | } |
---|
[5825] | 346 | } |
---|
[4578] | 347 | |
---|
[5868] | 348 | /* for the initialisation the value just has to be inside of the polygon soup -> first vertices (rand) */ |
---|
| 349 | tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[0]].indexToVertices[0]]); |
---|
| 350 | maxLength[1] = p1.distancePoint(*tmpVec); |
---|
| 351 | minLength[1] = p1.distancePoint(*tmpVec); |
---|
| 352 | for( int j = 0; j < length; ++j) |
---|
[5825] | 353 | { |
---|
[5868] | 354 | for( int i = 0; i < 3; ++i) |
---|
| 355 | { |
---|
| 356 | tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[j]].indexToVertices[i]]); |
---|
| 357 | tmpLength = p0.distancePoint(*tmpVec); |
---|
| 358 | if( tmpLength > maxLength[1]) |
---|
| 359 | maxLength[1] = tmpLength; |
---|
| 360 | else if( tmpLength < minLength[1]) |
---|
| 361 | minLength[1] = tmpLength; |
---|
| 362 | } |
---|
[5825] | 363 | } |
---|
[4585] | 364 | |
---|
[5868] | 365 | /* for the initialisation the value just has to be inside of the polygon soup -> first vertices (rand) */ |
---|
| 366 | tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[0]].indexToVertices[0]]); |
---|
| 367 | maxLength[2] = p2.distancePoint(*tmpVec); |
---|
| 368 | minLength[2] = p2.distancePoint(*tmpVec); |
---|
| 369 | for( int j = 0; j < length; ++j) |
---|
[5825] | 370 | { |
---|
[5868] | 371 | for( int i = 0; i < 3; ++i) |
---|
| 372 | { |
---|
| 373 | tmpVec = (sVec3D*)(&modelInf.pVertices[modelInf.pTriangles[triangleIndexes[j]].indexToVertices[i]]); |
---|
| 374 | tmpLength = p0.distancePoint(*tmpVec); |
---|
| 375 | if( tmpLength > maxLength[2]) |
---|
| 376 | maxLength[2] = tmpLength; |
---|
| 377 | else if( tmpLength < minLength[2]) |
---|
| 378 | minLength[2] = tmpLength; |
---|
| 379 | } |
---|
[5825] | 380 | } |
---|
[4585] | 381 | |
---|
[4660] | 382 | |
---|
[5825] | 383 | /* calculate the real centre of the body by using the axis length */ |
---|
| 384 | float centerOffset[3]; |
---|
| 385 | float newHalfLength[3]; |
---|
| 386 | for(int i = 0; i < 3; ++i) |
---|
| 387 | { |
---|
| 388 | centerOffset[i] = (maxLength[i] + minLength[i]) / 2.0f; // min length is negatie |
---|
| 389 | newHalfLength[i] = (maxLength[i] - minLength[i]) / 2.0f; // min length is negative |
---|
[5882] | 390 | box.center += (box.axis[i] * centerOffset[i]); // update the new center vector |
---|
[5825] | 391 | halfLength[i] = newHalfLength[i]; |
---|
| 392 | } |
---|
[5882] | 393 | PRINTF(3)("\n"); |
---|
| 394 | PRINT(3)("\tmax: %11.2f, \tmin: %11.2f\n", maxLength[0], minLength[0]); |
---|
| 395 | PRINT(3)("\tmax: %11.2f, \tmin: %11.2f\n", maxLength[1], minLength[1]); |
---|
| 396 | PRINT(3)("\tmax: %11.2f, \tmin: %11.2f\n", maxLength[2], minLength[2]); |
---|
[4660] | 397 | |
---|
| 398 | |
---|
[5712] | 399 | box.halfLength[0] = halfLength[0]; |
---|
| 400 | box.halfLength[1] = halfLength[1]; |
---|
| 401 | box.halfLength[2] = halfLength[2]; |
---|
[4542] | 402 | } |
---|
| 403 | |
---|
| 404 | |
---|
[4609] | 405 | |
---|
| 406 | /** |
---|
[5869] | 407 | * this separates an ob-box in the middle |
---|
| 408 | * @param box: the box to separate |
---|
| 409 | * |
---|
| 410 | * this will separate the box into to smaller boxes. the separation is done along the middle of the longest axis |
---|
[4609] | 411 | */ |
---|
[5712] | 412 | void OBBTreeNode::forkBox(OBB& box) |
---|
[4557] | 413 | { |
---|
[5869] | 414 | |
---|
[5882] | 415 | PRINTF(3)("Fork Box\n"); |
---|
| 416 | PRINTF(4)("Calculating the longest Axis\n"); |
---|
[4557] | 417 | /* get the longest axis of the box */ |
---|
[5882] | 418 | float longestAxis = -1.0f; //!< the length of the longest axis |
---|
| 419 | int longestAxisIndex = 0; //!< this is the nr of the longest axis |
---|
[4609] | 420 | |
---|
[5882] | 421 | |
---|
| 422 | /* now get the longest axis of the three exiting */ |
---|
| 423 | for( int i = 0; i < 3; ++i) |
---|
[4609] | 424 | { |
---|
[5882] | 425 | if( longestAxis < box.halfLength[i]) |
---|
[4557] | 426 | { |
---|
[5882] | 427 | longestAxis = box.halfLength[i]; |
---|
| 428 | longestAxisIndex = i; |
---|
[4557] | 429 | } |
---|
[4609] | 430 | } |
---|
[5882] | 431 | PRINTF(3)("\nLongest Axis is: Nr %i with a half-length of:%11.2f\n", longestAxisIndex, longestAxis); |
---|
[4588] | 432 | |
---|
[4609] | 433 | |
---|
[5882] | 434 | PRINTF(4)("Separating along the longest axis\n"); |
---|
[4557] | 435 | /* get the closest vertex near the center */ |
---|
[4611] | 436 | float dist = 999999.0f; //!< the smallest distance to each vertex |
---|
[5882] | 437 | float tmpDist; //!< variable to save diverse distances temporarily |
---|
| 438 | int vertexIndex; //!< index of the vertex near the center |
---|
| 439 | Plane middlePlane(box.axis[longestAxisIndex], box.center); //!< the middle plane |
---|
| 440 | const sVec3D* tmpVec; //!< temp simple 3D vector |
---|
[4588] | 441 | |
---|
[4609] | 442 | |
---|
[4611] | 443 | /* now definin the separation plane through this specified nearest point and partition |
---|
[4617] | 444 | the points depending on which side they are located |
---|
[4611] | 445 | */ |
---|
[5882] | 446 | std::list<int> partition1; //!< the vertex partition 1 |
---|
| 447 | std::list<int> partition2; //!< the vertex partition 2 |
---|
| 448 | float* triangleCenter; //!< the center of the triangle |
---|
| 449 | const float* a; //!< triangle edge a |
---|
| 450 | const float* b; //!< triangle edge b |
---|
| 451 | const float* c; //!< triangle edge c |
---|
[4611] | 452 | |
---|
[5882] | 453 | this->separationPlane = Plane(box.axis[longestAxisIndex], box.center); |
---|
| 454 | this->sepPlaneCenter[0] = box.center.x; |
---|
| 455 | this->sepPlaneCenter[1] = box.center.y; |
---|
| 456 | this->sepPlaneCenter[2] = box.center.z; |
---|
| 457 | this->longestAxisIndex = longestAxisIndex; |
---|
[4710] | 458 | |
---|
[5882] | 459 | for( int i = 0; i < box.triangleIndexesLength; ++i) |
---|
| 460 | { |
---|
| 461 | /* first calculate the middle of the triangle */ |
---|
| 462 | a = &box.modelInf->pVertices[box.modelInf->pTriangles[box.triangleIndexes[i]].indexToVertices[0]]; |
---|
| 463 | b = &box.modelInf->pVertices[box.modelInf->pTriangles[box.triangleIndexes[i]].indexToVertices[1]]; |
---|
| 464 | c = &box.modelInf->pVertices[box.modelInf->pTriangles[box.triangleIndexes[i]].indexToVertices[2]]; |
---|
[5869] | 465 | |
---|
[5882] | 466 | triangleCenter[0] = (a[0] + b[0] + c[0])/3.0f; |
---|
| 467 | triangleCenter[1] = (a[1] + b[1] + c[1])/3.0f; |
---|
| 468 | triangleCenter[2] = (a[2] + b[2] + c[2])/3.0f; |
---|
[5869] | 469 | |
---|
[5882] | 470 | tmpDist = this->separationPlane.distancePoint(*((sVec3D*)triangleCenter)); |
---|
| 471 | if( tmpDist > 0.0f) |
---|
| 472 | partition1.push_back(i); /* positive numbers plus zero */ |
---|
[4612] | 473 | else |
---|
[5882] | 474 | partition2.push_back(i); /* negatice numbers */ |
---|
[4612] | 475 | } |
---|
[5882] | 476 | PRINTF(3)("\nPartition1: got \t%i Vertices \nPartition2: got \t%i Vertices\n", partition1.size(), partition2.size()); |
---|
[4611] | 477 | |
---|
[4612] | 478 | |
---|
[4613] | 479 | /* now comes the separation into two different sVec3D arrays */ |
---|
| 480 | int index; //!< index storage place |
---|
[5882] | 481 | int* triangleIndexList1; //!< the vertex list 1 |
---|
| 482 | int* triangleIndexList2; //!< the vertex list 2 |
---|
| 483 | std::list<int>::iterator element; //!< the list iterator |
---|
[4613] | 484 | |
---|
[5882] | 485 | triangleIndexList1 = new int[partition1.size()]; |
---|
| 486 | triangleIndexList2 = new int[partition2.size()]; |
---|
[4613] | 487 | |
---|
[5882] | 488 | for( element = partition1.begin(), index = 0; element != partition1.end(); element++, index++) |
---|
| 489 | triangleIndexList1[index] = (*element); |
---|
[5869] | 490 | |
---|
[5882] | 491 | for( element = partition2.begin(), index = 0; element != partition2.end(); element++, index++) |
---|
| 492 | triangleIndexList2[index] = (*element); |
---|
[5869] | 493 | |
---|
[5882] | 494 | if( this->triangleIndexList1!= NULL) |
---|
| 495 | delete[] this->triangleIndexList1; |
---|
| 496 | this->triangleIndexList1 = triangleIndexList1; |
---|
| 497 | this->triangleIndexLength1 = partition1.size(); |
---|
[4613] | 498 | |
---|
[5882] | 499 | if( this->triangleIndexList2 != NULL) |
---|
| 500 | delete[] this->triangleIndexList2; |
---|
| 501 | this->triangleIndexList2 = triangleIndexList2; |
---|
| 502 | this->triangleIndexLength2 = partition2.size(); |
---|
[4557] | 503 | } |
---|
| 504 | |
---|
| 505 | |
---|
[4626] | 506 | |
---|
| 507 | |
---|
[5718] | 508 | void OBBTreeNode::collideWith(const BVTreeNode& treeNode, const WorldEntity& nodeA, const WorldEntity& nodeB) const |
---|
[4695] | 509 | { |
---|
[4705] | 510 | PRINTF(3)("collideWith\n"); |
---|
[4695] | 511 | /* if the obb overlap, make subtests: check which node is realy overlaping */ |
---|
[5718] | 512 | PRINTF(3)("Checking OBB %i vs %i: ", this->getIndex(), treeNode.getIndex()); |
---|
[5825] | 513 | // if( unlikely(treeNode == NULL)) return; |
---|
[5042] | 514 | |
---|
[5718] | 515 | |
---|
| 516 | if( this->overlapTest(*this->bvElement, *(((const OBBTreeNode*)&treeNode)->bvElement), nodeA, nodeB)) |
---|
[4695] | 517 | { |
---|
[5718] | 518 | PRINTF(3)("collision @ lvl %i, object %s vs. %s, (%p, %p)\n", this->depth, nodeA.getClassName(), nodeB.getClassName(), this->nodeLeft, this->nodeRight); |
---|
[5038] | 519 | |
---|
[4695] | 520 | /* check if left node overlaps */ |
---|
[4704] | 521 | if( likely( this->nodeLeft != NULL)) |
---|
| 522 | { |
---|
[5718] | 523 | PRINTF(3)("Checking OBB %i vs %i: ", this->nodeLeft->getIndex(), treeNode.getIndex()); |
---|
| 524 | if( this->overlapTest(*this->nodeLeft->bvElement, *(((const OBBTreeNode*)&treeNode)->bvElement), nodeA, nodeB)) |
---|
[4704] | 525 | { |
---|
[5718] | 526 | this->nodeLeft->collideWith(*(((const OBBTreeNode*)&treeNode)->nodeLeft), nodeA, nodeB); |
---|
| 527 | this->nodeLeft->collideWith(*(((const OBBTreeNode*)&treeNode)->nodeRight), nodeA, nodeB); |
---|
[4704] | 528 | } |
---|
| 529 | } |
---|
[4695] | 530 | /* check if right node overlaps */ |
---|
[4704] | 531 | if( likely( this->nodeRight != NULL)) |
---|
| 532 | { |
---|
[5718] | 533 | PRINTF(3)("Checking OBB %i vs %i: ", this->nodeRight->getIndex(), treeNode.getIndex()); |
---|
| 534 | if(this->overlapTest(*this->nodeRight->bvElement, *(((const OBBTreeNode*)&treeNode)->bvElement), nodeA, nodeB)) |
---|
[4704] | 535 | { |
---|
[5718] | 536 | this->nodeRight->collideWith(*(((const OBBTreeNode*)&treeNode)->nodeLeft), nodeA, nodeB); |
---|
| 537 | this->nodeRight->collideWith(*(((const OBBTreeNode*)&treeNode)->nodeRight), nodeA, nodeB); |
---|
[4704] | 538 | } |
---|
[5044] | 539 | } |
---|
[5028] | 540 | |
---|
[5044] | 541 | /* so there is a collision and this is the last box in the tree (i.e. leaf) */ |
---|
[5718] | 542 | /* FIXME: If we would choose || insead of && there would also be asymmetrical cases supported */ |
---|
[5044] | 543 | if( unlikely(this->nodeRight == NULL && this->nodeLeft == NULL)) |
---|
| 544 | { |
---|
[5718] | 545 | nodeA.collidesWith(nodeB, (((const OBBTreeNode*)&treeNode)->bvElement->center)); |
---|
[5046] | 546 | |
---|
[5718] | 547 | nodeB.collidesWith(nodeA, this->bvElement->center); |
---|
[4704] | 548 | } |
---|
[5044] | 549 | |
---|
[4695] | 550 | } |
---|
| 551 | } |
---|
[4542] | 552 | |
---|
| 553 | |
---|
[4626] | 554 | |
---|
[5718] | 555 | bool OBBTreeNode::overlapTest(const OBB& boxA, const OBB& boxB, const WorldEntity& nodeA, const WorldEntity& nodeB) const |
---|
[4695] | 556 | { |
---|
[5825] | 557 | // if( boxB == NULL || boxA == NULL) |
---|
| 558 | // return false; |
---|
[5711] | 559 | |
---|
[4696] | 560 | /* first check all axis */ |
---|
[4708] | 561 | Vector t; |
---|
[4700] | 562 | float rA = 0.0f; |
---|
| 563 | float rB = 0.0f; |
---|
| 564 | Vector l; |
---|
[4708] | 565 | Vector rotAxisA[3]; |
---|
| 566 | Vector rotAxisB[3]; |
---|
[4626] | 567 | |
---|
[5718] | 568 | rotAxisA[0] = nodeA.getAbsDir().apply(boxA.axis[0]); |
---|
| 569 | rotAxisA[1] = nodeA.getAbsDir().apply(boxA.axis[1]); |
---|
| 570 | rotAxisA[2] = nodeA.getAbsDir().apply(boxA.axis[2]); |
---|
[4708] | 571 | |
---|
[5718] | 572 | rotAxisB[0] = nodeB.getAbsDir().apply(boxB.axis[0]); |
---|
| 573 | rotAxisB[1] = nodeB.getAbsDir().apply(boxB.axis[1]); |
---|
| 574 | rotAxisB[2] = nodeB.getAbsDir().apply(boxB.axis[2]); |
---|
[4708] | 575 | |
---|
| 576 | |
---|
[5718] | 577 | t = nodeA.getAbsCoor() + nodeA.getAbsDir().apply(boxA.center) - ( nodeB.getAbsCoor() + nodeB.getAbsDir().apply(boxB.center)); |
---|
[5706] | 578 | |
---|
[5825] | 579 | // printf("\n"); |
---|
| 580 | // 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); |
---|
| 581 | // 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); |
---|
| 582 | // 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); |
---|
| 583 | // |
---|
| 584 | // 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); |
---|
| 585 | // 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); |
---|
| 586 | // 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); |
---|
[4708] | 587 | |
---|
| 588 | |
---|
[4703] | 589 | /* All 3 axis of the object A */ |
---|
[4701] | 590 | for( int j = 0; j < 3; ++j) |
---|
[4705] | 591 | { |
---|
| 592 | rA = 0.0f; |
---|
| 593 | rB = 0.0f; |
---|
[4708] | 594 | l = rotAxisA[j]; |
---|
[4705] | 595 | |
---|
[5718] | 596 | rA += fabs(boxA.halfLength[0] * rotAxisA[0].dot(l)); |
---|
| 597 | rA += fabs(boxA.halfLength[1] * rotAxisA[1].dot(l)); |
---|
| 598 | rA += fabs(boxA.halfLength[2] * rotAxisA[2].dot(l)); |
---|
[4705] | 599 | |
---|
[5718] | 600 | rB += fabs(boxB.halfLength[0] * rotAxisB[0].dot(l)); |
---|
| 601 | rB += fabs(boxB.halfLength[1] * rotAxisB[1].dot(l)); |
---|
| 602 | rB += fabs(boxB.halfLength[2] * rotAxisB[2].dot(l)); |
---|
[4705] | 603 | |
---|
| 604 | PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB); |
---|
| 605 | |
---|
| 606 | if( (rA + rB) < fabs(t.dot(l))) |
---|
[4700] | 607 | { |
---|
[5713] | 608 | PRINTF(3)("no Collision\n"); |
---|
[4705] | 609 | return false; |
---|
| 610 | } |
---|
| 611 | } |
---|
[4700] | 612 | |
---|
[4705] | 613 | /* All 3 axis of the object B */ |
---|
| 614 | for( int j = 0; j < 3; ++j) |
---|
| 615 | { |
---|
| 616 | rA = 0.0f; |
---|
| 617 | rB = 0.0f; |
---|
[4708] | 618 | l = rotAxisB[j]; |
---|
[4701] | 619 | |
---|
[5718] | 620 | rA += fabs(boxA.halfLength[0] * rotAxisA[0].dot(l)); |
---|
| 621 | rA += fabs(boxA.halfLength[1] * rotAxisA[1].dot(l)); |
---|
| 622 | rA += fabs(boxA.halfLength[2] * rotAxisA[2].dot(l)); |
---|
[4700] | 623 | |
---|
[5718] | 624 | rB += fabs(boxB.halfLength[0] * rotAxisB[0].dot(l)); |
---|
| 625 | rB += fabs(boxB.halfLength[1] * rotAxisB[1].dot(l)); |
---|
| 626 | rB += fabs(boxB.halfLength[2] * rotAxisB[2].dot(l)); |
---|
[4703] | 627 | |
---|
[4705] | 628 | PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB); |
---|
| 629 | |
---|
| 630 | if( (rA + rB) < fabs(t.dot(l))) |
---|
| 631 | { |
---|
[5713] | 632 | PRINTF(3)("no Collision\n"); |
---|
[4705] | 633 | return false; |
---|
[4701] | 634 | } |
---|
[4705] | 635 | } |
---|
[4700] | 636 | |
---|
[4705] | 637 | |
---|
| 638 | /* Now check for all face cross products */ |
---|
| 639 | |
---|
| 640 | for( int j = 0; j < 3; ++j) |
---|
| 641 | { |
---|
| 642 | for(int k = 0; k < 3; ++k ) |
---|
[4701] | 643 | { |
---|
| 644 | rA = 0.0f; |
---|
| 645 | rB = 0.0f; |
---|
[4708] | 646 | l = rotAxisA[j].cross(rotAxisB[k]); |
---|
[4701] | 647 | |
---|
[5718] | 648 | rA += fabs(boxA.halfLength[0] * rotAxisA[0].dot(l)); |
---|
| 649 | rA += fabs(boxA.halfLength[1] * rotAxisA[1].dot(l)); |
---|
| 650 | rA += fabs(boxA.halfLength[2] * rotAxisA[2].dot(l)); |
---|
[4701] | 651 | |
---|
[5718] | 652 | rB += fabs(boxB.halfLength[0] * rotAxisB[0].dot(l)); |
---|
| 653 | rB += fabs(boxB.halfLength[1] * rotAxisB[1].dot(l)); |
---|
| 654 | rB += fabs(boxB.halfLength[2] * rotAxisB[2].dot(l)); |
---|
[4701] | 655 | |
---|
[4703] | 656 | PRINTF(3)("s = %f, rA+rB = %f\n", fabs(t.dot(l)), rA+rB); |
---|
| 657 | |
---|
[4701] | 658 | if( (rA + rB) < fabs(t.dot(l))) |
---|
| 659 | { |
---|
[5713] | 660 | PRINTF(3)("keine Kollision\n"); |
---|
[4701] | 661 | return false; |
---|
| 662 | } |
---|
[4703] | 663 | } |
---|
[4705] | 664 | } |
---|
[4701] | 665 | |
---|
[5718] | 666 | /* FIXME: there is no collision mark set now */ |
---|
[5825] | 667 | // boxA.bCollided = true; /* use this ONLY(!!!!) for drawing operations */ |
---|
| 668 | // boxB.bCollided = true; |
---|
[4701] | 669 | |
---|
[5718] | 670 | |
---|
[5713] | 671 | PRINTF(3)("Kollision!\n"); |
---|
[4705] | 672 | return true; |
---|
[4695] | 673 | } |
---|
| 674 | |
---|
| 675 | |
---|
[4696] | 676 | |
---|
[4708] | 677 | |
---|
| 678 | |
---|
[5481] | 679 | void OBBTreeNode::drawBV(int depth, int drawMode, const Vector& color, bool top) const |
---|
[4553] | 680 | { |
---|
[4635] | 681 | |
---|
| 682 | /* draw the model itself, there is some problem concerning this: the vertices are drawn multiple times */ |
---|
| 683 | if( drawMode & DRAW_MODEL || drawMode & DRAW_ALL) |
---|
| 684 | { |
---|
[4638] | 685 | if( !(drawMode & DRAW_SINGLE && depth != 0)) |
---|
[4622] | 686 | { |
---|
[4712] | 687 | if( drawMode & DRAW_POINTS) |
---|
| 688 | glBegin(GL_POINTS); |
---|
[5882] | 689 | for( int i = 0; i < this->bvElement->modelInf->numVertices; i+=3) |
---|
[4638] | 690 | { |
---|
[4712] | 691 | if( drawMode & DRAW_POINTS) |
---|
[5882] | 692 | glVertex3f(this->bvElement->modelInf->pVertices[i], this->bvElement->modelInf->pVertices[i+1], this->bvElement->modelInf->pVertices[i+2]); |
---|
[4712] | 693 | else |
---|
| 694 | { |
---|
| 695 | glPushMatrix(); |
---|
[5882] | 696 | glVertex3f(this->bvElement->modelInf->pVertices[i], this->bvElement->modelInf->pVertices[i+1], this->bvElement->modelInf->pVertices[i+2]); |
---|
[5430] | 697 | gluSphere(OBBTreeNode_sphereObj, 0.1, 10, 10); |
---|
[4712] | 698 | glPopMatrix(); |
---|
| 699 | } |
---|
[4638] | 700 | } |
---|
[4712] | 701 | if( drawMode & DRAW_POINTS) |
---|
| 702 | glEnd(); |
---|
[4622] | 703 | } |
---|
[4635] | 704 | } |
---|
[4542] | 705 | |
---|
[5481] | 706 | if (top) |
---|
| 707 | { |
---|
| 708 | glPushAttrib(GL_ENABLE_BIT); |
---|
| 709 | glDisable(GL_LIGHTING); |
---|
| 710 | glDisable(GL_TEXTURE_2D); |
---|
| 711 | } |
---|
| 712 | glColor3f(color.x, color.y, color.z); |
---|
[4542] | 713 | |
---|
[5481] | 714 | |
---|
[4589] | 715 | /* draw world axes */ |
---|
[4676] | 716 | if( drawMode & DRAW_BV_AXIS) |
---|
| 717 | { |
---|
| 718 | glBegin(GL_LINES); |
---|
[5481] | 719 | glColor3f(1.0, 0.0, 0.0); |
---|
[4676] | 720 | glVertex3f(0.0, 0.0, 0.0); |
---|
| 721 | glVertex3f(3.0, 0.0, 0.0); |
---|
[4589] | 722 | |
---|
[5481] | 723 | glColor3f(0.0, 1.0, 0.0); |
---|
[4676] | 724 | glVertex3f(0.0, 0.0, 0.0); |
---|
| 725 | glVertex3f(0.0, 3.0, 0.0); |
---|
[4589] | 726 | |
---|
[5481] | 727 | glColor3f(0.0, 0.0, 1.0); |
---|
[4676] | 728 | glVertex3f(0.0, 0.0, 0.0); |
---|
| 729 | glVertex3f(0.0, 0.0, 3.0); |
---|
| 730 | glEnd(); |
---|
| 731 | } |
---|
[4674] | 732 | |
---|
[4688] | 733 | |
---|
[4635] | 734 | if( drawMode & DRAW_BV_AXIS || drawMode & DRAW_ALL) |
---|
| 735 | { |
---|
[4636] | 736 | if( !(drawMode & DRAW_SINGLE && depth != 0)) |
---|
[4635] | 737 | { |
---|
| 738 | /* draw the obb axes */ |
---|
| 739 | glBegin(GL_LINES); |
---|
| 740 | glColor3f(0.0, 0.4, 0.3); |
---|
[5706] | 741 | glVertex3f(this->bvElement->center.x, this->bvElement->center.y, this->bvElement->center.z); |
---|
| 742 | glVertex3f(this->bvElement->center.x + this->bvElement->axis[0].x * this->bvElement->halfLength[0], |
---|
| 743 | this->bvElement->center.y + this->bvElement->axis[0].y * this->bvElement->halfLength[0], |
---|
| 744 | this->bvElement->center.z + this->bvElement->axis[0].z * this->bvElement->halfLength[0]); |
---|
[4589] | 745 | |
---|
[5706] | 746 | glVertex3f(this->bvElement->center.x, this->bvElement->center.y, this->bvElement->center.z); |
---|
| 747 | glVertex3f(this->bvElement->center.x + this->bvElement->axis[1].x * this->bvElement->halfLength[1], |
---|
| 748 | this->bvElement->center.y + this->bvElement->axis[1].y * this->bvElement->halfLength[1], |
---|
| 749 | this->bvElement->center.z + this->bvElement->axis[1].z * this->bvElement->halfLength[1]); |
---|
[4588] | 750 | |
---|
[5706] | 751 | glVertex3f(this->bvElement->center.x, this->bvElement->center.y, this->bvElement->center.z); |
---|
| 752 | glVertex3f(this->bvElement->center.x + this->bvElement->axis[2].x * this->bvElement->halfLength[2], |
---|
| 753 | this->bvElement->center.y + this->bvElement->axis[2].y * this->bvElement->halfLength[2], |
---|
| 754 | this->bvElement->center.z + this->bvElement->axis[2].z * this->bvElement->halfLength[2]); |
---|
[4635] | 755 | glEnd(); |
---|
| 756 | } |
---|
| 757 | } |
---|
[4581] | 758 | |
---|
[4588] | 759 | |
---|
[4674] | 760 | /* DRAW POLYGONS */ |
---|
[4673] | 761 | if( drawMode & DRAW_BV_POLYGON || drawMode & DRAW_ALL || drawMode & DRAW_BV_BLENDED) |
---|
[4635] | 762 | { |
---|
[5487] | 763 | if (top) |
---|
| 764 | { |
---|
| 765 | glEnable(GL_BLEND); |
---|
| 766 | glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
---|
| 767 | } |
---|
| 768 | |
---|
[4711] | 769 | if(this->nodeLeft == NULL || this->nodeRight == NULL) |
---|
[4710] | 770 | depth = 0; |
---|
[4636] | 771 | if( !(drawMode & DRAW_SINGLE && depth != 0)) |
---|
[4635] | 772 | { |
---|
[5825] | 773 | Vector cen = this->bvElement->center; |
---|
| 774 | Vector* axis = this->bvElement->axis; |
---|
| 775 | float* len = this->bvElement->halfLength; |
---|
[4588] | 776 | |
---|
[5825] | 777 | if( this->bvElement->bCollided) |
---|
| 778 | { |
---|
| 779 | glColor4f(1.0, 1.0, 1.0, .5); // COLLISION COLOR |
---|
| 780 | } |
---|
| 781 | else if( drawMode & DRAW_BV_BLENDED) |
---|
| 782 | { |
---|
| 783 | glColor4f(color.x, color.y, color.z, .5); |
---|
| 784 | } |
---|
[4670] | 785 | |
---|
[5825] | 786 | /* draw bounding box */ |
---|
| 787 | if( drawMode & DRAW_BV_BLENDED) |
---|
| 788 | glBegin(GL_QUADS); |
---|
| 789 | else |
---|
| 790 | glBegin(GL_LINE_LOOP); |
---|
| 791 | glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 792 | cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 793 | cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); |
---|
[4671] | 794 | glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 795 | cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 796 | cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); |
---|
| 797 | glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 798 | cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 799 | cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); |
---|
[5825] | 800 | glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 801 | cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 802 | cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); |
---|
| 803 | glEnd(); |
---|
| 804 | |
---|
| 805 | if( drawMode & DRAW_BV_BLENDED) |
---|
| 806 | glBegin(GL_QUADS); |
---|
| 807 | else |
---|
| 808 | glBegin(GL_LINE_LOOP); |
---|
| 809 | glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 810 | cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 811 | cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); |
---|
| 812 | glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 813 | cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 814 | cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); |
---|
[4671] | 815 | glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 816 | cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 817 | cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); |
---|
[5825] | 818 | glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 819 | cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 820 | cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); |
---|
[4671] | 821 | glEnd(); |
---|
| 822 | |
---|
[5825] | 823 | if( drawMode & DRAW_BV_BLENDED) |
---|
| 824 | glBegin(GL_QUADS); |
---|
| 825 | else |
---|
| 826 | glBegin(GL_LINE_LOOP); |
---|
| 827 | glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 828 | cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 829 | cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); |
---|
| 830 | glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 831 | cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 832 | cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); |
---|
| 833 | glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 834 | cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 835 | cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); |
---|
[4671] | 836 | glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 837 | cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 838 | cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); |
---|
[5825] | 839 | glEnd(); |
---|
| 840 | |
---|
| 841 | if( drawMode & DRAW_BV_BLENDED) |
---|
| 842 | glBegin(GL_QUADS); |
---|
| 843 | else |
---|
| 844 | glBegin(GL_LINE_LOOP); |
---|
| 845 | glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 846 | cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 847 | cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); |
---|
| 848 | glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 849 | cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 850 | cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); |
---|
[4671] | 851 | glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 852 | cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 853 | cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); |
---|
[5825] | 854 | glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 855 | cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 856 | cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); |
---|
[4671] | 857 | glEnd(); |
---|
| 858 | |
---|
| 859 | |
---|
[5825] | 860 | if( drawMode & DRAW_BV_BLENDED) |
---|
| 861 | { |
---|
| 862 | glBegin(GL_QUADS); |
---|
| 863 | glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 864 | cen.y - axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 865 | cen.z - axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); |
---|
| 866 | glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 867 | cen.y + axis[0].y * len[0] + axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 868 | cen.z + axis[0].z * len[0] + axis[1].z * len[1] - axis[2].z * len[2]); |
---|
| 869 | glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 870 | cen.y + axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 871 | cen.z + axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); |
---|
| 872 | glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] - axis[2].x * len[2], |
---|
| 873 | cen.y - axis[0].y * len[0] - axis[1].y * len[1] - axis[2].y * len[2], |
---|
| 874 | cen.z - axis[0].z * len[0] - axis[1].z * len[1] - axis[2].z * len[2]); |
---|
| 875 | glEnd(); |
---|
| 876 | |
---|
| 877 | glBegin(GL_QUADS); |
---|
| 878 | glVertex3f(cen.x - axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 879 | cen.y - axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 880 | cen.z - axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); |
---|
| 881 | glVertex3f(cen.x + axis[0].x * len[0] + axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 882 | cen.y + axis[0].y * len[0] + axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 883 | cen.z + axis[0].z * len[0] + axis[1].z * len[1] + axis[2].z * len[2]); |
---|
| 884 | glVertex3f(cen.x + axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 885 | cen.y + axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 886 | cen.z + axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); |
---|
| 887 | glVertex3f(cen.x - axis[0].x * len[0] - axis[1].x * len[1] + axis[2].x * len[2], |
---|
| 888 | cen.y - axis[0].y * len[0] - axis[1].y * len[1] + axis[2].y * len[2], |
---|
| 889 | cen.z - axis[0].z * len[0] - axis[1].z * len[1] + axis[2].z * len[2]); |
---|
| 890 | glEnd(); |
---|
| 891 | } |
---|
| 892 | |
---|
| 893 | |
---|
| 894 | if( drawMode & DRAW_BV_BLENDED) |
---|
| 895 | glColor3f(color.x, color.y, color.z); |
---|
[4635] | 896 | } |
---|
[4636] | 897 | |
---|
[4635] | 898 | } |
---|
[4588] | 899 | |
---|
[4674] | 900 | /* DRAW SEPARATING PLANE */ |
---|
[4635] | 901 | if( drawMode & DRAW_SEPARATING_PLANE || drawMode & DRAW_ALL) |
---|
[4632] | 902 | { |
---|
[4636] | 903 | if( !(drawMode & DRAW_SINGLE && depth != 0)) |
---|
[4635] | 904 | { |
---|
[4671] | 905 | if( drawMode & DRAW_BV_BLENDED) |
---|
[5481] | 906 | glColor4f(color.x, color.y, color.z, .6); |
---|
[4671] | 907 | |
---|
[5825] | 908 | /* now draw the separation plane */ |
---|
| 909 | Vector a1 = this->bvElement->axis[(this->longestAxisIndex + 1)%3]; |
---|
| 910 | Vector a2 = this->bvElement->axis[(this->longestAxisIndex + 2)%3]; |
---|
| 911 | Vector c = this->bvElement->center; |
---|
| 912 | float l1 = this->bvElement->halfLength[(this->longestAxisIndex + 1)%3]; |
---|
| 913 | float l2 = this->bvElement->halfLength[(this->longestAxisIndex + 2)%3]; |
---|
| 914 | glBegin(GL_QUADS); |
---|
| 915 | 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); |
---|
| 916 | 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); |
---|
| 917 | 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); |
---|
| 918 | 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); |
---|
| 919 | glEnd(); |
---|
[4671] | 920 | |
---|
[5825] | 921 | if( drawMode & DRAW_BV_BLENDED) |
---|
| 922 | glColor4f(color.x, color.y, color.z, 1.0); |
---|
[4671] | 923 | |
---|
[4635] | 924 | } |
---|
[4632] | 925 | } |
---|
[4588] | 926 | |
---|
[4702] | 927 | |
---|
| 928 | |
---|
[5481] | 929 | if (depth > 0) |
---|
| 930 | { |
---|
| 931 | if( this->nodeLeft != NULL) |
---|
[5494] | 932 | this->nodeLeft->drawBV(depth - 1, drawMode, Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(15.0,0.0,0.0)), false); |
---|
[5481] | 933 | if( this->nodeRight != NULL) |
---|
[5494] | 934 | this->nodeRight->drawBV(depth - 1, drawMode, Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(30.0,0.0,0.0)), false); |
---|
[5481] | 935 | } |
---|
| 936 | this->bvElement->bCollided = false; |
---|
[4588] | 937 | |
---|
[5481] | 938 | if (top) |
---|
| 939 | glPopAttrib(); |
---|
[4557] | 940 | } |
---|
[4542] | 941 | |
---|
| 942 | |
---|
[4568] | 943 | |
---|
[4746] | 944 | void OBBTreeNode::debug() const |
---|
[4568] | 945 | { |
---|
[5825] | 946 | PRINT(0)("========OBBTreeNode::debug()=====\n"); |
---|
| 947 | PRINT(0)(" Current depth: %i", this->depth); |
---|
| 948 | PRINT(0)(" "); |
---|
| 949 | PRINT(0)("=================================\n"); |
---|
[4617] | 950 | } |
---|