… | |
… | |
2 | |
2 | |
3 | #include <vector> |
3 | #include <vector> |
4 | |
4 | |
5 | using namespace std; |
5 | using namespace std; |
6 | |
6 | |
7 | #include <GL/gl.h> |
7 | #include "opengl.h" |
8 | |
8 | |
9 | #include "oct.h" |
9 | #include "oct.h" |
10 | #include "view.h" |
10 | #include "view.h" |
11 | #include "entity.h" |
11 | #include "entity.h" |
12 | |
12 | |
… | |
… | |
44 | if (overlap (orig, extent, a, b)) |
44 | if (overlap (orig, extent, a, b)) |
45 | { |
45 | { |
46 | uoffs extent2 = extent / 2; |
46 | uoffs extent2 = extent / 2; |
47 | uoffs size = max (abs (b - a)); |
47 | uoffs size = max (abs (b - a)); |
48 | |
48 | |
49 | if (size > extent2 || !extent2) |
49 | if (size >= extent2 / 2) |
50 | { |
50 | { |
51 | push_back (e); |
51 | push_back (e); |
52 | e->o.push_back (this); |
52 | e->o.push_back (this); |
53 | return; |
53 | return; |
54 | } |
54 | } |
… | |
… | |
59 | s.offset (i, extent2); |
59 | s.offset (i, extent2); |
60 | |
60 | |
61 | if (overlap (s, extent2, a, b)) |
61 | if (overlap (s, extent2, a, b)) |
62 | { |
62 | { |
63 | if (!sub[i]) |
63 | if (!sub[i]) |
|
|
64 | { |
64 | sub[i] = new octant (this, s, extent2); |
65 | sub[i] = new octant (this, s, extent2); |
|
|
66 | fill++; |
|
|
67 | } |
65 | |
68 | |
66 | sub[i]->add (e); |
69 | sub[i]->add (e); |
67 | } |
70 | } |
68 | } |
71 | } |
69 | } |
72 | } |
… | |
… | |
75 | |
78 | |
76 | void octant::detect_visibility (view &ctx) |
79 | void octant::detect_visibility (view &ctx) |
77 | { |
80 | { |
78 | visibility_state &vs = ctx.vismap[this]; |
81 | visibility_state &vs = ctx.vismap[this]; |
79 | |
82 | |
80 | if (vs.generation != ctx.generation) |
83 | if (vs.generation + 1 != ctx.generation) |
81 | vs.visibility = visibility_state::UNKNOWN; |
84 | vs.visibility = visibility_state::UNKNOWN; |
82 | |
85 | |
|
|
86 | vs.generation = ctx.generation; |
|
|
87 | |
|
|
88 | GLfloat extent2 = 0.5F * (GLfloat)extent; |
|
|
89 | sector centeri = orig + (extent >> 1) - ctx.orig; |
|
|
90 | point centerf = point (centeri) + ((extent & 1) ? 0.5F : 0.F); |
|
|
91 | |
|
|
92 | GLfloat rad = ctx.diagfact * extent2; |
|
|
93 | |
83 | if (orig <= ctx.orig && ctx.orig <= orig + extent) |
94 | if (orig <= ctx.orig && ctx.orig <= orig + extent) |
84 | { |
|
|
85 | vs.visibility = visibility_state::PARTIAL; |
95 | vs.visibility = visibility_state::PARTIAL; |
86 | vs.generation = ctx.generation; |
|
|
87 | } |
|
|
88 | else |
96 | else |
89 | { |
97 | { |
90 | GLfloat extent2 = 0.5F * (GLfloat)extent; |
|
|
91 | point center = point (orig) + extent2 - point (ctx.orig); |
|
|
92 | |
|
|
93 | GLfloat rad = ctx.diagfact * extent2; |
|
|
94 | |
|
|
95 | if (ctx.frustum.t.distance (center) < -rad) return; |
98 | if (ctx.frustum.t.distance (centerf) < -rad) return; |
96 | if (ctx.frustum.b.distance (center) < -rad) return; |
99 | if (ctx.frustum.b.distance (centerf) < -rad) return; |
97 | if (ctx.frustum.l.distance (center) < -rad) return; |
100 | if (ctx.frustum.l.distance (centerf) < -rad) return; |
98 | if (ctx.frustum.r.distance (center) < -rad) return; |
101 | if (ctx.frustum.r.distance (centerf) < -rad) return; |
99 | if (ctx.frustum.n.distance (center) < -rad) return; |
102 | if (ctx.frustum.n.distance (centerf) < -rad) return; |
100 | |
103 | |
101 | GLfloat fd = ctx.frustum.f.distance (center); |
104 | GLfloat fd = ctx.frustum.f.distance (centerf); |
102 | |
105 | |
103 | if (fd < -rad) |
106 | if (fd < -(ctx.c_far - ctx.z_far) -rad * 3.F) |
104 | { |
|
|
105 | if (fd < -rad * 3.F) |
|
|
106 | return; |
107 | return; |
|
|
108 | } |
107 | |
109 | |
|
|
110 | if (vs.visibility == visibility_state::OCCLUDED |
|
|
111 | || vs.visibility == visibility_state::UNKNOWN) |
|
|
112 | { |
108 | ctx.farlist.push_back (this); |
113 | ctx.farlist.push_back (this); |
109 | return; |
114 | return; |
110 | } |
|
|
111 | } |
115 | } |
112 | |
116 | |
|
|
117 | #if 0 |
113 | if (vs.visibility == visibility_state::UNKNOWN) |
118 | if (vs.visibility == visibility_state::UNKNOWN) |
114 | vs.visibility = visibility_state::FULL; |
119 | vs.visibility = visibility_state::FULL; |
|
|
120 | #endif |
115 | |
121 | |
116 | if (size ()) |
122 | GLfloat z = ctx.z_near + ctx.frustum.n.distance (centerf) + rad; |
117 | ctx.vislist.push_back (this); |
123 | //printf ("z %f, perspfact %f, z*p %f\n", z, ctx.perspfact, ctx.perspfact / z); |
|
|
124 | |
|
|
125 | if (vs.visibility == visibility_state::FULL) |
|
|
126 | ctx.nc_far = max (ctx.nc_far, z); |
118 | |
127 | |
119 | // node to start with |
128 | // node to start with |
120 | unsigned char si = ctx.d.x < 0 ? 1 : 0 |
129 | unsigned char si = centeri.x > 0 ? 1 : 0 |
121 | | ctx.d.y < 0 ? 2 : 0 |
130 | | centeri.y > 0 ? 2 : 0 |
122 | | ctx.d.z < 0 ? 4 : 0; |
131 | | centeri.z > 0 ? 4 : 0; |
|
|
132 | |
|
|
133 | //printf ("si %d C %Ld,%Ld,%Ld\n", si, centeri.x, centeri.y, centeri.z); |
123 | |
134 | |
124 | // bit-toggle to find next child for front-to-back order |
135 | // bit-toggle to find next child for front-to-back order |
125 | static unsigned char next[8] |
136 | static unsigned char toggle[8+1] |
126 | = { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7 }; |
137 | = { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7, 0 }; |
127 | |
138 | |
128 | for (int i = 0; i < 8; i++) |
139 | unsigned char *next = toggle; |
|
|
140 | do |
129 | { |
141 | { |
130 | si ^= next[i]; |
142 | si ^= *next; |
131 | |
143 | |
132 | if (sub[si]) |
144 | if (sub[si]) |
133 | sub[si]->detect_visibility (ctx); |
145 | sub[si]->detect_visibility (ctx); |
134 | } |
146 | } |
|
|
147 | while (*++next); |
135 | |
148 | |
136 | vs.generation = ctx.generation; |
149 | if (size () |
|
|
150 | && (vs.visibility == visibility_state::PARTIAL |
|
|
151 | || vs.visibility == visibility_state::FULL)) |
|
|
152 | { |
|
|
153 | ctx.nz_far = max (ctx.nz_far, z); |
|
|
154 | ctx.vislist.push_back (this); |
|
|
155 | } |
137 | } |
156 | } |
138 | |
157 | |
139 | void octant::display (view &ctx) |
158 | void octant::display (view &ctx) |
140 | { |
159 | { |
141 | #if 0 |
160 | #if 0 |
|
|
161 | sector s = orig - ctx.orig; |
|
|
162 | |
142 | glBegin (GL_LINES); |
163 | glBegin (GL_LINES); |
143 | sector s = orig - ctx.orig; |
|
|
144 | vec3 clr(0, 0.8, 0); |
164 | vec3 clr(0, 0.8, 0); |
145 | glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, (const GLfloat*)&clr); |
165 | glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, (const GLfloat*)&clr); |
146 | |
166 | |
147 | for (int i = 8; i--; ) |
167 | for (int i = 8; i--; ) |
148 | for (int ji = 3; ji--; ) |
168 | for (int ji = 3; ji--; ) |
149 | { |
169 | { |
150 | int j = i | (1 << ji); |
170 | int j = i | (1 << ji); |
151 | if (i != j) |
171 | if (i < j) |
152 | { |
172 | { |
153 | glVertex3i (s.x + !!(i & 1) * extent, |
173 | glVertex3i (s.x + !!(i & 1) * extent, |
154 | s.y + !!(i & 2) * extent, |
174 | s.y + !!(i & 2) * extent, |
155 | s.z + !!(i & 4) * extent); |
175 | s.z + !!(i & 4) * extent); |
156 | glVertex3i (s.x + !!(j & 1) * extent, |
176 | glVertex3i (s.x + !!(j & 1) * extent, |
… | |
… | |
161 | |
181 | |
162 | glEnd (); |
182 | glEnd (); |
163 | #endif |
183 | #endif |
164 | |
184 | |
165 | for (iterator i = end (); i != begin (); ) |
185 | for (iterator i = end (); i != begin (); ) |
|
|
186 | { |
|
|
187 | entity *e = *--i; |
|
|
188 | |
|
|
189 | sector diff = (e->a + e->b) / 2 - ctx.orig; |
|
|
190 | GLfloat z = norm (vec3 ((e->a + e->b) / 2 - ctx.orig)); |
|
|
191 | ctx.pixfact = ctx.perspfact / z; |
|
|
192 | |
166 | (*--i)->display (ctx); |
193 | e->display (ctx); |
|
|
194 | } |
167 | } |
195 | } |
168 | |
196 | |
169 | void octant::draw_bbox (view &ctx) |
197 | void octant::draw_bbox (view &ctx) |
170 | { |
198 | { |
171 | sector s = orig - ctx.orig; |
199 | sector s = orig - ctx.orig; |
172 | |
200 | |
173 | gl::draw_box (ctx, s, s + extent); |
201 | gl::draw_bbox (ctx, s, s + extent); |
174 | } |
202 | } |
175 | |
203 | |
176 | void octant::event (occ_query &ev) |
204 | void octant::event (occ_query &ev) |
177 | { |
205 | { |
178 | if (ev.r <= 5) |
206 | visibility_state &vs = ev.v.vismap[this]; |
179 | return; |
|
|
180 | |
207 | |
181 | //ev.v.vismap[this].visibility = visibility_state::FULL; |
208 | vs.last = timer.now; |
182 | ev.v.far = ev.v.near + ev.v.frustum.n.distance (orig); |
209 | vs.visibility = ev.r <= 0 |
183 | printf ("OCT(%x,%x,%x+%x) samples %d\n", orig.x, orig.y, orig.z, extent, ev.r); |
210 | ? visibility_state::OCCLUDED |
|
|
211 | : visibility_state::FULL; |
184 | } |
212 | } |
185 | |
213 | |