1 | #include <cstdlib> |
1 | #include <cstdlib> |
2 | |
2 | |
3 | #include <vector> |
3 | #include <vector> |
|
|
4 | |
4 | using namespace std; |
5 | using namespace std; |
5 | |
6 | |
6 | #include "opengl.h" |
7 | #include "opengl.h" |
7 | |
8 | |
8 | #include "oct.h" |
9 | #include "oct.h" |
9 | #include "view.h" |
10 | #include "view.h" |
10 | #include "entity.h" |
11 | #include "entity.h" |
|
|
12 | #include "material.h" //DEBUG |
11 | |
13 | |
12 | enum visibility_state { FULL, PARTIAL, OCCLUDED }; |
14 | enum visibility_state { |
|
|
15 | PARTIAL, // surrounding the viewpoint |
|
|
16 | FULL, // in frustum |
|
|
17 | OCCLUDED, // in frustom, but occluded |
|
|
18 | }; |
13 | |
19 | |
14 | struct evis { |
20 | struct evis { |
15 | entity *e; |
21 | entity *e; |
16 | visibility_state state; |
22 | visibility_state state; |
17 | double last; // time of last check |
23 | double last; // time of last check |
… | |
… | |
31 | struct oct_visibility : visibility_base |
37 | struct oct_visibility : visibility_base |
32 | { |
38 | { |
33 | vector<evis> vismap; |
39 | vector<evis> vismap; |
34 | |
40 | |
35 | visibility_state state; |
41 | visibility_state state; |
|
|
42 | bool vis; |
36 | |
43 | |
37 | evis &get_visibility (int i, entity *e) |
44 | evis &get_visibility (int i, entity *e) |
38 | { |
45 | { |
39 | evis &evs = vismap [i]; |
46 | evis &evs = vismap [i]; |
40 | |
47 | |
… | |
… | |
46 | |
53 | |
47 | return evs; |
54 | return evs; |
48 | } |
55 | } |
49 | |
56 | |
50 | oct_visibility (octant &oct) |
57 | oct_visibility (octant &oct) |
51 | : state(FULL) |
58 | : state (FULL), vis (true) |
52 | { |
59 | { |
53 | } |
60 | } |
54 | }; |
61 | }; |
55 | |
62 | |
56 | octant world(0, sector (0, 0, 0), MAXEXTENT); |
63 | octant world(0, sector (0, 0, 0), MAXEXTENT); |
… | |
… | |
139 | { |
146 | { |
140 | } |
147 | } |
141 | |
148 | |
142 | bool octant::detect_visibility (view &ctx) //, bool fully_visible) |
149 | bool octant::detect_visibility (view &ctx) //, bool fully_visible) |
143 | { |
150 | { |
144 | ctx.stat1++;//D |
|
|
145 | |
|
|
146 | sector centeri = orig - ctx.orig; |
151 | sector centeri = orig - ctx.orig; |
147 | point centerf = point (centeri); |
152 | point centerf = point (centeri); |
148 | |
153 | |
149 | GLfloat rad = ctx.diagfact * extent; |
154 | GLfloat rad = ctx.diagfact * extent; |
150 | |
155 | |
151 | if (!overlap (ctx.frustum.c, sphere (centerf, rad))) |
156 | if (!overlap (ctx.frustum.c, sphere (centerf, rad))) |
152 | return false; |
157 | return false; |
153 | |
158 | |
154 | oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
159 | oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
155 | |
160 | |
156 | //vs.state = FULL;//D |
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 | } |
157 | |
171 | |
158 | #if 0 |
172 | #if 0 |
159 | if (max (abs (centeri)) <= extent) |
|
|
160 | ; |
|
|
161 | else |
|
|
162 | { |
|
|
163 | #endif |
|
|
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 | #if 0 |
|
|
172 | GLfloat fd = distance (ctx.frustum.f, centerf); |
173 | GLfloat fd = distance (ctx.frustum.f, centerf); |
173 | |
174 | |
174 | if (fd < -(ctx.c_far - ctx.z_far) -rad * 3.F) |
175 | if (fd < -(ctx.c_far - ctx.z_far) -rad * 3.F) |
175 | return false; |
176 | return false; |
176 | #endif |
177 | #endif |
177 | |
178 | |
178 | // very important, optimize? |
179 | // very important, optimize? |
179 | GLfloat z = length (ctx.p - centerf) + rad; |
180 | GLfloat z = length (ctx.p - centerf) + rad; |
180 | if (z < 0.F || ctx.perspfact * extent / z < 10.) // very crude "too small to see" check |
181 | if (z < 0.F || ctx.perspfact * extent / z < 10.) // very crude "too small to see" check |
… | |
… | |
185 | ctx.nc_far = max (ctx.nc_far, z); |
186 | ctx.nc_far = max (ctx.nc_far, z); |
186 | #endif |
187 | #endif |
187 | |
188 | |
188 | if (vs.state == OCCLUDED) |
189 | if (vs.state == OCCLUDED) |
189 | { |
190 | { |
190 | #if 0 |
|
|
191 | if (extent < ctx.z_far) |
|
|
192 | { |
|
|
193 | #endif |
|
|
194 | ctx.postdepthlist.push_back (this); |
191 | ctx.postdepthlist.push_back (this); |
|
|
192 | |
195 | return false; |
193 | return false; |
196 | #if 0 |
|
|
197 | } |
|
|
198 | else |
|
|
199 | vs.state == PARTIAL; |
|
|
200 | #endif |
|
|
201 | } |
194 | } |
202 | |
195 | |
203 | bool visible = size () && vs.state == FULL; |
196 | //bool visible = size () && vs.state != OCCLUDED; |
|
|
197 | bool visible = false; |
204 | |
198 | |
205 | // node to start with |
199 | // node to start with |
206 | unsigned char si = centeri.x > 0 ? 1 : 0 |
200 | unsigned char si = centeri.x > 0 ? 1 : 0 |
207 | | centeri.y > 0 ? 2 : 0 |
201 | | centeri.y > 0 ? 2 : 0 |
208 | | centeri.z > 0 ? 4 : 0; |
202 | | centeri.z > 0 ? 4 : 0; |
… | |
… | |
222 | while (*++next); |
216 | while (*++next); |
223 | |
217 | |
224 | if (visible) |
218 | if (visible) |
225 | { |
219 | { |
226 | if (size ()) |
220 | if (size ()) |
|
|
221 | { |
|
|
222 | ctx.postdepthlist.push_back (this); |
227 | ctx.vislist.push_back (this); |
223 | ctx.vislist.push_back (this); |
|
|
224 | } |
228 | |
225 | |
|
|
226 | vs.state = FULL; |
|
|
227 | |
|
|
228 | return true; |
|
|
229 | } |
|
|
230 | else if (vs.state == OCCLUDED) |
|
|
231 | { |
229 | ctx.postdepthlist.push_back (this); |
232 | ctx.postdepthlist.push_back (this); |
230 | } |
233 | |
231 | else |
234 | return false; |
232 | { |
235 | } |
233 | //vs.state = OCCLUDED; |
236 | else if (size()) |
|
|
237 | { |
234 | ctx.postdepthlist.push_back (this); |
238 | ctx.postdepthlist.push_back (this); |
|
|
239 | ctx.vislist.push_back (this); |
|
|
240 | |
|
|
241 | return true; |
|
|
242 | } |
|
|
243 | else |
235 | } |
244 | { |
236 | |
245 | return vs.state != PARTIAL; |
237 | return visible; |
246 | } |
238 | } |
247 | } |
239 | |
248 | |
240 | void octant::draw_depth (view &ctx) |
249 | void octant::draw_depth (view &ctx) |
241 | { |
250 | { |
242 | oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
251 | oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
243 | |
252 | |
244 | vs.vismap.resize (size ()); |
253 | vs.vismap.resize (size ()); |
245 | |
254 | |
246 | if (vs.state == PARTIAL || vs.state == OCCLUDED) |
255 | if (vs.state == OCCLUDED) |
247 | return; |
256 | return; |
248 | |
257 | |
249 | for (int i = 0; i < size (); ++i) |
258 | for (int i = 0; i < size (); ++i) |
250 | { |
259 | { |
251 | entity *e = (*this)[i]; |
260 | entity *e = (*this)[i]; |
… | |
… | |
270 | |
279 | |
271 | void octant::draw_postdepth (view &ctx) |
280 | void octant::draw_postdepth (view &ctx) |
272 | { |
281 | { |
273 | oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
282 | oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
274 | |
283 | |
275 | if (vs.state == PARTIAL || vs.state == OCCLUDED) |
284 | if (vs.state == OCCLUDED || !vs.vis) |
276 | { |
285 | { |
277 | ctx.begin_occ_query (*this, 0); |
286 | ctx.begin_occ_query (*this, 0); |
278 | sector s = orig - ctx.orig; |
287 | sector s = orig - ctx.orig; |
279 | gl::draw_bbox (s - extent, s + extent); |
288 | gl::draw_bbox (s - extent, s + extent); |
280 | ctx.end_occ_query (); |
289 | ctx.end_occ_query (); |
281 | } |
290 | } |
282 | else |
291 | else |
283 | { |
292 | { |
284 | int nvis = 0; |
293 | vs.vis = !size (); |
285 | |
294 | |
286 | for (int i = 0; i < size (); ++i) |
295 | for (int i = 0; i < size (); ++i) |
287 | { |
296 | { |
288 | entity *e = (*this)[i]; |
297 | entity *e = (*this)[i]; |
289 | const evis &evs = vs.get_visibility (i, e); |
298 | const evis &evs = vs.get_visibility (i, e); |
… | |
… | |
296 | ctx.begin_occ_query (*this, e); |
305 | ctx.begin_occ_query (*this, e); |
297 | gl::draw_bbox (e->a - ctx.orig, e->b - ctx.orig); |
306 | gl::draw_bbox (e->a - ctx.orig, e->b - ctx.orig); |
298 | ctx.end_occ_query (); |
307 | ctx.end_occ_query (); |
299 | } |
308 | } |
300 | else |
309 | else |
301 | nvis++; |
310 | vs.vis = true; |
302 | } |
311 | } |
|
|
312 | vs.vis = true;//D |
|
|
313 | } |
|
|
314 | } |
|
|
315 | |
|
|
316 | void octant::draw_lighted (view &ctx) |
|
|
317 | { |
|
|
318 | oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
303 | |
319 | |
304 | #if 0 |
320 | #if 0 |
305 | if (nvis == 0 && size ()) |
|
|
306 | vs.state = PARTIAL; |
|
|
307 | #endif |
|
|
308 | } |
|
|
309 | } |
|
|
310 | |
|
|
311 | void octant::draw_lighted (view &ctx) |
|
|
312 | { |
|
|
313 | oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
|
|
314 | |
|
|
315 | #if 0 |
|
|
316 | if (vs.state == PARTIAL || vs.state == OCCLUDED) |
321 | if (vs.state == FULL || vs.state == OCCLUDED) |
317 | { |
322 | { |
318 | sector s = orig - ctx.orig; |
323 | sector s = orig - ctx.orig; |
|
|
324 | debugmat->enable (ctx); |
|
|
325 | if (max (s) >= extent) |
319 | gl::draw_bbox (s - extent, s + extent); |
326 | gl::draw_bbox (s - extent, s + extent); |
320 | printf ("DLP %p\n", this);//D |
327 | debugmat->disable (ctx); |
|
|
328 | //printf ("DLP %ld %ld %ld (%ld)\n", orig.x, orig.y, orig.z, extent);//D |
321 | } |
329 | } |
322 | #endif |
330 | #endif |
323 | |
331 | |
324 | if (vs.state == PARTIAL || vs.state == OCCLUDED) |
332 | if (vs.state == OCCLUDED) |
325 | return; |
333 | return; |
326 | |
|
|
327 | #if 0 |
|
|
328 | { |
334 | |
329 | static vertex_buffer vb; |
|
|
330 | static index_buffer ib; |
|
|
331 | |
|
|
332 | sector s = orig - ctx.orig; |
|
|
333 | sector a = s - extent, b = s + extent; |
|
|
334 | |
|
|
335 | vertex_v3f vd[] = { |
|
|
336 | point (a.x, a.y, a.z), |
|
|
337 | point (b.x, a.y, a.z), |
|
|
338 | point (a.x, b.y, a.z), |
|
|
339 | point (b.x, b.y, a.z), |
|
|
340 | point (a.x, a.y, b.z), |
|
|
341 | point (b.x, a.y, b.z), |
|
|
342 | point (a.x, b.y, b.z), |
|
|
343 | point (b.x, b.y, b.z), |
|
|
344 | }; |
|
|
345 | |
|
|
346 | if (!ib) |
|
|
347 | { |
|
|
348 | static GLushort verts[4*6] = { |
|
|
349 | 0, 4, 6, 2, // -x |
|
|
350 | 1, 3, 7, 5, // +x |
|
|
351 | 0, 1, 5, 4, // -y |
|
|
352 | 7, 3, 2, 6, // +y |
|
|
353 | 0, 2, 3, 1, // -z |
|
|
354 | 4, 5, 7, 6, // +z |
|
|
355 | }; |
|
|
356 | |
|
|
357 | ib.set (verts, 4*6, GL_STATIC_DRAW_ARB); |
|
|
358 | } |
|
|
359 | |
|
|
360 | vb.set (vd, 8, GL_STREAM_DRAW_ARB); |
|
|
361 | vb.bind (); |
|
|
362 | for (int i = 0; i < 6; i++) |
|
|
363 | ib.draw (GL_LINE_LOOP, i*4, 4); |
|
|
364 | |
|
|
365 | } |
|
|
366 | #endif |
|
|
367 | |
|
|
368 | for (int i = 0; i < size (); ++i) |
335 | for (int i = 0; i < size (); ++i) |
369 | { |
336 | { |
370 | entity *e = (*this)[i]; |
337 | entity *e = (*this)[i]; |
371 | evis &evs = vs.get_visibility (i, e); |
338 | evis &evs = vs.get_visibility (i, e); |
372 | |
339 | |
… | |
… | |
377 | |
344 | |
378 | sector center = ((e->a + e->b) >> 1) - ctx.orig; |
345 | sector center = ((e->a + e->b) >> 1) - ctx.orig; |
379 | GLfloat z = length (vec3 (center)); |
346 | GLfloat z = length (vec3 (center)); |
380 | ctx.pixfact = ctx.perspfact / z; |
347 | ctx.pixfact = ctx.perspfact / z; |
381 | |
348 | |
382 | if (ctx.pass->type != LIGHTED |
|
|
383 | || !ctx.first_lighted |
349 | if (!ctx.first_lighted |
384 | || evs.last + 1. > timer.now) |
350 | || evs.last + 1. > timer.now) |
385 | e->draw (ctx); |
351 | e->draw (ctx); |
386 | else |
352 | else |
387 | { |
353 | { |
388 | evs.last = timer.now; |
354 | evs.last = timer.now; |
… | |
… | |
399 | oct_visibility &vs = *(oct_visibility *)get_visibility (ev.ctx); |
365 | oct_visibility &vs = *(oct_visibility *)get_visibility (ev.ctx); |
400 | entity *e = (entity *)ev.id; |
366 | entity *e = (entity *)ev.id; |
401 | |
367 | |
402 | if (e) |
368 | if (e) |
403 | { |
369 | { |
|
|
370 | #if 1 |
404 | for (vector<evis>::iterator i = vs.vismap.begin (); |
371 | for (vector<evis>::iterator i = vs.vismap.begin (); |
405 | i != vs.vismap.end (); |
372 | i != vs.vismap.end (); |
406 | ++i) |
373 | ++i) |
407 | if (i->e == e) |
374 | if (i->e == e) |
408 | { |
375 | { |
409 | i->state = ev.count ? FULL : OCCLUDED; |
376 | i->state = ev.count ? FULL : OCCLUDED; |
410 | return; |
377 | return; |
411 | } |
378 | } |
|
|
379 | #endif |
|
|
380 | } |
|
|
381 | else |
412 | } |
382 | { |
413 | else |
383 | vs.state = ev.count ? FULL : OCCLUDED; |
414 | vs.state = ev.count ? (vs.state == PARTIAL ? PARTIAL : FULL) : OCCLUDED; |
384 | vs.vis = ev.count ? true : false; |
|
|
385 | } |
415 | } |
386 | } |
416 | |
387 | |
417 | |
388 | |