-
Notifications
You must be signed in to change notification settings - Fork 2
/
PrimitiveOctree.cpp
358 lines (324 loc) · 10.2 KB
/
PrimitiveOctree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// @Begin License@
// This file is part of Coldest.
//
// Coldest is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Coldest is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Coldest. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2008-2012 Ben Nemec
// @End License@
#include "PrimitiveOctree.h"
PrimitiveOctree::PrimitiveOctree(vector<GenericPrimitive> p)
{
prims = p;
haschildren = false;
vector<GenericPrimitive>::iterator i;
for (i = p.begin(); i != p.end(); ++i)
{
if (!i->collide)
p.erase(i);
}
}
/*
First remove any primitives that are not actually within the bounding
box of this octree node. Then try to further subdivide self by
inserting all of our primitives into the children and recursively
calling refine on them.
*/
void PrimitiveOctree::refine()
{
refine(0);
}
/* refine() now just calls refine(int) so that we can keep track of how many
levels the tree has. It is possible with this algorithm to end up in
infinite recursion so we cut it off at a semi-arbitrary level*/
void PrimitiveOctree::refine(int level)
{
vector<GenericPrimitive>::iterator it = prims.begin();
bool remove[prims.size()];
for (int i = 0; i < prims.size(); ++i)
remove[i] = false;
bool allempty = true;
for (int i = 0; i < 8; ++i)
{
//logout << "Child: " << i << endl;
if (!haschildren)
{
vector<GenericPrimitive> temp;
child[i] = new PrimitiveOctree(temp);
}
else
{
child[i]->clear();
}
Vector3 v[8];
float halfx = vertices[0].x + vertices[7].x;
halfx /= 2;
float halfy = vertices[0].y + vertices[7].y;
halfy /= 2;
float halfz = vertices[0].z + vertices[7].z;
halfz /= 2;
switch (i) // No doubt a more elegant way to do this
{
case 0:
v[0].x = vertices[0].x;
v[0].y = vertices[0].y;
v[0].z = vertices[0].z;
v[7].x = halfx;
v[7].y = halfy;
v[7].z = halfz;
break;
case 1:
v[0].x = halfx;
v[0].y = vertices[0].y;
v[0].z = vertices[0].z;
v[7].x = vertices[7].x;
v[7].y = halfy;
v[7].z = halfz;
break;
case 2:
v[0].x = vertices[0].x;
v[0].y = halfy;
v[0].z = vertices[0].z;
v[7].x = halfx;
v[7].y = vertices[7].y;
v[7].z = halfz;
break;
case 3:
v[0].x = halfx;
v[0].y = halfy;
v[0].z = vertices[0].z;
v[7].x = vertices[7].x;
v[7].y = vertices[7].y;
v[7].z = halfz;
break;
case 4:
v[0].x = vertices[0].x;
v[0].y = vertices[0].y;
v[0].z = halfz;
v[7].x = halfx;
v[7].y = halfy;
v[7].z = vertices[7].z;
break;
case 5:
v[0].x = halfx;
v[0].y = vertices[0].y;
v[0].z = halfz;
v[7].x = vertices[7].x;
v[7].y = halfy;
v[7].z = vertices[7].z;
break;
case 6:
v[0].x = vertices[0].x;
v[0].y = halfy;
v[0].z = halfz;
v[7].x = halfx;
v[7].y = vertices[7].y;
v[7].z = vertices[7].z;
break;
case 7:
v[0].x = halfx;
v[0].y = halfy;
v[0].z = halfz;
v[7].x = vertices[7].x;
v[7].y = vertices[7].y;
v[7].z = vertices[7].z;
break;
}
child[i]->setvertices(v);
int j = 0;
while (it != prims.end())
{
if (child[i]->innode(*it)) // Add to child and schedule for removal
{
child[i]->add(*it);
//it = prims.erase(it); // Returns iterator to next
remove[j] = true;
}
++it;
++j;
}
it = prims.begin(); // Reset in case we loop again
if (!child[i]->empty())
allempty = false;
//logout << size() << endl;
if (child[i]->size() > 40 && level < 10)
{
child[i]->refine(level + 1);
//logout << "Returned\n";
}
}
it = prims.begin();
int i = 0;
while (it != prims.end())
{
if (remove[i]) // Remove it
it = prims.erase(it); // Returns iterator to next
else // Go to the next manually
++it;
++i;
}
if (allempty)
for (int i = 0; i < 8; ++i)
delete child[i];
haschildren = !allempty;
}
void PrimitiveOctree::add(GenericPrimitive p)
{
prims.push_back(p);
}
void PrimitiveOctree::addall(vector<GenericPrimitive> p)
{
prims = p;
}
/* Helper function that just returns whether the primitive p is
contained within the bounding box of node
Want to use vertices 0 and 7
Because tristrips that are being used to collision detect cylinders
need to be considered part of any node within the cylinder's radius,
we set the rad and rad1 properties of them to the radius of the
cylinder and use those values here. v[0] is the cylinder's position.*/
bool PrimitiveOctree::innode(GenericPrimitive &p)
{
bool contained = false;
for (int i = 0; i < 4; ++i)
{
if (p.rad < .001)
{
if (p.v[i].x >= vertices[0].x && p.v[i].x <= vertices[7].x &&
p.v[i].y <= vertices[0].y && p.v[i].y >= vertices[7].y &&
p.v[i].z >= vertices[0].z && p.v[i].z <= vertices[7].z)
contained = true;
}
else
{
float r;
if (p.rad > p.rad1)
r = p.rad;
else r = p.rad1;
if (p.v[i].x >= vertices[0].x - r && p.v[i].x <= vertices[7].x + r &&
p.v[i].y <= vertices[0].y + r && p.v[i].y >= vertices[7].y - r &&
p.v[i].z >= vertices[0].z - r && p.v[i].z <= vertices[7].z + r)
contained = true;
}
}
return contained;
}
// Top then bottom, starting with 0,0 (or closest to it) and following
// the right-hand rule as the worldbounds vectors do
void PrimitiveOctree::setvertices(Vector3 v[8])
{
for (int i = 0; i < 8; ++i)
{
vertices[i].x = v[i].x;
vertices[i].y = v[i].y;
vertices[i].z = v[i].z;
}
}
bool PrimitiveOctree::empty()
{
return prims.empty();
}
void PrimitiveOctree::clear()
{
prims.clear();
}
int PrimitiveOctree::size()
{
return prims.size();
}
/* Return a vector containing all of the primitives that it is
possible for the sphere at pos of radius size to intercept */
vector<GenericPrimitive> PrimitiveOctree::getprims(Vector3 pos, float size)
{
vector<GenericPrimitive> ret = prims;
if (haschildren) // If no children we'll get a null pointer error
{
bool foundchild[8];
for (int i = 0; i < 8; ++i)
foundchild[i] = false;
GenericPrimitive temp;
for (int j = 0; j < 4; ++j) // Generate dummy primitive
temp.v[j] = pos;
temp.rad = temp.rad1 = size;
for (int j = 0; j < 8; ++j) // Check eight children
{
if (child[j]->innode(temp))
{
if (!foundchild[j]) // Don't return a child's prims > once
{
vector<GenericPrimitive> tempret = child[j]->getprims(pos, size);
for (int k = 0; k < tempret.size(); ++k)
ret.push_back(tempret[k]);
}
foundchild[j] = true;
}
}
}
return ret;
}
// Render translucent boxes representing the tree for debugging purposes
void PrimitiveOctree::visualize()
{
float alpha = .05;
glDisable(GL_TEXTURE_2D);
glDisable(GL_FOG);
glColor4f(0, 0, 1, alpha);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(vertices[0].x - 1, vertices[0].y - 1, vertices[0].z - 1);
glVertex3f(vertices[7].x - 1, vertices[0].y - 1, vertices[0].z - 1);
glVertex3f(vertices[0].x, vertices[7].y, vertices[0].z);
glVertex3f(vertices[7].x, vertices[7].y, vertices[0].z);
glEnd();
glColor4f(1, 0, 1, alpha);
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(vertices[0].x - 1, vertices[0].y - 1, vertices[0].z - 1);
glVertex3f(vertices[7].x - 1, vertices[0].y - 1, vertices[0].z - 1);
glVertex3f(vertices[0].x, vertices[0].y, vertices[7].z);
glVertex3f(vertices[7].x, vertices[0].y, vertices[7].z);
glEnd();
glColor4f(1, 1, 1, alpha);
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(vertices[0].x, vertices[0].y, vertices[7].z);
glVertex3f(vertices[7].x, vertices[0].y, vertices[7].z);
glVertex3f(vertices[0].x, vertices[7].y, vertices[7].z);
glVertex3f(vertices[7].x, vertices[7].y, vertices[7].z);
glEnd();
glColor4f(1, 0, 0, alpha);
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(vertices[0].x - 1, vertices[0].y - 1, vertices[0].z - 1);
glVertex3f(vertices[0].x - 1, vertices[0].y - 1, vertices[7].z - 1);
glVertex3f(vertices[0].x, vertices[7].y, vertices[0].z);
glVertex3f(vertices[0].x, vertices[7].y, vertices[7].z);
glEnd();
glColor4f(1, 1, 0, alpha);
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(vertices[7].x, vertices[0].y, vertices[0].z);
glVertex3f(vertices[7].x, vertices[0].y, vertices[7].z);
glVertex3f(vertices[7].x, vertices[7].y, vertices[0].z);
glVertex3f(vertices[7].x, vertices[7].y, vertices[7].z);
glEnd();
glColor4f(0, 0, 0, alpha);
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(vertices[0].x, vertices[7].y, vertices[0].z);
glVertex3f(vertices[7].x, vertices[7].y, vertices[0].z);
glVertex3f(vertices[0].x, vertices[7].y, vertices[7].z);
glVertex3f(vertices[7].x, vertices[7].y, vertices[7].z);
glEnd();
glColor4f(0, 0, 0, 1);
glEnable(GL_TEXTURE_2D);
glEnable(GL_FOG);
if (!haschildren) return;
for (int i = 0; i < 8; ++i)
child[i]->visualize();
}