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