1 | #include <cstdlib> |
1 | #include <cstdlib> |
2 | |
2 | |
|
|
3 | #include <vector> |
|
|
4 | using namespace std; |
|
|
5 | |
|
|
6 | #include "opengl.h" |
|
|
7 | |
3 | #include "oct.h" |
8 | #include "oct.h" |
|
|
9 | #include "view.h" |
|
|
10 | #include "entity.h" |
4 | |
11 | |
5 | void octant::remove (entity_base *e) |
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 |
6 | { |
21 | { |
7 | abort (); |
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 | |
|
|
33 | octant world(0, sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN), MAXEXTENT); |
|
|
34 | |
|
|
35 | octant::octant (octant *parent, const sector &orig, uoffs extent) |
|
|
36 | : parent(parent) |
|
|
37 | , orig(orig) |
|
|
38 | , extent(extent) |
|
|
39 | { |
|
|
40 | for (fill = 8; fill--; ) |
|
|
41 | sub[fill] = 0; |
|
|
42 | } |
|
|
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 (); |
8 | } |
52 | } |
9 | |
53 | |
10 | octant::~octant () |
54 | octant::~octant () |
11 | { |
55 | { |
12 | for (int i = 8; i--; ) |
56 | for (fill = 8; fill--; ) |
13 | delete sub[i]; |
57 | delete sub[fill]; |
14 | } |
58 | } |
|
|
59 | |
|
|
60 | static bool overlap (const sector &o1, uoffs ea, const sector &a, const sector &b) |
|
|
61 | { |
|
|
62 | ea /= 2; |
|
|
63 | |
|
|
64 | sector center_1 = o1 + ea; |
|
|
65 | sector size_2 = b - a; |
|
|
66 | sector center_2 = a + (size_2 >> 1); |
|
|
67 | |
|
|
68 | return abs (center_1 - center_2) <= ea + size_2; |
|
|
69 | } |
|
|
70 | |
|
|
71 | void octant::add (entity *e) |
|
|
72 | { |
|
|
73 | const sector &a = e->a; |
|
|
74 | const sector &b = e->b; |
|
|
75 | |
|
|
76 | if (overlap (orig, extent, a, b)) |
|
|
77 | { |
|
|
78 | uoffs extent2 = extent >> 1; |
|
|
79 | uoffs size = max (abs (b - a)); |
|
|
80 | |
|
|
81 | if (size >= extent2 >> 1) |
|
|
82 | { |
|
|
83 | push_back (e); |
|
|
84 | e->o.push_back (this); |
|
|
85 | return; |
|
|
86 | } |
|
|
87 | |
|
|
88 | for (int i = 8; i--; ) |
|
|
89 | { |
|
|
90 | sector s = orig; |
|
|
91 | s.offset (i, extent2); |
|
|
92 | |
|
|
93 | if (overlap (s, extent2, a, b)) |
|
|
94 | { |
|
|
95 | if (!sub[i]) |
|
|
96 | { |
|
|
97 | sub[i] = new octant (this, s, extent2); |
|
|
98 | fill++; |
|
|
99 | } |
|
|
100 | |
|
|
101 | sub[i]->add (e); |
|
|
102 | } |
|
|
103 | } |
|
|
104 | } |
|
|
105 | } |
|
|
106 | |
|
|
107 | void octant::remove (entity *e) |
|
|
108 | { |
|
|
109 | } |
|
|
110 | |
|
|
111 | bool octant::detect_visibility (view &ctx) |
|
|
112 | { |
|
|
113 | GLfloat extent2 = 0.5F * (GLfloat)extent; |
|
|
114 | sector centeri = orig + (extent >> 1) - ctx.orig; |
|
|
115 | point centerf = point (centeri) + ((extent & 1) ? 0.5F : 0.F); |
|
|
116 | |
|
|
117 | GLfloat rad = ctx.diagfact * extent2; |
|
|
118 | |
|
|
119 | if (!overlap (ctx.frustum.c, sphere (centerf, rad))) |
|
|
120 | return false; |
|
|
121 | |
|
|
122 | oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
|
|
123 | |
|
|
124 | if (orig <= ctx.orig && ctx.orig <= orig + extent) |
|
|
125 | vs.state = PARTIAL; |
|
|
126 | else |
|
|
127 | { |
|
|
128 | if (distance (ctx.frustum.t, centerf) < -rad) return false; |
|
|
129 | if (distance (ctx.frustum.b, centerf) < -rad) return false; |
|
|
130 | if (distance (ctx.frustum.l, centerf) < -rad) return false; |
|
|
131 | if (distance (ctx.frustum.r, centerf) < -rad) return false; |
|
|
132 | if (distance (ctx.frustum.n, centerf) < -rad) return false; |
|
|
133 | |
|
|
134 | #if 0 |
|
|
135 | GLfloat fd = distance (ctx.frustum.f, centerf); |
|
|
136 | |
|
|
137 | if (fd < -(ctx.c_far - ctx.z_far) -rad * 3.F) |
|
|
138 | return false; |
|
|
139 | #endif |
|
|
140 | } |
|
|
141 | |
|
|
142 | #if 0 |
|
|
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 |
|
|
153 | return false; |
|
|
154 | //printf ("z %f, perspfact %f, z*p %f\n", z, ctx.perspfact, ctx.perspfact / z); |
|
|
155 | |
|
|
156 | #if 0 |
|
|
157 | if (vs.state == PARTIAL || vs.state == FULL) |
|
|
158 | ctx.nc_far = max (ctx.nc_far, z); |
|
|
159 | #endif |
|
|
160 | |
|
|
161 | // node to start with |
|
|
162 | unsigned char si = centeri.x > 0 ? 1 : 0 |
|
|
163 | | centeri.y > 0 ? 2 : 0 |
|
|
164 | | centeri.z > 0 ? 4 : 0; |
|
|
165 | |
|
|
166 | // bit-toggle to find next child for front-to-back order |
|
|
167 | static unsigned char toggle[8+1] |
|
|
168 | = { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7, 0 }; |
|
|
169 | |
|
|
170 | bool visible = vs.state == PARTIAL || vs.state == FULL; |
|
|
171 | |
|
|
172 | unsigned char *next = toggle; |
|
|
173 | do |
|
|
174 | { |
|
|
175 | si ^= *next; |
|
|
176 | |
|
|
177 | if (sub[si]) |
|
|
178 | visible = visible | sub[si]->detect_visibility (ctx); |
|
|
179 | } |
|
|
180 | while (*++next); |
|
|
181 | |
|
|
182 | if (visible) |
|
|
183 | { |
|
|
184 | if (size ()) |
|
|
185 | ctx.vislist.push_back (this); |
|
|
186 | } |
|
|
187 | else |
|
|
188 | { |
|
|
189 | vs.state = OCCLUDED; |
|
|
190 | ctx.vislist.push_back (this); |
|
|
191 | } |
|
|
192 | |
|
|
193 | return visible; |
|
|
194 | } |
|
|
195 | |
|
|
196 | void octant::display (view &ctx) |
|
|
197 | { |
|
|
198 | oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
|
|
199 | |
|
|
200 | if (vs.state == OCCLUDED) |
|
|
201 | { |
|
|
202 | if (ctx.pass_type == view::POSTDEPTH) |
|
|
203 | { |
|
|
204 | ctx.begin_occ_query (*this, 0); |
|
|
205 | sector s = orig - ctx.orig; |
|
|
206 | gl::draw_bbox (s, s + extent); |
|
|
207 | ctx.end_occ_query (); |
|
|
208 | } |
|
|
209 | } |
|
|
210 | else |
|
|
211 | { |
|
|
212 | int nvis = 0; |
|
|
213 | |
|
|
214 | for (iterator i = begin (); i != end (); ) |
|
|
215 | { |
|
|
216 | entity *e = *i++; |
|
|
217 | |
|
|
218 | evis &evs = vs.vismap[e]; |
|
|
219 | |
|
|
220 | if (ctx.pass_type == view::POSTDEPTH) |
|
|
221 | { |
|
|
222 | if (evs.state == OCCLUDED) |
|
|
223 | { |
|
|
224 | ctx.begin_occ_query (*this, e); |
|
|
225 | gl::draw_bbox (e->a - ctx.orig, e->b - ctx.orig); |
|
|
226 | ctx.end_occ_query (); |
|
|
227 | } |
|
|
228 | else |
|
|
229 | nvis++; |
|
|
230 | } |
|
|
231 | else |
|
|
232 | { |
|
|
233 | if (!ctx.may_draw (e)) |
|
|
234 | continue; |
|
|
235 | |
|
|
236 | |
|
|
237 | if (evs.state != OCCLUDED) |
|
|
238 | { |
|
|
239 | sector center = ((e->a + e->b) >> 1) - ctx.orig; |
|
|
240 | GLfloat z = length (vec3 (center)); |
|
|
241 | ctx.pixfact = ctx.perspfact / z; |
|
|
242 | |
|
|
243 | ctx.nz_far = max (ctx.nz_far, z + extent); |
|
|
244 | ctx.nz_near = min (ctx.nz_near, z - extent); |
|
|
245 | |
|
|
246 | if (ctx.pass_type == view::DEPTH || evs.last + 0.3 > timer.now) |
|
|
247 | e->draw (ctx); |
|
|
248 | else |
|
|
249 | { |
|
|
250 | evs.last = timer.now; |
|
|
251 | ctx.begin_occ_query (*this, e); |
|
|
252 | e->draw (ctx); |
|
|
253 | ctx.end_occ_query (); |
|
|
254 | } |
|
|
255 | } |
|
|
256 | } |
|
|
257 | } |
|
|
258 | |
|
|
259 | #if 1 |
|
|
260 | if (ctx.pass_type == view::POSTDEPTH && nvis == 0 && size ()) |
|
|
261 | vs.state = OCCLUDED; |
|
|
262 | #endif |
|
|
263 | } |
|
|
264 | } |
|
|
265 | |
|
|
266 | void octant::event (occ_query &ev) |
|
|
267 | { |
|
|
268 | oct_visibility &vs = *(oct_visibility *)get_visibility (ev.ctx); |
|
|
269 | entity *e = (entity *)ev.id; |
|
|
270 | |
|
|
271 | if (e) |
|
|
272 | { |
|
|
273 | evis &evs = vs.vismap[e]; |
|
|
274 | evs.state = ev.count ? FULL : OCCLUDED; |
|
|
275 | } |
|
|
276 | else |
|
|
277 | vs.state = ev.count ? FULL : OCCLUDED; |
|
|
278 | } |
|
|
279 | |
|
|
280 | |