1 |
#include <cstdlib> |
2 |
|
3 |
#include <vector> |
4 |
|
5 |
using namespace std; |
6 |
|
7 |
#define GL_GLEXT_PROTOTYPES |
8 |
#include <GL/gl.h> |
9 |
|
10 |
#include "oct.h" |
11 |
#include "view.h" |
12 |
#include "entity.h" |
13 |
|
14 |
vector<GLuint> occ_query_objects; |
15 |
|
16 |
static GLuint begin_occ_query () |
17 |
{ |
18 |
GLuint id; |
19 |
|
20 |
if (occ_query_objects.size ()) |
21 |
{ |
22 |
id = *(occ_query_objects.end () - 1); |
23 |
occ_query_objects.pop_back (); |
24 |
} |
25 |
else |
26 |
glGenQueries (1, &id); |
27 |
|
28 |
glBeginQuery (GL_SAMPLES_PASSED, id); |
29 |
return id; |
30 |
} |
31 |
|
32 |
#define end_occ_query() glEndQuery (GL_SAMPLES_PASSED); |
33 |
|
34 |
static GLuint occ_query_result (GLuint id) |
35 |
{ |
36 |
GLuint count; |
37 |
|
38 |
glGetQueryObjectuiv (id, GL_QUERY_RESULT, &count); |
39 |
occ_query_objects.push_back (id); |
40 |
|
41 |
return count; |
42 |
} |
43 |
|
44 |
octant world(0, sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN), MAXEXTENT); |
45 |
|
46 |
octant::octant (octant *parent, const sector &orig, uoffs extent) |
47 |
: parent(parent), orig(orig), extent(extent) |
48 |
{ |
49 |
for (fill = 8; fill--; ) |
50 |
sub[fill] = 0; |
51 |
} |
52 |
|
53 |
octant::~octant () |
54 |
{ |
55 |
for (fill = 8; fill--; ) |
56 |
delete sub[fill]; |
57 |
} |
58 |
|
59 |
static bool overlap (const sector &o1, uoffs ea, const box &bbox) |
60 |
{ |
61 |
sector a2, b2; |
62 |
|
63 |
ea /= 2; |
64 |
|
65 |
a2.x = o1.x + ea; |
66 |
a2.y = o1.y + ea; |
67 |
a2.z = o1.z + ea; |
68 |
|
69 |
b2.x = (bbox.a.x + bbox.b.x) / 2; |
70 |
b2.y = (bbox.a.y + bbox.b.y) / 2; |
71 |
b2.z = (bbox.a.z + bbox.b.z) / 2; |
72 |
|
73 |
return abs (a2.x - b2.x) <= ea + (bbox.b.x - bbox.a.x) |
74 |
&& abs (a2.y - b2.y) <= ea + (bbox.b.y - bbox.a.y) |
75 |
&& abs (a2.z - b2.z) <= ea + (bbox.b.z - bbox.a.z); |
76 |
} |
77 |
|
78 |
void octant::add (entity_base *e) |
79 |
{ |
80 |
box bbox = translate (e->bbox, e->orig, sector (0, 0, 0)); |
81 |
|
82 |
uoffs size = max (abs (bbox.b.x - bbox.a.x), |
83 |
max (abs (bbox.b.y - bbox.a.y), |
84 |
abs (bbox.b.z - bbox.a.z))); |
85 |
|
86 |
if (overlap (orig, extent, bbox)) |
87 |
{ |
88 |
uoffs extent2 = extent / 2; |
89 |
|
90 |
if (size > extent2 || !extent2) |
91 |
{ |
92 |
printf ("overlap, add\n"); |
93 |
push_back (e); |
94 |
e->o.push_back (this); |
95 |
return; |
96 |
} |
97 |
|
98 |
for (int i = 8; i--; ) |
99 |
{ |
100 |
sector s = orig; |
101 |
s.offset (i, extent2); |
102 |
if (overlap (s, extent2, bbox)) |
103 |
{ |
104 |
if (!sub[i]) |
105 |
sub[i] = new octant (this, s, extent2); |
106 |
|
107 |
sub[i]->add (e); |
108 |
} |
109 |
} |
110 |
} |
111 |
else |
112 |
printf ("no overlap\n"); |
113 |
} |
114 |
|
115 |
void octant::remove (entity_base *e) |
116 |
{ |
117 |
} |
118 |
|
119 |
void octant::draw (draw_context &ctx) |
120 |
{ |
121 |
visibility_state &vs = ctx.vismap[this]; |
122 |
|
123 |
if (vs.generation != ctx.generation) |
124 |
vs.visibility = visibility_state::UNKNOWN; |
125 |
|
126 |
const sector &cam = ctx.v.orig; |
127 |
if (cam.x >= orig.x && cam.x <= orig.x + extent |
128 |
&& cam.y >= orig.y && cam.y <= orig.y + extent |
129 |
&& cam.z >= orig.z && cam.z <= orig.z + extent) |
130 |
{ |
131 |
vs.visibility = visibility_state::PARTIAL; |
132 |
vs.generation = ctx.generation; |
133 |
} |
134 |
else |
135 |
{ |
136 |
//printf ("OCTANT %d,%d,%d+%d\n", orig.x, orig.y, orig.z, extent); |
137 |
point center ( |
138 |
orig.x + (soffs)extent / 2 - cam.x, |
139 |
orig.y + (soffs)extent / 2 - cam.y, |
140 |
orig.z + (soffs)extent / 2 - cam.z |
141 |
); |
142 |
|
143 |
if (size () && center.x > 0 && center.y > 0) |
144 |
{ |
145 |
#if 0 |
146 |
printf ("DISTANCE %f,%f,%f TO near: %f\n", |
147 |
center.x, center.y, center.z, |
148 |
ctx.frustum.n.distance (center) |
149 |
); |
150 |
#endif |
151 |
#if 1 |
152 |
printf ("DISTANCE %f,%f,%f TO far: %f\n", |
153 |
center.x, center.y, center.z, |
154 |
ctx.frustum.f.distance (center) |
155 |
); |
156 |
#endif |
157 |
} |
158 |
#if 0 |
159 |
if (ctx.frustum.n.distance (center) < -extent) |
160 |
return; //cull |
161 |
#endif |
162 |
|
163 |
} |
164 |
|
165 |
#if 1 |
166 |
glBegin (GL_LINES); |
167 |
sector s = orig; |
168 |
s.x -= ctx.v.orig.x; |
169 |
s.y -= ctx.v.orig.y; |
170 |
s.z -= ctx.v.orig.z; |
171 |
glColor3f (1, 1, 1); |
172 |
|
173 |
for (int i = 8; i--; ) |
174 |
for (int ji = 3; ji--; ) |
175 |
{ |
176 |
int j = i | (1 << ji); |
177 |
if (i != j) |
178 |
{ |
179 |
glVertex3i (s.x + !!(i & 1) * extent, |
180 |
s.y + !!(i & 2) * extent, |
181 |
s.z + !!(i & 4) * extent); |
182 |
glVertex3i (s.x + !!(j & 1) * extent, |
183 |
s.y + !!(j & 2) * extent, |
184 |
s.z + !!(j & 4) * extent); |
185 |
} |
186 |
} |
187 |
|
188 |
glEnd (); |
189 |
#endif |
190 |
|
191 |
#if 0 |
192 |
if (vs.visibility == visibility_state::PARTIAL |
193 |
&& vs.generation == ctx.generation) |
194 |
#endif |
195 |
{ |
196 |
for (iterator i = end (); i-- != begin (); ) |
197 |
#if 0 |
198 |
printf ("draw %p %d,%d,%d (%d,%d,%d - %d,%d,%d)\n", *i, |
199 |
(*i)->orig.x, |
200 |
(*i)->orig.y, |
201 |
(*i)->orig.z, |
202 |
(*i)->bbox.a.x, |
203 |
(*i)->bbox.a.y, |
204 |
(*i)->bbox.a.z, |
205 |
(*i)->bbox.b.x, |
206 |
(*i)->bbox.b.y, |
207 |
(*i)->bbox.b.z |
208 |
), |
209 |
#endif |
210 |
(*i)->display (ctx); |
211 |
|
212 |
// node to start with |
213 |
unsigned char si = ctx.v.d.x < 0 ? 1 : 0 |
214 |
| ctx.v.d.y < 0 ? 2 : 0 |
215 |
| ctx.v.d.z < 0 ? 4 : 0; |
216 |
|
217 |
// bit-toggle to find next child for front-to-back order |
218 |
static unsigned char next[8] |
219 |
= { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7 }; |
220 |
|
221 |
for (int i = 0; i < 8; i++) |
222 |
{ |
223 |
si ^= next[i]; |
224 |
|
225 |
if (sub[si]) |
226 |
sub[si]->draw (ctx); |
227 |
} |
228 |
} |
229 |
|
230 |
vs.generation = ctx.generation; |
231 |
} |
232 |
|
233 |
|
234 |
|