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.72 |
enum visibility_state { FULL, PARTIAL, SMALL, OCCLUDED, SUBTREE_OCCLUDED }; |
13 |
root |
1.56 |
|
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.65 |
|
54 |
root |
1.2 |
octant::~octant () |
55 |
|
|
{ |
56 |
|
|
for (fill = 8; fill--; ) |
57 |
|
|
delete sub[fill]; |
58 |
|
|
} |
59 |
|
|
|
60 |
root |
1.32 |
static bool overlap (const sector &o1, uoffs ea, const sector &a, const sector &b) |
61 |
root |
1.4 |
{ |
62 |
|
|
ea /= 2; |
63 |
|
|
|
64 |
root |
1.34 |
sector center_1 = o1 + ea; |
65 |
|
|
sector size_2 = b - a; |
66 |
root |
1.54 |
sector center_2 = a + (size_2 >> 1); |
67 |
root |
1.33 |
|
68 |
root |
1.34 |
return abs (center_1 - center_2) <= ea + size_2; |
69 |
root |
1.4 |
} |
70 |
|
|
|
71 |
root |
1.32 |
void octant::add (entity *e) |
72 |
root |
1.2 |
{ |
73 |
root |
1.32 |
const sector &a = e->a; |
74 |
|
|
const sector &b = e->b; |
75 |
root |
1.7 |
|
76 |
root |
1.32 |
if (overlap (orig, extent, a, b)) |
77 |
root |
1.4 |
{ |
78 |
root |
1.54 |
uoffs extent2 = extent >> 1; |
79 |
root |
1.34 |
uoffs size = max (abs (b - a)); |
80 |
root |
1.32 |
|
81 |
root |
1.54 |
if (size >= extent2 >> 1) |
82 |
root |
1.8 |
{ |
83 |
|
|
push_back (e); |
84 |
|
|
e->o.push_back (this); |
85 |
|
|
return; |
86 |
|
|
} |
87 |
root |
1.5 |
|
88 |
root |
1.8 |
for (int i = 8; i--; ) |
89 |
root |
1.5 |
{ |
90 |
root |
1.8 |
sector s = orig; |
91 |
|
|
s.offset (i, extent2); |
92 |
root |
1.32 |
|
93 |
|
|
if (overlap (s, extent2, a, b)) |
94 |
root |
1.5 |
{ |
95 |
root |
1.8 |
if (!sub[i]) |
96 |
root |
1.50 |
{ |
97 |
|
|
sub[i] = new octant (this, s, extent2); |
98 |
|
|
fill++; |
99 |
|
|
} |
100 |
root |
1.8 |
|
101 |
|
|
sub[i]->add (e); |
102 |
root |
1.5 |
} |
103 |
root |
1.8 |
} |
104 |
root |
1.4 |
} |
105 |
root |
1.2 |
} |
106 |
root |
1.1 |
|
107 |
root |
1.32 |
void octant::remove (entity *e) |
108 |
root |
1.1 |
{ |
109 |
|
|
} |
110 |
|
|
|
111 |
root |
1.62 |
bool octant::detect_visibility (view &ctx) |
112 |
root |
1.1 |
{ |
113 |
root |
1.43 |
GLfloat extent2 = 0.5F * (GLfloat)extent; |
114 |
root |
1.51 |
sector centeri = orig + (extent >> 1) - ctx.orig; |
115 |
|
|
point centerf = point (centeri) + ((extent & 1) ? 0.5F : 0.F); |
116 |
|
|
|
117 |
root |
1.43 |
GLfloat rad = ctx.diagfact * extent2; |
118 |
|
|
|
119 |
root |
1.59 |
if (!overlap (ctx.frustum.c, sphere (centerf, rad))) |
120 |
|
|
return false; |
121 |
root |
1.65 |
|
122 |
|
|
oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
123 |
root |
1.59 |
|
124 |
root |
1.34 |
if (orig <= ctx.orig && ctx.orig <= orig + extent) |
125 |
root |
1.56 |
vs.state = PARTIAL; |
126 |
root |
1.10 |
else |
127 |
|
|
{ |
128 |
root |
1.59 |
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 |
root |
1.25 |
|
134 |
root |
1.59 |
#if 0 |
135 |
|
|
GLfloat fd = distance (ctx.frustum.f, centerf); |
136 |
root |
1.25 |
|
137 |
root |
1.47 |
if (fd < -(ctx.c_far - ctx.z_far) -rad * 3.F) |
138 |
root |
1.52 |
return false; |
139 |
root |
1.58 |
#endif |
140 |
root |
1.10 |
} |
141 |
root |
1.8 |
|
142 |
root |
1.68 |
#if 0 |
143 |
root |
1.69 |
if (vs.state == OCCLUDED && size ()) |
144 |
root |
1.64 |
{ |
145 |
|
|
if (size ()) |
146 |
|
|
ctx.vislist.push_back (this); |
147 |
root |
1.69 |
return false; |
148 |
root |
1.64 |
} |
149 |
root |
1.66 |
#endif |
150 |
root |
1.36 |
|
151 |
root |
1.59 |
GLfloat z = ctx.z_near + distance (ctx.frustum.n, centerf) + rad; |
152 |
root |
1.58 |
if (ctx.perspfact * extent / z < 1.) // very crude "too small to see" check |
153 |
|
|
return false; |
154 |
root |
1.50 |
//printf ("z %f, perspfact %f, z*p %f\n", z, ctx.perspfact, ctx.perspfact / z); |
155 |
root |
1.45 |
|
156 |
root |
1.59 |
#if 0 |
157 |
root |
1.56 |
if (vs.state == PARTIAL || vs.state == FULL) |
158 |
root |
1.45 |
ctx.nc_far = max (ctx.nc_far, z); |
159 |
root |
1.59 |
#endif |
160 |
root |
1.45 |
|
161 |
root |
1.72 |
if (vs.state == SUBTREE_OCCLUDED) |
162 |
|
|
{ |
163 |
|
|
ctx.vislist.push_back (this); |
164 |
|
|
return false; |
165 |
|
|
} |
166 |
|
|
|
167 |
root |
1.51 |
// node to start with |
168 |
|
|
unsigned char si = centeri.x > 0 ? 1 : 0 |
169 |
|
|
| centeri.y > 0 ? 2 : 0 |
170 |
|
|
| centeri.z > 0 ? 4 : 0; |
171 |
root |
1.19 |
|
172 |
|
|
// bit-toggle to find next child for front-to-back order |
173 |
root |
1.50 |
static unsigned char toggle[8+1] |
174 |
root |
1.51 |
= { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7, 0 }; |
175 |
root |
1.19 |
|
176 |
root |
1.73 |
bool visible = size () && (vs.state == PARTIAL || vs.state == FULL); |
177 |
root |
1.69 |
|
178 |
root |
1.50 |
unsigned char *next = toggle; |
179 |
|
|
do |
180 |
root |
1.19 |
{ |
181 |
root |
1.51 |
si ^= *next; |
182 |
root |
1.19 |
|
183 |
|
|
if (sub[si]) |
184 |
root |
1.69 |
visible = visible | sub[si]->detect_visibility (ctx); |
185 |
root |
1.19 |
} |
186 |
root |
1.51 |
while (*++next); |
187 |
|
|
|
188 |
root |
1.71 |
if (visible) |
189 |
|
|
{ |
190 |
|
|
if (size ()) |
191 |
|
|
ctx.vislist.push_back (this); |
192 |
|
|
} |
193 |
|
|
else |
194 |
|
|
{ |
195 |
root |
1.72 |
vs.state = SUBTREE_OCCLUDED; |
196 |
root |
1.71 |
ctx.vislist.push_back (this); |
197 |
|
|
} |
198 |
root |
1.52 |
|
199 |
root |
1.69 |
return visible; |
200 |
root |
1.19 |
} |
201 |
|
|
|
202 |
|
|
void octant::display (view &ctx) |
203 |
|
|
{ |
204 |
root |
1.54 |
oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
205 |
root |
1.8 |
|
206 |
root |
1.72 |
if (vs.state == OCCLUDED || vs.state == SUBTREE_OCCLUDED) |
207 |
root |
1.50 |
{ |
208 |
root |
1.70 |
if (ctx.pass_type == view::POSTDEPTH) |
209 |
root |
1.54 |
{ |
210 |
root |
1.56 |
ctx.begin_occ_query (*this, 0); |
211 |
root |
1.57 |
sector s = orig - ctx.orig; |
212 |
root |
1.61 |
gl::draw_bbox (s, s + extent); |
213 |
root |
1.56 |
ctx.end_occ_query (); |
214 |
|
|
} |
215 |
|
|
} |
216 |
|
|
else |
217 |
|
|
{ |
218 |
root |
1.64 |
int nvis = 0; |
219 |
|
|
|
220 |
root |
1.57 |
for (iterator i = begin (); i != end (); ) |
221 |
root |
1.56 |
{ |
222 |
root |
1.57 |
entity *e = *i++; |
223 |
root |
1.56 |
|
224 |
|
|
evis &evs = vs.vismap[e]; |
225 |
|
|
|
226 |
root |
1.70 |
if (ctx.pass_type == view::POSTDEPTH) |
227 |
root |
1.56 |
{ |
228 |
|
|
if (evs.state == OCCLUDED) |
229 |
|
|
{ |
230 |
|
|
ctx.begin_occ_query (*this, e); |
231 |
root |
1.61 |
gl::draw_bbox (e->a - ctx.orig, e->b - ctx.orig); |
232 |
root |
1.56 |
ctx.end_occ_query (); |
233 |
|
|
} |
234 |
root |
1.64 |
else |
235 |
|
|
nvis++; |
236 |
root |
1.56 |
} |
237 |
|
|
else |
238 |
|
|
{ |
239 |
root |
1.66 |
if (!ctx.may_draw (e)) |
240 |
|
|
continue; |
241 |
|
|
|
242 |
|
|
|
243 |
root |
1.56 |
if (evs.state != OCCLUDED) |
244 |
|
|
{ |
245 |
|
|
sector center = ((e->a + e->b) >> 1) - ctx.orig; |
246 |
root |
1.59 |
GLfloat z = length (vec3 (center)); |
247 |
root |
1.56 |
ctx.pixfact = ctx.perspfact / z; |
248 |
root |
1.59 |
|
249 |
root |
1.60 |
ctx.nz_far = max (ctx.nz_far, z + extent); |
250 |
|
|
ctx.nz_near = min (ctx.nz_near, z - extent); |
251 |
root |
1.56 |
|
252 |
root |
1.72 |
if (ctx.pass_type == view::DEPTH || evs.last + 0.1 > timer.now) |
253 |
root |
1.56 |
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 |
root |
1.54 |
} |
264 |
root |
1.64 |
|
265 |
root |
1.66 |
#if 1 |
266 |
root |
1.70 |
if (ctx.pass_type == view::POSTDEPTH && nvis == 0 && size ()) |
267 |
root |
1.64 |
vs.state = OCCLUDED; |
268 |
|
|
#endif |
269 |
root |
1.50 |
} |
270 |
root |
1.1 |
} |
271 |
root |
1.8 |
|
272 |
root |
1.38 |
void octant::event (occ_query &ev) |
273 |
|
|
{ |
274 |
root |
1.56 |
oct_visibility &vs = *(oct_visibility *)get_visibility (ev.ctx); |
275 |
|
|
entity *e = (entity *)ev.id; |
276 |
|
|
|
277 |
|
|
if (e) |
278 |
|
|
{ |
279 |
|
|
evis &evs = vs.vismap[e]; |
280 |
|
|
evs.state = ev.count ? FULL : OCCLUDED; |
281 |
|
|
} |
282 |
|
|
else |
283 |
root |
1.64 |
vs.state = ev.count ? FULL : OCCLUDED; |
284 |
root |
1.52 |
} |
285 |
|
|
|
286 |
root |
1.2 |
|