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