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