1 | #include <cstdlib> |
1 | #include <cstdlib> |
2 | |
2 | |
|
|
3 | #include <vector> |
|
|
4 | |
|
|
5 | using namespace std; |
|
|
6 | |
|
|
7 | #include "opengl.h" |
|
|
8 | |
3 | #include "oct.h" |
9 | #include "oct.h" |
|
|
10 | #include "view.h" |
4 | #include "entity.h" |
11 | #include "entity.h" |
|
|
12 | #include "material.h" //DEBUG |
5 | |
13 | |
6 | octant world(0, 0); |
14 | enum visibility_state { |
|
|
15 | PARTIAL, // surrounding the viewpoint |
|
|
16 | FULL, // in frustum |
|
|
17 | OCCLUDED, // in frustom, but occluded |
|
|
18 | }; |
7 | |
19 | |
8 | octant::octant (octant *parent, int subindex) |
20 | struct evis { |
|
|
21 | entity *e; |
|
|
22 | visibility_state state; |
|
|
23 | double next; // time of next check |
|
|
24 | |
|
|
25 | void clear () |
|
|
26 | { |
|
|
27 | next = 0.; |
|
|
28 | state = FULL; |
|
|
29 | } |
|
|
30 | |
|
|
31 | evis () |
|
|
32 | { |
|
|
33 | clear (); |
|
|
34 | } |
|
|
35 | }; |
|
|
36 | |
|
|
37 | struct oct_visibility : visibility_base |
|
|
38 | { |
|
|
39 | vector<evis> vismap; |
|
|
40 | |
|
|
41 | visibility_state state; |
|
|
42 | bool vis; |
|
|
43 | |
|
|
44 | evis &get_visibility (int i, entity *e) |
|
|
45 | { |
|
|
46 | evis &evs = vismap [i]; |
|
|
47 | |
|
|
48 | if (evs.e != e) |
|
|
49 | { |
|
|
50 | evs.clear (); |
|
|
51 | evs.e = e; |
|
|
52 | } |
|
|
53 | |
|
|
54 | return evs; |
|
|
55 | } |
|
|
56 | |
|
|
57 | oct_visibility (octant &oct) |
|
|
58 | : state (FULL), vis (true) |
|
|
59 | { |
|
|
60 | } |
|
|
61 | }; |
|
|
62 | |
|
|
63 | octant world(0, sector (0, 0, 0), MAXEXTENT); |
|
|
64 | |
|
|
65 | octant::octant (octant *parent, const sector &orig, uoffs extent) |
9 | : parent(parent) |
66 | : parent(parent) |
|
|
67 | , orig(orig) |
|
|
68 | , extent(extent) |
10 | { |
69 | { |
11 | for (fill = 8; fill--; ) |
70 | for (fill = 8; fill--; ) |
12 | sub[fill] = 0; |
71 | sub[fill] = 0; |
|
|
72 | } |
13 | |
73 | |
14 | if (parent) |
74 | visibility_base *octant::new_visibility () |
15 | { |
75 | { |
16 | extent = (parent->extent + 1) >> 1; |
76 | return new oct_visibility (*this); |
17 | orig = parent->orig; |
77 | } |
18 | orig.offset (subindex, extent); |
78 | |
19 | } |
79 | void octant::clear_visibility (visibility_base *vs) |
20 | else |
80 | { |
21 | { |
81 | ((oct_visibility *)vs)->vismap.clear (); |
22 | extent = MAXEXTENT; |
82 | ((oct_visibility *)vs)->state = FULL; |
23 | orig.x = orig.y = orig.z = SOFFS_MIN; |
|
|
24 | } |
|
|
25 | } |
83 | } |
26 | |
84 | |
27 | octant::~octant () |
85 | octant::~octant () |
28 | { |
86 | { |
29 | for (fill = 8; fill--; ) |
87 | for (fill = 8; fill--; ) |
30 | delete sub[fill]; |
88 | delete sub[fill]; |
31 | } |
89 | } |
32 | |
90 | |
33 | static bool overlap (const sector &o1, uoffs ea, const sector &o2, const box &bbox) |
91 | static bool overlap (const sector &orig, uoffs extent, const sector &a, const sector &b) |
34 | { |
92 | { |
35 | sector a2; |
93 | sector size = (b - a + 1) >> 1; |
|
|
94 | sector center = a + size; |
36 | |
95 | |
37 | ea /= 2; |
96 | return abs (orig - center) <= extent + size; |
|
|
97 | } |
38 | |
98 | |
39 | a2.x = o1.x + ea; |
99 | static sector offset (const sector &s, int subindex, uoffs extent) |
40 | a2.y = o1.y + ea; |
100 | { |
41 | a2.z = o1.z + ea; |
101 | return sector ( |
|
|
102 | s.x + (subindex & 1 ? extent : -extent), |
|
|
103 | s.y + (subindex & 2 ? extent : -extent), |
|
|
104 | s.z + (subindex & 4 ? extent : -extent) |
|
|
105 | ); |
|
|
106 | } |
|
|
107 | |
|
|
108 | void octant::add (entity *e) |
|
|
109 | { |
|
|
110 | const sector &a = e->a; |
|
|
111 | const sector &b = e->b; |
|
|
112 | |
|
|
113 | uoffs size = max (abs (b - a)); |
|
|
114 | |
|
|
115 | if (size >= extent >> 4) |
|
|
116 | { |
|
|
117 | if (overlap (orig, extent, a, b)) |
|
|
118 | { |
|
|
119 | push_back (e); |
|
|
120 | e->o.push_back (this); |
|
|
121 | } |
|
|
122 | } |
|
|
123 | else |
|
|
124 | { |
|
|
125 | uoffs extent2 = extent >> 1; |
|
|
126 | |
|
|
127 | for (int i = 8; i--; ) |
|
|
128 | { |
|
|
129 | sector s = offset (orig, i, extent2); |
|
|
130 | |
|
|
131 | if (overlap (s, extent2, a, b)) |
|
|
132 | { |
|
|
133 | if (!sub[i]) |
|
|
134 | { |
|
|
135 | sub[i] = new octant (this, s, extent2); |
|
|
136 | fill++; |
|
|
137 | } |
|
|
138 | |
|
|
139 | sub[i]->add (e); |
|
|
140 | } |
|
|
141 | } |
|
|
142 | } |
|
|
143 | } |
|
|
144 | |
|
|
145 | void octant::remove (entity *e) |
|
|
146 | { |
|
|
147 | } |
|
|
148 | |
|
|
149 | bool octant::detect_visibility (view &ctx) //, bool fully_visible) |
|
|
150 | { |
|
|
151 | sector centeri = orig - ctx.orig; |
|
|
152 | point centerf = point (centeri); |
|
|
153 | |
|
|
154 | GLfloat rad = ctx.diagfact * extent; |
|
|
155 | |
|
|
156 | if (!overlap (ctx.frustum.c, sphere (centerf, rad))) |
|
|
157 | return false; |
|
|
158 | |
|
|
159 | oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
|
|
160 | |
|
|
161 | if (max (centeri) < extent) |
|
|
162 | vs.state = PARTIAL; |
|
|
163 | else |
|
|
164 | { |
|
|
165 | if (distance (ctx.frustum.t, centerf) < -rad) return false; |
|
|
166 | if (distance (ctx.frustum.b, centerf) < -rad) return false; |
|
|
167 | if (distance (ctx.frustum.l, centerf) < -rad) return false; |
|
|
168 | if (distance (ctx.frustum.r, centerf) < -rad) return false; |
|
|
169 | if (distance (ctx.frustum.n, centerf) < -rad) return false; |
|
|
170 | } |
|
|
171 | |
|
|
172 | #if 0 |
|
|
173 | GLfloat fd = distance (ctx.frustum.f, centerf); |
|
|
174 | |
|
|
175 | if (fd < -(ctx.c_far - ctx.z_far) -rad * 3.F) |
|
|
176 | return false; |
|
|
177 | #endif |
|
|
178 | |
|
|
179 | // very important, optimize? |
|
|
180 | GLfloat z = length (ctx.p - centerf) + rad; |
|
|
181 | if (z < 0.F || ctx.perspfact * extent / z < 10.) // very crude "too small to see" check |
|
|
182 | return false; |
|
|
183 | |
|
|
184 | #if 0 |
|
|
185 | if (vs.state == PARTIAL || vs.state == FULL) |
|
|
186 | ctx.nc_far = max (ctx.nc_far, z); |
|
|
187 | #endif |
|
|
188 | |
|
|
189 | if (vs.state == OCCLUDED) |
|
|
190 | { |
|
|
191 | ctx.postdepthlist.push_back (this); |
|
|
192 | |
|
|
193 | return false; |
|
|
194 | } |
|
|
195 | |
|
|
196 | //bool visible = size () && vs.state != OCCLUDED; |
|
|
197 | bool visible = false; |
|
|
198 | |
|
|
199 | // node to start with |
|
|
200 | unsigned char si = centeri.x > 0 ? 1 : 0 |
|
|
201 | | centeri.y > 0 ? 2 : 0 |
|
|
202 | | centeri.z > 0 ? 4 : 0; |
|
|
203 | |
|
|
204 | // bit-toggle to find next child for front-to-back order |
|
|
205 | static unsigned char toggle[8+1] |
|
|
206 | = { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7, 0 }; |
|
|
207 | |
|
|
208 | unsigned char *next = toggle; |
|
|
209 | do |
|
|
210 | { |
|
|
211 | si ^= *next; |
|
|
212 | |
|
|
213 | if (sub[si]) |
|
|
214 | visible = visible | sub[si]->detect_visibility (ctx); |
|
|
215 | } |
|
|
216 | while (*++next); |
|
|
217 | |
|
|
218 | if (visible) |
|
|
219 | { |
|
|
220 | if (size ()) |
|
|
221 | { |
|
|
222 | ctx.postdepthlist.push_back (this); |
|
|
223 | if (vs.vis) |
|
|
224 | ctx.vislist.push_back (this); |
|
|
225 | } |
|
|
226 | |
|
|
227 | vs.state = FULL; |
|
|
228 | |
|
|
229 | return true; |
|
|
230 | } |
|
|
231 | else if (vs.state == OCCLUDED) |
|
|
232 | { |
|
|
233 | ctx.postdepthlist.push_back (this); |
|
|
234 | |
|
|
235 | return false; |
|
|
236 | } |
|
|
237 | else if (size()) |
|
|
238 | { |
|
|
239 | if (vs.vis) |
|
|
240 | { |
|
|
241 | ctx.postdepthlist.push_back (this); |
|
|
242 | ctx.vislist.push_back (this); |
|
|
243 | } |
|
|
244 | else |
|
|
245 | vs.state = OCCLUDED; |
|
|
246 | |
|
|
247 | return true; |
|
|
248 | } |
|
|
249 | else |
|
|
250 | { |
|
|
251 | return vs.state != PARTIAL; |
|
|
252 | } |
|
|
253 | } |
|
|
254 | |
|
|
255 | void octant::draw_depth (view &ctx) |
|
|
256 | { |
|
|
257 | oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
|
|
258 | |
|
|
259 | vs.vismap.resize (size ()); |
|
|
260 | |
|
|
261 | if (vs.state == OCCLUDED) |
|
|
262 | return; |
|
|
263 | |
|
|
264 | for (int i = 0; i < size (); ++i) |
|
|
265 | { |
|
|
266 | entity *e = (*this)[i]; |
|
|
267 | const evis &evs = vs.get_visibility (i, e); |
|
|
268 | |
|
|
269 | if (evs.state != OCCLUDED) |
|
|
270 | { |
|
|
271 | if (!ctx.may_draw (e)) |
|
|
272 | continue; |
|
|
273 | |
|
|
274 | sector center = ((e->a + e->b) >> 1) - ctx.orig; |
|
|
275 | GLfloat z = length (vec3 (center)); |
|
|
276 | ctx.pixfact = ctx.perspfact / z; |
|
|
277 | |
|
|
278 | ctx.nz_far = max (ctx.nz_far, z + extent); |
|
|
279 | ctx.nz_near = min (ctx.nz_near, z - extent); |
|
|
280 | |
|
|
281 | e->draw (ctx); |
|
|
282 | } |
|
|
283 | } |
|
|
284 | } |
|
|
285 | |
|
|
286 | void octant::draw_postdepth (view &ctx) |
|
|
287 | { |
|
|
288 | oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
|
|
289 | |
|
|
290 | if (vs.state == OCCLUDED || !vs.vis) |
|
|
291 | { |
|
|
292 | ctx.begin_occ_query (*this, 0); |
|
|
293 | sector s = orig - ctx.orig; |
|
|
294 | gl::draw_bbox (s - extent, s + extent); |
|
|
295 | ctx.end_occ_query (); |
|
|
296 | } |
|
|
297 | else |
|
|
298 | { |
|
|
299 | vs.vis = false; |
|
|
300 | |
|
|
301 | for (int i = 0; i < size (); ++i) |
|
|
302 | { |
|
|
303 | entity *e = (*this)[i]; |
|
|
304 | const evis &evs = vs.get_visibility (i, e); |
|
|
305 | |
|
|
306 | if (evs.state == OCCLUDED) |
|
|
307 | { |
|
|
308 | if (!ctx.may_draw (e)) |
|
|
309 | continue; |
|
|
310 | |
|
|
311 | ctx.begin_occ_query (*this, e); |
|
|
312 | gl::draw_bbox (e->a - ctx.orig, e->b - ctx.orig); |
|
|
313 | ctx.end_occ_query (); |
|
|
314 | } |
|
|
315 | else |
|
|
316 | vs.vis = true; |
|
|
317 | } |
|
|
318 | } |
|
|
319 | } |
|
|
320 | |
|
|
321 | void octant::draw_lighted (view &ctx) |
|
|
322 | { |
|
|
323 | oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
|
|
324 | |
|
|
325 | #if 0 |
|
|
326 | if (vs.state == FULL || vs.state == OCCLUDED) |
|
|
327 | { |
|
|
328 | sector s = orig - ctx.orig; |
|
|
329 | debugmat->enable (ctx); |
|
|
330 | if (max (s) >= extent) |
|
|
331 | gl::draw_bbox (s - extent, s + extent); |
|
|
332 | debugmat->disable (ctx); |
|
|
333 | //printf ("DLP %ld %ld %ld (%ld)\n", orig.x, orig.y, orig.z, extent);//D |
|
|
334 | } |
|
|
335 | #endif |
|
|
336 | |
|
|
337 | if (vs.state == OCCLUDED) |
|
|
338 | return; |
42 | |
339 | |
43 | sector b2; |
340 | for (int i = 0; i < size (); ++i) |
44 | sector eb; |
|
|
45 | |
|
|
46 | b2.x = o2.x + (soffs)bbox.a.x; |
|
|
47 | b2.y = o2.y + (soffs)bbox.a.y; |
|
|
48 | b2.z = o2.z + (soffs)bbox.a.z; |
|
|
49 | |
|
|
50 | eb.x = (soffs)(bbox.b.x - bbox.a.x) / 2; |
|
|
51 | eb.y = (soffs)(bbox.b.y - bbox.a.y) / 2; |
|
|
52 | eb.z = (soffs)(bbox.b.z - bbox.a.z) / 2; |
|
|
53 | |
|
|
54 | b2.x += eb.x; |
|
|
55 | b2.y += eb.y; |
|
|
56 | b2.z += eb.z; |
|
|
57 | |
|
|
58 | return abs (a2.x - b2.x) <= ea + eb.x |
|
|
59 | && abs (a2.y - b2.y) <= ea + eb.y |
|
|
60 | && abs (a2.z - b2.z) <= ea + eb.z; |
|
|
61 | } |
|
|
62 | |
|
|
63 | void octant::add (const sector &sec, entity_base *e) |
|
|
64 | { |
|
|
65 | printf ("OCTANT %d,%d,%d+%d\n", orig.x, orig.y, orig.z, extent); |
|
|
66 | |
|
|
67 | if (overlap (orig, extent, sec, e->bbox)) |
|
|
68 | { |
|
|
69 | printf ("overlap, add\n"); |
|
|
70 | push_back (e); |
|
|
71 | e->o.push_back (this); |
|
|
72 | } |
341 | { |
73 | else |
342 | entity *e = (*this)[i]; |
74 | printf ("no overlap\n"); |
343 | evis &evs = vs.get_visibility (i, e); |
75 | } |
|
|
76 | |
344 | |
77 | void octant::remove (entity_base *e) |
345 | if (evs.state != OCCLUDED) |
78 | { |
346 | { |
79 | } |
347 | if (!ctx.may_draw (e)) |
|
|
348 | continue; |
80 | |
349 | |
81 | void octant::draw (draw_context &ctx) |
350 | sector center = ((e->a + e->b) >> 1) - ctx.orig; |
82 | { |
351 | GLfloat z = length (vec3 (center)); |
83 | for (iterator i = end (); i-- != begin (); ) |
352 | ctx.pixfact = ctx.perspfact / z; |
84 | (*i)->try_draw (ctx); |
|
|
85 | } |
|
|
86 | |
353 | |
|
|
354 | if (!ctx.first_lighted |
|
|
355 | || evs.next > timer.now) |
|
|
356 | e->draw (ctx); |
|
|
357 | else |
|
|
358 | { |
|
|
359 | evs.next = timer.now + 0.2; |
|
|
360 | ctx.begin_occ_query (*this, e); |
|
|
361 | e->draw (ctx); |
|
|
362 | ctx.end_occ_query (); |
|
|
363 | } |
|
|
364 | } |
|
|
365 | } |
|
|
366 | } |
|
|
367 | |
|
|
368 | void octant::event (occ_query &ev) |
|
|
369 | { |
|
|
370 | oct_visibility &vs = *(oct_visibility *)get_visibility (ev.ctx); |
|
|
371 | entity *e = (entity *)ev.id; |
|
|
372 | |
|
|
373 | if (e) |
|
|
374 | { |
|
|
375 | #if 1 |
|
|
376 | for (vector<evis>::iterator i = vs.vismap.begin (); |
|
|
377 | i != vs.vismap.end (); |
|
|
378 | ++i) |
|
|
379 | if (i->e == e) |
|
|
380 | { |
|
|
381 | i->state = ev.count ? FULL : OCCLUDED; |
|
|
382 | return; |
|
|
383 | } |
|
|
384 | #endif |
|
|
385 | } |
|
|
386 | else |
|
|
387 | { |
|
|
388 | vs.state = ev.count ? FULL : OCCLUDED; |
|
|
389 | vs.vis = ev.count ? true : false; |
|
|
390 | } |
|
|
391 | } |
|
|
392 | |
|
|
393 | |