1 |
root |
1.1 |
#include <cstdlib> |
2 |
|
|
|
3 |
root |
1.8 |
#include <vector> |
4 |
|
|
|
5 |
|
|
using namespace std; |
6 |
|
|
|
7 |
|
|
#define GL_GLEXT_PROTOTYPES |
8 |
|
|
#include <GL/gl.h> |
9 |
root |
1.24 |
#include <GL/glext.h> |
10 |
root |
1.8 |
|
11 |
root |
1.1 |
#include "oct.h" |
12 |
root |
1.8 |
#include "view.h" |
13 |
root |
1.2 |
#include "entity.h" |
14 |
|
|
|
15 |
root |
1.8 |
vector<GLuint> occ_query_objects; |
16 |
root |
1.2 |
|
17 |
root |
1.8 |
static GLuint begin_occ_query () |
18 |
root |
1.2 |
{ |
19 |
root |
1.8 |
GLuint id; |
20 |
root |
1.2 |
|
21 |
root |
1.8 |
if (occ_query_objects.size ()) |
22 |
root |
1.3 |
{ |
23 |
root |
1.8 |
id = *(occ_query_objects.end () - 1); |
24 |
|
|
occ_query_objects.pop_back (); |
25 |
root |
1.3 |
} |
26 |
|
|
else |
27 |
root |
1.20 |
glGenQueriesARB (1, &id); |
28 |
root |
1.8 |
|
29 |
root |
1.20 |
glBeginQueryARB (GL_SAMPLES_PASSED, id); |
30 |
root |
1.8 |
return id; |
31 |
|
|
} |
32 |
|
|
|
33 |
root |
1.23 |
#define end_occ_query() glEndQueryARB (GL_SAMPLES_PASSED) |
34 |
root |
1.8 |
|
35 |
root |
1.23 |
GLuint occ_query_result (GLuint id) |
36 |
root |
1.8 |
{ |
37 |
|
|
GLuint count; |
38 |
|
|
|
39 |
root |
1.20 |
glGetQueryObjectuivARB (id, GL_QUERY_RESULT, &count); |
40 |
root |
1.8 |
occ_query_objects.push_back (id); |
41 |
|
|
|
42 |
|
|
return count; |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
octant world(0, sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN), MAXEXTENT); |
46 |
|
|
|
47 |
|
|
octant::octant (octant *parent, const sector &orig, uoffs extent) |
48 |
|
|
: parent(parent), orig(orig), extent(extent) |
49 |
|
|
{ |
50 |
|
|
for (fill = 8; fill--; ) |
51 |
|
|
sub[fill] = 0; |
52 |
root |
1.2 |
} |
53 |
|
|
|
54 |
|
|
octant::~octant () |
55 |
|
|
{ |
56 |
|
|
for (fill = 8; fill--; ) |
57 |
|
|
delete sub[fill]; |
58 |
|
|
} |
59 |
|
|
|
60 |
root |
1.8 |
static bool overlap (const sector &o1, uoffs ea, const box &bbox) |
61 |
root |
1.4 |
{ |
62 |
root |
1.8 |
sector a2, b2; |
63 |
root |
1.4 |
|
64 |
|
|
ea /= 2; |
65 |
|
|
|
66 |
|
|
a2.x = o1.x + ea; |
67 |
|
|
a2.y = o1.y + ea; |
68 |
|
|
a2.z = o1.z + ea; |
69 |
|
|
|
70 |
root |
1.8 |
b2.x = (bbox.a.x + bbox.b.x) / 2; |
71 |
|
|
b2.y = (bbox.a.y + bbox.b.y) / 2; |
72 |
|
|
b2.z = (bbox.a.z + bbox.b.z) / 2; |
73 |
|
|
|
74 |
|
|
return abs (a2.x - b2.x) <= ea + (bbox.b.x - bbox.a.x) |
75 |
|
|
&& abs (a2.y - b2.y) <= ea + (bbox.b.y - bbox.a.y) |
76 |
|
|
&& abs (a2.z - b2.z) <= ea + (bbox.b.z - bbox.a.z); |
77 |
root |
1.4 |
} |
78 |
|
|
|
79 |
root |
1.6 |
void octant::add (entity_base *e) |
80 |
root |
1.2 |
{ |
81 |
root |
1.8 |
box bbox = translate (e->bbox, e->orig, sector (0, 0, 0)); |
82 |
root |
1.7 |
|
83 |
|
|
uoffs size = max (abs (bbox.b.x - bbox.a.x), |
84 |
root |
1.8 |
max (abs (bbox.b.y - bbox.a.y), |
85 |
|
|
abs (bbox.b.z - bbox.a.z))); |
86 |
root |
1.3 |
|
87 |
root |
1.8 |
if (overlap (orig, extent, bbox)) |
88 |
root |
1.4 |
{ |
89 |
root |
1.8 |
uoffs extent2 = extent / 2; |
90 |
|
|
|
91 |
|
|
if (size > extent2 || !extent2) |
92 |
|
|
{ |
93 |
|
|
push_back (e); |
94 |
|
|
e->o.push_back (this); |
95 |
|
|
return; |
96 |
|
|
} |
97 |
root |
1.5 |
|
98 |
root |
1.8 |
for (int i = 8; i--; ) |
99 |
root |
1.5 |
{ |
100 |
root |
1.8 |
sector s = orig; |
101 |
|
|
s.offset (i, extent2); |
102 |
|
|
if (overlap (s, extent2, bbox)) |
103 |
root |
1.5 |
{ |
104 |
root |
1.8 |
if (!sub[i]) |
105 |
|
|
sub[i] = new octant (this, s, extent2); |
106 |
|
|
|
107 |
|
|
sub[i]->add (e); |
108 |
root |
1.5 |
} |
109 |
root |
1.8 |
} |
110 |
root |
1.4 |
} |
111 |
root |
1.2 |
} |
112 |
root |
1.1 |
|
113 |
|
|
void octant::remove (entity_base *e) |
114 |
|
|
{ |
115 |
|
|
} |
116 |
|
|
|
117 |
root |
1.19 |
void octant::detect_visibility (view &ctx) |
118 |
root |
1.1 |
{ |
119 |
root |
1.8 |
visibility_state &vs = ctx.vismap[this]; |
120 |
|
|
|
121 |
|
|
if (vs.generation != ctx.generation) |
122 |
root |
1.10 |
vs.visibility = visibility_state::UNKNOWN; |
123 |
|
|
|
124 |
root |
1.19 |
const sector &cam = ctx.orig; |
125 |
root |
1.10 |
if (cam.x >= orig.x && cam.x <= orig.x + extent |
126 |
|
|
&& cam.y >= orig.y && cam.y <= orig.y + extent |
127 |
|
|
&& cam.z >= orig.z && cam.z <= orig.z + extent) |
128 |
|
|
{ |
129 |
|
|
vs.visibility = visibility_state::PARTIAL; |
130 |
|
|
vs.generation = ctx.generation; |
131 |
|
|
} |
132 |
|
|
else |
133 |
|
|
{ |
134 |
|
|
point center ( |
135 |
root |
1.18 |
orig.x + (soffs)extent / 2 - cam.x, |
136 |
|
|
orig.y + (soffs)extent / 2 - cam.y, |
137 |
root |
1.14 |
orig.z + (soffs)extent / 2 - cam.z |
138 |
root |
1.10 |
); |
139 |
|
|
|
140 |
root |
1.14 |
GLfloat dia = (0.5 * sqrtf (3))*(GLfloat)extent; |
141 |
|
|
|
142 |
|
|
if (ctx.frustum.t.distance (center) < -dia) return; |
143 |
|
|
if (ctx.frustum.b.distance (center) < -dia) return; |
144 |
|
|
if (ctx.frustum.l.distance (center) < -dia) return; |
145 |
|
|
if (ctx.frustum.r.distance (center) < -dia) return; |
146 |
|
|
if (ctx.frustum.n.distance (center) < -dia) return; |
147 |
root |
1.25 |
|
148 |
|
|
GLfloat fd = ctx.frustum.f.distance (center); |
149 |
|
|
|
150 |
|
|
if (fd < -dia) |
151 |
|
|
{ |
152 |
root |
1.29 |
if (fd < -dia * 2) |
153 |
root |
1.26 |
return; |
154 |
|
|
|
155 |
|
|
// cull, but do check for potential visibility |
156 |
|
|
if (size ()) |
157 |
|
|
ctx.farlist.push_back (this); |
158 |
root |
1.25 |
} |
159 |
root |
1.10 |
} |
160 |
root |
1.8 |
|
161 |
root |
1.19 |
if (size ()) |
162 |
|
|
ctx.vislist.push_back (this); |
163 |
|
|
|
164 |
|
|
// node to start with |
165 |
|
|
unsigned char si = ctx.d.x < 0 ? 1 : 0 |
166 |
|
|
| ctx.d.y < 0 ? 2 : 0 |
167 |
|
|
| ctx.d.z < 0 ? 4 : 0; |
168 |
|
|
|
169 |
|
|
// bit-toggle to find next child for front-to-back order |
170 |
|
|
static unsigned char next[8] |
171 |
|
|
= { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7 }; |
172 |
|
|
|
173 |
|
|
for (int i = 0; i < 8; i++) |
174 |
|
|
{ |
175 |
|
|
si ^= next[i]; |
176 |
|
|
|
177 |
|
|
if (sub[si]) |
178 |
|
|
sub[si]->detect_visibility (ctx); |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
vs.generation = ctx.generation; |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
void octant::display (view &ctx) |
185 |
|
|
{ |
186 |
root |
1.17 |
#if 0 |
187 |
root |
1.8 |
glBegin (GL_LINES); |
188 |
root |
1.25 |
sector s = orig - ctx.orig; |
189 |
root |
1.13 |
vec3 clr(0, 0.8, 0); |
190 |
|
|
glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, (const GLfloat*)&clr); |
191 |
root |
1.8 |
|
192 |
|
|
for (int i = 8; i--; ) |
193 |
|
|
for (int ji = 3; ji--; ) |
194 |
|
|
{ |
195 |
|
|
int j = i | (1 << ji); |
196 |
|
|
if (i != j) |
197 |
|
|
{ |
198 |
|
|
glVertex3i (s.x + !!(i & 1) * extent, |
199 |
|
|
s.y + !!(i & 2) * extent, |
200 |
|
|
s.z + !!(i & 4) * extent); |
201 |
|
|
glVertex3i (s.x + !!(j & 1) * extent, |
202 |
|
|
s.y + !!(j & 2) * extent, |
203 |
|
|
s.z + !!(j & 4) * extent); |
204 |
|
|
} |
205 |
|
|
} |
206 |
|
|
|
207 |
|
|
glEnd (); |
208 |
root |
1.13 |
glDisable(GL_COLOR_MATERIAL); |
209 |
root |
1.8 |
#endif |
210 |
|
|
|
211 |
root |
1.19 |
for (iterator i = end (); i != begin (); ) |
212 |
|
|
(*--i)->display (ctx); |
213 |
root |
1.1 |
} |
214 |
root |
1.8 |
|
215 |
root |
1.25 |
void |
216 |
|
|
octant::draw_bbox (view &ctx) |
217 |
|
|
{ |
218 |
|
|
sector s = orig - ctx.orig; |
219 |
root |
1.27 |
int i; |
220 |
|
|
GLint verts[4 * 6] = { |
221 |
root |
1.31 |
0x00, 0x40, 0x60, 0x20, // -x |
222 |
|
|
0x10, 0x30, 0x70, 0x50, // +x |
223 |
|
|
0x00, 0x10, 0x50, 0x40, // -y |
224 |
|
|
0x70, 0x30, 0x20, 0x60, // +y |
225 |
|
|
0x00, 0x20, 0x30, 0x10, // -z |
226 |
|
|
0x40, 0x50, 0x70, 0x60, // +z |
227 |
root |
1.27 |
}; |
228 |
|
|
|
229 |
root |
1.25 |
GLfloat cube[8][3] = |
230 |
|
|
{ |
231 |
root |
1.31 |
{ s.x , s.y , s.z }, |
232 |
|
|
{ s.x + (soffs)extent, s.y , s.z }, |
233 |
|
|
{ s.x , s.y + (soffs)extent, s.z }, |
234 |
|
|
{ s.x + (soffs)extent, s.y + (soffs)extent, s.z }, |
235 |
|
|
{ s.x , s.y , s.z + (soffs)extent }, |
236 |
|
|
{ s.x + (soffs)extent, s.y , s.z + (soffs)extent }, |
237 |
|
|
{ s.x , s.y + (soffs)extent, s.z + (soffs)extent }, |
238 |
|
|
{ s.x + (soffs)extent, s.y + (soffs)extent, s.z + (soffs)extent }, |
239 |
root |
1.25 |
}; |
240 |
|
|
|
241 |
|
|
glBegin (GL_QUADS); |
242 |
root |
1.31 |
|
243 |
|
|
for (i = 0; i < 4 * 6; i++) |
244 |
|
|
glVertex3fv (cube [verts [i] >> 4]); |
245 |
|
|
|
246 |
root |
1.25 |
glEnd (); |
247 |
|
|
} |
248 |
root |
1.8 |
|
249 |
root |
1.2 |
|