ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/oct.C
Revision: 1.34
Committed: Fri Oct 8 09:59:25 2004 UTC (19 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.33: +8 -18 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #include <cstdlib>
2
3 #include <vector>
4
5 using namespace std;
6
7 #define GL_GLEXT_PROTOTYPES
8 #include <GL/gl.h>
9 #include <GL/glext.h>
10
11 #include "oct.h"
12 #include "view.h"
13 #include "entity.h"
14
15 vector<GLuint> occ_query_objects;
16
17 static GLuint begin_occ_query ()
18 {
19 GLuint id;
20
21 if (occ_query_objects.size ())
22 {
23 id = *(occ_query_objects.end () - 1);
24 occ_query_objects.pop_back ();
25 }
26 else
27 glGenQueriesARB (1, &id);
28
29 glBeginQueryARB (GL_SAMPLES_PASSED, id);
30 return id;
31 }
32
33 #define end_occ_query() glEndQueryARB (GL_SAMPLES_PASSED)
34
35 GLuint occ_query_result (GLuint id)
36 {
37 GLuint count;
38
39 glGetQueryObjectuivARB (id, GL_QUERY_RESULT, &count);
40 occ_query_objects.push_back (id);
41
42 return count;
43 }
44
45 octant world(0, sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN), MAXEXTENT);
46
47 octant::octant (octant *parent, const sector &orig, uoffs extent)
48 : parent(parent), orig(orig), extent(extent)
49 {
50 for (fill = 8; fill--; )
51 sub[fill] = 0;
52 }
53
54 octant::~octant ()
55 {
56 for (fill = 8; fill--; )
57 delete sub[fill];
58 }
59
60 static bool overlap (const sector &o1, uoffs ea, const sector &a, const sector &b)
61 {
62 ea /= 2;
63
64 sector center_1 = o1 + ea;
65 sector size_2 = b - a;
66 sector center_2 = a + size_2 / 2;
67
68 return abs (center_1 - center_2) <= ea + size_2;
69 }
70
71 void octant::add (entity *e)
72 {
73 const sector &a = e->a;
74 const sector &b = e->b;
75
76 if (overlap (orig, extent, a, b))
77 {
78 uoffs extent2 = extent / 2;
79 uoffs size = max (abs (b - a));
80
81 if (size > extent2 || !extent2)
82 {
83 push_back (e);
84 e->o.push_back (this);
85 return;
86 }
87
88 for (int i = 8; i--; )
89 {
90 sector s = orig;
91 s.offset (i, extent2);
92
93 if (overlap (s, extent2, a, b))
94 {
95 if (!sub[i])
96 sub[i] = new octant (this, s, extent2);
97
98 sub[i]->add (e);
99 }
100 }
101 }
102 }
103
104 void octant::remove (entity *e)
105 {
106 }
107
108 void octant::detect_visibility (view &ctx)
109 {
110 visibility_state &vs = ctx.vismap[this];
111
112 if (vs.generation != ctx.generation)
113 vs.visibility = visibility_state::UNKNOWN;
114
115 if (orig <= ctx.orig && ctx.orig <= orig + extent)
116 {
117 vs.visibility = visibility_state::PARTIAL;
118 vs.generation = ctx.generation;
119 }
120 else
121 {
122 sector center_s (orig + extent / 2 - ctx.orig);
123 point center (center_s.x, center_s.y, center_s.z);
124
125 GLfloat dia = (2. * sqrtf (3))*(GLfloat)extent;
126
127 if (ctx.frustum.t.distance (center) < -dia) return;
128 if (ctx.frustum.b.distance (center) < -dia) return;
129 if (ctx.frustum.l.distance (center) < -dia) return;
130 if (ctx.frustum.r.distance (center) < -dia) return;
131 if (ctx.frustum.n.distance (center) < -dia) return;
132
133 GLfloat fd = ctx.frustum.f.distance (center);
134
135 if (fd < -dia)
136 {
137 if (fd < -dia * 2)
138 return;
139
140 // cull, but do check for potential visibility
141 if (size ())
142 {
143 ctx.farlist.push_back (this);
144 return;
145 }
146 }
147 }
148
149 if (size ())
150 ctx.vislist.push_back (this);
151
152 // node to start with
153 unsigned char si = ctx.d.x < 0 ? 1 : 0
154 | ctx.d.y < 0 ? 2 : 0
155 | ctx.d.z < 0 ? 4 : 0;
156
157 // bit-toggle to find next child for front-to-back order
158 static unsigned char next[8]
159 = { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7 };
160
161 for (int i = 0; i < 8; i++)
162 {
163 si ^= next[i];
164
165 if (sub[si])
166 sub[si]->detect_visibility (ctx);
167 }
168
169 vs.generation = ctx.generation;
170 }
171
172 void octant::display (view &ctx)
173 {
174 #if 0
175 glBegin (GL_LINES);
176 sector s = orig - ctx.orig;
177 vec3 clr(0, 0.8, 0);
178 glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, (const GLfloat*)&clr);
179
180 for (int i = 8; i--; )
181 for (int ji = 3; ji--; )
182 {
183 int j = i | (1 << ji);
184 if (i != j)
185 {
186 glVertex3i (s.x + !!(i & 1) * extent,
187 s.y + !!(i & 2) * extent,
188 s.z + !!(i & 4) * extent);
189 glVertex3i (s.x + !!(j & 1) * extent,
190 s.y + !!(j & 2) * extent,
191 s.z + !!(j & 4) * extent);
192 }
193 }
194
195 glEnd ();
196 glDisable(GL_COLOR_MATERIAL);
197 #endif
198
199 for (iterator i = end (); i != begin (); )
200 (*--i)->display (ctx);
201 }
202
203 void
204 octant::draw_bbox (view &ctx)
205 {
206 sector s = orig - ctx.orig;
207 int i;
208 GLint verts[4 * 6] = {
209 0x00, 0x40, 0x60, 0x20, // -x
210 0x10, 0x30, 0x70, 0x50, // +x
211 0x00, 0x10, 0x50, 0x40, // -y
212 0x70, 0x30, 0x20, 0x60, // +y
213 0x00, 0x20, 0x30, 0x10, // -z
214 0x40, 0x50, 0x70, 0x60, // +z
215 };
216
217 GLfloat cube[8][3] =
218 {
219 { s.x , s.y , s.z },
220 { s.x + (soffs)extent, s.y , s.z },
221 { s.x , s.y + (soffs)extent, s.z },
222 { s.x + (soffs)extent, s.y + (soffs)extent, s.z },
223 { s.x , s.y , s.z + (soffs)extent },
224 { s.x + (soffs)extent, s.y , s.z + (soffs)extent },
225 { s.x , s.y + (soffs)extent, s.z + (soffs)extent },
226 { s.x + (soffs)extent, s.y + (soffs)extent, s.z + (soffs)extent },
227 };
228
229 glBegin (GL_QUADS);
230
231 for (i = 0; i < 4 * 6; i++)
232 glVertex3fv (cube [verts [i] >> 4]);
233
234 glEnd ();
235 }
236
237