[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 | |
---|
[4511] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION |
---|
[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 | */ |
---|
[4573] | 33 | OBBTree::OBBTree () |
---|
[4510] | 34 | { |
---|
[4682] | 35 | this->init(); |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | OBBTree::OBBTree(int depth, sVec3D *verticesList, const int length) |
---|
| 39 | { |
---|
| 40 | this->init(); |
---|
| 41 | this->spawnBVTree(depth, verticesList, length); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | void OBBTree::init() |
---|
| 47 | { |
---|
[4616] | 48 | this->setClassID(CL_OBB_TREE, "OBBTree"); |
---|
[4622] | 49 | |
---|
[4638] | 50 | material = new Material*[5]; |
---|
| 51 | for(int i = 0; i < 5; ++i) |
---|
[4622] | 52 | { |
---|
| 53 | material[i] = new Material(); |
---|
[4638] | 54 | material[i]->setIllum(3); |
---|
[4622] | 55 | } |
---|
| 56 | material[0]->setAmbient(0.0, 0.3, 0.0); |
---|
[4638] | 57 | material[1]->setAmbient(0.4, 0.0, 0.2); |
---|
[4622] | 58 | material[2]->setAmbient(1.0, 0.0, 0.0); |
---|
[4638] | 59 | material[3]->setAmbient(5.0, 3.0, 1.0); |
---|
| 60 | material[4]->setAmbient(1.0, 0.0, 7.0); |
---|
| 61 | |
---|
[4670] | 62 | |
---|
| 63 | transparentMaterial = new Material*[5]; |
---|
| 64 | for(int i = 0; i < 5; ++i) |
---|
| 65 | { |
---|
| 66 | transparentMaterial[i] = new Material(); |
---|
| 67 | transparentMaterial[i]->setIllum(3); |
---|
[4671] | 68 | transparentMaterial[i]->setTransparency(0.2); |
---|
[4670] | 69 | } |
---|
| 70 | transparentMaterial[0]->setAmbient(0.0, 0.3, 0.0); |
---|
| 71 | transparentMaterial[1]->setAmbient(0.4, 0.0, 0.2); |
---|
| 72 | transparentMaterial[2]->setAmbient(1.0, 0.0, 0.0); |
---|
| 73 | transparentMaterial[3]->setAmbient(5.0, 3.0, 1.0); |
---|
| 74 | transparentMaterial[4]->setAmbient(1.0, 0.0, 7.0); |
---|
| 75 | |
---|
[4702] | 76 | this->collisionMaterial = new Material(); |
---|
| 77 | this->collisionMaterial->setIllum(5); |
---|
| 78 | this->collisionMaterial->setTransparency(0.5); |
---|
| 79 | this->collisionMaterial->setAmbient(1.0, 1.0, 1.0); |
---|
| 80 | |
---|
[4638] | 81 | this->id = 0; |
---|
[4510] | 82 | } |
---|
| 83 | |
---|
| 84 | /** |
---|
[4836] | 85 | * standard deconstructor |
---|
[4510] | 86 | |
---|
| 87 | */ |
---|
[4573] | 88 | OBBTree::~OBBTree () |
---|
[4510] | 89 | { |
---|
[4814] | 90 | delete this->rootNode; |
---|
[4510] | 91 | } |
---|
[4528] | 92 | |
---|
| 93 | |
---|
[4551] | 94 | void OBBTree::spawnBVTree(int depth, sVec3D *verticesList, const int length) |
---|
[4531] | 95 | { |
---|
[4551] | 96 | if( unlikely(this->rootNode != NULL)) |
---|
| 97 | { |
---|
| 98 | PRINTF(2)("The BVTree has already been spawned, flushing and respawning again...\n"); |
---|
| 99 | this->flushTree(); |
---|
| 100 | } |
---|
| 101 | OBBTreeNode* node = new OBBTreeNode(); |
---|
| 102 | this->rootNode = node; |
---|
[4622] | 103 | this->rootNode->setTreeRef(this); |
---|
[4626] | 104 | this->rootNode->spawnBVTree(--depth, verticesList, length); |
---|
[4531] | 105 | } |
---|
[4528] | 106 | |
---|
| 107 | |
---|
| 108 | void OBBTree:: flushTree() |
---|
| 109 | {} |
---|
| 110 | |
---|
| 111 | |
---|
[5026] | 112 | void OBBTree::collideWith(WorldEntity* entity1, WorldEntity* entity2) |
---|
| 113 | { |
---|
[5027] | 114 | if( likely(entity2->getOBBTree() != NULL) ) |
---|
[5028] | 115 | this->rootNode->collideWith(((OBBTree*)entity2->getOBBTree())->getRootNode(), entity1, entity2); |
---|
[5026] | 116 | } |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | /** |
---|
| 120 | * this collides two bvtrees together. the trees are attached to pnodes Objects A and B |
---|
| 121 | * @param tree: the other tree to collide with (Object B) |
---|
| 122 | * @param nodeA: PNode of object A |
---|
| 123 | * @param nodeB: Pnode of object B |
---|
| 124 | */ |
---|
[5028] | 125 | void OBBTree::collideWith(BVTree* tree, WorldEntity* nodeA, WorldEntity* nodeB) |
---|
[4695] | 126 | { |
---|
[4700] | 127 | this->rootNode->collideWith(((OBBTree*)tree)->getRootNode(), nodeA, nodeB); |
---|
[4695] | 128 | } |
---|
[4528] | 129 | |
---|
| 130 | |
---|
[4635] | 131 | void OBBTree::drawBV(int depth, int drawMode) const |
---|
[4550] | 132 | { |
---|
| 133 | if( likely(this->rootNode != NULL)) |
---|
[4581] | 134 | { |
---|
[4635] | 135 | this->rootNode->drawBV(depth, drawMode); |
---|
[4581] | 136 | } |
---|
[4550] | 137 | } |
---|
[4528] | 138 | |
---|
| 139 | |
---|
| 140 | |
---|
[4546] | 141 | void OBBTree::debug() |
---|
| 142 | { |
---|
| 143 | PRINT(0)("\n==============================| OBBTree::debug() |===\n"); |
---|
| 144 | PRINT(0)("= Spawning Tree: Start\n"); |
---|
[4573] | 145 | |
---|
[4551] | 146 | /* generate some test vertices */ |
---|
[4638] | 147 | int const length = 9; |
---|
[4589] | 148 | sVec3D* vertList = new sVec3D[length]; |
---|
[4668] | 149 | // sVec3D data[length] = {{0.0, 0.0, 0.0},{1.0, 2.0, 5.0},{0.0, 6.0, 9.0}, |
---|
| 150 | // {1.0, 4.0, 12.0}, {1.0, 2.0, 16.0}, {0.0, 0.0, 19.0}, |
---|
| 151 | // {0.0, 3.0, 23.0}, {1.0, 5.0, 30.0}, {0.0, 10.0, 35.0}}; |
---|
[4551] | 152 | |
---|
[4668] | 153 | sVec3D data[length] = {{0.0, 0.0, 0.0},{1.0, 2.0, 5.0},{1.0, 5.0, 30.0}, |
---|
| 154 | {0.0, 3.0, 23.0}, {0.0, 6.0, 9.0}, {0.0, 10.0, 35.0}, |
---|
| 155 | {1.0, 4.0, 12.0}, {1.0, 2.0, 16.0}, {0.0, 0.0, 19.0}}; |
---|
| 156 | |
---|
[4589] | 157 | for(int i = 0; i < length; ++i) |
---|
[4553] | 158 | { |
---|
| 159 | vertList[i][0] = data[i][0]; |
---|
| 160 | vertList[i][1] = data[i][1]; |
---|
| 161 | vertList[i][2] = data[i][2]; |
---|
| 162 | } |
---|
| 163 | |
---|
[4638] | 164 | this->spawnBVTree(3, vertList, length); |
---|
[4551] | 165 | |
---|
[4546] | 166 | PRINT(0)("= Spawning Tree: Finished\n"); |
---|
[4573] | 167 | PRINT(0)("=======================================================\n"); |
---|
[4546] | 168 | |
---|
| 169 | } |
---|