1 |
root |
1.1 |
#include <cstdlib> |
2 |
|
|
|
3 |
root |
1.8 |
#include <vector> |
4 |
|
|
|
5 |
|
|
using namespace std; |
6 |
|
|
|
7 |
|
|
#include <GL/gl.h> |
8 |
|
|
|
9 |
root |
1.1 |
#include "oct.h" |
10 |
root |
1.8 |
#include "view.h" |
11 |
root |
1.2 |
#include "entity.h" |
12 |
|
|
|
13 |
root |
1.8 |
octant world(0, sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN), MAXEXTENT); |
14 |
|
|
|
15 |
|
|
octant::octant (octant *parent, const sector &orig, uoffs extent) |
16 |
|
|
: parent(parent), orig(orig), extent(extent) |
17 |
|
|
{ |
18 |
|
|
for (fill = 8; fill--; ) |
19 |
|
|
sub[fill] = 0; |
20 |
root |
1.2 |
} |
21 |
|
|
|
22 |
|
|
octant::~octant () |
23 |
|
|
{ |
24 |
|
|
for (fill = 8; fill--; ) |
25 |
|
|
delete sub[fill]; |
26 |
|
|
} |
27 |
|
|
|
28 |
root |
1.32 |
static bool overlap (const sector &o1, uoffs ea, const sector &a, const sector &b) |
29 |
root |
1.4 |
{ |
30 |
|
|
ea /= 2; |
31 |
|
|
|
32 |
root |
1.34 |
sector center_1 = o1 + ea; |
33 |
|
|
sector size_2 = b - a; |
34 |
|
|
sector center_2 = a + size_2 / 2; |
35 |
root |
1.33 |
|
36 |
root |
1.34 |
return abs (center_1 - center_2) <= ea + size_2; |
37 |
root |
1.4 |
} |
38 |
|
|
|
39 |
root |
1.32 |
void octant::add (entity *e) |
40 |
root |
1.2 |
{ |
41 |
root |
1.32 |
const sector &a = e->a; |
42 |
|
|
const sector &b = e->b; |
43 |
root |
1.7 |
|
44 |
root |
1.32 |
if (overlap (orig, extent, a, b)) |
45 |
root |
1.4 |
{ |
46 |
root |
1.8 |
uoffs extent2 = extent / 2; |
47 |
root |
1.34 |
uoffs size = max (abs (b - a)); |
48 |
root |
1.32 |
|
49 |
root |
1.8 |
if (size > extent2 || !extent2) |
50 |
|
|
{ |
51 |
|
|
push_back (e); |
52 |
|
|
e->o.push_back (this); |
53 |
|
|
return; |
54 |
|
|
} |
55 |
root |
1.5 |
|
56 |
root |
1.8 |
for (int i = 8; i--; ) |
57 |
root |
1.5 |
{ |
58 |
root |
1.8 |
sector s = orig; |
59 |
|
|
s.offset (i, extent2); |
60 |
root |
1.32 |
|
61 |
|
|
if (overlap (s, extent2, a, b)) |
62 |
root |
1.5 |
{ |
63 |
root |
1.8 |
if (!sub[i]) |
64 |
|
|
sub[i] = new octant (this, s, extent2); |
65 |
|
|
|
66 |
|
|
sub[i]->add (e); |
67 |
root |
1.5 |
} |
68 |
root |
1.8 |
} |
69 |
root |
1.4 |
} |
70 |
root |
1.2 |
} |
71 |
root |
1.1 |
|
72 |
root |
1.32 |
void octant::remove (entity *e) |
73 |
root |
1.1 |
{ |
74 |
|
|
} |
75 |
|
|
|
76 |
root |
1.19 |
void octant::detect_visibility (view &ctx) |
77 |
root |
1.1 |
{ |
78 |
root |
1.8 |
visibility_state &vs = ctx.vismap[this]; |
79 |
|
|
|
80 |
root |
1.43 |
GLfloat extent2 = 0.5F * (GLfloat)extent; |
81 |
|
|
point center = point (orig + (extent >> 1) - ctx.orig); |
82 |
|
|
if (extent & 1) center = center + 0.5F; |
83 |
|
|
GLfloat rad = ctx.diagfact * extent2; |
84 |
|
|
|
85 |
root |
1.45 |
if (vs.generation + 1 != ctx.generation) |
86 |
root |
1.10 |
vs.visibility = visibility_state::UNKNOWN; |
87 |
|
|
|
88 |
root |
1.43 |
vs.generation = ctx.generation; |
89 |
|
|
|
90 |
root |
1.34 |
if (orig <= ctx.orig && ctx.orig <= orig + extent) |
91 |
root |
1.43 |
vs.visibility = visibility_state::PARTIAL; |
92 |
root |
1.10 |
else |
93 |
|
|
{ |
94 |
root |
1.37 |
if (ctx.frustum.t.distance (center) < -rad) return; |
95 |
|
|
if (ctx.frustum.b.distance (center) < -rad) return; |
96 |
|
|
if (ctx.frustum.l.distance (center) < -rad) return; |
97 |
|
|
if (ctx.frustum.r.distance (center) < -rad) return; |
98 |
|
|
if (ctx.frustum.n.distance (center) < -rad) return; |
99 |
root |
1.25 |
|
100 |
|
|
GLfloat fd = ctx.frustum.f.distance (center); |
101 |
|
|
|
102 |
root |
1.37 |
if (fd < -rad) |
103 |
root |
1.25 |
{ |
104 |
root |
1.46 |
if (fd < -(ctx.c_far - ctx.z_far) -rad * 3.F) |
105 |
root |
1.26 |
return; |
106 |
|
|
|
107 |
root |
1.40 |
if (vs.visibility == visibility_state::UNKNOWN) |
108 |
|
|
{ |
109 |
|
|
ctx.farlist.push_back (this); |
110 |
|
|
return; |
111 |
|
|
} |
112 |
root |
1.25 |
} |
113 |
root |
1.10 |
} |
114 |
root |
1.8 |
|
115 |
root |
1.46 |
if (vs.visibility == visibility_state::OCCLUDED |
116 |
|
|
|| vs.visibility == visibility_state::UNKNOWN) |
117 |
root |
1.45 |
{ |
118 |
|
|
ctx.farlist.push_back (this); |
119 |
|
|
return; |
120 |
|
|
} |
121 |
|
|
|
122 |
root |
1.46 |
#if 0 |
123 |
root |
1.36 |
if (vs.visibility == visibility_state::UNKNOWN) |
124 |
|
|
vs.visibility = visibility_state::FULL; |
125 |
root |
1.46 |
#endif |
126 |
root |
1.36 |
|
127 |
root |
1.45 |
GLfloat z = ctx.z_near + ctx.frustum.n.distance (center) + rad; |
128 |
|
|
|
129 |
|
|
if (vs.visibility == visibility_state::FULL) |
130 |
|
|
ctx.nc_far = max (ctx.nc_far, z); |
131 |
|
|
|
132 |
root |
1.41 |
if (size () |
133 |
|
|
&& (vs.visibility == visibility_state::PARTIAL |
134 |
|
|
|| vs.visibility == visibility_state::FULL)) |
135 |
root |
1.43 |
{ |
136 |
root |
1.45 |
ctx.nz_far = max (ctx.nz_far, z); |
137 |
root |
1.43 |
ctx.vislist.push_back (this); |
138 |
root |
1.44 |
} |
139 |
|
|
|
140 |
root |
1.19 |
// node to start with |
141 |
|
|
unsigned char si = ctx.d.x < 0 ? 1 : 0 |
142 |
|
|
| ctx.d.y < 0 ? 2 : 0 |
143 |
|
|
| ctx.d.z < 0 ? 4 : 0; |
144 |
|
|
|
145 |
|
|
// bit-toggle to find next child for front-to-back order |
146 |
|
|
static unsigned char next[8] |
147 |
|
|
= { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7 }; |
148 |
|
|
|
149 |
|
|
for (int i = 0; i < 8; i++) |
150 |
|
|
{ |
151 |
|
|
si ^= next[i]; |
152 |
|
|
|
153 |
|
|
if (sub[si]) |
154 |
|
|
sub[si]->detect_visibility (ctx); |
155 |
|
|
} |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
void octant::display (view &ctx) |
159 |
|
|
{ |
160 |
root |
1.17 |
#if 0 |
161 |
root |
1.8 |
glBegin (GL_LINES); |
162 |
root |
1.25 |
sector s = orig - ctx.orig; |
163 |
root |
1.13 |
vec3 clr(0, 0.8, 0); |
164 |
|
|
glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, (const GLfloat*)&clr); |
165 |
root |
1.8 |
|
166 |
|
|
for (int i = 8; i--; ) |
167 |
|
|
for (int ji = 3; ji--; ) |
168 |
|
|
{ |
169 |
|
|
int j = i | (1 << ji); |
170 |
|
|
if (i != j) |
171 |
|
|
{ |
172 |
|
|
glVertex3i (s.x + !!(i & 1) * extent, |
173 |
|
|
s.y + !!(i & 2) * extent, |
174 |
|
|
s.z + !!(i & 4) * extent); |
175 |
|
|
glVertex3i (s.x + !!(j & 1) * extent, |
176 |
|
|
s.y + !!(j & 2) * extent, |
177 |
|
|
s.z + !!(j & 4) * extent); |
178 |
|
|
} |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
glEnd (); |
182 |
|
|
#endif |
183 |
|
|
|
184 |
root |
1.19 |
for (iterator i = end (); i != begin (); ) |
185 |
|
|
(*--i)->display (ctx); |
186 |
root |
1.1 |
} |
187 |
root |
1.8 |
|
188 |
root |
1.38 |
void octant::draw_bbox (view &ctx) |
189 |
root |
1.25 |
{ |
190 |
|
|
sector s = orig - ctx.orig; |
191 |
root |
1.27 |
|
192 |
root |
1.37 |
gl::draw_box (ctx, s, s + extent); |
193 |
root |
1.25 |
} |
194 |
root |
1.8 |
|
195 |
root |
1.38 |
void octant::event (occ_query &ev) |
196 |
|
|
{ |
197 |
root |
1.44 |
visibility_state &vs = ev.v.vismap[this]; |
198 |
|
|
|
199 |
|
|
vs.last = timer.now; |
200 |
|
|
vs.visibility = ev.r < 4 |
201 |
|
|
? visibility_state::OCCLUDED |
202 |
|
|
: visibility_state::FULL; |
203 |
|
|
//printf ("OCT(%x,%x,%x+%x) samples %d\n", orig.x, orig.y, orig.z, extent, ev.r); |
204 |
root |
1.38 |
} |
205 |
root |
1.2 |
|