1 |
root |
1.1 |
#include <cstdlib> |
2 |
|
|
|
3 |
root |
1.8 |
#include <vector> |
4 |
|
|
|
5 |
|
|
using namespace std; |
6 |
|
|
|
7 |
|
|
#define GL_GLEXT_PROTOTYPES |
8 |
|
|
#include <GL/gl.h> |
9 |
root |
1.24 |
#include <GL/glext.h> |
10 |
root |
1.8 |
|
11 |
root |
1.1 |
#include "oct.h" |
12 |
root |
1.8 |
#include "view.h" |
13 |
root |
1.2 |
#include "entity.h" |
14 |
|
|
|
15 |
root |
1.8 |
vector<GLuint> occ_query_objects; |
16 |
root |
1.2 |
|
17 |
root |
1.8 |
static GLuint begin_occ_query () |
18 |
root |
1.2 |
{ |
19 |
root |
1.8 |
GLuint id; |
20 |
root |
1.2 |
|
21 |
root |
1.8 |
if (occ_query_objects.size ()) |
22 |
root |
1.3 |
{ |
23 |
root |
1.8 |
id = *(occ_query_objects.end () - 1); |
24 |
|
|
occ_query_objects.pop_back (); |
25 |
root |
1.3 |
} |
26 |
|
|
else |
27 |
root |
1.20 |
glGenQueriesARB (1, &id); |
28 |
root |
1.8 |
|
29 |
root |
1.20 |
glBeginQueryARB (GL_SAMPLES_PASSED, id); |
30 |
root |
1.8 |
return id; |
31 |
|
|
} |
32 |
|
|
|
33 |
root |
1.23 |
#define end_occ_query() glEndQueryARB (GL_SAMPLES_PASSED) |
34 |
root |
1.8 |
|
35 |
root |
1.23 |
GLuint occ_query_result (GLuint id) |
36 |
root |
1.8 |
{ |
37 |
|
|
GLuint count; |
38 |
|
|
|
39 |
root |
1.20 |
glGetQueryObjectuivARB (id, GL_QUERY_RESULT, &count); |
40 |
root |
1.8 |
occ_query_objects.push_back (id); |
41 |
|
|
|
42 |
|
|
return count; |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
octant world(0, sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN), MAXEXTENT); |
46 |
|
|
|
47 |
|
|
octant::octant (octant *parent, const sector &orig, uoffs extent) |
48 |
|
|
: parent(parent), orig(orig), extent(extent) |
49 |
|
|
{ |
50 |
|
|
for (fill = 8; fill--; ) |
51 |
|
|
sub[fill] = 0; |
52 |
root |
1.2 |
} |
53 |
|
|
|
54 |
|
|
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 |
|
|
sector center_2 = a + size_2 / 2; |
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.8 |
uoffs extent2 = extent / 2; |
79 |
root |
1.34 |
uoffs size = max (abs (b - a)); |
80 |
root |
1.32 |
|
81 |
root |
1.8 |
if (size > extent2 || !extent2) |
82 |
|
|
{ |
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 |
|
|
sub[i] = new octant (this, s, extent2); |
97 |
|
|
|
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.19 |
void octant::detect_visibility (view &ctx) |
109 |
root |
1.1 |
{ |
110 |
root |
1.8 |
visibility_state &vs = ctx.vismap[this]; |
111 |
|
|
|
112 |
|
|
if (vs.generation != ctx.generation) |
113 |
root |
1.10 |
vs.visibility = visibility_state::UNKNOWN; |
114 |
|
|
|
115 |
root |
1.34 |
if (orig <= ctx.orig && ctx.orig <= orig + extent) |
116 |
root |
1.10 |
{ |
117 |
|
|
vs.visibility = visibility_state::PARTIAL; |
118 |
|
|
vs.generation = ctx.generation; |
119 |
|
|
} |
120 |
|
|
else |
121 |
|
|
{ |
122 |
root |
1.34 |
sector center_s (orig + extent / 2 - ctx.orig); |
123 |
|
|
point center (center_s.x, center_s.y, center_s.z); |
124 |
root |
1.10 |
|
125 |
root |
1.36 |
GLfloat dia = sqrtf (3) * (GLfloat)extent; |
126 |
root |
1.14 |
|
127 |
|
|
if (ctx.frustum.t.distance (center) < -dia) return; |
128 |
|
|
if (ctx.frustum.b.distance (center) < -dia) return; |
129 |
|
|
if (ctx.frustum.l.distance (center) < -dia) return; |
130 |
|
|
if (ctx.frustum.r.distance (center) < -dia) return; |
131 |
|
|
if (ctx.frustum.n.distance (center) < -dia) return; |
132 |
root |
1.25 |
|
133 |
|
|
GLfloat fd = ctx.frustum.f.distance (center); |
134 |
|
|
|
135 |
|
|
if (fd < -dia) |
136 |
|
|
{ |
137 |
root |
1.29 |
if (fd < -dia * 2) |
138 |
root |
1.26 |
return; |
139 |
|
|
|
140 |
root |
1.35 |
ctx.farlist.push_back (this); |
141 |
|
|
return; |
142 |
root |
1.25 |
} |
143 |
root |
1.10 |
} |
144 |
root |
1.8 |
|
145 |
root |
1.36 |
if (vs.visibility == visibility_state::UNKNOWN) |
146 |
|
|
vs.visibility = visibility_state::FULL; |
147 |
|
|
|
148 |
root |
1.19 |
if (size ()) |
149 |
|
|
ctx.vislist.push_back (this); |
150 |
|
|
|
151 |
|
|
// node to start with |
152 |
|
|
unsigned char si = ctx.d.x < 0 ? 1 : 0 |
153 |
|
|
| ctx.d.y < 0 ? 2 : 0 |
154 |
|
|
| ctx.d.z < 0 ? 4 : 0; |
155 |
|
|
|
156 |
|
|
// bit-toggle to find next child for front-to-back order |
157 |
|
|
static unsigned char next[8] |
158 |
|
|
= { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7 }; |
159 |
|
|
|
160 |
|
|
for (int i = 0; i < 8; i++) |
161 |
|
|
{ |
162 |
|
|
si ^= next[i]; |
163 |
|
|
|
164 |
|
|
if (sub[si]) |
165 |
|
|
sub[si]->detect_visibility (ctx); |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
vs.generation = ctx.generation; |
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
void octant::display (view &ctx) |
172 |
|
|
{ |
173 |
root |
1.17 |
#if 0 |
174 |
root |
1.8 |
glBegin (GL_LINES); |
175 |
root |
1.25 |
sector s = orig - ctx.orig; |
176 |
root |
1.13 |
vec3 clr(0, 0.8, 0); |
177 |
|
|
glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, (const GLfloat*)&clr); |
178 |
root |
1.8 |
|
179 |
|
|
for (int i = 8; i--; ) |
180 |
|
|
for (int ji = 3; ji--; ) |
181 |
|
|
{ |
182 |
|
|
int j = i | (1 << ji); |
183 |
|
|
if (i != j) |
184 |
|
|
{ |
185 |
|
|
glVertex3i (s.x + !!(i & 1) * extent, |
186 |
|
|
s.y + !!(i & 2) * extent, |
187 |
|
|
s.z + !!(i & 4) * extent); |
188 |
|
|
glVertex3i (s.x + !!(j & 1) * extent, |
189 |
|
|
s.y + !!(j & 2) * extent, |
190 |
|
|
s.z + !!(j & 4) * extent); |
191 |
|
|
} |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
glEnd (); |
195 |
root |
1.13 |
glDisable(GL_COLOR_MATERIAL); |
196 |
root |
1.8 |
#endif |
197 |
|
|
|
198 |
root |
1.19 |
for (iterator i = end (); i != begin (); ) |
199 |
|
|
(*--i)->display (ctx); |
200 |
root |
1.1 |
} |
201 |
root |
1.8 |
|
202 |
root |
1.25 |
void |
203 |
|
|
octant::draw_bbox (view &ctx) |
204 |
|
|
{ |
205 |
|
|
sector s = orig - ctx.orig; |
206 |
root |
1.27 |
int i; |
207 |
|
|
GLint verts[4 * 6] = { |
208 |
root |
1.31 |
0x00, 0x40, 0x60, 0x20, // -x |
209 |
|
|
0x10, 0x30, 0x70, 0x50, // +x |
210 |
|
|
0x00, 0x10, 0x50, 0x40, // -y |
211 |
|
|
0x70, 0x30, 0x20, 0x60, // +y |
212 |
|
|
0x00, 0x20, 0x30, 0x10, // -z |
213 |
|
|
0x40, 0x50, 0x70, 0x60, // +z |
214 |
root |
1.27 |
}; |
215 |
|
|
|
216 |
root |
1.25 |
GLfloat cube[8][3] = |
217 |
|
|
{ |
218 |
root |
1.31 |
{ s.x , s.y , s.z }, |
219 |
|
|
{ s.x + (soffs)extent, s.y , s.z }, |
220 |
|
|
{ s.x , s.y + (soffs)extent, s.z }, |
221 |
|
|
{ s.x + (soffs)extent, s.y + (soffs)extent, s.z }, |
222 |
|
|
{ s.x , s.y , s.z + (soffs)extent }, |
223 |
|
|
{ s.x + (soffs)extent, s.y , s.z + (soffs)extent }, |
224 |
|
|
{ s.x , s.y + (soffs)extent, s.z + (soffs)extent }, |
225 |
|
|
{ s.x + (soffs)extent, s.y + (soffs)extent, s.z + (soffs)extent }, |
226 |
root |
1.25 |
}; |
227 |
|
|
|
228 |
root |
1.36 |
glBegin (GL_LINE_LOOP); |
229 |
root |
1.31 |
|
230 |
|
|
for (i = 0; i < 4 * 6; i++) |
231 |
|
|
glVertex3fv (cube [verts [i] >> 4]); |
232 |
|
|
|
233 |
root |
1.25 |
glEnd (); |
234 |
|
|
} |
235 |
root |
1.8 |
|
236 |
root |
1.2 |
|