1 |
root |
1.1 |
#include <cstdlib> |
2 |
|
|
|
3 |
root |
1.8 |
#include <vector> |
4 |
|
|
using namespace std; |
5 |
|
|
|
6 |
root |
1.48 |
#include "opengl.h" |
7 |
root |
1.8 |
|
8 |
root |
1.1 |
#include "oct.h" |
9 |
root |
1.8 |
#include "view.h" |
10 |
root |
1.2 |
#include "entity.h" |
11 |
|
|
|
12 |
root |
1.56 |
enum visibility_state { FULL, PARTIAL, SMALL, OCCLUDED }; |
13 |
|
|
|
14 |
root |
1.54 |
struct evis { |
15 |
root |
1.56 |
visibility_state state; |
16 |
root |
1.54 |
double last; // time of last check |
17 |
|
|
evis () : last(0.), state(FULL) { }; |
18 |
|
|
}; |
19 |
|
|
|
20 |
root |
1.52 |
struct oct_visibility : visibility_base |
21 |
|
|
{ |
22 |
root |
1.54 |
typedef map<entity *, evis> evismap; |
23 |
|
|
evismap vismap; |
24 |
root |
1.52 |
|
25 |
root |
1.56 |
visibility_state state; |
26 |
root |
1.52 |
|
27 |
|
|
oct_visibility (octant &oct) |
28 |
root |
1.54 |
: state(FULL) |
29 |
root |
1.52 |
{ |
30 |
|
|
} |
31 |
|
|
}; |
32 |
|
|
|
33 |
root |
1.8 |
octant world(0, sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN), MAXEXTENT); |
34 |
|
|
|
35 |
|
|
octant::octant (octant *parent, const sector &orig, uoffs extent) |
36 |
root |
1.54 |
: parent(parent) |
37 |
|
|
, orig(orig) |
38 |
|
|
, extent(extent) |
39 |
root |
1.8 |
{ |
40 |
|
|
for (fill = 8; fill--; ) |
41 |
|
|
sub[fill] = 0; |
42 |
root |
1.2 |
} |
43 |
|
|
|
44 |
root |
1.53 |
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 |
root |
1.2 |
octant::~octant () |
54 |
|
|
{ |
55 |
|
|
for (fill = 8; fill--; ) |
56 |
|
|
delete sub[fill]; |
57 |
|
|
} |
58 |
|
|
|
59 |
root |
1.32 |
static bool overlap (const sector &o1, uoffs ea, const sector &a, const sector &b) |
60 |
root |
1.4 |
{ |
61 |
|
|
ea /= 2; |
62 |
|
|
|
63 |
root |
1.34 |
sector center_1 = o1 + ea; |
64 |
|
|
sector size_2 = b - a; |
65 |
root |
1.54 |
sector center_2 = a + (size_2 >> 1); |
66 |
root |
1.33 |
|
67 |
root |
1.34 |
return abs (center_1 - center_2) <= ea + size_2; |
68 |
root |
1.4 |
} |
69 |
|
|
|
70 |
root |
1.32 |
void octant::add (entity *e) |
71 |
root |
1.2 |
{ |
72 |
root |
1.32 |
const sector &a = e->a; |
73 |
|
|
const sector &b = e->b; |
74 |
root |
1.7 |
|
75 |
root |
1.32 |
if (overlap (orig, extent, a, b)) |
76 |
root |
1.4 |
{ |
77 |
root |
1.54 |
uoffs extent2 = extent >> 1; |
78 |
root |
1.34 |
uoffs size = max (abs (b - a)); |
79 |
root |
1.32 |
|
80 |
root |
1.54 |
if (size >= extent2 >> 1) |
81 |
root |
1.8 |
{ |
82 |
|
|
push_back (e); |
83 |
|
|
e->o.push_back (this); |
84 |
|
|
return; |
85 |
|
|
} |
86 |
root |
1.5 |
|
87 |
root |
1.8 |
for (int i = 8; i--; ) |
88 |
root |
1.5 |
{ |
89 |
root |
1.8 |
sector s = orig; |
90 |
|
|
s.offset (i, extent2); |
91 |
root |
1.32 |
|
92 |
|
|
if (overlap (s, extent2, a, b)) |
93 |
root |
1.5 |
{ |
94 |
root |
1.8 |
if (!sub[i]) |
95 |
root |
1.50 |
{ |
96 |
|
|
sub[i] = new octant (this, s, extent2); |
97 |
|
|
fill++; |
98 |
|
|
} |
99 |
root |
1.8 |
|
100 |
|
|
sub[i]->add (e); |
101 |
root |
1.5 |
} |
102 |
root |
1.8 |
} |
103 |
root |
1.4 |
} |
104 |
root |
1.2 |
} |
105 |
root |
1.1 |
|
106 |
root |
1.32 |
void octant::remove (entity *e) |
107 |
root |
1.1 |
{ |
108 |
|
|
} |
109 |
|
|
|
110 |
root |
1.55 |
bool octant::depth_pass (view &ctx) |
111 |
root |
1.1 |
{ |
112 |
root |
1.52 |
oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
113 |
root |
1.50 |
|
114 |
root |
1.43 |
GLfloat extent2 = 0.5F * (GLfloat)extent; |
115 |
root |
1.51 |
sector centeri = orig + (extent >> 1) - ctx.orig; |
116 |
|
|
point centerf = point (centeri) + ((extent & 1) ? 0.5F : 0.F); |
117 |
|
|
|
118 |
root |
1.43 |
GLfloat rad = ctx.diagfact * extent2; |
119 |
|
|
|
120 |
root |
1.59 |
if (!overlap (ctx.frustum.c, sphere (centerf, rad))) |
121 |
|
|
return false; |
122 |
|
|
|
123 |
root |
1.34 |
if (orig <= ctx.orig && ctx.orig <= orig + extent) |
124 |
root |
1.56 |
vs.state = PARTIAL; |
125 |
root |
1.10 |
else |
126 |
|
|
{ |
127 |
root |
1.59 |
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 |
root |
1.25 |
|
133 |
root |
1.59 |
#if 0 |
134 |
|
|
GLfloat fd = distance (ctx.frustum.f, centerf); |
135 |
root |
1.25 |
|
136 |
root |
1.47 |
if (fd < -(ctx.c_far - ctx.z_far) -rad * 3.F) |
137 |
root |
1.52 |
return false; |
138 |
root |
1.58 |
#endif |
139 |
root |
1.10 |
} |
140 |
root |
1.8 |
|
141 |
root |
1.56 |
if (vs.state == OCCLUDED) |
142 |
|
|
return false; |
143 |
root |
1.36 |
|
144 |
root |
1.59 |
GLfloat z = ctx.z_near + distance (ctx.frustum.n, centerf) + rad; |
145 |
root |
1.58 |
if (ctx.perspfact * extent / z < 1.) // very crude "too small to see" check |
146 |
|
|
return false; |
147 |
root |
1.50 |
//printf ("z %f, perspfact %f, z*p %f\n", z, ctx.perspfact, ctx.perspfact / z); |
148 |
root |
1.45 |
|
149 |
root |
1.59 |
#if 0 |
150 |
root |
1.56 |
if (vs.state == PARTIAL || vs.state == FULL) |
151 |
root |
1.45 |
ctx.nc_far = max (ctx.nc_far, z); |
152 |
root |
1.59 |
#endif |
153 |
root |
1.45 |
|
154 |
root |
1.51 |
// 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 |
root |
1.44 |
|
159 |
root |
1.51 |
//printf ("si %d C %Ld,%Ld,%Ld\n", si, centeri.x, centeri.y, centeri.z); |
160 |
root |
1.19 |
|
161 |
|
|
// bit-toggle to find next child for front-to-back order |
162 |
root |
1.50 |
static unsigned char toggle[8+1] |
163 |
root |
1.51 |
= { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7, 0 }; |
164 |
root |
1.19 |
|
165 |
root |
1.50 |
unsigned char *next = toggle; |
166 |
|
|
do |
167 |
root |
1.19 |
{ |
168 |
root |
1.51 |
si ^= *next; |
169 |
root |
1.19 |
|
170 |
|
|
if (sub[si]) |
171 |
root |
1.55 |
sub[si]->depth_pass (ctx); |
172 |
root |
1.19 |
} |
173 |
root |
1.51 |
while (*++next); |
174 |
|
|
|
175 |
|
|
if (size () |
176 |
root |
1.56 |
&& (vs.state == PARTIAL || vs.state == FULL)) |
177 |
root |
1.51 |
{ |
178 |
root |
1.55 |
display (ctx); |
179 |
root |
1.51 |
ctx.vislist.push_back (this); |
180 |
|
|
} |
181 |
root |
1.52 |
|
182 |
|
|
return true; |
183 |
root |
1.19 |
} |
184 |
|
|
|
185 |
|
|
void octant::display (view &ctx) |
186 |
|
|
{ |
187 |
root |
1.17 |
#if 0 |
188 |
root |
1.50 |
sector s = orig - ctx.orig; |
189 |
|
|
|
190 |
root |
1.8 |
glBegin (GL_LINES); |
191 |
root |
1.13 |
vec3 clr(0, 0.8, 0); |
192 |
|
|
glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, (const GLfloat*)&clr); |
193 |
root |
1.8 |
|
194 |
|
|
for (int i = 8; i--; ) |
195 |
|
|
for (int ji = 3; ji--; ) |
196 |
|
|
{ |
197 |
|
|
int j = i | (1 << ji); |
198 |
root |
1.50 |
if (i < j) |
199 |
root |
1.8 |
{ |
200 |
|
|
glVertex3i (s.x + !!(i & 1) * extent, |
201 |
|
|
s.y + !!(i & 2) * extent, |
202 |
|
|
s.z + !!(i & 4) * extent); |
203 |
|
|
glVertex3i (s.x + !!(j & 1) * extent, |
204 |
|
|
s.y + !!(j & 2) * extent, |
205 |
|
|
s.z + !!(j & 4) * extent); |
206 |
|
|
} |
207 |
|
|
} |
208 |
|
|
|
209 |
|
|
glEnd (); |
210 |
|
|
#endif |
211 |
root |
1.54 |
oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
212 |
root |
1.8 |
|
213 |
root |
1.56 |
if (vs.state == OCCLUDED) |
214 |
root |
1.50 |
{ |
215 |
root |
1.56 |
abort (); |
216 |
|
|
if (ctx.pass == view::POSTDEPTH) |
217 |
root |
1.54 |
{ |
218 |
root |
1.56 |
ctx.begin_occ_query (*this, 0); |
219 |
root |
1.57 |
sector s = orig - ctx.orig; |
220 |
|
|
gl::draw_bbox (vb_bbox, s, s + extent); |
221 |
root |
1.56 |
ctx.end_occ_query (); |
222 |
|
|
} |
223 |
|
|
} |
224 |
|
|
else |
225 |
|
|
{ |
226 |
root |
1.57 |
for (iterator i = begin (); i != end (); ) |
227 |
root |
1.56 |
{ |
228 |
root |
1.57 |
entity *e = *i++; |
229 |
root |
1.56 |
|
230 |
|
|
if (!ctx.may_draw (e)) |
231 |
|
|
continue; |
232 |
|
|
|
233 |
|
|
evis &evs = vs.vismap[e]; |
234 |
|
|
|
235 |
|
|
if (ctx.pass == view::POSTDEPTH) |
236 |
|
|
{ |
237 |
|
|
if (evs.state == OCCLUDED) |
238 |
|
|
{ |
239 |
|
|
ctx.begin_occ_query (*this, e); |
240 |
root |
1.57 |
gl::draw_bbox (vb_bbox, e->a - ctx.orig, e->b - ctx.orig); |
241 |
root |
1.56 |
ctx.end_occ_query (); |
242 |
|
|
} |
243 |
|
|
} |
244 |
|
|
else |
245 |
|
|
{ |
246 |
|
|
if (evs.state != OCCLUDED) |
247 |
|
|
{ |
248 |
|
|
sector center = ((e->a + e->b) >> 1) - ctx.orig; |
249 |
root |
1.59 |
GLfloat z = length (vec3 (center)); |
250 |
root |
1.56 |
ctx.pixfact = ctx.perspfact / z; |
251 |
root |
1.59 |
|
252 |
|
|
ctx.nz_far = max (ctx.nz_far, z); |
253 |
root |
1.56 |
|
254 |
|
|
if (ctx.pass == view::DEPTH || evs.last + 1. > timer.now) |
255 |
|
|
e->draw (ctx); |
256 |
|
|
else |
257 |
|
|
{ |
258 |
|
|
evs.last = timer.now; |
259 |
|
|
ctx.begin_occ_query (*this, e); |
260 |
|
|
e->draw (ctx); |
261 |
|
|
ctx.end_occ_query (); |
262 |
|
|
} |
263 |
|
|
} |
264 |
|
|
} |
265 |
root |
1.54 |
} |
266 |
root |
1.50 |
} |
267 |
root |
1.1 |
} |
268 |
root |
1.8 |
|
269 |
root |
1.53 |
#if 0 |
270 |
root |
1.38 |
void octant::draw_bbox (view &ctx) |
271 |
root |
1.25 |
{ |
272 |
|
|
sector s = orig - ctx.orig; |
273 |
root |
1.27 |
|
274 |
root |
1.50 |
gl::draw_bbox (ctx, s, s + extent); |
275 |
root |
1.25 |
} |
276 |
root |
1.53 |
#endif |
277 |
root |
1.8 |
|
278 |
root |
1.38 |
void octant::event (occ_query &ev) |
279 |
|
|
{ |
280 |
root |
1.56 |
oct_visibility &vs = *(oct_visibility *)get_visibility (ev.ctx); |
281 |
|
|
entity *e = (entity *)ev.id; |
282 |
|
|
|
283 |
|
|
if (e) |
284 |
|
|
{ |
285 |
|
|
evis &evs = vs.vismap[e]; |
286 |
|
|
evs.state = ev.count ? FULL : OCCLUDED; |
287 |
|
|
} |
288 |
|
|
else |
289 |
|
|
{ |
290 |
root |
1.57 |
//vs.state = ev.count ? FULL : OCCLUDED; |
291 |
root |
1.56 |
} |
292 |
|
|
|
293 |
root |
1.52 |
#if 0 |
294 |
root |
1.44 |
visibility_state &vs = ev.v.vismap[this]; |
295 |
|
|
|
296 |
|
|
vs.last = timer.now; |
297 |
root |
1.56 |
vs.state = ev.r <= 0 |
298 |
root |
1.44 |
? visibility_state::OCCLUDED |
299 |
|
|
: visibility_state::FULL; |
300 |
root |
1.52 |
#endif |
301 |
|
|
} |
302 |
|
|
|
303 |
root |
1.2 |
|