1 | /* |
---|
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: |
---|
12 | main-programmer: Patrick Boenzli |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION |
---|
17 | |
---|
18 | #include "obb_tree.h" |
---|
19 | #include "obb_tree_node.h" |
---|
20 | #include "obb.h" |
---|
21 | #include "debug.h" |
---|
22 | #include "compiler.h" |
---|
23 | #include "material.h" |
---|
24 | #include "world_entity.h" |
---|
25 | #include "p_node.h" |
---|
26 | |
---|
27 | using namespace std; |
---|
28 | |
---|
29 | |
---|
30 | /** |
---|
31 | * standard constructor |
---|
32 | */ |
---|
33 | OBBTree::OBBTree () |
---|
34 | { |
---|
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 | { |
---|
48 | this->setClassID(CL_OBB_TREE, "OBBTree"); |
---|
49 | |
---|
50 | this->rootNode = NULL; |
---|
51 | |
---|
52 | material = new Material*[5]; |
---|
53 | for(int i = 0; i < 5; ++i) |
---|
54 | { |
---|
55 | material[i] = new Material(); |
---|
56 | material[i]->setIllum(3); |
---|
57 | } |
---|
58 | material[0]->setAmbient(0.0, 0.3, 0.0); |
---|
59 | material[1]->setAmbient(0.4, 0.0, 0.2); |
---|
60 | material[2]->setAmbient(1.0, 0.0, 0.0); |
---|
61 | material[3]->setAmbient(5.0, 3.0, 1.0); |
---|
62 | material[4]->setAmbient(1.0, 0.0, 7.0); |
---|
63 | |
---|
64 | |
---|
65 | transparentMaterial = new Material*[5]; |
---|
66 | for(int i = 0; i < 5; ++i) |
---|
67 | { |
---|
68 | transparentMaterial[i] = new Material(); |
---|
69 | transparentMaterial[i]->setIllum(3); |
---|
70 | transparentMaterial[i]->setTransparency(0.2); |
---|
71 | } |
---|
72 | transparentMaterial[0]->setAmbient(0.0, 0.3, 0.0); |
---|
73 | transparentMaterial[1]->setAmbient(0.4, 0.0, 0.2); |
---|
74 | transparentMaterial[2]->setAmbient(1.0, 0.0, 0.0); |
---|
75 | transparentMaterial[3]->setAmbient(5.0, 3.0, 1.0); |
---|
76 | transparentMaterial[4]->setAmbient(1.0, 0.0, 7.0); |
---|
77 | |
---|
78 | this->collisionMaterial = new Material(); |
---|
79 | this->collisionMaterial->setIllum(5); |
---|
80 | this->collisionMaterial->setTransparency(0.5); |
---|
81 | this->collisionMaterial->setAmbient(1.0, 1.0, 1.0); |
---|
82 | |
---|
83 | this->id = 0; |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * standard deconstructor |
---|
88 | |
---|
89 | */ |
---|
90 | OBBTree::~OBBTree () |
---|
91 | { |
---|
92 | delete this->rootNode; |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | void OBBTree::spawnBVTree(int depth, sVec3D *verticesList, const int length) |
---|
97 | { |
---|
98 | if( unlikely(this->rootNode != NULL)) |
---|
99 | { |
---|
100 | PRINTF(2)("The BVTree has already been spawned, flushing and respawning again...\n"); |
---|
101 | this->flushTree(); |
---|
102 | } |
---|
103 | OBBTreeNode* node = new OBBTreeNode(); |
---|
104 | this->rootNode = node; |
---|
105 | this->rootNode->setTreeRef(this); |
---|
106 | this->rootNode->spawnBVTree(--depth, verticesList, length); |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | void OBBTree:: flushTree() |
---|
111 | {} |
---|
112 | |
---|
113 | |
---|
114 | void OBBTree::collideWith(WorldEntity* entity1, WorldEntity* entity2) |
---|
115 | { |
---|
116 | if( likely(entity2->getOBBTree() != NULL) ) |
---|
117 | this->rootNode->collideWith(((OBBTree*)entity2->getOBBTree())->getRootNode(), entity1, entity2); |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | /** |
---|
122 | * this collides two bvtrees together. the trees are attached to pnodes Objects A and B |
---|
123 | * @param tree: the other tree to collide with (Object B) |
---|
124 | * @param nodeA: PNode of object A |
---|
125 | * @param nodeB: Pnode of object B |
---|
126 | */ |
---|
127 | void OBBTree::collideWith(BVTree* tree, WorldEntity* nodeA, WorldEntity* nodeB) |
---|
128 | { |
---|
129 | this->rootNode->collideWith(((OBBTree*)tree)->getRootNode(), nodeA, nodeB); |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | void OBBTree::drawBV(int depth, int drawMode) const |
---|
134 | { |
---|
135 | if( likely(this->rootNode != NULL)) |
---|
136 | { |
---|
137 | this->rootNode->drawBV(depth, drawMode); |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | |
---|
143 | void OBBTree::debug() |
---|
144 | { |
---|
145 | PRINT(0)("\n==============================| OBBTree::debug() |===\n"); |
---|
146 | PRINT(0)("= Spawning Tree: Start\n"); |
---|
147 | |
---|
148 | /* generate some test vertices */ |
---|
149 | int const length = 9; |
---|
150 | sVec3D* vertList = new sVec3D[length]; |
---|
151 | // sVec3D data[length] = {{0.0, 0.0, 0.0},{1.0, 2.0, 5.0},{0.0, 6.0, 9.0}, |
---|
152 | // {1.0, 4.0, 12.0}, {1.0, 2.0, 16.0}, {0.0, 0.0, 19.0}, |
---|
153 | // {0.0, 3.0, 23.0}, {1.0, 5.0, 30.0}, {0.0, 10.0, 35.0}}; |
---|
154 | |
---|
155 | sVec3D data[length] = {{0.0, 0.0, 0.0},{1.0, 2.0, 5.0},{1.0, 5.0, 30.0}, |
---|
156 | {0.0, 3.0, 23.0}, {0.0, 6.0, 9.0}, {0.0, 10.0, 35.0}, |
---|
157 | {1.0, 4.0, 12.0}, {1.0, 2.0, 16.0}, {0.0, 0.0, 19.0}}; |
---|
158 | |
---|
159 | for(int i = 0; i < length; ++i) |
---|
160 | { |
---|
161 | vertList[i][0] = data[i][0]; |
---|
162 | vertList[i][1] = data[i][1]; |
---|
163 | vertList[i][2] = data[i][2]; |
---|
164 | } |
---|
165 | |
---|
166 | this->spawnBVTree(3, vertList, length); |
---|
167 | |
---|
168 | PRINT(0)("= Spawning Tree: Finished\n"); |
---|
169 | PRINT(0)("=======================================================\n"); |
---|
170 | |
---|
171 | } |
---|