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