ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/oct.C
Revision: 1.76
Committed: Tue Nov 9 22:33:18 2004 UTC (19 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.75: +0 -9 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #include <cstdlib>
2    
3 root 1.8 #include <vector>
4     using namespace std;
5    
6 root 1.48 #include "opengl.h"
7 root 1.8
8 root 1.1 #include "oct.h"
9 root 1.8 #include "view.h"
10 root 1.2 #include "entity.h"
11    
12 root 1.72 enum visibility_state { FULL, PARTIAL, SMALL, OCCLUDED, SUBTREE_OCCLUDED };
13 root 1.56
14 root 1.54 struct evis {
15 root 1.56 visibility_state state;
16 root 1.54 double last; // time of last check
17     evis () : last(0.), state(FULL) { };
18     };
19    
20 root 1.52 struct oct_visibility : visibility_base
21     {
22 root 1.54 typedef map<entity *, evis> evismap;
23     evismap vismap;
24 root 1.52
25 root 1.56 visibility_state state;
26 root 1.52
27     oct_visibility (octant &oct)
28 root 1.54 : state(FULL)
29 root 1.52 {
30     }
31     };
32    
33 root 1.75 octant world(0, sector (0, 0, 0), MAXEXTENT);
34 root 1.8
35     octant::octant (octant *parent, const sector &orig, uoffs extent)
36 root 1.54 : parent(parent)
37     , orig(orig)
38     , extent(extent)
39 root 1.8 {
40     for (fill = 8; fill--; )
41     sub[fill] = 0;
42 root 1.2 }
43    
44 root 1.53 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 root 1.75 ((oct_visibility *)vs)->state = FULL;
53 root 1.53 }
54 root 1.65
55 root 1.2 octant::~octant ()
56     {
57     for (fill = 8; fill--; )
58     delete sub[fill];
59     }
60    
61 root 1.75 static bool overlap (const sector &orig, uoffs extent, const sector &a, const sector &b)
62 root 1.4 {
63 root 1.75 sector size = (b - a + 1) >> 1;
64     sector center = a + size;
65 root 1.4
66 root 1.75 return abs (orig - center) <= extent + size;
67     }
68 root 1.33
69 root 1.75 static sector offset (const sector &s, int subindex, uoffs extent)
70     {
71     return sector (
72     s.x + (subindex & 1 ? extent : -extent),
73     s.y + (subindex & 2 ? extent : -extent),
74     s.z + (subindex & 4 ? extent : -extent)
75     );
76 root 1.4 }
77    
78 root 1.32 void octant::add (entity *e)
79 root 1.2 {
80 root 1.32 const sector &a = e->a;
81     const sector &b = e->b;
82 root 1.7
83 root 1.32 if (overlap (orig, extent, a, b))
84 root 1.4 {
85 root 1.54 uoffs extent2 = extent >> 1;
86 root 1.34 uoffs size = max (abs (b - a));
87 root 1.32
88 root 1.75 if (size >= extent2)
89 root 1.8 {
90     push_back (e);
91     e->o.push_back (this);
92     return;
93     }
94 root 1.5
95 root 1.8 for (int i = 8; i--; )
96 root 1.5 {
97 root 1.75 sector s = offset (orig, i, extent2);
98 root 1.32
99     if (overlap (s, extent2, a, b))
100 root 1.5 {
101 root 1.8 if (!sub[i])
102 root 1.50 {
103     sub[i] = new octant (this, s, extent2);
104     fill++;
105     }
106 root 1.8
107     sub[i]->add (e);
108 root 1.5 }
109 root 1.8 }
110 root 1.4 }
111 root 1.2 }
112 root 1.1
113 root 1.32 void octant::remove (entity *e)
114 root 1.1 {
115     }
116    
117 root 1.62 bool octant::detect_visibility (view &ctx)
118 root 1.1 {
119 root 1.75 sector centeri = orig - ctx.orig;
120     point centerf = point (centeri);
121 root 1.51
122 root 1.75 GLfloat rad = ctx.diagfact * extent;
123 root 1.43
124 root 1.59 if (!overlap (ctx.frustum.c, sphere (centerf, rad)))
125     return false;
126 root 1.65
127     oct_visibility &vs = *(oct_visibility *)get_visibility (ctx);
128 root 1.59
129 root 1.75 if (max (abs (centeri)) <= extent)
130 root 1.56 vs.state = PARTIAL;
131 root 1.10 else
132     {
133 root 1.59 if (distance (ctx.frustum.t, centerf) < -rad) return false;
134     if (distance (ctx.frustum.b, centerf) < -rad) return false;
135     if (distance (ctx.frustum.l, centerf) < -rad) return false;
136     if (distance (ctx.frustum.r, centerf) < -rad) return false;
137     if (distance (ctx.frustum.n, centerf) < -rad) return false;
138 root 1.25
139 root 1.59 #if 0
140     GLfloat fd = distance (ctx.frustum.f, centerf);
141 root 1.25
142 root 1.47 if (fd < -(ctx.c_far - ctx.z_far) -rad * 3.F)
143 root 1.52 return false;
144 root 1.58 #endif
145 root 1.10 }
146 root 1.8
147 root 1.75 // very important, optimize?
148 root 1.59 GLfloat z = ctx.z_near + distance (ctx.frustum.n, centerf) + rad;
149 root 1.58 if (ctx.perspfact * extent / z < 1.) // very crude "too small to see" check
150     return false;
151 root 1.45
152 root 1.59 #if 0
153 root 1.56 if (vs.state == PARTIAL || vs.state == FULL)
154 root 1.45 ctx.nc_far = max (ctx.nc_far, z);
155 root 1.59 #endif
156 root 1.45
157 root 1.72 if (vs.state == SUBTREE_OCCLUDED)
158     {
159     ctx.vislist.push_back (this);
160     return false;
161     }
162    
163 root 1.74 bool visible = size () && (vs.state == PARTIAL || vs.state == FULL);
164    
165 root 1.51 // node to start with
166     unsigned char si = centeri.x > 0 ? 1 : 0
167     | centeri.y > 0 ? 2 : 0
168     | centeri.z > 0 ? 4 : 0;
169 root 1.19
170     // bit-toggle to find next child for front-to-back order
171 root 1.50 static unsigned char toggle[8+1]
172 root 1.51 = { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7, 0 };
173 root 1.19
174 root 1.50 unsigned char *next = toggle;
175     do
176 root 1.19 {
177 root 1.51 si ^= *next;
178 root 1.19
179     if (sub[si])
180 root 1.69 visible = visible | sub[si]->detect_visibility (ctx);
181 root 1.19 }
182 root 1.51 while (*++next);
183    
184 root 1.71 if (visible)
185     {
186     if (size ())
187     ctx.vislist.push_back (this);
188     }
189     else
190     {
191 root 1.72 vs.state = SUBTREE_OCCLUDED;
192 root 1.71 ctx.vislist.push_back (this);
193     }
194 root 1.52
195 root 1.69 return visible;
196 root 1.19 }
197    
198 root 1.75 void octant::draw_depth (view &ctx)
199 root 1.19 {
200 root 1.54 oct_visibility &vs = *(oct_visibility *)get_visibility (ctx);
201 root 1.8
202 root 1.72 if (vs.state == OCCLUDED || vs.state == SUBTREE_OCCLUDED)
203 root 1.75 return;
204    
205     for (iterator i = begin (); i != end (); )
206 root 1.50 {
207 root 1.75 entity *e = *i++;
208    
209     evis &evs = vs.vismap[e];
210    
211     if (evs.state != OCCLUDED)
212 root 1.54 {
213 root 1.75 if (!ctx.may_draw (e))
214     continue;
215    
216     sector center = ((e->a + e->b) >> 1) - ctx.orig;
217     GLfloat z = length (vec3 (center));
218     ctx.pixfact = ctx.perspfact / z;
219    
220     ctx.nz_far = max (ctx.nz_far, z + extent);
221     ctx.nz_near = min (ctx.nz_near, z - extent);
222    
223     e->draw (ctx);
224 root 1.56 }
225     }
226 root 1.75 }
227    
228     void octant::draw_postdepth (view &ctx)
229     {
230     oct_visibility &vs = *(oct_visibility *)get_visibility (ctx);
231    
232     if (vs.state == OCCLUDED || vs.state == SUBTREE_OCCLUDED)
233     {
234     ctx.begin_occ_query (*this, 0);
235     sector s = orig - ctx.orig;
236     gl::draw_bbox (s - extent, s + extent);
237     ctx.end_occ_query ();
238     }
239 root 1.56 else
240     {
241 root 1.64 int nvis = 0;
242    
243 root 1.57 for (iterator i = begin (); i != end (); )
244 root 1.56 {
245 root 1.57 entity *e = *i++;
246 root 1.56
247     evis &evs = vs.vismap[e];
248    
249 root 1.75 if (evs.state == OCCLUDED)
250 root 1.56 {
251 root 1.75 if (!ctx.may_draw (e))
252     continue;
253    
254     ctx.begin_occ_query (*this, e);
255     gl::draw_bbox (e->a - ctx.orig, e->b - ctx.orig);
256     ctx.end_occ_query ();
257 root 1.56 }
258     else
259 root 1.75 nvis++;
260     }
261    
262     if (nvis == 0 && size ())
263     vs.state = OCCLUDED;
264     }
265     }
266    
267     void octant::draw_lighted (view &ctx)
268     {
269     oct_visibility &vs = *(oct_visibility *)get_visibility (ctx);
270    
271     if (vs.state == OCCLUDED || vs.state == SUBTREE_OCCLUDED)
272     return;
273    
274     for (iterator i = begin (); i != end (); )
275     {
276     entity *e = *i++;
277    
278     evis &evs = vs.vismap[e];
279    
280     if (evs.state != OCCLUDED)
281     {
282 root 1.66 if (!ctx.may_draw (e))
283     continue;
284    
285 root 1.75 sector center = ((e->a + e->b) >> 1) - ctx.orig;
286     GLfloat z = length (vec3 (center));
287     ctx.pixfact = ctx.perspfact / z;
288    
289     if (ctx.pass->type != LIGHTED
290     || !ctx.first_lighted
291     || evs.last + 0.1 > timer.now)
292     e->draw (ctx);
293     else
294     {
295     evs.last = timer.now;
296     ctx.begin_occ_query (*this, e);
297     e->draw (ctx);
298     ctx.end_occ_query ();
299 root 1.56 }
300 root 1.54 }
301 root 1.50 }
302 root 1.1 }
303 root 1.8
304 root 1.38 void octant::event (occ_query &ev)
305     {
306 root 1.56 oct_visibility &vs = *(oct_visibility *)get_visibility (ev.ctx);
307     entity *e = (entity *)ev.id;
308    
309     if (e)
310     {
311     evis &evs = vs.vismap[e];
312     evs.state = ev.count ? FULL : OCCLUDED;
313     }
314     else
315 root 1.64 vs.state = ev.count ? FULL : OCCLUDED;
316 root 1.52 }
317    
318 root 1.2