[4573] | 1 | /* |
---|
[4510] | 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 | |
---|
| 11 | ### File Specific: |
---|
[4511] | 12 | main-programmer: Patrick Boenzli |
---|
[4510] | 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
[6894] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION_DETECTION |
---|
[4510] | 17 | |
---|
[4511] | 18 | #include "obb_tree.h" |
---|
[4550] | 19 | #include "obb_tree_node.h" |
---|
[4531] | 20 | #include "obb.h" |
---|
[4546] | 21 | #include "debug.h" |
---|
[4550] | 22 | #include "compiler.h" |
---|
[4616] | 23 | #include "material.h" |
---|
[5026] | 24 | #include "world_entity.h" |
---|
[4700] | 25 | #include "p_node.h" |
---|
[4510] | 26 | |
---|
| 27 | using namespace std; |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | /** |
---|
[4836] | 31 | * standard constructor |
---|
[4510] | 32 | */ |
---|
[6894] | 33 | OBBTree::OBBTree(int depth, const modelInfo* modelInf) |
---|
| 34 | : BVTree() |
---|
[4510] | 35 | { |
---|
[6894] | 36 | this->depth = depth; |
---|
[4682] | 37 | this->init(); |
---|
[6894] | 38 | this->spawnBVTree(*modelInf); |
---|
[4682] | 39 | } |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | void OBBTree::init() |
---|
| 44 | { |
---|
[4616] | 45 | this->setClassID(CL_OBB_TREE, "OBBTree"); |
---|
[5115] | 46 | this->rootNode = NULL; |
---|
[4638] | 47 | this->id = 0; |
---|
[4510] | 48 | } |
---|
| 49 | |
---|
| 50 | /** |
---|
[4836] | 51 | * standard deconstructor |
---|
[4510] | 52 | |
---|
| 53 | */ |
---|
[4573] | 54 | OBBTree::~OBBTree () |
---|
[4510] | 55 | { |
---|
[4814] | 56 | delete this->rootNode; |
---|
[4510] | 57 | } |
---|
[4528] | 58 | |
---|
| 59 | |
---|
[6894] | 60 | /** |
---|
| 61 | * this function creates a bv tree out of a modelInf structure |
---|
| 62 | * @param modelInf the model info of a model (modelInfo), containing vertices, triangle and normal infos |
---|
| 63 | */ |
---|
| 64 | void OBBTree::spawnBVTree(const modelInfo& modelInf) |
---|
[4531] | 65 | { |
---|
[4551] | 66 | if( unlikely(this->rootNode != NULL)) |
---|
[5684] | 67 | { |
---|
[6897] | 68 | PRINTF(2)("The BVTree has already been spawned, returning\n"); |
---|
| 69 | return; |
---|
[5684] | 70 | } |
---|
[6897] | 71 | |
---|
[6894] | 72 | this->rootNode = new OBBTreeNode(*this, NULL, depth-1); |
---|
| 73 | /* triangles indexes created */ |
---|
| 74 | int* triangleIndexes = new int[modelInf.numTriangles]; |
---|
| 75 | for( int i = 0; i < modelInf.numTriangles; ++i) |
---|
| 76 | triangleIndexes[i] = i; |
---|
| 77 | |
---|
| 78 | this->rootNode->spawnBVTree(modelInf, triangleIndexes, modelInf.numTriangles); |
---|
[5684] | 79 | } |
---|
| 80 | |
---|
| 81 | |
---|
[6894] | 82 | /** |
---|
| 83 | * release the current bv tree if any |
---|
| 84 | */ |
---|
[4528] | 85 | void OBBTree:: flushTree() |
---|
| 86 | {} |
---|
| 87 | |
---|
| 88 | |
---|
[5026] | 89 | /** |
---|
| 90 | * this collides two bvtrees together. the trees are attached to pnodes Objects A and B |
---|
| 91 | * @param nodeA: PNode of object A |
---|
| 92 | * @param nodeB: Pnode of object B |
---|
| 93 | */ |
---|
[6894] | 94 | void OBBTree::collideWith(WorldEntity* entity1, WorldEntity* entity2) const |
---|
[4695] | 95 | { |
---|
[6894] | 96 | if( likely(entity2->getOBBTree() != NULL) ) |
---|
| 97 | this->rootNode->collideWith(((OBBTree*)entity2->getOBBTree())->getRootNode(), entity1, entity2); |
---|
[4695] | 98 | } |
---|
[4528] | 99 | |
---|
| 100 | |
---|
[6894] | 101 | /** |
---|
| 102 | * draw bv tree |
---|
| 103 | */ |
---|
[4635] | 104 | void OBBTree::drawBV(int depth, int drawMode) const |
---|
[4550] | 105 | { |
---|
| 106 | if( likely(this->rootNode != NULL)) |
---|
[4581] | 107 | { |
---|
[4635] | 108 | this->rootNode->drawBV(depth, drawMode); |
---|
[4581] | 109 | } |
---|
[4550] | 110 | } |
---|
[4528] | 111 | |
---|
| 112 | |
---|
[6894] | 113 | /** |
---|
| 114 | * some debug output and creation function |
---|
| 115 | * |
---|
| 116 | * doesn't work at the moment |
---|
| 117 | */ |
---|
[4546] | 118 | void OBBTree::debug() |
---|
| 119 | { |
---|
| 120 | PRINT(0)("\n==============================| OBBTree::debug() |===\n"); |
---|
| 121 | PRINT(0)("= Spawning Tree: Start\n"); |
---|
[4573] | 122 | |
---|
[4551] | 123 | /* generate some test vertices */ |
---|
[4638] | 124 | int const length = 9; |
---|
[4589] | 125 | sVec3D* vertList = new sVec3D[length]; |
---|
[4668] | 126 | // sVec3D data[length] = {{0.0, 0.0, 0.0},{1.0, 2.0, 5.0},{0.0, 6.0, 9.0}, |
---|
| 127 | // {1.0, 4.0, 12.0}, {1.0, 2.0, 16.0}, {0.0, 0.0, 19.0}, |
---|
| 128 | // {0.0, 3.0, 23.0}, {1.0, 5.0, 30.0}, {0.0, 10.0, 35.0}}; |
---|
[4551] | 129 | |
---|
[4668] | 130 | sVec3D data[length] = {{0.0, 0.0, 0.0},{1.0, 2.0, 5.0},{1.0, 5.0, 30.0}, |
---|
| 131 | {0.0, 3.0, 23.0}, {0.0, 6.0, 9.0}, {0.0, 10.0, 35.0}, |
---|
| 132 | {1.0, 4.0, 12.0}, {1.0, 2.0, 16.0}, {0.0, 0.0, 19.0}}; |
---|
| 133 | |
---|
[4589] | 134 | for(int i = 0; i < length; ++i) |
---|
[4553] | 135 | { |
---|
| 136 | vertList[i][0] = data[i][0]; |
---|
| 137 | vertList[i][1] = data[i][1]; |
---|
| 138 | vertList[i][2] = data[i][2]; |
---|
| 139 | } |
---|
| 140 | |
---|
[6894] | 141 | // this->spawnBVTree(vertList, length); |
---|
[4551] | 142 | |
---|
[4546] | 143 | PRINT(0)("= Spawning Tree: Finished\n"); |
---|
[4573] | 144 | PRINT(0)("=======================================================\n"); |
---|
[4546] | 145 | |
---|
| 146 | } |
---|