… | |
… | |
4 | |
4 | |
5 | using namespace std; |
5 | using namespace std; |
6 | |
6 | |
7 | #define GL_GLEXT_PROTOTYPES |
7 | #define GL_GLEXT_PROTOTYPES |
8 | #include <GL/gl.h> |
8 | #include <GL/gl.h> |
|
|
9 | #include <GL/glext.h> |
9 | |
10 | |
10 | #include "oct.h" |
11 | #include "oct.h" |
11 | #include "view.h" |
12 | #include "view.h" |
12 | #include "entity.h" |
13 | #include "entity.h" |
13 | |
14 | |
… | |
… | |
27 | |
28 | |
28 | glBeginQueryARB (GL_SAMPLES_PASSED, id); |
29 | glBeginQueryARB (GL_SAMPLES_PASSED, id); |
29 | return id; |
30 | return id; |
30 | } |
31 | } |
31 | |
32 | |
32 | #define end_occ_query() glEndQueryARB (GL_SAMPLES_PASSED); |
33 | #define end_occ_query() glEndQueryARB (GL_SAMPLES_PASSED) |
33 | |
34 | |
34 | static GLuint occ_query_result (GLuint id) |
35 | GLuint occ_query_result (GLuint id) |
35 | { |
36 | { |
36 | GLuint count; |
37 | GLuint count; |
37 | |
38 | |
38 | glGetQueryObjectuivARB (id, GL_QUERY_RESULT, &count); |
39 | glGetQueryObjectuivARB (id, GL_QUERY_RESULT, &count); |
39 | occ_query_objects.push_back (id); |
40 | occ_query_objects.push_back (id); |
… | |
… | |
54 | { |
55 | { |
55 | for (fill = 8; fill--; ) |
56 | for (fill = 8; fill--; ) |
56 | delete sub[fill]; |
57 | delete sub[fill]; |
57 | } |
58 | } |
58 | |
59 | |
59 | static bool overlap (const sector &o1, uoffs ea, const box &bbox) |
60 | static bool overlap (const sector &o1, uoffs ea, const sector &a, const sector &b) |
60 | { |
61 | { |
61 | sector a2, b2; |
|
|
62 | |
|
|
63 | ea /= 2; |
62 | ea /= 2; |
64 | |
63 | |
65 | a2.x = o1.x + ea; |
64 | sector center_1 = o1 + ea; |
66 | a2.y = o1.y + ea; |
65 | sector size_2 = b - a; |
67 | a2.z = o1.z + ea; |
66 | sector center_2 = a + size_2 / 2; |
68 | |
|
|
69 | b2.x = (bbox.a.x + bbox.b.x) / 2; |
|
|
70 | b2.y = (bbox.a.y + bbox.b.y) / 2; |
|
|
71 | b2.z = (bbox.a.z + bbox.b.z) / 2; |
|
|
72 | |
67 | |
73 | return abs (a2.x - b2.x) <= ea + (bbox.b.x - bbox.a.x) |
68 | return abs (center_1 - center_2) <= ea + size_2; |
74 | && abs (a2.y - b2.y) <= ea + (bbox.b.y - bbox.a.y) |
|
|
75 | && abs (a2.z - b2.z) <= ea + (bbox.b.z - bbox.a.z); |
|
|
76 | } |
69 | } |
77 | |
70 | |
78 | void octant::add (entity_base *e) |
71 | void octant::add (entity *e) |
79 | { |
72 | { |
80 | box bbox = translate (e->bbox, e->orig, sector (0, 0, 0)); |
73 | const sector &a = e->a; |
|
|
74 | const sector &b = e->b; |
81 | |
75 | |
82 | uoffs size = max (abs (bbox.b.x - bbox.a.x), |
|
|
83 | max (abs (bbox.b.y - bbox.a.y), |
|
|
84 | abs (bbox.b.z - bbox.a.z))); |
|
|
85 | |
|
|
86 | if (overlap (orig, extent, bbox)) |
76 | if (overlap (orig, extent, a, b)) |
87 | { |
77 | { |
88 | uoffs extent2 = extent / 2; |
78 | uoffs extent2 = extent / 2; |
|
|
79 | uoffs size = max (abs (b - a)); |
89 | |
80 | |
90 | if (size > extent2 || !extent2) |
81 | if (size > extent2 || !extent2) |
91 | { |
82 | { |
92 | push_back (e); |
83 | push_back (e); |
93 | e->o.push_back (this); |
84 | e->o.push_back (this); |
… | |
… | |
96 | |
87 | |
97 | for (int i = 8; i--; ) |
88 | for (int i = 8; i--; ) |
98 | { |
89 | { |
99 | sector s = orig; |
90 | sector s = orig; |
100 | s.offset (i, extent2); |
91 | s.offset (i, extent2); |
|
|
92 | |
101 | if (overlap (s, extent2, bbox)) |
93 | if (overlap (s, extent2, a, b)) |
102 | { |
94 | { |
103 | if (!sub[i]) |
95 | if (!sub[i]) |
104 | sub[i] = new octant (this, s, extent2); |
96 | sub[i] = new octant (this, s, extent2); |
105 | |
97 | |
106 | sub[i]->add (e); |
98 | sub[i]->add (e); |
107 | } |
99 | } |
108 | } |
100 | } |
109 | } |
101 | } |
110 | } |
102 | } |
111 | |
103 | |
112 | void octant::remove (entity_base *e) |
104 | void octant::remove (entity *e) |
113 | { |
105 | { |
114 | } |
106 | } |
115 | |
107 | |
116 | void octant::detect_visibility (view &ctx) |
108 | void octant::detect_visibility (view &ctx) |
117 | { |
109 | { |
118 | visibility_state &vs = ctx.vismap[this]; |
110 | visibility_state &vs = ctx.vismap[this]; |
119 | |
111 | |
120 | if (vs.generation != ctx.generation) |
112 | if (vs.generation != ctx.generation) |
121 | vs.visibility = visibility_state::UNKNOWN; |
113 | vs.visibility = visibility_state::UNKNOWN; |
122 | |
114 | |
123 | const sector &cam = ctx.orig; |
115 | if (orig <= ctx.orig && ctx.orig <= orig + extent) |
124 | if (cam.x >= orig.x && cam.x <= orig.x + extent |
|
|
125 | && cam.y >= orig.y && cam.y <= orig.y + extent |
|
|
126 | && cam.z >= orig.z && cam.z <= orig.z + extent) |
|
|
127 | { |
116 | { |
128 | vs.visibility = visibility_state::PARTIAL; |
117 | vs.visibility = visibility_state::PARTIAL; |
129 | vs.generation = ctx.generation; |
118 | vs.generation = ctx.generation; |
130 | } |
119 | } |
131 | else |
120 | else |
132 | { |
121 | { |
133 | point center ( |
122 | sector center_s (orig + extent / 2 - ctx.orig); |
134 | orig.x + (soffs)extent / 2 - cam.x, |
123 | point center (center_s.x, center_s.y, center_s.z); |
135 | orig.y + (soffs)extent / 2 - cam.y, |
|
|
136 | orig.z + (soffs)extent / 2 - cam.z |
|
|
137 | ); |
|
|
138 | |
124 | |
139 | GLfloat dia = (0.5 * sqrtf (3))*(GLfloat)extent; |
125 | GLfloat dia = (2. * sqrtf (3)) * (GLfloat)extent; |
140 | |
126 | |
141 | if (ctx.frustum.t.distance (center) < -dia) return; |
127 | if (ctx.frustum.t.distance (center) < -dia) return; |
142 | if (ctx.frustum.b.distance (center) < -dia) return; |
128 | if (ctx.frustum.b.distance (center) < -dia) return; |
143 | if (ctx.frustum.l.distance (center) < -dia) return; |
129 | if (ctx.frustum.l.distance (center) < -dia) return; |
144 | if (ctx.frustum.r.distance (center) < -dia) return; |
130 | if (ctx.frustum.r.distance (center) < -dia) return; |
145 | if (ctx.frustum.n.distance (center) < -dia) return; |
131 | if (ctx.frustum.n.distance (center) < -dia) return; |
146 | if (ctx.frustum.f.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 | ctx.farlist.push_back (this); |
|
|
141 | return; |
|
|
142 | } |
147 | } |
143 | } |
148 | |
144 | |
149 | if (size ()) |
145 | if (size ()) |
150 | ctx.vislist.push_back (this); |
146 | ctx.vislist.push_back (this); |
151 | |
147 | |
… | |
… | |
171 | |
167 | |
172 | void octant::display (view &ctx) |
168 | void octant::display (view &ctx) |
173 | { |
169 | { |
174 | #if 0 |
170 | #if 0 |
175 | glBegin (GL_LINES); |
171 | glBegin (GL_LINES); |
176 | sector s = orig; |
172 | sector s = orig - ctx.orig; |
177 | s.x -= ctx.orig.x; |
|
|
178 | s.y -= ctx.orig.y; |
|
|
179 | s.z -= ctx.orig.z; |
|
|
180 | vec3 clr(0, 0.8, 0); |
173 | vec3 clr(0, 0.8, 0); |
181 | glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, (const GLfloat*)&clr); |
174 | glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, (const GLfloat*)&clr); |
182 | |
175 | |
183 | for (int i = 8; i--; ) |
176 | for (int i = 8; i--; ) |
184 | for (int ji = 3; ji--; ) |
177 | for (int ji = 3; ji--; ) |
… | |
… | |
197 | |
190 | |
198 | glEnd (); |
191 | glEnd (); |
199 | glDisable(GL_COLOR_MATERIAL); |
192 | glDisable(GL_COLOR_MATERIAL); |
200 | #endif |
193 | #endif |
201 | |
194 | |
202 | GLuint i = begin_occ_query (); |
|
|
203 | for (iterator i = end (); i != begin (); ) |
195 | for (iterator i = end (); i != begin (); ) |
204 | (*--i)->display (ctx); |
196 | (*--i)->display (ctx); |
205 | end_occ_query (); |
|
|
206 | } |
197 | } |
207 | |
198 | |
|
|
199 | void |
|
|
200 | octant::draw_bbox (view &ctx) |
|
|
201 | { |
|
|
202 | sector s = orig - ctx.orig; |
|
|
203 | int i; |
|
|
204 | GLint verts[4 * 6] = { |
|
|
205 | 0x00, 0x40, 0x60, 0x20, // -x |
|
|
206 | 0x10, 0x30, 0x70, 0x50, // +x |
|
|
207 | 0x00, 0x10, 0x50, 0x40, // -y |
|
|
208 | 0x70, 0x30, 0x20, 0x60, // +y |
|
|
209 | 0x00, 0x20, 0x30, 0x10, // -z |
|
|
210 | 0x40, 0x50, 0x70, 0x60, // +z |
|
|
211 | }; |
208 | |
212 | |
|
|
213 | GLfloat cube[8][3] = |
|
|
214 | { |
|
|
215 | { s.x , s.y , s.z }, |
|
|
216 | { s.x + (soffs)extent, s.y , s.z }, |
|
|
217 | { s.x , s.y + (soffs)extent, s.z }, |
|
|
218 | { s.x + (soffs)extent, s.y + (soffs)extent, s.z }, |
|
|
219 | { s.x , s.y , s.z + (soffs)extent }, |
|
|
220 | { s.x + (soffs)extent, s.y , s.z + (soffs)extent }, |
|
|
221 | { s.x , s.y + (soffs)extent, s.z + (soffs)extent }, |
|
|
222 | { s.x + (soffs)extent, s.y + (soffs)extent, s.z + (soffs)extent }, |
|
|
223 | }; |
209 | |
224 | |
|
|
225 | glBegin (GL_QUADS); |
|
|
226 | |
|
|
227 | for (i = 0; i < 4 * 6; i++) |
|
|
228 | glVertex3fv (cube [verts [i] >> 4]); |
|
|
229 | |
|
|
230 | glEnd (); |
|
|
231 | } |
|
|
232 | |
|
|
233 | |