… | |
… | |
2 | |
2 | |
3 | #include <vector> |
3 | #include <vector> |
4 | |
4 | |
5 | using namespace std; |
5 | using namespace std; |
6 | |
6 | |
7 | #define GL_GLEXT_PROTOTYPES |
|
|
8 | #include <GL/gl.h> |
7 | #include <GL/gl.h> |
9 | #include <GL/glext.h> |
|
|
10 | |
8 | |
11 | #include "oct.h" |
9 | #include "oct.h" |
12 | #include "view.h" |
10 | #include "view.h" |
13 | #include "entity.h" |
11 | #include "entity.h" |
14 | |
|
|
15 | vector<GLuint> occ_query_objects; |
|
|
16 | |
|
|
17 | static GLuint begin_occ_query () |
|
|
18 | { |
|
|
19 | GLuint id; |
|
|
20 | |
|
|
21 | if (occ_query_objects.size ()) |
|
|
22 | { |
|
|
23 | id = *(occ_query_objects.end () - 1); |
|
|
24 | occ_query_objects.pop_back (); |
|
|
25 | } |
|
|
26 | else |
|
|
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 | |
12 | |
45 | octant world(0, sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN), MAXEXTENT); |
13 | octant world(0, sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN), MAXEXTENT); |
46 | |
14 | |
47 | octant::octant (octant *parent, const sector &orig, uoffs extent) |
15 | octant::octant (octant *parent, const sector &orig, uoffs extent) |
48 | : parent(parent), orig(orig), extent(extent) |
16 | : parent(parent), orig(orig), extent(extent) |
… | |
… | |
55 | { |
23 | { |
56 | for (fill = 8; fill--; ) |
24 | for (fill = 8; fill--; ) |
57 | delete sub[fill]; |
25 | delete sub[fill]; |
58 | } |
26 | } |
59 | |
27 | |
60 | static bool overlap (const sector &o1, uoffs ea, const box &bbox) |
28 | static bool overlap (const sector &o1, uoffs ea, const sector &a, const sector &b) |
61 | { |
29 | { |
62 | sector a2, b2; |
|
|
63 | |
|
|
64 | ea /= 2; |
30 | ea /= 2; |
65 | |
31 | |
66 | a2.x = o1.x + ea; |
32 | sector center_1 = o1 + ea; |
67 | a2.y = o1.y + ea; |
33 | sector size_2 = b - a; |
68 | a2.z = o1.z + ea; |
34 | sector center_2 = a + size_2 / 2; |
69 | |
|
|
70 | b2.x = (bbox.a.x + bbox.b.x) / 2; |
|
|
71 | b2.y = (bbox.a.y + bbox.b.y) / 2; |
|
|
72 | b2.z = (bbox.a.z + bbox.b.z) / 2; |
|
|
73 | |
35 | |
74 | return abs (a2.x - b2.x) <= ea + (bbox.b.x - bbox.a.x) |
36 | return abs (center_1 - center_2) <= ea + size_2; |
75 | && abs (a2.y - b2.y) <= ea + (bbox.b.y - bbox.a.y) |
|
|
76 | && abs (a2.z - b2.z) <= ea + (bbox.b.z - bbox.a.z); |
|
|
77 | } |
37 | } |
78 | |
38 | |
79 | void octant::add (entity_base *e) |
39 | void octant::add (entity *e) |
80 | { |
40 | { |
81 | box bbox = translate (e->bbox, e->orig, sector (0, 0, 0)); |
41 | const sector &a = e->a; |
|
|
42 | const sector &b = e->b; |
82 | |
43 | |
83 | uoffs size = max (abs (bbox.b.x - bbox.a.x), |
|
|
84 | max (abs (bbox.b.y - bbox.a.y), |
|
|
85 | abs (bbox.b.z - bbox.a.z))); |
|
|
86 | |
|
|
87 | if (overlap (orig, extent, bbox)) |
44 | if (overlap (orig, extent, a, b)) |
88 | { |
45 | { |
89 | uoffs extent2 = extent / 2; |
46 | uoffs extent2 = extent / 2; |
|
|
47 | uoffs size = max (abs (b - a)); |
90 | |
48 | |
91 | if (size > extent2 || !extent2) |
49 | if (size > extent2 || !extent2) |
92 | { |
50 | { |
93 | push_back (e); |
51 | push_back (e); |
94 | e->o.push_back (this); |
52 | e->o.push_back (this); |
… | |
… | |
97 | |
55 | |
98 | for (int i = 8; i--; ) |
56 | for (int i = 8; i--; ) |
99 | { |
57 | { |
100 | sector s = orig; |
58 | sector s = orig; |
101 | s.offset (i, extent2); |
59 | s.offset (i, extent2); |
|
|
60 | |
102 | if (overlap (s, extent2, bbox)) |
61 | if (overlap (s, extent2, a, b)) |
103 | { |
62 | { |
104 | if (!sub[i]) |
63 | if (!sub[i]) |
105 | sub[i] = new octant (this, s, extent2); |
64 | sub[i] = new octant (this, s, extent2); |
106 | |
65 | |
107 | sub[i]->add (e); |
66 | sub[i]->add (e); |
108 | } |
67 | } |
109 | } |
68 | } |
110 | } |
69 | } |
111 | } |
70 | } |
112 | |
71 | |
113 | void octant::remove (entity_base *e) |
72 | void octant::remove (entity *e) |
114 | { |
73 | { |
115 | } |
74 | } |
116 | |
75 | |
117 | void octant::detect_visibility (view &ctx) |
76 | void octant::detect_visibility (view &ctx) |
118 | { |
77 | { |
119 | visibility_state &vs = ctx.vismap[this]; |
78 | visibility_state &vs = ctx.vismap[this]; |
120 | |
79 | |
121 | if (vs.generation != ctx.generation) |
80 | if (vs.generation != ctx.generation) |
122 | vs.visibility = visibility_state::UNKNOWN; |
81 | vs.visibility = visibility_state::UNKNOWN; |
123 | |
82 | |
124 | const sector &cam = ctx.orig; |
83 | if (orig <= ctx.orig && ctx.orig <= orig + extent) |
125 | if (cam.x >= orig.x && cam.x <= orig.x + extent |
|
|
126 | && cam.y >= orig.y && cam.y <= orig.y + extent |
|
|
127 | && cam.z >= orig.z && cam.z <= orig.z + extent) |
|
|
128 | { |
84 | { |
129 | vs.visibility = visibility_state::PARTIAL; |
85 | vs.visibility = visibility_state::PARTIAL; |
130 | vs.generation = ctx.generation; |
86 | vs.generation = ctx.generation; |
131 | } |
87 | } |
132 | else |
88 | else |
133 | { |
89 | { |
134 | point center ( |
90 | GLfloat extent2 = 0.5F * (GLfloat)extent; |
135 | orig.x + (soffs)extent / 2 - cam.x, |
91 | point center = point (orig) + extent2 - point (ctx.orig); |
136 | orig.y + (soffs)extent / 2 - cam.y, |
|
|
137 | orig.z + (soffs)extent / 2 - cam.z |
|
|
138 | ); |
|
|
139 | |
92 | |
140 | GLfloat dia = (0.5 * sqrtf (3))*(GLfloat)extent; |
93 | GLfloat rad = ctx.diagfact * extent2; |
141 | |
94 | |
142 | if (ctx.frustum.t.distance (center) < -dia) return; |
95 | if (ctx.frustum.t.distance (center) < -rad) return; |
143 | if (ctx.frustum.b.distance (center) < -dia) return; |
96 | if (ctx.frustum.b.distance (center) < -rad) return; |
144 | if (ctx.frustum.l.distance (center) < -dia) return; |
97 | if (ctx.frustum.l.distance (center) < -rad) return; |
145 | if (ctx.frustum.r.distance (center) < -dia) return; |
98 | if (ctx.frustum.r.distance (center) < -rad) return; |
146 | if (ctx.frustum.n.distance (center) < -dia) return; |
99 | if (ctx.frustum.n.distance (center) < -rad) return; |
147 | |
100 | |
148 | GLfloat fd = ctx.frustum.f.distance (center); |
101 | GLfloat fd = ctx.frustum.f.distance (center); |
149 | |
102 | |
150 | if (fd < -dia) |
103 | if (fd < -rad) |
151 | { |
104 | { |
152 | if (fd < -dia * 2) |
105 | if (fd < -rad * 3.F) |
153 | return; |
106 | return; |
154 | |
107 | |
155 | // cull, but do check for potential visibility |
|
|
156 | if (size ()) |
|
|
157 | ctx.farlist.push_back (this); |
108 | ctx.farlist.push_back (this); |
|
|
109 | return; |
158 | } |
110 | } |
159 | } |
111 | } |
|
|
112 | |
|
|
113 | if (vs.visibility == visibility_state::UNKNOWN) |
|
|
114 | vs.visibility = visibility_state::FULL; |
160 | |
115 | |
161 | if (size ()) |
116 | if (size ()) |
162 | ctx.vislist.push_back (this); |
117 | ctx.vislist.push_back (this); |
163 | |
118 | |
164 | // node to start with |
119 | // node to start with |
… | |
… | |
203 | s.z + !!(j & 4) * extent); |
158 | s.z + !!(j & 4) * extent); |
204 | } |
159 | } |
205 | } |
160 | } |
206 | |
161 | |
207 | glEnd (); |
162 | glEnd (); |
208 | glDisable(GL_COLOR_MATERIAL); |
|
|
209 | #endif |
163 | #endif |
210 | |
164 | |
211 | for (iterator i = end (); i != begin (); ) |
165 | for (iterator i = end (); i != begin (); ) |
212 | (*--i)->display (ctx); |
166 | (*--i)->display (ctx); |
213 | } |
167 | } |
214 | |
168 | |
215 | void |
|
|
216 | octant::draw_bbox (view &ctx) |
169 | void octant::draw_bbox (view &ctx) |
217 | { |
170 | { |
218 | sector s = orig - ctx.orig; |
171 | sector s = orig - ctx.orig; |
219 | int i; |
|
|
220 | GLint verts[4 * 6] = { |
|
|
221 | 0, 2, 3, 1, // -z |
|
|
222 | 4, 5, 7, 6, // +z |
|
|
223 | 0, 1, 5, 4, // -y |
|
|
224 | 7, 3, 2, 6, // +y |
|
|
225 | 0, 4, 6, 2, // -x |
|
|
226 | 1, 3, 7, 5, // +x |
|
|
227 | }; |
|
|
228 | |
172 | |
229 | /* / |
173 | gl::draw_box (ctx, s, s + extent); |
230 | * ------- | |
|
|
231 | * | | | |
|
|
232 | * | | / |
|
|
233 | * | | / |
|
|
234 | * ------- |
|
|
235 | */ |
|
|
236 | |
|
|
237 | GLfloat cube[8][3] = |
|
|
238 | { |
|
|
239 | { s.x , s.y , s.z }, |
|
|
240 | { s.x + (soffs)extent, s.y , s.z }, |
|
|
241 | { s.x , s.y + (soffs)extent, s.z }, |
|
|
242 | { s.x + (soffs)extent, s.y + (soffs)extent, s.z }, |
|
|
243 | { s.x , s.y , s.z + (soffs)extent }, |
|
|
244 | { s.x + (soffs)extent, s.y , s.z + (soffs)extent }, |
|
|
245 | { s.x , s.y + (soffs)extent, s.z + (soffs)extent }, |
|
|
246 | { s.x + (soffs)extent, s.y + (soffs)extent, s.z + (soffs)extent }, |
|
|
247 | }; |
|
|
248 | |
|
|
249 | glBegin (GL_QUADS); |
|
|
250 | for (i = 0; i < 4 * 6; i++) { |
|
|
251 | glVertex3fv (cube [verts [i]]); |
|
|
252 | } |
|
|
253 | glEnd (); |
|
|
254 | } |
174 | } |
255 | |
175 | |
|
|
176 | void octant::event (occ_query &ev) |
|
|
177 | { |
|
|
178 | if (ev.r <= 5) |
|
|
179 | return; |
256 | |
180 | |
|
|
181 | //ev.v.vismap[this].visibility = visibility_state::FULL; |
|
|
182 | ev.v.far = ev.v.near + ev.v.frustum.n.distance (orig); |
|
|
183 | printf ("OCT(%x,%x,%x+%x) samples %d\n", orig.x, orig.y, orig.z, extent, ev.r); |
|
|
184 | } |
|
|
185 | |