… | |
… | |
7 | |
7 | |
8 | #include "oct.h" |
8 | #include "oct.h" |
9 | #include "view.h" |
9 | #include "view.h" |
10 | #include "entity.h" |
10 | #include "entity.h" |
11 | |
11 | |
12 | enum visibility_state { FULL, PARTIAL, SMALL, OCCLUDED }; |
12 | enum visibility_state { FULL, PARTIAL, SMALL, OCCLUDED, SUBTREE_OCCLUDED }; |
13 | |
13 | |
14 | struct evis { |
14 | struct evis { |
15 | visibility_state state; |
15 | visibility_state state; |
16 | double last; // time of last check |
16 | double last; // time of last check |
17 | evis () : last(0.), state(FULL) { }; |
17 | evis () : last(0.), state(FULL) { }; |
… | |
… | |
48 | |
48 | |
49 | void octant::clear_visibility (visibility_base *vs) |
49 | void octant::clear_visibility (visibility_base *vs) |
50 | { |
50 | { |
51 | ((oct_visibility *)vs)->vismap.clear (); |
51 | ((oct_visibility *)vs)->vismap.clear (); |
52 | } |
52 | } |
|
|
53 | |
53 | octant::~octant () |
54 | octant::~octant () |
54 | { |
55 | { |
55 | for (fill = 8; fill--; ) |
56 | for (fill = 8; fill--; ) |
56 | delete sub[fill]; |
57 | delete sub[fill]; |
57 | } |
58 | } |
… | |
… | |
105 | |
106 | |
106 | void octant::remove (entity *e) |
107 | void octant::remove (entity *e) |
107 | { |
108 | { |
108 | } |
109 | } |
109 | |
110 | |
110 | bool octant::depth_pass (view &ctx) |
111 | bool octant::detect_visibility (view &ctx) |
111 | { |
112 | { |
112 | oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
|
|
113 | |
|
|
114 | GLfloat extent2 = 0.5F * (GLfloat)extent; |
113 | GLfloat extent2 = 0.5F * (GLfloat)extent; |
115 | sector centeri = orig + (extent >> 1) - ctx.orig; |
114 | sector centeri = orig + (extent >> 1) - ctx.orig; |
116 | point centerf = point (centeri) + ((extent & 1) ? 0.5F : 0.F); |
115 | point centerf = point (centeri) + ((extent & 1) ? 0.5F : 0.F); |
117 | |
116 | |
118 | GLfloat rad = ctx.diagfact * extent2; |
117 | GLfloat rad = ctx.diagfact * extent2; |
119 | |
118 | |
|
|
119 | if (!overlap (ctx.frustum.c, sphere (centerf, rad))) |
|
|
120 | return false; |
|
|
121 | |
|
|
122 | oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
|
|
123 | |
120 | if (orig <= ctx.orig && ctx.orig <= orig + extent) |
124 | if (orig <= ctx.orig && ctx.orig <= orig + extent) |
121 | vs.state = PARTIAL; |
125 | vs.state = PARTIAL; |
122 | else |
126 | else |
123 | { |
127 | { |
124 | if (ctx.frustum.t.distance (centerf) < -rad) return false; |
128 | if (distance (ctx.frustum.t, centerf) < -rad) return false; |
125 | if (ctx.frustum.b.distance (centerf) < -rad) return false; |
129 | if (distance (ctx.frustum.b, centerf) < -rad) return false; |
126 | if (ctx.frustum.l.distance (centerf) < -rad) return false; |
130 | if (distance (ctx.frustum.l, centerf) < -rad) return false; |
127 | if (ctx.frustum.r.distance (centerf) < -rad) return false; |
131 | if (distance (ctx.frustum.r, centerf) < -rad) return false; |
128 | if (ctx.frustum.n.distance (centerf) < -rad) return false; |
132 | if (distance (ctx.frustum.n, centerf) < -rad) return false; |
129 | |
133 | |
|
|
134 | #if 0 |
130 | GLfloat fd = ctx.frustum.f.distance (centerf); |
135 | GLfloat fd = distance (ctx.frustum.f, centerf); |
131 | |
136 | |
132 | if (fd < -(ctx.c_far - ctx.z_far) -rad * 3.F) |
137 | if (fd < -(ctx.c_far - ctx.z_far) -rad * 3.F) |
133 | return false; |
138 | return false; |
|
|
139 | #endif |
134 | } |
140 | } |
135 | |
141 | |
|
|
142 | #if 0 |
136 | if (vs.state == OCCLUDED) |
143 | if (vs.state == OCCLUDED && size ()) |
|
|
144 | { |
|
|
145 | if (size ()) |
|
|
146 | ctx.vislist.push_back (this); |
|
|
147 | return false; |
|
|
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 |
137 | return false; |
153 | return false; |
138 | |
|
|
139 | GLfloat z = ctx.z_near + ctx.frustum.n.distance (centerf) + rad; |
|
|
140 | //printf ("z %f, perspfact %f, z*p %f\n", z, ctx.perspfact, ctx.perspfact / z); |
154 | //printf ("z %f, perspfact %f, z*p %f\n", z, ctx.perspfact, ctx.perspfact / z); |
141 | |
155 | |
|
|
156 | #if 0 |
142 | if (vs.state == PARTIAL || vs.state == FULL) |
157 | if (vs.state == PARTIAL || vs.state == FULL) |
143 | ctx.nc_far = max (ctx.nc_far, z); |
158 | ctx.nc_far = max (ctx.nc_far, z); |
|
|
159 | #endif |
|
|
160 | |
|
|
161 | if (vs.state == SUBTREE_OCCLUDED) |
|
|
162 | { |
|
|
163 | ctx.vislist.push_back (this); |
|
|
164 | return false; |
|
|
165 | } |
|
|
166 | |
|
|
167 | bool visible = size () && (vs.state == PARTIAL || vs.state == FULL); |
144 | |
168 | |
145 | // node to start with |
169 | // node to start with |
146 | unsigned char si = centeri.x > 0 ? 1 : 0 |
170 | unsigned char si = centeri.x > 0 ? 1 : 0 |
147 | | centeri.y > 0 ? 2 : 0 |
171 | | centeri.y > 0 ? 2 : 0 |
148 | | centeri.z > 0 ? 4 : 0; |
172 | | centeri.z > 0 ? 4 : 0; |
149 | |
173 | |
150 | //printf ("si %d C %Ld,%Ld,%Ld\n", si, centeri.x, centeri.y, centeri.z); |
|
|
151 | |
|
|
152 | // bit-toggle to find next child for front-to-back order |
174 | // bit-toggle to find next child for front-to-back order |
153 | static unsigned char toggle[8+1] |
175 | static unsigned char toggle[8+1] |
154 | = { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7, 0 }; |
176 | = { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7, 0 }; |
155 | |
177 | |
156 | unsigned char *next = toggle; |
178 | unsigned char *next = toggle; |
157 | do |
179 | do |
158 | { |
180 | { |
159 | si ^= *next; |
181 | si ^= *next; |
160 | |
182 | |
161 | if (sub[si]) |
183 | if (sub[si]) |
162 | sub[si]->depth_pass (ctx); |
184 | visible = visible | sub[si]->detect_visibility (ctx); |
163 | } |
185 | } |
164 | while (*++next); |
186 | while (*++next); |
165 | |
187 | |
|
|
188 | if (visible) |
|
|
189 | { |
166 | if (size () |
190 | if (size ()) |
167 | && (vs.state == PARTIAL || vs.state == FULL)) |
191 | ctx.vislist.push_back (this); |
168 | { |
192 | } |
169 | ctx.nz_far = max (ctx.nz_far, z); |
193 | else |
170 | display (ctx); |
194 | { |
|
|
195 | vs.state = SUBTREE_OCCLUDED; |
171 | ctx.vislist.push_back (this); |
196 | ctx.vislist.push_back (this); |
172 | } |
197 | } |
173 | |
198 | |
174 | return true; |
199 | return visible; |
175 | } |
200 | } |
176 | |
201 | |
177 | void octant::display (view &ctx) |
202 | void octant::display (view &ctx) |
178 | { |
203 | { |
179 | #if 0 |
|
|
180 | sector s = orig - ctx.orig; |
|
|
181 | |
|
|
182 | glBegin (GL_LINES); |
|
|
183 | vec3 clr(0, 0.8, 0); |
|
|
184 | glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, (const GLfloat*)&clr); |
|
|
185 | |
|
|
186 | for (int i = 8; i--; ) |
|
|
187 | for (int ji = 3; ji--; ) |
|
|
188 | { |
|
|
189 | int j = i | (1 << ji); |
|
|
190 | if (i < j) |
|
|
191 | { |
|
|
192 | glVertex3i (s.x + !!(i & 1) * extent, |
|
|
193 | s.y + !!(i & 2) * extent, |
|
|
194 | s.z + !!(i & 4) * extent); |
|
|
195 | glVertex3i (s.x + !!(j & 1) * extent, |
|
|
196 | s.y + !!(j & 2) * extent, |
|
|
197 | s.z + !!(j & 4) * extent); |
|
|
198 | } |
|
|
199 | } |
|
|
200 | |
|
|
201 | glEnd (); |
|
|
202 | #endif |
|
|
203 | oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
204 | oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
204 | |
205 | |
205 | if (vs.state == OCCLUDED) |
206 | if (vs.state == OCCLUDED || vs.state == SUBTREE_OCCLUDED) |
206 | { |
207 | { |
207 | abort (); |
|
|
208 | if (ctx.pass == view::POSTDEPTH) |
208 | if (ctx.pass->type == POSTDEPTH) |
209 | { |
|
|
210 | if (!vb_bbox) |
|
|
211 | { |
209 | { |
212 | sector s = orig - ctx.orig; |
|
|
213 | gl::gen_bbox (vb_bbox, s, s + extent); |
|
|
214 | } |
|
|
215 | |
|
|
216 | ctx.begin_occ_query (*this, 0); |
210 | ctx.begin_occ_query (*this, 0); |
217 | vb_bbox.draw (gl::bbox_mode, 0, gl::bbox_count); |
211 | sector s = orig - ctx.orig; |
|
|
212 | gl::draw_bbox (s, s + extent); |
218 | ctx.end_occ_query (); |
213 | ctx.end_occ_query (); |
219 | } |
214 | } |
220 | } |
215 | } |
221 | else |
216 | else |
222 | { |
217 | { |
223 | for (iterator i = end (); i != begin (); ) |
218 | int nvis = 0; |
|
|
219 | |
|
|
220 | for (iterator i = begin (); i != end (); ) |
224 | { |
221 | { |
225 | entity *e = *--i; |
222 | entity *e = *i++; |
226 | |
|
|
227 | if (!ctx.may_draw (e)) |
|
|
228 | continue; |
|
|
229 | |
223 | |
230 | evis &evs = vs.vismap[e]; |
224 | evis &evs = vs.vismap[e]; |
231 | |
225 | |
232 | if (ctx.pass == view::POSTDEPTH) |
226 | if (ctx.pass->type == POSTDEPTH) |
233 | { |
227 | { |
234 | if (evs.state == OCCLUDED) |
228 | if (evs.state == OCCLUDED) |
235 | { |
229 | { |
236 | if (!e->vb_bbox) |
|
|
237 | gl::gen_bbox (e->vb_bbox, e->a - ctx.orig, e->b - ctx.orig); |
|
|
238 | |
|
|
239 | ctx.begin_occ_query (*this, e); |
230 | ctx.begin_occ_query (*this, e); |
240 | e->vb_bbox.draw (gl::bbox_mode, 0, gl::bbox_count); |
231 | gl::draw_bbox (e->a - ctx.orig, e->b - ctx.orig); |
241 | ctx.end_occ_query (); |
232 | ctx.end_occ_query (); |
242 | } |
233 | } |
|
|
234 | else |
|
|
235 | nvis++; |
243 | } |
236 | } |
244 | else |
237 | else |
245 | { |
238 | { |
|
|
239 | if (!ctx.may_draw (e)) |
|
|
240 | continue; |
|
|
241 | |
|
|
242 | |
246 | if (evs.state != OCCLUDED) |
243 | if (evs.state != OCCLUDED) |
247 | { |
244 | { |
248 | sector center = ((e->a + e->b) >> 1) - ctx.orig; |
245 | sector center = ((e->a + e->b) >> 1) - ctx.orig; |
249 | GLfloat z = norm (vec3 (center)); |
246 | GLfloat z = length (vec3 (center)); |
250 | ctx.pixfact = ctx.perspfact / z; |
247 | ctx.pixfact = ctx.perspfact / z; |
251 | |
248 | |
|
|
249 | ctx.nz_far = max (ctx.nz_far, z + extent); |
|
|
250 | ctx.nz_near = min (ctx.nz_near, z - extent); |
|
|
251 | |
252 | if (ctx.pass == view::DEPTH || evs.last + 1. > timer.now) |
252 | if (ctx.pass->type == DEPTH || evs.last + 0.1 > timer.now) |
253 | e->draw (ctx); |
253 | e->draw (ctx); |
254 | else |
254 | else |
255 | { |
255 | { |
256 | evs.last = timer.now; |
256 | evs.last = timer.now; |
257 | ctx.begin_occ_query (*this, e); |
257 | ctx.begin_occ_query (*this, e); |
… | |
… | |
259 | ctx.end_occ_query (); |
259 | ctx.end_occ_query (); |
260 | } |
260 | } |
261 | } |
261 | } |
262 | } |
262 | } |
263 | } |
263 | } |
264 | } |
|
|
265 | } |
|
|
266 | |
264 | |
267 | #if 0 |
265 | #if 1 |
268 | void octant::draw_bbox (view &ctx) |
266 | if (ctx.pass->type == POSTDEPTH && nvis == 0 && size ()) |
269 | { |
267 | vs.state = OCCLUDED; |
270 | sector s = orig - ctx.orig; |
|
|
271 | |
|
|
272 | gl::draw_bbox (ctx, s, s + extent); |
|
|
273 | } |
|
|
274 | #endif |
268 | #endif |
|
|
269 | } |
|
|
270 | } |
275 | |
271 | |
276 | void octant::event (occ_query &ev) |
272 | void octant::event (occ_query &ev) |
277 | { |
273 | { |
278 | oct_visibility &vs = *(oct_visibility *)get_visibility (ev.ctx); |
274 | oct_visibility &vs = *(oct_visibility *)get_visibility (ev.ctx); |
279 | entity *e = (entity *)ev.id; |
275 | entity *e = (entity *)ev.id; |
… | |
… | |
282 | { |
278 | { |
283 | evis &evs = vs.vismap[e]; |
279 | evis &evs = vs.vismap[e]; |
284 | evs.state = ev.count ? FULL : OCCLUDED; |
280 | evs.state = ev.count ? FULL : OCCLUDED; |
285 | } |
281 | } |
286 | else |
282 | else |
287 | { |
|
|
288 | vs.state = ev.count ? FULL : OCCLUDED; |
283 | vs.state = ev.count ? FULL : OCCLUDED; |
289 | } |
|
|
290 | |
|
|
291 | #if 0 |
|
|
292 | visibility_state &vs = ev.v.vismap[this]; |
|
|
293 | |
|
|
294 | vs.last = timer.now; |
|
|
295 | vs.state = ev.r <= 0 |
|
|
296 | ? visibility_state::OCCLUDED |
|
|
297 | : visibility_state::FULL; |
|
|
298 | #endif |
|
|
299 | } |
284 | } |
300 | |
285 | |
301 | |
286 | |