ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/oct.C
Revision: 1.42
Committed: Sat Oct 9 16:52:06 2004 UTC (19 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.41: +3 -4 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #include <cstdlib>
2
3 #include <vector>
4
5 using namespace std;
6
7 #include <GL/gl.h>
8
9 #include "oct.h"
10 #include "view.h"
11 #include "entity.h"
12
13 octant world(0, sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN), MAXEXTENT);
14
15 octant::octant (octant *parent, const sector &orig, uoffs extent)
16 : parent(parent), orig(orig), extent(extent)
17 {
18 for (fill = 8; fill--; )
19 sub[fill] = 0;
20 }
21
22 octant::~octant ()
23 {
24 for (fill = 8; fill--; )
25 delete sub[fill];
26 }
27
28 static bool overlap (const sector &o1, uoffs ea, const sector &a, const sector &b)
29 {
30 ea /= 2;
31
32 sector center_1 = o1 + ea;
33 sector size_2 = b - a;
34 sector center_2 = a + size_2 / 2;
35
36 return abs (center_1 - center_2) <= ea + size_2;
37 }
38
39 void octant::add (entity *e)
40 {
41 const sector &a = e->a;
42 const sector &b = e->b;
43
44 if (overlap (orig, extent, a, b))
45 {
46 uoffs extent2 = extent / 2;
47 uoffs size = max (abs (b - a));
48
49 if (size > extent2 || !extent2)
50 {
51 push_back (e);
52 e->o.push_back (this);
53 return;
54 }
55
56 for (int i = 8; i--; )
57 {
58 sector s = orig;
59 s.offset (i, extent2);
60
61 if (overlap (s, extent2, a, b))
62 {
63 if (!sub[i])
64 sub[i] = new octant (this, s, extent2);
65
66 sub[i]->add (e);
67 }
68 }
69 }
70 }
71
72 void octant::remove (entity *e)
73 {
74 }
75
76 void octant::detect_visibility (view &ctx)
77 {
78 visibility_state &vs = ctx.vismap[this];
79
80 if (vs.generation != ctx.generation)
81 vs.visibility = visibility_state::UNKNOWN;
82
83 if (orig <= ctx.orig && ctx.orig <= orig + extent)
84 {
85 vs.visibility = visibility_state::PARTIAL;
86 vs.generation = ctx.generation;
87 }
88 else
89 {
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;
96 if (ctx.frustum.b.distance (center) < -rad) return;
97 if (ctx.frustum.l.distance (center) < -rad) return;
98 if (ctx.frustum.r.distance (center) < -rad) return;
99 if (ctx.frustum.n.distance (center) < -rad) return;
100
101 GLfloat fd = ctx.frustum.f.distance (center);
102
103 if (fd < -rad)
104 {
105 if (fd < -rad * 3.F)
106 return;
107
108 if (vs.visibility == visibility_state::UNKNOWN)
109 {
110 ctx.farlist.push_back (this);
111 return;
112 }
113 }
114 }
115
116 if (vs.visibility == visibility_state::UNKNOWN)
117 vs.visibility = visibility_state::FULL;
118
119 if (size ()
120 && (vs.visibility == visibility_state::PARTIAL
121 || vs.visibility == visibility_state::FULL))
122 ctx.vislist.push_back (this);
123
124 // node to start with
125 unsigned char si = ctx.d.x < 0 ? 1 : 0
126 | ctx.d.y < 0 ? 2 : 0
127 | ctx.d.z < 0 ? 4 : 0;
128
129 // bit-toggle to find next child for front-to-back order
130 static unsigned char next[8]
131 = { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7 };
132
133 for (int i = 0; i < 8; i++)
134 {
135 si ^= next[i];
136
137 if (sub[si])
138 sub[si]->detect_visibility (ctx);
139 }
140
141 vs.generation = ctx.generation;
142 }
143
144 void octant::display (view &ctx)
145 {
146 #if 0
147 glBegin (GL_LINES);
148 sector s = orig - ctx.orig;
149 vec3 clr(0, 0.8, 0);
150 glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, (const GLfloat*)&clr);
151
152 for (int i = 8; i--; )
153 for (int ji = 3; ji--; )
154 {
155 int j = i | (1 << ji);
156 if (i != j)
157 {
158 glVertex3i (s.x + !!(i & 1) * extent,
159 s.y + !!(i & 2) * extent,
160 s.z + !!(i & 4) * extent);
161 glVertex3i (s.x + !!(j & 1) * extent,
162 s.y + !!(j & 2) * extent,
163 s.z + !!(j & 4) * extent);
164 }
165 }
166
167 glEnd ();
168 #endif
169
170 for (iterator i = end (); i != begin (); )
171 (*--i)->display (ctx);
172 }
173
174 void octant::draw_bbox (view &ctx)
175 {
176 sector s = orig - ctx.orig;
177
178 gl::draw_box (ctx, s, s + extent);
179 }
180
181 void octant::event (occ_query &ev)
182 {
183 ev.v.vismap[this].visibility = ev.r < 4
184 ? visibility_state::OCCLUDED
185 : visibility_state::FULL;
186 ev.v.far = ev.v.near + ev.v.frustum.n.distance (orig);
187 printf ("OCT(%x,%x,%x+%x) samples %d\n", orig.x, orig.y, orig.z, extent, ev.r);
188 }
189