1 | #include <cstdlib> |
1 | #include <cstdlib> |
2 | |
2 | |
3 | #include <vector> |
3 | #include <vector> |
4 | |
|
|
5 | using namespace std; |
4 | using namespace std; |
6 | |
5 | |
7 | #define GL_GLEXT_PROTOTYPES |
6 | #include "opengl.h" |
8 | #include <GL/gl.h> |
|
|
9 | #include <GL/glext.h> |
|
|
10 | |
7 | |
11 | #include "oct.h" |
8 | #include "oct.h" |
12 | #include "view.h" |
9 | #include "view.h" |
13 | #include "entity.h" |
10 | #include "entity.h" |
14 | |
11 | |
15 | vector<GLuint> occ_query_objects; |
12 | enum visibility_state { FULL, PARTIAL, SMALL, OCCLUDED }; |
16 | |
13 | |
17 | static GLuint begin_occ_query () |
14 | struct evis { |
18 | { |
15 | visibility_state state; |
19 | GLuint id; |
16 | double last; // time of last check |
|
|
17 | evis () : last(0.), state(FULL) { }; |
|
|
18 | }; |
20 | |
19 | |
21 | if (occ_query_objects.size ()) |
20 | struct oct_visibility : visibility_base |
|
|
21 | { |
|
|
22 | typedef map<entity *, evis> evismap; |
|
|
23 | evismap vismap; |
|
|
24 | |
|
|
25 | visibility_state state; |
|
|
26 | |
|
|
27 | oct_visibility (octant &oct) |
|
|
28 | : state(FULL) |
22 | { |
29 | { |
23 | id = *(occ_query_objects.end () - 1); |
|
|
24 | occ_query_objects.pop_back (); |
|
|
25 | } |
30 | } |
26 | else |
31 | }; |
27 | glGenQueriesARB (1, &id); |
|
|
28 | |
|
|
29 | glBeginQueryARB (GL_SAMPLES_PASSED, id); |
|
|
30 | return id; |
|
|
31 | } |
|
|
32 | |
|
|
33 | #define end_occ_query() glEndQueryARB (GL_SAMPLES_PASSED) |
|
|
34 | |
|
|
35 | GLuint occ_query_result (GLuint id) |
|
|
36 | { |
|
|
37 | GLuint count; |
|
|
38 | |
|
|
39 | glGetQueryObjectuivARB (id, GL_QUERY_RESULT, &count); |
|
|
40 | occ_query_objects.push_back (id); |
|
|
41 | |
|
|
42 | return count; |
|
|
43 | } |
|
|
44 | |
32 | |
45 | octant world(0, sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN), MAXEXTENT); |
33 | octant world(0, sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN), MAXEXTENT); |
46 | |
34 | |
47 | octant::octant (octant *parent, const sector &orig, uoffs extent) |
35 | octant::octant (octant *parent, const sector &orig, uoffs extent) |
48 | : parent(parent), orig(orig), extent(extent) |
36 | : parent(parent) |
|
|
37 | , orig(orig) |
|
|
38 | , extent(extent) |
49 | { |
39 | { |
50 | for (fill = 8; fill--; ) |
40 | for (fill = 8; fill--; ) |
51 | sub[fill] = 0; |
41 | sub[fill] = 0; |
52 | } |
42 | } |
53 | |
43 | |
|
|
44 | 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 | |
54 | octant::~octant () |
54 | octant::~octant () |
55 | { |
55 | { |
56 | for (fill = 8; fill--; ) |
56 | for (fill = 8; fill--; ) |
57 | delete sub[fill]; |
57 | delete sub[fill]; |
58 | } |
58 | } |
… | |
… | |
61 | { |
61 | { |
62 | ea /= 2; |
62 | ea /= 2; |
63 | |
63 | |
64 | sector center_1 = o1 + ea; |
64 | sector center_1 = o1 + ea; |
65 | sector size_2 = b - a; |
65 | sector size_2 = b - a; |
66 | sector center_2 = a + size_2 / 2; |
66 | sector center_2 = a + (size_2 >> 1); |
67 | |
67 | |
68 | return abs (center_1 - center_2) <= ea + size_2; |
68 | return abs (center_1 - center_2) <= ea + size_2; |
69 | } |
69 | } |
70 | |
70 | |
71 | void octant::add (entity *e) |
71 | void octant::add (entity *e) |
… | |
… | |
73 | const sector &a = e->a; |
73 | const sector &a = e->a; |
74 | const sector &b = e->b; |
74 | const sector &b = e->b; |
75 | |
75 | |
76 | if (overlap (orig, extent, a, b)) |
76 | if (overlap (orig, extent, a, b)) |
77 | { |
77 | { |
78 | uoffs extent2 = extent / 2; |
78 | uoffs extent2 = extent >> 1; |
79 | uoffs size = max (abs (b - a)); |
79 | uoffs size = max (abs (b - a)); |
80 | |
80 | |
81 | if (size > extent2 || !extent2) |
81 | if (size >= extent2 >> 1) |
82 | { |
82 | { |
83 | push_back (e); |
83 | push_back (e); |
84 | e->o.push_back (this); |
84 | e->o.push_back (this); |
85 | return; |
85 | return; |
86 | } |
86 | } |
… | |
… | |
91 | s.offset (i, extent2); |
91 | s.offset (i, extent2); |
92 | |
92 | |
93 | if (overlap (s, extent2, a, b)) |
93 | if (overlap (s, extent2, a, b)) |
94 | { |
94 | { |
95 | if (!sub[i]) |
95 | if (!sub[i]) |
|
|
96 | { |
96 | sub[i] = new octant (this, s, extent2); |
97 | sub[i] = new octant (this, s, extent2); |
|
|
98 | fill++; |
|
|
99 | } |
97 | |
100 | |
98 | sub[i]->add (e); |
101 | sub[i]->add (e); |
99 | } |
102 | } |
100 | } |
103 | } |
101 | } |
104 | } |
… | |
… | |
103 | |
106 | |
104 | void octant::remove (entity *e) |
107 | void octant::remove (entity *e) |
105 | { |
108 | { |
106 | } |
109 | } |
107 | |
110 | |
108 | void octant::detect_visibility (view &ctx) |
111 | bool octant::detect_visibility (view &ctx) |
109 | { |
112 | { |
110 | visibility_state &vs = ctx.vismap[this]; |
113 | GLfloat extent2 = 0.5F * (GLfloat)extent; |
|
|
114 | sector centeri = orig + (extent >> 1) - ctx.orig; |
|
|
115 | point centerf = point (centeri) + ((extent & 1) ? 0.5F : 0.F); |
111 | |
116 | |
112 | if (vs.generation != ctx.generation) |
117 | GLfloat rad = ctx.diagfact * extent2; |
113 | vs.visibility = visibility_state::UNKNOWN; |
118 | |
|
|
119 | if (!overlap (ctx.frustum.c, sphere (centerf, rad))) |
|
|
120 | return false; |
|
|
121 | |
|
|
122 | oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
114 | |
123 | |
115 | if (orig <= ctx.orig && ctx.orig <= orig + extent) |
124 | if (orig <= ctx.orig && ctx.orig <= orig + extent) |
116 | { |
125 | vs.state = PARTIAL; |
117 | vs.visibility = visibility_state::PARTIAL; |
|
|
118 | vs.generation = ctx.generation; |
|
|
119 | } |
|
|
120 | else |
126 | else |
121 | { |
127 | { |
122 | sector center_s (orig + extent / 2 - ctx.orig); |
|
|
123 | point center (center_s.x, center_s.y, center_s.z); |
|
|
124 | |
|
|
125 | GLfloat dia = sqrtf (3) * (GLfloat)extent; |
|
|
126 | GLfloat rad = dia * 0.5; |
|
|
127 | |
|
|
128 | if (ctx.frustum.t.distance (center) < -rad) return; |
128 | if (distance (ctx.frustum.t, centerf) < -rad) return false; |
129 | if (ctx.frustum.b.distance (center) < -rad) return; |
129 | if (distance (ctx.frustum.b, centerf) < -rad) return false; |
130 | if (ctx.frustum.l.distance (center) < -rad) return; |
130 | if (distance (ctx.frustum.l, centerf) < -rad) return false; |
131 | if (ctx.frustum.r.distance (center) < -rad) return; |
131 | if (distance (ctx.frustum.r, centerf) < -rad) return false; |
132 | if (ctx.frustum.n.distance (center) < -rad) return; |
132 | if (distance (ctx.frustum.n, centerf) < -rad) return false; |
133 | |
133 | |
|
|
134 | #if 0 |
134 | GLfloat fd = ctx.frustum.f.distance (center); |
135 | GLfloat fd = distance (ctx.frustum.f, centerf); |
135 | |
136 | |
136 | if (fd < -rad) |
137 | if (fd < -(ctx.c_far - ctx.z_far) -rad * 3.F) |
137 | { |
|
|
138 | if (fd < -dia) |
|
|
139 | return; |
|
|
140 | |
|
|
141 | ctx.farlist.push_back (this); |
|
|
142 | return; |
138 | return false; |
143 | } |
139 | #endif |
|
|
140 | } |
|
|
141 | |
|
|
142 | #if 1 |
|
|
143 | if (vs.state == OCCLUDED) |
144 | } |
144 | { |
145 | |
|
|
146 | if (vs.visibility == visibility_state::UNKNOWN) |
|
|
147 | vs.visibility = visibility_state::FULL; |
|
|
148 | |
|
|
149 | if (size ()) |
145 | if (size ()) |
150 | ctx.vislist.push_back (this); |
146 | ctx.vislist.push_back (this); |
|
|
147 | //return false; |
|
|
148 | } |
|
|
149 | #endif |
|
|
150 | |
|
|
151 | GLfloat z = ctx.z_near + distance (ctx.frustum.n, centerf) + rad; |
|
|
152 | if (ctx.perspfact * extent / z < 1.) // very crude "too small to see" check |
|
|
153 | return false; |
|
|
154 | //printf ("z %f, perspfact %f, z*p %f\n", z, ctx.perspfact, ctx.perspfact / z); |
|
|
155 | |
|
|
156 | #if 0 |
|
|
157 | if (vs.state == PARTIAL || vs.state == FULL) |
|
|
158 | ctx.nc_far = max (ctx.nc_far, z); |
|
|
159 | #endif |
151 | |
160 | |
152 | // node to start with |
161 | // node to start with |
153 | unsigned char si = ctx.d.x < 0 ? 1 : 0 |
162 | unsigned char si = centeri.x > 0 ? 1 : 0 |
154 | | ctx.d.y < 0 ? 2 : 0 |
163 | | centeri.y > 0 ? 2 : 0 |
155 | | ctx.d.z < 0 ? 4 : 0; |
164 | | centeri.z > 0 ? 4 : 0; |
|
|
165 | |
|
|
166 | //printf ("si %d C %Ld,%Ld,%Ld\n", si, centeri.x, centeri.y, centeri.z); |
156 | |
167 | |
157 | // bit-toggle to find next child for front-to-back order |
168 | // bit-toggle to find next child for front-to-back order |
158 | static unsigned char next[8] |
169 | static unsigned char toggle[8+1] |
159 | = { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7 }; |
170 | = { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7, 0 }; |
160 | |
171 | |
161 | for (int i = 0; i < 8; i++) |
172 | unsigned char *next = toggle; |
|
|
173 | do |
162 | { |
174 | { |
163 | si ^= next[i]; |
175 | si ^= *next; |
164 | |
176 | |
165 | if (sub[si]) |
177 | if (sub[si]) |
166 | sub[si]->detect_visibility (ctx); |
178 | sub[si]->detect_visibility (ctx); |
167 | } |
179 | } |
|
|
180 | while (*++next); |
168 | |
181 | |
169 | vs.generation = ctx.generation; |
182 | if (size () |
|
|
183 | && (vs.state == PARTIAL || vs.state == FULL || vs.state == OCCLUDED)) |
|
|
184 | ctx.vislist.push_back (this); |
|
|
185 | |
|
|
186 | return true; |
170 | } |
187 | } |
171 | |
188 | |
172 | void octant::display (view &ctx) |
189 | void octant::display (view &ctx) |
173 | { |
190 | { |
174 | #if 0 |
191 | #if 0 |
|
|
192 | sector s = orig - ctx.orig; |
|
|
193 | |
175 | glBegin (GL_LINES); |
194 | glBegin (GL_LINES); |
176 | sector s = orig - ctx.orig; |
|
|
177 | vec3 clr(0, 0.8, 0); |
195 | vec3 clr(0, 0.8, 0); |
178 | glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, (const GLfloat*)&clr); |
196 | glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, (const GLfloat*)&clr); |
179 | |
197 | |
180 | for (int i = 8; i--; ) |
198 | for (int i = 8; i--; ) |
181 | for (int ji = 3; ji--; ) |
199 | for (int ji = 3; ji--; ) |
182 | { |
200 | { |
183 | int j = i | (1 << ji); |
201 | int j = i | (1 << ji); |
184 | if (i != j) |
202 | if (i < j) |
185 | { |
203 | { |
186 | glVertex3i (s.x + !!(i & 1) * extent, |
204 | glVertex3i (s.x + !!(i & 1) * extent, |
187 | s.y + !!(i & 2) * extent, |
205 | s.y + !!(i & 2) * extent, |
188 | s.z + !!(i & 4) * extent); |
206 | s.z + !!(i & 4) * extent); |
189 | glVertex3i (s.x + !!(j & 1) * extent, |
207 | glVertex3i (s.x + !!(j & 1) * extent, |
… | |
… | |
191 | s.z + !!(j & 4) * extent); |
209 | s.z + !!(j & 4) * extent); |
192 | } |
210 | } |
193 | } |
211 | } |
194 | |
212 | |
195 | glEnd (); |
213 | glEnd (); |
196 | glDisable(GL_COLOR_MATERIAL); |
|
|
197 | #endif |
214 | #endif |
|
|
215 | oct_visibility &vs = *(oct_visibility *)get_visibility (ctx); |
198 | |
216 | |
199 | for (iterator i = end (); i != begin (); ) |
217 | if (vs.state == OCCLUDED) |
200 | (*--i)->display (ctx); |
218 | { |
201 | } |
219 | if (ctx.pass == view::POSTDEPTH) |
202 | |
220 | { |
203 | void |
221 | ctx.begin_occ_query (*this, 0); |
204 | octant::draw_bbox (view &ctx) |
|
|
205 | { |
|
|
206 | sector s = orig - ctx.orig; |
222 | sector s = orig - ctx.orig; |
207 | |
|
|
208 | gl::draw_box (ctx, s, s + extent); |
223 | gl::draw_bbox (s, s + extent); |
209 | } |
224 | ctx.end_occ_query (); |
|
|
225 | } |
|
|
226 | } |
|
|
227 | else |
|
|
228 | { |
|
|
229 | int nvis = 0; |
210 | |
230 | |
|
|
231 | for (iterator i = begin (); i != end (); ) |
|
|
232 | { |
|
|
233 | entity *e = *i++; |
211 | |
234 | |
|
|
235 | evis &evs = vs.vismap[e]; |
|
|
236 | |
|
|
237 | if (ctx.pass == view::POSTDEPTH) |
|
|
238 | { |
|
|
239 | if (evs.state == OCCLUDED) |
|
|
240 | { |
|
|
241 | ctx.begin_occ_query (*this, e); |
|
|
242 | gl::draw_bbox (e->a - ctx.orig, e->b - ctx.orig); |
|
|
243 | ctx.end_occ_query (); |
|
|
244 | } |
|
|
245 | else |
|
|
246 | nvis++; |
|
|
247 | } |
|
|
248 | else |
|
|
249 | { |
|
|
250 | if (!ctx.may_draw (e)) |
|
|
251 | continue; |
|
|
252 | |
|
|
253 | |
|
|
254 | if (evs.state != OCCLUDED) |
|
|
255 | { |
|
|
256 | sector center = ((e->a + e->b) >> 1) - ctx.orig; |
|
|
257 | GLfloat z = length (vec3 (center)); |
|
|
258 | ctx.pixfact = ctx.perspfact / z; |
|
|
259 | |
|
|
260 | ctx.nz_far = max (ctx.nz_far, z + extent); |
|
|
261 | ctx.nz_near = min (ctx.nz_near, z - extent); |
|
|
262 | |
|
|
263 | if (ctx.pass == view::DEPTH || evs.last + 0.3 > timer.now) |
|
|
264 | e->draw (ctx); |
|
|
265 | else |
|
|
266 | { |
|
|
267 | evs.last = timer.now; |
|
|
268 | ctx.begin_occ_query (*this, e); |
|
|
269 | e->draw (ctx); |
|
|
270 | ctx.end_occ_query (); |
|
|
271 | } |
|
|
272 | } |
|
|
273 | } |
|
|
274 | } |
|
|
275 | |
|
|
276 | #if 1 |
|
|
277 | if (ctx.pass == view::POSTDEPTH && nvis == 0 && size ()) |
|
|
278 | vs.state = OCCLUDED; |
|
|
279 | #endif |
|
|
280 | } |
|
|
281 | } |
|
|
282 | |
|
|
283 | void octant::event (occ_query &ev) |
|
|
284 | { |
|
|
285 | oct_visibility &vs = *(oct_visibility *)get_visibility (ev.ctx); |
|
|
286 | entity *e = (entity *)ev.id; |
|
|
287 | |
|
|
288 | if (e) |
|
|
289 | { |
|
|
290 | evis &evs = vs.vismap[e]; |
|
|
291 | evs.state = ev.count ? FULL : OCCLUDED; |
|
|
292 | } |
|
|
293 | else |
|
|
294 | vs.state = ev.count ? FULL : OCCLUDED; |
|
|
295 | } |
|
|
296 | |
|
|
297 | |