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 || !extent2) |
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 | } |
… | |
… | |
59 | s.offset (i, extent2); |
91 | s.offset (i, extent2); |
60 | |
92 | |
61 | if (overlap (s, extent2, a, b)) |
93 | if (overlap (s, extent2, a, b)) |
62 | { |
94 | { |
63 | if (!sub[i]) |
95 | if (!sub[i]) |
|
|
96 | { |
64 | sub[i] = new octant (this, s, extent2); |
97 | sub[i] = new octant (this, s, extent2); |
|
|
98 | fill++; |
|
|
99 | } |
65 | |
100 | |
66 | sub[i]->add (e); |
101 | sub[i]->add (e); |
67 | } |
102 | } |
68 | } |
103 | } |
69 | } |
104 | } |
… | |
… | |
71 | |
106 | |
72 | void octant::remove (entity *e) |
107 | void octant::remove (entity *e) |
73 | { |
108 | { |
74 | } |
109 | } |
75 | |
110 | |
76 | void octant::detect_visibility (view &ctx) |
111 | bool octant::detect_visibility (view &ctx) |
77 | { |
112 | { |
78 | visibility_state &vs = ctx.vismap[this]; |
|
|
79 | |
|
|
80 | GLfloat extent2 = 0.5F * (GLfloat)extent; |
113 | GLfloat extent2 = 0.5F * (GLfloat)extent; |
81 | point center = point (orig + (extent >> 1) - ctx.orig); |
114 | sector centeri = orig + (extent >> 1) - ctx.orig; |
82 | if (extent & 1) center = center + 0.5F; |
115 | point centerf = point (centeri) + ((extent & 1) ? 0.5F : 0.F); |
|
|
116 | |
83 | GLfloat rad = ctx.diagfact * extent2; |
117 | GLfloat rad = ctx.diagfact * extent2; |
84 | |
118 | |
85 | if (vs.generation + 1 != ctx.generation) |
119 | if (!overlap (ctx.frustum.c, sphere (centerf, rad))) |
86 | vs.visibility = visibility_state::UNKNOWN; |
120 | return false; |
87 | |
121 | |
88 | vs.generation = ctx.generation; |
122 | oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
89 | |
123 | |
90 | if (orig <= ctx.orig && ctx.orig <= orig + extent) |
124 | if (orig <= ctx.orig && ctx.orig <= orig + extent) |
91 | vs.visibility = visibility_state::PARTIAL; |
125 | vs.state = PARTIAL; |
92 | else |
126 | else |
93 | { |
127 | { |
94 | if (ctx.frustum.t.distance (center) < -rad) return; |
128 | if (distance (ctx.frustum.t, centerf) < -rad) return false; |
95 | if (ctx.frustum.b.distance (center) < -rad) return; |
129 | if (distance (ctx.frustum.b, centerf) < -rad) return false; |
96 | if (ctx.frustum.l.distance (center) < -rad) return; |
130 | if (distance (ctx.frustum.l, centerf) < -rad) return false; |
97 | if (ctx.frustum.r.distance (center) < -rad) return; |
131 | if (distance (ctx.frustum.r, centerf) < -rad) return false; |
98 | if (ctx.frustum.n.distance (center) < -rad) return; |
132 | if (distance (ctx.frustum.n, centerf) < -rad) return false; |
99 | |
133 | |
|
|
134 | #if 0 |
100 | GLfloat fd = ctx.frustum.f.distance (center); |
135 | GLfloat fd = distance (ctx.frustum.f, centerf); |
101 | |
136 | |
102 | if (fd < -(ctx.c_far - ctx.z_far) -rad * 3.F) |
137 | if (fd < -(ctx.c_far - ctx.z_far) -rad * 3.F) |
103 | return; |
138 | return false; |
104 | } |
139 | #endif |
105 | |
|
|
106 | if (vs.visibility == visibility_state::OCCLUDED |
|
|
107 | || vs.visibility == visibility_state::UNKNOWN) |
|
|
108 | { |
140 | } |
|
|
141 | |
|
|
142 | #if 1 |
|
|
143 | if (vs.state == OCCLUDED) |
|
|
144 | { |
|
|
145 | if (size ()) |
109 | ctx.farlist.push_back (this); |
146 | ctx.vislist.push_back (this); |
110 | return; |
147 | //return false; |
111 | } |
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); |
112 | |
155 | |
113 | #if 0 |
156 | #if 0 |
114 | if (vs.visibility == visibility_state::UNKNOWN) |
157 | if (vs.state == PARTIAL || vs.state == FULL) |
115 | vs.visibility = visibility_state::FULL; |
|
|
116 | #endif |
|
|
117 | |
|
|
118 | GLfloat z = ctx.z_near + ctx.frustum.n.distance (center) + rad; |
|
|
119 | |
|
|
120 | if (vs.visibility == visibility_state::FULL) |
|
|
121 | ctx.nc_far = max (ctx.nc_far, z); |
158 | ctx.nc_far = max (ctx.nc_far, z); |
122 | |
159 | #endif |
123 | if (size () |
|
|
124 | && (vs.visibility == visibility_state::PARTIAL |
|
|
125 | || vs.visibility == visibility_state::FULL)) |
|
|
126 | { |
|
|
127 | ctx.nz_far = max (ctx.nz_far, z); |
|
|
128 | ctx.vislist.push_back (this); |
|
|
129 | } |
|
|
130 | |
160 | |
131 | // node to start with |
161 | // node to start with |
132 | unsigned char si = ctx.d.x < 0 ? 1 : 0 |
162 | unsigned char si = centeri.x > 0 ? 1 : 0 |
133 | | ctx.d.y < 0 ? 2 : 0 |
163 | | centeri.y > 0 ? 2 : 0 |
134 | | ctx.d.z < 0 ? 4 : 0; |
164 | | centeri.z > 0 ? 4 : 0; |
|
|
165 | |
|
|
166 | //printf ("si %d C %Ld,%Ld,%Ld\n", si, centeri.x, centeri.y, centeri.z); |
135 | |
167 | |
136 | // bit-toggle to find next child for front-to-back order |
168 | // bit-toggle to find next child for front-to-back order |
137 | static unsigned char next[8] |
169 | static unsigned char toggle[8+1] |
138 | = { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7 }; |
170 | = { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7, 0 }; |
139 | |
171 | |
140 | for (int i = 0; i < 8; i++) |
172 | unsigned char *next = toggle; |
|
|
173 | do |
141 | { |
174 | { |
142 | si ^= next[i]; |
175 | si ^= *next; |
143 | |
176 | |
144 | if (sub[si]) |
177 | if (sub[si]) |
145 | sub[si]->detect_visibility (ctx); |
178 | sub[si]->detect_visibility (ctx); |
146 | } |
179 | } |
|
|
180 | while (*++next); |
|
|
181 | |
|
|
182 | if (size () |
|
|
183 | && (vs.state == PARTIAL || vs.state == FULL || vs.state == OCCLUDED)) |
|
|
184 | ctx.vislist.push_back (this); |
|
|
185 | |
|
|
186 | return true; |
147 | } |
187 | } |
148 | |
188 | |
149 | void octant::display (view &ctx) |
189 | void octant::display (view &ctx) |
150 | { |
190 | { |
151 | #if 0 |
191 | #if 0 |
|
|
192 | sector s = orig - ctx.orig; |
|
|
193 | |
152 | glBegin (GL_LINES); |
194 | glBegin (GL_LINES); |
153 | sector s = orig - ctx.orig; |
|
|
154 | vec3 clr(0, 0.8, 0); |
195 | vec3 clr(0, 0.8, 0); |
155 | glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, (const GLfloat*)&clr); |
196 | glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, (const GLfloat*)&clr); |
156 | |
197 | |
157 | for (int i = 8; i--; ) |
198 | for (int i = 8; i--; ) |
158 | for (int ji = 3; ji--; ) |
199 | for (int ji = 3; ji--; ) |
159 | { |
200 | { |
160 | int j = i | (1 << ji); |
201 | int j = i | (1 << ji); |
161 | if (i != j) |
202 | if (i < j) |
162 | { |
203 | { |
163 | glVertex3i (s.x + !!(i & 1) * extent, |
204 | glVertex3i (s.x + !!(i & 1) * extent, |
164 | s.y + !!(i & 2) * extent, |
205 | s.y + !!(i & 2) * extent, |
165 | s.z + !!(i & 4) * extent); |
206 | s.z + !!(i & 4) * extent); |
166 | glVertex3i (s.x + !!(j & 1) * extent, |
207 | glVertex3i (s.x + !!(j & 1) * extent, |
… | |
… | |
169 | } |
210 | } |
170 | } |
211 | } |
171 | |
212 | |
172 | glEnd (); |
213 | glEnd (); |
173 | #endif |
214 | #endif |
|
|
215 | oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
174 | |
216 | |
175 | for (iterator i = end (); i != begin (); ) |
217 | if (vs.state == OCCLUDED) |
176 | (*--i)->display (ctx); |
218 | { |
177 | } |
219 | if (ctx.pass == view::POSTDEPTH) |
178 | |
220 | { |
179 | void octant::draw_bbox (view &ctx) |
221 | ctx.begin_occ_query (*this, 0); |
180 | { |
|
|
181 | sector s = orig - ctx.orig; |
222 | sector s = orig - ctx.orig; |
182 | |
|
|
183 | gl::draw_box (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 | } |
184 | } |
281 | } |
185 | |
282 | |
186 | void octant::event (occ_query &ev) |
283 | void octant::event (occ_query &ev) |
187 | { |
284 | { |
188 | visibility_state &vs = ev.v.vismap[this]; |
285 | oct_visibility &vs = *(oct_visibility *)get_visibility (ev.ctx); |
|
|
286 | entity *e = (entity *)ev.id; |
189 | |
287 | |
190 | vs.last = timer.now; |
288 | if (e) |
191 | vs.visibility = ev.r <= 0 |
289 | { |
192 | ? visibility_state::OCCLUDED |
290 | evis &evs = vs.vismap[e]; |
193 | : visibility_state::FULL; |
291 | evs.state = ev.count ? FULL : OCCLUDED; |
194 | //if (extent > 0x16) |
292 | } |
195 | //printf ("OCT(%x,%x,%x+%x) samples %d\n", orig.x, orig.y, orig.z, extent, ev.r); |
293 | else |
|
|
294 | vs.state = ev.count ? FULL : OCCLUDED; |
196 | } |
295 | } |
197 | |
296 | |
|
|
297 | |