1 |
#include <cstdlib> |
2 |
|
3 |
#include <vector> |
4 |
using namespace std; |
5 |
|
6 |
#include "opengl.h" |
7 |
|
8 |
#include "oct.h" |
9 |
#include "view.h" |
10 |
#include "entity.h" |
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 |
|
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 (); |
52 |
} |
53 |
|
54 |
octant::~octant () |
55 |
{ |
56 |
for (fill = 8; fill--; ) |
57 |
delete sub[fill]; |
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) |
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 |
//printf ("si %d C %Ld,%Ld,%Ld\n", si, centeri.x, centeri.y, centeri.z); |
167 |
|
168 |
// bit-toggle to find next child for front-to-back order |
169 |
static unsigned char toggle[8+1] |
170 |
= { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7, 0 }; |
171 |
|
172 |
unsigned char *next = toggle; |
173 |
do |
174 |
{ |
175 |
si ^= *next; |
176 |
|
177 |
if (sub[si]) |
178 |
sub[si]->detect_visibility (ctx); |
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; |
187 |
} |
188 |
|
189 |
void octant::display (view &ctx) |
190 |
{ |
191 |
#if 0 |
192 |
sector s = orig - ctx.orig; |
193 |
|
194 |
glBegin (GL_LINES); |
195 |
vec3 clr(0, 0.8, 0); |
196 |
glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, (const GLfloat*)&clr); |
197 |
|
198 |
for (int i = 8; i--; ) |
199 |
for (int ji = 3; ji--; ) |
200 |
{ |
201 |
int j = i | (1 << ji); |
202 |
if (i < j) |
203 |
{ |
204 |
glVertex3i (s.x + !!(i & 1) * extent, |
205 |
s.y + !!(i & 2) * extent, |
206 |
s.z + !!(i & 4) * extent); |
207 |
glVertex3i (s.x + !!(j & 1) * extent, |
208 |
s.y + !!(j & 2) * extent, |
209 |
s.z + !!(j & 4) * extent); |
210 |
} |
211 |
} |
212 |
|
213 |
glEnd (); |
214 |
#endif |
215 |
oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
216 |
|
217 |
if (vs.state == OCCLUDED) |
218 |
{ |
219 |
if (ctx.pass == view::POSTDEPTH) |
220 |
{ |
221 |
ctx.begin_occ_query (*this, 0); |
222 |
sector s = orig - ctx.orig; |
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 |
} |
281 |
} |
282 |
|
283 |
void octant::event (occ_query &ev) |
284 |
{ |
285 |
oct_visibility &vs = *(oct_visibility *)get_visibility (ev.ctx); |
286 |
entity *e = (entity *)ev.id; |
287 |
|
288 |
if (e) |
289 |
{ |
290 |
evis &evs = vs.vismap[e]; |
291 |
evs.state = ev.count ? FULL : OCCLUDED; |
292 |
} |
293 |
else |
294 |
vs.state = ev.count ? FULL : OCCLUDED; |
295 |
} |
296 |
|
297 |
|