ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/oct.C
Revision: 1.57
Committed: Sun Oct 17 14:03:52 2004 UTC (19 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.56: +6 -14 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.56 enum visibility_state { FULL, PARTIAL, SMALL, OCCLUDED };
13    
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.8 octant world(0, sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN), MAXEXTENT);
34    
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     }
53 root 1.2 octant::~octant ()
54     {
55     for (fill = 8; fill--; )
56     delete sub[fill];
57     }
58    
59 root 1.32 static bool overlap (const sector &o1, uoffs ea, const sector &a, const sector &b)
60 root 1.4 {
61     ea /= 2;
62    
63 root 1.34 sector center_1 = o1 + ea;
64     sector size_2 = b - a;
65 root 1.54 sector center_2 = a + (size_2 >> 1);
66 root 1.33
67 root 1.34 return abs (center_1 - center_2) <= ea + size_2;
68 root 1.4 }
69    
70 root 1.32 void octant::add (entity *e)
71 root 1.2 {
72 root 1.32 const sector &a = e->a;
73     const sector &b = e->b;
74 root 1.7
75 root 1.32 if (overlap (orig, extent, a, b))
76 root 1.4 {
77 root 1.54 uoffs extent2 = extent >> 1;
78 root 1.34 uoffs size = max (abs (b - a));
79 root 1.32
80 root 1.54 if (size >= extent2 >> 1)
81 root 1.8 {
82     push_back (e);
83     e->o.push_back (this);
84     return;
85     }
86 root 1.5
87 root 1.8 for (int i = 8; i--; )
88 root 1.5 {
89 root 1.8 sector s = orig;
90     s.offset (i, extent2);
91 root 1.32
92     if (overlap (s, extent2, a, b))
93 root 1.5 {
94 root 1.8 if (!sub[i])
95 root 1.50 {
96     sub[i] = new octant (this, s, extent2);
97     fill++;
98     }
99 root 1.8
100     sub[i]->add (e);
101 root 1.5 }
102 root 1.8 }
103 root 1.4 }
104 root 1.2 }
105 root 1.1
106 root 1.32 void octant::remove (entity *e)
107 root 1.1 {
108     }
109    
110 root 1.55 bool octant::depth_pass (view &ctx)
111 root 1.1 {
112 root 1.52 oct_visibility &vs = *(oct_visibility *)get_visibility (ctx);
113 root 1.50
114 root 1.43 GLfloat extent2 = 0.5F * (GLfloat)extent;
115 root 1.51 sector centeri = orig + (extent >> 1) - ctx.orig;
116     point centerf = point (centeri) + ((extent & 1) ? 0.5F : 0.F);
117    
118 root 1.43 GLfloat rad = ctx.diagfact * extent2;
119    
120 root 1.34 if (orig <= ctx.orig && ctx.orig <= orig + extent)
121 root 1.56 vs.state = PARTIAL;
122 root 1.10 else
123     {
124 root 1.52 if (ctx.frustum.t.distance (centerf) < -rad) return false;
125     if (ctx.frustum.b.distance (centerf) < -rad) return false;
126     if (ctx.frustum.l.distance (centerf) < -rad) return false;
127     if (ctx.frustum.r.distance (centerf) < -rad) return false;
128     if (ctx.frustum.n.distance (centerf) < -rad) return false;
129 root 1.25
130 root 1.51 GLfloat fd = ctx.frustum.f.distance (centerf);
131 root 1.25
132 root 1.47 if (fd < -(ctx.c_far - ctx.z_far) -rad * 3.F)
133 root 1.52 return false;
134 root 1.10 }
135 root 1.8
136 root 1.56 if (vs.state == OCCLUDED)
137     return false;
138 root 1.36
139 root 1.51 GLfloat z = ctx.z_near + ctx.frustum.n.distance (centerf) + rad;
140 root 1.50 //printf ("z %f, perspfact %f, z*p %f\n", z, ctx.perspfact, ctx.perspfact / z);
141 root 1.45
142 root 1.56 if (vs.state == PARTIAL || vs.state == FULL)
143 root 1.45 ctx.nc_far = max (ctx.nc_far, z);
144    
145 root 1.51 // node to start with
146     unsigned char si = centeri.x > 0 ? 1 : 0
147     | centeri.y > 0 ? 2 : 0
148     | centeri.z > 0 ? 4 : 0;
149 root 1.44
150 root 1.51 //printf ("si %d C %Ld,%Ld,%Ld\n", si, centeri.x, centeri.y, centeri.z);
151 root 1.19
152     // bit-toggle to find next child for front-to-back order
153 root 1.50 static unsigned char toggle[8+1]
154 root 1.51 = { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7, 0 };
155 root 1.19
156 root 1.50 unsigned char *next = toggle;
157     do
158 root 1.19 {
159 root 1.51 si ^= *next;
160 root 1.19
161     if (sub[si])
162 root 1.55 sub[si]->depth_pass (ctx);
163 root 1.19 }
164 root 1.51 while (*++next);
165    
166     if (size ()
167 root 1.56 && (vs.state == PARTIAL || vs.state == FULL))
168 root 1.51 {
169     ctx.nz_far = max (ctx.nz_far, z);
170 root 1.55 display (ctx);
171 root 1.51 ctx.vislist.push_back (this);
172     }
173 root 1.52
174     return true;
175 root 1.19 }
176    
177     void octant::display (view &ctx)
178     {
179 root 1.17 #if 0
180 root 1.50 sector s = orig - ctx.orig;
181    
182 root 1.8 glBegin (GL_LINES);
183 root 1.13 vec3 clr(0, 0.8, 0);
184     glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, (const GLfloat*)&clr);
185 root 1.8
186     for (int i = 8; i--; )
187     for (int ji = 3; ji--; )
188     {
189     int j = i | (1 << ji);
190 root 1.50 if (i < j)
191 root 1.8 {
192     glVertex3i (s.x + !!(i & 1) * extent,
193     s.y + !!(i & 2) * extent,
194     s.z + !!(i & 4) * extent);
195     glVertex3i (s.x + !!(j & 1) * extent,
196     s.y + !!(j & 2) * extent,
197     s.z + !!(j & 4) * extent);
198     }
199     }
200    
201     glEnd ();
202     #endif
203 root 1.54 oct_visibility &vs = *(oct_visibility *)get_visibility (ctx);
204 root 1.8
205 root 1.56 if (vs.state == OCCLUDED)
206 root 1.50 {
207 root 1.56 abort ();
208     if (ctx.pass == view::POSTDEPTH)
209 root 1.54 {
210 root 1.56 ctx.begin_occ_query (*this, 0);
211 root 1.57 sector s = orig - ctx.orig;
212     gl::draw_bbox (vb_bbox, s, s + extent);
213 root 1.56 ctx.end_occ_query ();
214     }
215     }
216     else
217     {
218 root 1.57 for (iterator i = begin (); i != end (); )
219 root 1.56 {
220 root 1.57 entity *e = *i++;
221 root 1.56
222     if (!ctx.may_draw (e))
223     continue;
224    
225     evis &evs = vs.vismap[e];
226    
227     if (ctx.pass == view::POSTDEPTH)
228     {
229     if (evs.state == OCCLUDED)
230     {
231     ctx.begin_occ_query (*this, e);
232 root 1.57 gl::draw_bbox (vb_bbox, e->a - ctx.orig, e->b - ctx.orig);
233 root 1.56 ctx.end_occ_query ();
234     }
235     }
236     else
237     {
238     if (evs.state != OCCLUDED)
239     {
240     sector center = ((e->a + e->b) >> 1) - ctx.orig;
241     GLfloat z = norm (vec3 (center));
242     ctx.pixfact = ctx.perspfact / z;
243    
244     if (ctx.pass == view::DEPTH || evs.last + 1. > timer.now)
245     e->draw (ctx);
246     else
247     {
248     evs.last = timer.now;
249     ctx.begin_occ_query (*this, e);
250     e->draw (ctx);
251     ctx.end_occ_query ();
252     }
253     }
254     }
255 root 1.54 }
256 root 1.50 }
257 root 1.1 }
258 root 1.8
259 root 1.53 #if 0
260 root 1.38 void octant::draw_bbox (view &ctx)
261 root 1.25 {
262     sector s = orig - ctx.orig;
263 root 1.27
264 root 1.50 gl::draw_bbox (ctx, s, s + extent);
265 root 1.25 }
266 root 1.53 #endif
267 root 1.8
268 root 1.38 void octant::event (occ_query &ev)
269     {
270 root 1.56 oct_visibility &vs = *(oct_visibility *)get_visibility (ev.ctx);
271     entity *e = (entity *)ev.id;
272    
273     if (e)
274     {
275     evis &evs = vs.vismap[e];
276     evs.state = ev.count ? FULL : OCCLUDED;
277     }
278     else
279     {
280 root 1.57 //vs.state = ev.count ? FULL : OCCLUDED;
281 root 1.56 }
282    
283 root 1.52 #if 0
284 root 1.44 visibility_state &vs = ev.v.vismap[this];
285    
286     vs.last = timer.now;
287 root 1.56 vs.state = ev.r <= 0
288 root 1.44 ? visibility_state::OCCLUDED
289     : visibility_state::FULL;
290 root 1.52 #endif
291     }
292    
293 root 1.2