ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/oct.C
Revision: 1.91
Committed: Wed Aug 10 02:13:50 2005 UTC (18 years, 10 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.90: +37 -17 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 root 1.91
5 root 1.8 using namespace std;
6    
7 root 1.48 #include "opengl.h"
8 root 1.8
9 root 1.1 #include "oct.h"
10 root 1.8 #include "view.h"
11 root 1.2 #include "entity.h"
12 root 1.89 #include "material.h" //DEBUG
13 root 1.2
14 root 1.90 enum visibility_state {
15     PARTIAL, // surrounding the viewpoint
16     FULL, // in frustum
17     OCCLUDED, // in frustom, but occluded
18     };
19 root 1.56
20 root 1.54 struct evis {
21 root 1.79 entity *e;
22 root 1.56 visibility_state state;
23 root 1.54 double last; // time of last check
24 root 1.79
25     void clear ()
26     {
27     last = 0.;
28     state = FULL;
29     }
30    
31     evis ()
32     {
33     clear ();
34     }
35 root 1.54 };
36    
37 root 1.52 struct oct_visibility : visibility_base
38     {
39 root 1.79 vector<evis> vismap;
40 root 1.52
41 root 1.56 visibility_state state;
42 root 1.91 bool vis;
43 root 1.52
44 root 1.79 evis &get_visibility (int i, entity *e)
45     {
46     evis &evs = vismap [i];
47    
48     if (evs.e != e)
49     {
50     evs.clear ();
51     evs.e = e;
52     }
53    
54     return evs;
55     }
56    
57 root 1.52 oct_visibility (octant &oct)
58 root 1.91 : state (FULL), vis (true)
59 root 1.52 {
60     }
61     };
62    
63 root 1.75 octant world(0, sector (0, 0, 0), MAXEXTENT);
64 root 1.8
65     octant::octant (octant *parent, const sector &orig, uoffs extent)
66 root 1.54 : parent(parent)
67     , orig(orig)
68     , extent(extent)
69 root 1.8 {
70     for (fill = 8; fill--; )
71     sub[fill] = 0;
72 root 1.2 }
73    
74 root 1.53 visibility_base *octant::new_visibility ()
75     {
76     return new oct_visibility (*this);
77     }
78    
79     void octant::clear_visibility (visibility_base *vs)
80     {
81     ((oct_visibility *)vs)->vismap.clear ();
82 root 1.75 ((oct_visibility *)vs)->state = FULL;
83 root 1.53 }
84 root 1.65
85 root 1.2 octant::~octant ()
86     {
87     for (fill = 8; fill--; )
88     delete sub[fill];
89     }
90    
91 root 1.75 static bool overlap (const sector &orig, uoffs extent, const sector &a, const sector &b)
92 root 1.4 {
93 root 1.75 sector size = (b - a + 1) >> 1;
94     sector center = a + size;
95 root 1.4
96 root 1.75 return abs (orig - center) <= extent + size;
97     }
98 root 1.33
99 root 1.75 static sector offset (const sector &s, int subindex, uoffs extent)
100     {
101     return sector (
102     s.x + (subindex & 1 ? extent : -extent),
103     s.y + (subindex & 2 ? extent : -extent),
104     s.z + (subindex & 4 ? extent : -extent)
105     );
106 root 1.4 }
107    
108 root 1.32 void octant::add (entity *e)
109 root 1.2 {
110 root 1.32 const sector &a = e->a;
111     const sector &b = e->b;
112 root 1.7
113 root 1.80 uoffs size = max (abs (b - a));
114    
115     if (size >= extent >> 4)
116 root 1.4 {
117 root 1.80 if (overlap (orig, extent, a, b))
118 root 1.8 {
119     push_back (e);
120     e->o.push_back (this);
121     }
122 root 1.80 }
123     else
124     {
125     uoffs extent2 = extent >> 1;
126 root 1.5
127 root 1.8 for (int i = 8; i--; )
128 root 1.5 {
129 root 1.75 sector s = offset (orig, i, extent2);
130 root 1.32
131     if (overlap (s, extent2, a, b))
132 root 1.5 {
133 root 1.8 if (!sub[i])
134 root 1.50 {
135     sub[i] = new octant (this, s, extent2);
136     fill++;
137     }
138 root 1.8
139     sub[i]->add (e);
140 root 1.5 }
141 root 1.8 }
142 root 1.4 }
143 root 1.2 }
144 root 1.1
145 root 1.32 void octant::remove (entity *e)
146 root 1.1 {
147     }
148    
149 root 1.80 bool octant::detect_visibility (view &ctx) //, bool fully_visible)
150 root 1.1 {
151 root 1.75 sector centeri = orig - ctx.orig;
152     point centerf = point (centeri);
153 root 1.51
154 root 1.75 GLfloat rad = ctx.diagfact * extent;
155 root 1.43
156 root 1.59 if (!overlap (ctx.frustum.c, sphere (centerf, rad)))
157     return false;
158 root 1.65
159     oct_visibility &vs = *(oct_visibility *)get_visibility (ctx);
160 root 1.59
161 root 1.90 if (max (centeri) < extent)
162     vs.state = PARTIAL;
163 root 1.10 else
164     {
165 root 1.90 if (distance (ctx.frustum.t, centerf) < -rad) return false;
166     if (distance (ctx.frustum.b, centerf) < -rad) return false;
167     if (distance (ctx.frustum.l, centerf) < -rad) return false;
168     if (distance (ctx.frustum.r, centerf) < -rad) return false;
169     if (distance (ctx.frustum.n, centerf) < -rad) return false;
170     }
171 root 1.25
172 root 1.59 #if 0
173 root 1.91 GLfloat fd = distance (ctx.frustum.f, centerf);
174 root 1.25
175 root 1.91 if (fd < -(ctx.c_far - ctx.z_far) -rad * 3.F)
176     return false;
177 root 1.80 #endif
178 root 1.8
179 root 1.75 // very important, optimize?
180 root 1.85 GLfloat z = length (ctx.p - centerf) + rad;
181 root 1.86 if (z < 0.F || ctx.perspfact * extent / z < 10.) // very crude "too small to see" check
182 root 1.58 return false;
183 root 1.45
184 root 1.59 #if 0
185 root 1.56 if (vs.state == PARTIAL || vs.state == FULL)
186 root 1.45 ctx.nc_far = max (ctx.nc_far, z);
187 root 1.59 #endif
188 root 1.45
189 root 1.80 if (vs.state == OCCLUDED)
190 root 1.72 {
191 root 1.90 ctx.postdepthlist.push_back (this);
192 root 1.91
193 root 1.90 return false;
194 root 1.72 }
195    
196 root 1.91 //bool visible = size () && vs.state != OCCLUDED;
197     bool visible = false;
198 root 1.74
199 root 1.51 // node to start with
200     unsigned char si = centeri.x > 0 ? 1 : 0
201     | centeri.y > 0 ? 2 : 0
202     | centeri.z > 0 ? 4 : 0;
203 root 1.19
204     // bit-toggle to find next child for front-to-back order
205 root 1.50 static unsigned char toggle[8+1]
206 root 1.51 = { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7, 0 };
207 root 1.19
208 root 1.50 unsigned char *next = toggle;
209     do
210 root 1.19 {
211 root 1.51 si ^= *next;
212 root 1.19
213     if (sub[si])
214 root 1.69 visible = visible | sub[si]->detect_visibility (ctx);
215 root 1.19 }
216 root 1.51 while (*++next);
217    
218 root 1.90 if (visible)
219 root 1.71 {
220     if (size ())
221 root 1.91 {
222     ctx.postdepthlist.push_back (this);
223     ctx.vislist.push_back (this);
224     }
225    
226     vs.state = FULL;
227 root 1.80
228 root 1.91 return true;
229     }
230     else if (vs.state == OCCLUDED)
231     {
232 root 1.80 ctx.postdepthlist.push_back (this);
233 root 1.91
234     return false;
235 root 1.71 }
236 root 1.91 else if (size())
237 root 1.81 {
238     ctx.postdepthlist.push_back (this);
239 root 1.91 ctx.vislist.push_back (this);
240    
241     return true;
242     }
243     else
244     {
245     return vs.state != PARTIAL;
246 root 1.81 }
247 root 1.19 }
248    
249 root 1.75 void octant::draw_depth (view &ctx)
250 root 1.19 {
251 root 1.54 oct_visibility &vs = *(oct_visibility *)get_visibility (ctx);
252 root 1.8
253 root 1.79 vs.vismap.resize (size ());
254    
255 root 1.90 if (vs.state == OCCLUDED)
256 root 1.75 return;
257    
258 root 1.79 for (int i = 0; i < size (); ++i)
259 root 1.50 {
260 root 1.79 entity *e = (*this)[i];
261     const evis &evs = vs.get_visibility (i, e);
262 root 1.75
263     if (evs.state != OCCLUDED)
264 root 1.54 {
265 root 1.75 if (!ctx.may_draw (e))
266     continue;
267    
268     sector center = ((e->a + e->b) >> 1) - ctx.orig;
269     GLfloat z = length (vec3 (center));
270     ctx.pixfact = ctx.perspfact / z;
271    
272     ctx.nz_far = max (ctx.nz_far, z + extent);
273     ctx.nz_near = min (ctx.nz_near, z - extent);
274    
275     e->draw (ctx);
276 root 1.56 }
277     }
278 root 1.75 }
279    
280     void octant::draw_postdepth (view &ctx)
281     {
282     oct_visibility &vs = *(oct_visibility *)get_visibility (ctx);
283    
284 root 1.91 if (vs.state == OCCLUDED || !vs.vis)
285 root 1.75 {
286     ctx.begin_occ_query (*this, 0);
287     sector s = orig - ctx.orig;
288     gl::draw_bbox (s - extent, s + extent);
289     ctx.end_occ_query ();
290     }
291 root 1.56 else
292     {
293 root 1.91 vs.vis = !size ();
294 root 1.64
295 root 1.79 for (int i = 0; i < size (); ++i)
296 root 1.56 {
297 root 1.79 entity *e = (*this)[i];
298     const evis &evs = vs.get_visibility (i, e);
299 root 1.56
300 root 1.75 if (evs.state == OCCLUDED)
301 root 1.56 {
302 root 1.75 if (!ctx.may_draw (e))
303     continue;
304    
305     ctx.begin_occ_query (*this, e);
306     gl::draw_bbox (e->a - ctx.orig, e->b - ctx.orig);
307     ctx.end_occ_query ();
308 root 1.56 }
309     else
310 root 1.91 vs.vis = true;
311 root 1.75 }
312 root 1.91 vs.vis = true;//D
313 root 1.75 }
314     }
315    
316     void octant::draw_lighted (view &ctx)
317     {
318     oct_visibility &vs = *(oct_visibility *)get_visibility (ctx);
319    
320 root 1.90 #if 0
321     if (vs.state == FULL || vs.state == OCCLUDED)
322 root 1.87 {
323 root 1.90 sector s = orig - ctx.orig;
324     debugmat->enable (ctx);
325     if (max (s) >= extent)
326     gl::draw_bbox (s - extent, s + extent);
327     debugmat->disable (ctx);
328     //printf ("DLP %ld %ld %ld (%ld)\n", orig.x, orig.y, orig.z, extent);//D
329 root 1.87 }
330     #endif
331    
332 root 1.90 if (vs.state == OCCLUDED)
333 root 1.75 return;
334 root 1.90
335 root 1.79 for (int i = 0; i < size (); ++i)
336 root 1.75 {
337 root 1.79 entity *e = (*this)[i];
338     evis &evs = vs.get_visibility (i, e);
339 root 1.75
340     if (evs.state != OCCLUDED)
341     {
342 root 1.66 if (!ctx.may_draw (e))
343     continue;
344    
345 root 1.75 sector center = ((e->a + e->b) >> 1) - ctx.orig;
346     GLfloat z = length (vec3 (center));
347     ctx.pixfact = ctx.perspfact / z;
348    
349 root 1.91 if (!ctx.first_lighted
350 root 1.87 || evs.last + 1. > timer.now)
351 root 1.75 e->draw (ctx);
352     else
353     {
354     evs.last = timer.now;
355     ctx.begin_occ_query (*this, e);
356     e->draw (ctx);
357     ctx.end_occ_query ();
358 root 1.56 }
359 root 1.54 }
360 root 1.50 }
361 root 1.1 }
362 root 1.8
363 root 1.38 void octant::event (occ_query &ev)
364     {
365 root 1.56 oct_visibility &vs = *(oct_visibility *)get_visibility (ev.ctx);
366     entity *e = (entity *)ev.id;
367    
368     if (e)
369     {
370 root 1.91 #if 1
371 root 1.79 for (vector<evis>::iterator i = vs.vismap.begin ();
372     i != vs.vismap.end ();
373     ++i)
374     if (i->e == e)
375     {
376     i->state = ev.count ? FULL : OCCLUDED;
377     return;
378     }
379 root 1.91 #endif
380 root 1.56 }
381     else
382 root 1.90 {
383     vs.state = ev.count ? FULL : OCCLUDED;
384 root 1.91 vs.vis = ev.count ? true : false;
385 root 1.90 }
386 root 1.52 }
387    
388 root 1.2