… | |
… | |
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 | } |
… | |
… | |
74 | } |
77 | } |
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]; |
|
|
82 | |
|
|
83 | if (vs.generation + 1 != ctx.generation) |
|
|
84 | vs.visibility = visibility_state::UNKNOWN; |
|
|
85 | |
|
|
86 | vs.generation = ctx.generation; |
79 | |
87 | |
80 | GLfloat extent2 = 0.5F * (GLfloat)extent; |
88 | GLfloat extent2 = 0.5F * (GLfloat)extent; |
81 | point center = point (orig + (extent >> 1) - ctx.orig); |
89 | point center = point (orig + (extent >> 1) - ctx.orig); |
82 | if (extent & 1) center = center + 0.5F; |
90 | if (extent & 1) center = center + 0.5F; |
83 | GLfloat rad = ctx.diagfact * extent2; |
91 | GLfloat rad = ctx.diagfact * extent2; |
84 | |
|
|
85 | if (vs.generation + 1 != ctx.generation) |
|
|
86 | vs.visibility = visibility_state::UNKNOWN; |
|
|
87 | |
|
|
88 | vs.generation = ctx.generation; |
|
|
89 | |
92 | |
90 | if (orig <= ctx.orig && ctx.orig <= orig + extent) |
93 | if (orig <= ctx.orig && ctx.orig <= orig + extent) |
91 | vs.visibility = visibility_state::PARTIAL; |
94 | vs.visibility = visibility_state::PARTIAL; |
92 | else |
95 | else |
93 | { |
96 | { |
… | |
… | |
114 | if (vs.visibility == visibility_state::UNKNOWN) |
117 | if (vs.visibility == visibility_state::UNKNOWN) |
115 | vs.visibility = visibility_state::FULL; |
118 | vs.visibility = visibility_state::FULL; |
116 | #endif |
119 | #endif |
117 | |
120 | |
118 | GLfloat z = ctx.z_near + ctx.frustum.n.distance (center) + rad; |
121 | GLfloat z = ctx.z_near + ctx.frustum.n.distance (center) + rad; |
|
|
122 | //printf ("z %f, perspfact %f, z*p %f\n", z, ctx.perspfact, ctx.perspfact / z); |
119 | |
123 | |
120 | if (vs.visibility == visibility_state::FULL) |
124 | if (vs.visibility == visibility_state::FULL) |
121 | ctx.nc_far = max (ctx.nc_far, z); |
125 | ctx.nc_far = max (ctx.nc_far, z); |
122 | |
126 | |
123 | if (size () |
127 | if (size () |
… | |
… | |
132 | unsigned char si = ctx.d.x < 0 ? 1 : 0 |
136 | unsigned char si = ctx.d.x < 0 ? 1 : 0 |
133 | | ctx.d.y < 0 ? 2 : 0 |
137 | | ctx.d.y < 0 ? 2 : 0 |
134 | | ctx.d.z < 0 ? 4 : 0; |
138 | | ctx.d.z < 0 ? 4 : 0; |
135 | |
139 | |
136 | // bit-toggle to find next child for front-to-back order |
140 | // bit-toggle to find next child for front-to-back order |
137 | static unsigned char next[8] |
141 | static unsigned char toggle[8+1] |
138 | = { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7 }; |
142 | = { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7, 0 }; |
139 | |
143 | |
140 | for (int i = 0; i < 8; i++) |
144 | unsigned char *next = toggle; |
|
|
145 | do |
141 | { |
146 | { |
142 | si ^= next[i]; |
147 | si ^= *next++; |
143 | |
148 | |
144 | if (sub[si]) |
149 | if (sub[si]) |
145 | sub[si]->detect_visibility (ctx); |
150 | sub[si]->detect_visibility (ctx); |
146 | } |
151 | } |
|
|
152 | while (*next); |
147 | } |
153 | } |
148 | |
154 | |
149 | void octant::display (view &ctx) |
155 | void octant::display (view &ctx) |
150 | { |
156 | { |
151 | #if 0 |
157 | #if 0 |
|
|
158 | sector s = orig - ctx.orig; |
|
|
159 | |
152 | glBegin (GL_LINES); |
160 | glBegin (GL_LINES); |
153 | sector s = orig - ctx.orig; |
|
|
154 | vec3 clr(0, 0.8, 0); |
161 | vec3 clr(0, 0.8, 0); |
155 | glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, (const GLfloat*)&clr); |
162 | glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, (const GLfloat*)&clr); |
156 | |
163 | |
157 | for (int i = 8; i--; ) |
164 | for (int i = 8; i--; ) |
158 | for (int ji = 3; ji--; ) |
165 | for (int ji = 3; ji--; ) |
159 | { |
166 | { |
160 | int j = i | (1 << ji); |
167 | int j = i | (1 << ji); |
161 | if (i != j) |
168 | if (i < j) |
162 | { |
169 | { |
163 | glVertex3i (s.x + !!(i & 1) * extent, |
170 | glVertex3i (s.x + !!(i & 1) * extent, |
164 | s.y + !!(i & 2) * extent, |
171 | s.y + !!(i & 2) * extent, |
165 | s.z + !!(i & 4) * extent); |
172 | s.z + !!(i & 4) * extent); |
166 | glVertex3i (s.x + !!(j & 1) * extent, |
173 | glVertex3i (s.x + !!(j & 1) * extent, |
… | |
… | |
171 | |
178 | |
172 | glEnd (); |
179 | glEnd (); |
173 | #endif |
180 | #endif |
174 | |
181 | |
175 | for (iterator i = end (); i != begin (); ) |
182 | for (iterator i = end (); i != begin (); ) |
|
|
183 | { |
|
|
184 | entity *e = *--i; |
|
|
185 | |
|
|
186 | sector diff = (e->a + e->b) / 2 - ctx.orig; |
|
|
187 | GLfloat z = norm (vec3 ((e->a + e->b) / 2 - ctx.orig)); |
|
|
188 | ctx.pixfact = ctx.perspfact / z; |
|
|
189 | |
176 | (*--i)->display (ctx); |
190 | e->display (ctx); |
|
|
191 | } |
177 | } |
192 | } |
178 | |
193 | |
179 | void octant::draw_bbox (view &ctx) |
194 | void octant::draw_bbox (view &ctx) |
180 | { |
195 | { |
181 | sector s = orig - ctx.orig; |
196 | sector s = orig - ctx.orig; |
182 | |
197 | |
183 | gl::draw_box (ctx, s, s + extent); |
198 | gl::draw_bbox (ctx, s, s + extent); |
184 | } |
199 | } |
185 | |
200 | |
186 | void octant::event (occ_query &ev) |
201 | void octant::event (occ_query &ev) |
187 | { |
202 | { |
188 | visibility_state &vs = ev.v.vismap[this]; |
203 | visibility_state &vs = ev.v.vismap[this]; |