1 | #include <cstdlib> |
1 | #include <cstdlib> |
2 | |
2 | |
3 | #include <vector> |
3 | #include <vector> |
4 | |
|
|
5 | using namespace std; |
4 | using namespace std; |
6 | |
5 | |
7 | #include "opengl.h" |
6 | #include "opengl.h" |
8 | |
7 | |
9 | #include "oct.h" |
8 | #include "oct.h" |
10 | #include "view.h" |
9 | #include "view.h" |
11 | #include "entity.h" |
10 | #include "entity.h" |
12 | |
11 | |
|
|
12 | enum visibility_state { FULL, PARTIAL, SMALL, OCCLUDED }; |
|
|
13 | |
|
|
14 | struct evis { |
|
|
15 | visibility_state state; |
|
|
16 | double last; // time of last check |
|
|
17 | evis () : last(0.), state(FULL) { }; |
|
|
18 | }; |
|
|
19 | |
|
|
20 | struct oct_visibility : visibility_base |
|
|
21 | { |
|
|
22 | typedef map<entity *, evis> evismap; |
|
|
23 | evismap vismap; |
|
|
24 | |
|
|
25 | visibility_state state; |
|
|
26 | |
|
|
27 | oct_visibility (octant &oct) |
|
|
28 | : state(FULL) |
|
|
29 | { |
|
|
30 | } |
|
|
31 | }; |
|
|
32 | |
13 | octant world(0, sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN), MAXEXTENT); |
33 | octant world(0, sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN), MAXEXTENT); |
14 | |
34 | |
15 | octant::octant (octant *parent, const sector &orig, uoffs extent) |
35 | octant::octant (octant *parent, const sector &orig, uoffs extent) |
16 | : parent(parent), orig(orig), extent(extent) |
36 | : parent(parent) |
|
|
37 | , orig(orig) |
|
|
38 | , extent(extent) |
17 | { |
39 | { |
18 | for (fill = 8; fill--; ) |
40 | for (fill = 8; fill--; ) |
19 | sub[fill] = 0; |
41 | sub[fill] = 0; |
20 | } |
42 | } |
21 | |
43 | |
|
|
44 | visibility_base *octant::new_visibility () |
|
|
45 | { |
|
|
46 | return new oct_visibility (*this); |
|
|
47 | } |
|
|
48 | |
|
|
49 | void octant::clear_visibility (visibility_base *vs) |
|
|
50 | { |
|
|
51 | ((oct_visibility *)vs)->vismap.clear (); |
|
|
52 | } |
|
|
53 | |
22 | octant::~octant () |
54 | octant::~octant () |
23 | { |
55 | { |
24 | for (fill = 8; fill--; ) |
56 | for (fill = 8; fill--; ) |
25 | delete sub[fill]; |
57 | delete sub[fill]; |
26 | } |
58 | } |
… | |
… | |
29 | { |
61 | { |
30 | ea /= 2; |
62 | ea /= 2; |
31 | |
63 | |
32 | sector center_1 = o1 + ea; |
64 | sector center_1 = o1 + ea; |
33 | sector size_2 = b - a; |
65 | sector size_2 = b - a; |
34 | sector center_2 = a + size_2 / 2; |
66 | sector center_2 = a + (size_2 >> 1); |
35 | |
67 | |
36 | return abs (center_1 - center_2) <= ea + size_2; |
68 | return abs (center_1 - center_2) <= ea + size_2; |
37 | } |
69 | } |
38 | |
70 | |
39 | void octant::add (entity *e) |
71 | void octant::add (entity *e) |
… | |
… | |
41 | const sector &a = e->a; |
73 | const sector &a = e->a; |
42 | const sector &b = e->b; |
74 | const sector &b = e->b; |
43 | |
75 | |
44 | if (overlap (orig, extent, a, b)) |
76 | if (overlap (orig, extent, a, b)) |
45 | { |
77 | { |
46 | uoffs extent2 = extent / 2; |
78 | uoffs extent2 = extent >> 1; |
47 | uoffs size = max (abs (b - a)); |
79 | uoffs size = max (abs (b - a)); |
48 | |
80 | |
49 | if (size >= extent2 / 2) |
81 | if (size >= extent2 >> 1) |
50 | { |
82 | { |
51 | push_back (e); |
83 | push_back (e); |
52 | e->o.push_back (this); |
84 | e->o.push_back (this); |
53 | return; |
85 | return; |
54 | } |
86 | } |
… | |
… | |
74 | |
106 | |
75 | void octant::remove (entity *e) |
107 | void octant::remove (entity *e) |
76 | { |
108 | { |
77 | } |
109 | } |
78 | |
110 | |
79 | void octant::detect_visibility (view &ctx) |
111 | bool octant::detect_visibility (view &ctx) |
80 | { |
112 | { |
81 | visibility_state &vs = ctx.vismap[this]; |
|
|
82 | |
|
|
83 | if (vs.generation + 1 != ctx.generation) |
|
|
84 | vs.visibility = visibility_state::UNKNOWN; |
|
|
85 | |
|
|
86 | vs.generation = ctx.generation; |
|
|
87 | |
|
|
88 | GLfloat extent2 = 0.5F * (GLfloat)extent; |
113 | GLfloat extent2 = 0.5F * (GLfloat)extent; |
89 | sector centeri = orig + (extent >> 1) - ctx.orig; |
114 | sector centeri = orig + (extent >> 1) - ctx.orig; |
90 | point centerf = point (centeri) + ((extent & 1) ? 0.5F : 0.F); |
115 | point centerf = point (centeri) + ((extent & 1) ? 0.5F : 0.F); |
91 | |
116 | |
92 | GLfloat rad = ctx.diagfact * extent2; |
117 | GLfloat rad = ctx.diagfact * extent2; |
93 | |
118 | |
|
|
119 | if (!overlap (ctx.frustum.c, sphere (centerf, rad))) |
|
|
120 | return false; |
|
|
121 | |
|
|
122 | oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
|
|
123 | |
94 | if (orig <= ctx.orig && ctx.orig <= orig + extent) |
124 | if (orig <= ctx.orig && ctx.orig <= orig + extent) |
95 | vs.visibility = visibility_state::PARTIAL; |
125 | vs.state = PARTIAL; |
96 | else |
126 | else |
97 | { |
127 | { |
98 | if (ctx.frustum.t.distance (centerf) < -rad) return; |
128 | if (distance (ctx.frustum.t, centerf) < -rad) return false; |
99 | if (ctx.frustum.b.distance (centerf) < -rad) return; |
129 | if (distance (ctx.frustum.b, centerf) < -rad) return false; |
100 | if (ctx.frustum.l.distance (centerf) < -rad) return; |
130 | if (distance (ctx.frustum.l, centerf) < -rad) return false; |
101 | if (ctx.frustum.r.distance (centerf) < -rad) return; |
131 | if (distance (ctx.frustum.r, centerf) < -rad) return false; |
102 | if (ctx.frustum.n.distance (centerf) < -rad) return; |
132 | if (distance (ctx.frustum.n, centerf) < -rad) return false; |
103 | |
133 | |
|
|
134 | #if 0 |
104 | GLfloat fd = ctx.frustum.f.distance (centerf); |
135 | GLfloat fd = distance (ctx.frustum.f, centerf); |
105 | |
136 | |
106 | if (fd < -(ctx.c_far - ctx.z_far) -rad * 3.F) |
137 | if (fd < -(ctx.c_far - ctx.z_far) -rad * 3.F) |
107 | return; |
138 | return false; |
108 | } |
139 | #endif |
109 | |
|
|
110 | if (vs.visibility == visibility_state::OCCLUDED |
|
|
111 | || vs.visibility == visibility_state::UNKNOWN) |
|
|
112 | { |
140 | } |
|
|
141 | |
|
|
142 | #if 1 |
|
|
143 | if (vs.state == OCCLUDED) |
|
|
144 | { |
|
|
145 | if (size ()) |
113 | ctx.farlist.push_back (this); |
146 | ctx.vislist.push_back (this); |
114 | return; |
147 | //return false; |
115 | } |
148 | } |
|
|
149 | #endif |
|
|
150 | |
|
|
151 | GLfloat z = ctx.z_near + distance (ctx.frustum.n, centerf) + rad; |
|
|
152 | if (ctx.perspfact * extent / z < 1.) // very crude "too small to see" check |
|
|
153 | return false; |
|
|
154 | //printf ("z %f, perspfact %f, z*p %f\n", z, ctx.perspfact, ctx.perspfact / z); |
116 | |
155 | |
117 | #if 0 |
156 | #if 0 |
118 | if (vs.visibility == visibility_state::UNKNOWN) |
157 | if (vs.state == PARTIAL || vs.state == FULL) |
119 | vs.visibility = visibility_state::FULL; |
|
|
120 | #endif |
|
|
121 | |
|
|
122 | GLfloat z = ctx.z_near + ctx.frustum.n.distance (centerf) + rad; |
|
|
123 | //printf ("z %f, perspfact %f, z*p %f\n", z, ctx.perspfact, ctx.perspfact / z); |
|
|
124 | |
|
|
125 | if (vs.visibility == visibility_state::FULL) |
|
|
126 | ctx.nc_far = max (ctx.nc_far, z); |
158 | ctx.nc_far = max (ctx.nc_far, z); |
|
|
159 | #endif |
127 | |
160 | |
128 | // node to start with |
161 | // node to start with |
129 | unsigned char si = centeri.x > 0 ? 1 : 0 |
162 | unsigned char si = centeri.x > 0 ? 1 : 0 |
130 | | centeri.y > 0 ? 2 : 0 |
163 | | centeri.y > 0 ? 2 : 0 |
131 | | centeri.z > 0 ? 4 : 0; |
164 | | centeri.z > 0 ? 4 : 0; |
… | |
… | |
145 | sub[si]->detect_visibility (ctx); |
178 | sub[si]->detect_visibility (ctx); |
146 | } |
179 | } |
147 | while (*++next); |
180 | while (*++next); |
148 | |
181 | |
149 | if (size () |
182 | if (size () |
150 | && (vs.visibility == visibility_state::PARTIAL |
183 | && (vs.state == PARTIAL || vs.state == FULL || vs.state == OCCLUDED)) |
151 | || vs.visibility == visibility_state::FULL)) |
|
|
152 | { |
|
|
153 | ctx.nz_far = max (ctx.nz_far, z); |
|
|
154 | ctx.vislist.push_back (this); |
184 | ctx.vislist.push_back (this); |
155 | } |
185 | |
|
|
186 | return true; |
156 | } |
187 | } |
157 | |
188 | |
158 | void octant::display (view &ctx) |
189 | void octant::display (view &ctx) |
159 | { |
190 | { |
160 | #if 0 |
191 | #if 0 |
… | |
… | |
179 | } |
210 | } |
180 | } |
211 | } |
181 | |
212 | |
182 | glEnd (); |
213 | glEnd (); |
183 | #endif |
214 | #endif |
|
|
215 | oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
184 | |
216 | |
185 | for (iterator i = end (); i != begin (); ) |
217 | if (vs.state == OCCLUDED) |
186 | { |
|
|
187 | entity *e = *--i; |
|
|
188 | |
|
|
189 | sector diff = (e->a + e->b) / 2 - ctx.orig; |
|
|
190 | GLfloat z = norm (vec3 ((e->a + e->b) / 2 - ctx.orig)); |
|
|
191 | ctx.pixfact = ctx.perspfact / z; |
|
|
192 | |
|
|
193 | e->display (ctx); |
|
|
194 | } |
218 | { |
195 | } |
219 | if (ctx.pass == view::POSTDEPTH) |
196 | |
220 | { |
197 | void octant::draw_bbox (view &ctx) |
221 | ctx.begin_occ_query (*this, 0); |
198 | { |
|
|
199 | sector s = orig - ctx.orig; |
222 | sector s = orig - ctx.orig; |
200 | |
|
|
201 | gl::draw_bbox (ctx, s, s + extent); |
223 | gl::draw_bbox (s, s + extent); |
|
|
224 | ctx.end_occ_query (); |
|
|
225 | } |
|
|
226 | } |
|
|
227 | else |
|
|
228 | { |
|
|
229 | int nvis = 0; |
|
|
230 | |
|
|
231 | for (iterator i = begin (); i != end (); ) |
|
|
232 | { |
|
|
233 | entity *e = *i++; |
|
|
234 | |
|
|
235 | evis &evs = vs.vismap[e]; |
|
|
236 | |
|
|
237 | if (ctx.pass == view::POSTDEPTH) |
|
|
238 | { |
|
|
239 | if (evs.state == OCCLUDED) |
|
|
240 | { |
|
|
241 | ctx.begin_occ_query (*this, e); |
|
|
242 | gl::draw_bbox (e->a - ctx.orig, e->b - ctx.orig); |
|
|
243 | ctx.end_occ_query (); |
|
|
244 | } |
|
|
245 | else |
|
|
246 | nvis++; |
|
|
247 | } |
|
|
248 | else |
|
|
249 | { |
|
|
250 | if (!ctx.may_draw (e)) |
|
|
251 | continue; |
|
|
252 | |
|
|
253 | |
|
|
254 | if (evs.state != OCCLUDED) |
|
|
255 | { |
|
|
256 | sector center = ((e->a + e->b) >> 1) - ctx.orig; |
|
|
257 | GLfloat z = length (vec3 (center)); |
|
|
258 | ctx.pixfact = ctx.perspfact / z; |
|
|
259 | |
|
|
260 | ctx.nz_far = max (ctx.nz_far, z + extent); |
|
|
261 | ctx.nz_near = min (ctx.nz_near, z - extent); |
|
|
262 | |
|
|
263 | if (ctx.pass == view::DEPTH || evs.last + 0.3 > timer.now) |
|
|
264 | e->draw (ctx); |
|
|
265 | else |
|
|
266 | { |
|
|
267 | evs.last = timer.now; |
|
|
268 | ctx.begin_occ_query (*this, e); |
|
|
269 | e->draw (ctx); |
|
|
270 | ctx.end_occ_query (); |
|
|
271 | } |
|
|
272 | } |
|
|
273 | } |
|
|
274 | } |
|
|
275 | |
|
|
276 | #if 1 |
|
|
277 | if (ctx.pass == view::POSTDEPTH && nvis == 0 && size ()) |
|
|
278 | vs.state = OCCLUDED; |
|
|
279 | #endif |
|
|
280 | } |
202 | } |
281 | } |
203 | |
282 | |
204 | void octant::event (occ_query &ev) |
283 | void octant::event (occ_query &ev) |
205 | { |
284 | { |
206 | visibility_state &vs = ev.v.vismap[this]; |
285 | oct_visibility &vs = *(oct_visibility *)get_visibility (ev.ctx); |
|
|
286 | entity *e = (entity *)ev.id; |
207 | |
287 | |
208 | vs.last = timer.now; |
288 | if (e) |
209 | vs.visibility = ev.r <= 0 |
289 | { |
210 | ? visibility_state::OCCLUDED |
290 | evis &evs = vs.vismap[e]; |
211 | : visibility_state::FULL; |
291 | evs.state = ev.count ? FULL : OCCLUDED; |
|
|
292 | } |
|
|
293 | else |
|
|
294 | vs.state = ev.count ? FULL : OCCLUDED; |
212 | } |
295 | } |
213 | |
296 | |
|
|
297 | |