… | |
… | |
5 | #include "view.h" |
5 | #include "view.h" |
6 | #include "oct.h" |
6 | #include "oct.h" |
7 | |
7 | |
8 | using namespace gl; |
8 | using namespace gl; |
9 | |
9 | |
|
|
10 | pass pass_depth (0); |
|
|
11 | |
10 | vector<GLuint> occ_query_objects; |
12 | vector<GLuint> occ_query_objects; |
11 | |
13 | |
12 | static GLuint begin_occ_query () |
14 | static GLuint begin_occ_query () |
13 | { |
15 | { |
14 | GLuint id; |
16 | GLuint id; |
… | |
… | |
17 | { |
19 | { |
18 | id = *(occ_query_objects.end () - 1); |
20 | id = *(occ_query_objects.end () - 1); |
19 | occ_query_objects.pop_back (); |
21 | occ_query_objects.pop_back (); |
20 | } |
22 | } |
21 | else |
23 | else |
22 | glGenQueriesARB (1, &id); |
24 | glGenQueries (1, &id); |
23 | |
25 | |
24 | glBeginQueryARB (GL_SAMPLES_PASSED, id); |
26 | glBeginQuery (GL_SAMPLES_PASSED, id); |
25 | return id; |
27 | return id; |
26 | } |
28 | } |
27 | |
29 | |
28 | inline void end_occ_query () |
30 | inline void end_occ_query () |
29 | { |
31 | { |
30 | glEndQueryARB (GL_SAMPLES_PASSED); |
32 | glEndQuery (GL_SAMPLES_PASSED); |
31 | } |
33 | } |
32 | |
34 | |
33 | static GLuint occ_query_result (GLuint id) |
35 | static GLuint occ_query_result (GLuint id) |
34 | { |
36 | { |
35 | GLuint count; |
37 | GLuint count; |
36 | |
38 | |
37 | glGetQueryObjectuivARB (id, GL_QUERY_RESULT, &count); |
39 | glGetQueryObjectuiv (id, GL_QUERY_RESULT, &count); |
38 | occ_query_objects.push_back (id); |
40 | occ_query_objects.push_back (id); |
39 | |
41 | |
40 | return count; |
42 | return count; |
41 | } |
43 | } |
42 | |
44 | |
… | |
… | |
90 | vs->generation = ctx.generation; |
92 | vs->generation = ctx.generation; |
91 | } |
93 | } |
92 | } |
94 | } |
93 | |
95 | |
94 | return vs; |
96 | return vs; |
95 | |
|
|
96 | } |
97 | } |
97 | |
98 | |
98 | void view::reset_projection () |
99 | void view::reset_projection () |
99 | { |
100 | { |
100 | renormalize (orig, p); |
101 | renormalize (orig, p); |
… | |
… | |
129 | diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z); |
130 | diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z); |
130 | //printf ("diagfact = %f\n", diagfact); |
131 | //printf ("diagfact = %f\n", diagfact); |
131 | diagfact = sqrtf (3.);//D WHY??? |
132 | diagfact = sqrtf (3.);//D WHY??? |
132 | |
133 | |
133 | glMultMatrixf (m); |
134 | glMultMatrixf (m); |
134 | glTranslatef (-p.x, -p.y, -p.z); |
135 | |
135 | |
|
|
136 | glGetFloatv (GL_PROJECTION_MATRIX, m); |
136 | glGetFloatv (GL_PROJECTION_MATRIX, m); |
137 | |
137 | |
138 | frustum.l = plane ( m(3,0) + m(0,0), m(3,1) + m(0,1), m(3,2) + m(0,2), m(3,3) + m(0,3) ); |
138 | frustum.l = plane ( m(3,0) + m(0,0), m(3,1) + m(0,1), m(3,2) + m(0,2), m(3,3) + m(0,3) ); |
139 | frustum.r = plane ( m(3,0) - m(0,0), m(3,1) - m(0,1), m(3,2) - m(0,2), m(3,3) - m(0,3) ); |
139 | frustum.r = plane ( m(3,0) - m(0,0), m(3,1) - m(0,1), m(3,2) - m(0,2), m(3,3) - m(0,3) ); |
140 | frustum.b = plane ( m(3,0) + m(1,0), m(3,1) + m(1,1), m(3,2) + m(1,2), m(3,3) + m(1,3) ); |
140 | frustum.b = plane ( m(3,0) + m(1,0), m(3,1) + m(1,1), m(3,2) + m(1,2), m(3,3) + m(1,3) ); |
… | |
… | |
158 | frustum.c = cone (p, d, atan (sqrt (GLdouble (w * w + h * h)) * ftan / h)); |
158 | frustum.c = cone (p, d, atan (sqrt (GLdouble (w * w + h * h)) * ftan / h)); |
159 | } |
159 | } |
160 | |
160 | |
161 | glMatrixMode (GL_MODELVIEW); |
161 | glMatrixMode (GL_MODELVIEW); |
162 | glLoadIdentity (); |
162 | glLoadIdentity (); |
|
|
163 | glTranslatef (-p.x, -p.y, -p.z); |
163 | } |
164 | } |
164 | |
165 | |
165 | void view::begin () |
166 | void view::begin () |
166 | { |
167 | { |
167 | generation++; |
168 | generation++; |
168 | |
|
|
169 | glColorMask (1, 1, 1, 1); |
|
|
170 | glDepthMask (1); |
|
|
171 | glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
172 | |
169 | |
173 | vislist.clear (); |
170 | vislist.clear (); |
174 | |
171 | |
175 | z_near = max (nz_near, 1.F); |
172 | z_near = max (nz_near, 1.F); |
176 | z_far = max (nz_far, z_near * 2.F); |
173 | z_far = max (nz_far, z_near * 2.F); |
177 | c_far = nc_far; |
174 | c_far = nc_far; |
178 | |
175 | |
179 | reset_projection (); |
176 | reset_projection (); |
180 | |
177 | |
181 | nz_near = 1e37; |
178 | nz_near = 100.; |
182 | nc_far = nz_far = 1.F; |
179 | nc_far = nz_far = 1.F; |
183 | } |
180 | } |
184 | |
181 | |
185 | void view::end () |
182 | void view::end () |
186 | { |
183 | { |
187 | vislist.clear (); |
184 | vislist.clear (); |
188 | } |
185 | } |
189 | |
186 | |
190 | #define DEPTH_OFFSET (1. / (GLdouble)(1L << 16)) |
187 | #define DEPTH_OFFSET (1. / (GLdouble)(1L << 16)) |
191 | |
188 | |
192 | void view::render (enum pass p) |
189 | void view::render (enum pass_type p, pass &data) |
193 | { |
190 | { |
194 | pass = p; |
191 | pass_type = p; |
|
|
192 | pass_data = &data; |
195 | |
193 | |
196 | switch (pass) |
194 | switch (pass_type) |
197 | { |
195 | { |
198 | case DEPTH: |
196 | case DEPTH: |
|
|
197 | glColorMask (1, 1, 1, 1); |
|
|
198 | glDepthMask (1); |
|
|
199 | |
|
|
200 | glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
|
|
201 | |
|
|
202 | //glEnable (GL_STENCIL_TEST); // for depth-passes |
|
|
203 | //glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE); |
|
|
204 | //glStencilFunc (GL_LESS, 1, 255); |
|
|
205 | |
199 | //glEnable (GL_POLYGON_OFFSET_FILL); |
206 | //glEnable (GL_POLYGON_OFFSET_FILL); |
200 | //glPolygonOffset (0, 5); |
207 | //glPolygonOffset (0, 5); |
201 | glDisable (GL_MINMAX); |
208 | glDisable (GL_MINMAX); |
202 | glDepthRange (DEPTH_OFFSET, 1.); |
209 | glDepthRange (DEPTH_OFFSET, 1.); |
203 | glDepthFunc (GL_LESS); |
210 | glDepthFunc (GL_LESS); |
204 | glEnable (GL_DEPTH_TEST); |
211 | glEnable (GL_DEPTH_TEST); |
205 | glColorMask (0, 0, 0, 0); |
212 | glColorMask (0, 0, 0, 0); |
206 | glDepthMask (1); |
|
|
207 | glDisable (GL_DEPTH_CLAMP_NV); |
213 | glDisable (GL_DEPTH_CLAMP_NV); |
208 | |
214 | |
209 | world.depth_pass (*this); |
215 | world.detect_visibility (*this); |
210 | |
216 | |
211 | printf ("fps %f NF %f:%f vis %d CAM (%d,%d,%d)\n", timer.fps, z_near, z_far, drawn.size (), orig.x, orig.y, orig.z);//D |
|
|
212 | break; |
217 | break; |
213 | |
218 | |
214 | case POSTDEPTH: |
219 | case POSTDEPTH: |
215 | glDepthRange (0., 1. - DEPTH_OFFSET); |
220 | glDepthRange (0., 1. - DEPTH_OFFSET); |
216 | glDepthFunc (GL_LESS); |
221 | glDepthFunc (GL_LESS); |
217 | glColorMask (0, 0, 0, 0); |
222 | glColorMask (0, 0, 0, 0); |
218 | glDepthMask (0); |
223 | glDepthMask (0); |
219 | glEnable (GL_DEPTH_CLAMP_NV); |
224 | glEnable (GL_DEPTH_CLAMP_NV); |
|
|
225 | glDisable (GL_STENCIL_TEST); |
220 | |
226 | |
221 | // check occlusion queries |
227 | // check occlusion queries |
222 | for (vector<oq_data>::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i) |
228 | for (vector<oq_data>::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i) |
223 | { |
229 | { |
224 | occ_query oq(*this, i->data, ::occ_query_result (i->id)); |
230 | occ_query oq(*this, i->data, ::occ_query_result (i->id)); |
225 | i->recv->event (oq); |
231 | i->recv->event (oq); |
226 | } |
232 | } |
227 | |
233 | |
228 | occ_queries.clear (); |
234 | occ_queries.clear (); |
229 | |
235 | |
230 | for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i) |
|
|
231 | (*i)->display (*this); |
|
|
232 | |
|
|
233 | break; |
236 | break; |
234 | |
237 | |
235 | case LIGHTED: |
238 | case LIGHTED: |
|
|
239 | glClear (GL_STENCIL_BUFFER_BIT); |
|
|
240 | //glEnable (GL_STENCIL_TEST); |
236 | glEnable (GL_MINMAX); |
241 | glEnable (GL_MINMAX); |
237 | glDepthRange (0., 1. - DEPTH_OFFSET); |
242 | glDepthRange (0., 1. - DEPTH_OFFSET); |
238 | glDepthFunc (GL_LESS); |
243 | glDepthFunc (GL_LESS); |
239 | glColorMask (1, 1, 1, 1); |
244 | glColorMask (1, 1, 1, 1); |
240 | glDepthMask (0); |
245 | glDepthMask (0); |
241 | glDisable (GL_DEPTH_CLAMP_NV); |
246 | glDisable (GL_DEPTH_CLAMP_NV); |
242 | |
247 | |
243 | for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i) |
|
|
244 | (*i)->display (*this); |
|
|
245 | |
|
|
246 | break; |
248 | break; |
247 | } |
249 | } |
248 | |
250 | |
|
|
251 | for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i) |
|
|
252 | (*i)->display (*this); |
|
|
253 | |
|
|
254 | if (pass_type == DEPTH) |
|
|
255 | printf ("fps %f NF %f:%f vis %d CAM (%d,%d,%d)\n", timer.fps, z_near, z_far, drawn.size (), orig.x, orig.y, orig.z);//D |
|
|
256 | |
249 | drawn.clear (); |
257 | drawn.clear (); |
250 | |
258 | |
251 | #if 0 |
259 | #if 0 |
252 | if (pass == view::DEPTH) |
260 | if (pass == view::DEPTH) |
253 | { |
261 | { |
… | |
… | |
265 | glDisable (GL_DEPTH_CLAMP_NV); |
273 | glDisable (GL_DEPTH_CLAMP_NV); |
266 | } |
274 | } |
267 | #endif |
275 | #endif |
268 | } |
276 | } |
269 | |
277 | |
|
|
278 | void light::enable () |
|
|
279 | { |
|
|
280 | lightpos->set (p); |
|
|
281 | } |
270 | |
282 | |
|
|
283 | void light::disable () |
|
|
284 | { |
|
|
285 | } |
|
|
286 | |
|
|
287 | static shader::varying_1f camdist; |
|
|
288 | static shader::varying_3f lightvec; |
|
|
289 | |
|
|
290 | void linear_light::vsh () |
|
|
291 | { |
|
|
292 | using namespace shader::compile; |
|
|
293 | |
|
|
294 | lightvec = xyz (lightpos - model_view_matrix * vin.vertex); |
|
|
295 | camdist = max (1 - length (lightvec) / radius, 0); |
|
|
296 | } |
|
|
297 | |
|
|
298 | shader::temp_3f linear_light::operator ()() |
|
|
299 | { |
|
|
300 | using namespace shader::compile; |
|
|
301 | |
|
|
302 | temp_3f res; |
|
|
303 | res = float3 (c.r / 255.F, c.g / 255.F, c.b / 255.F) * (min (intensity * camdist * 0.6F + 0.9F, 1.F)); |
|
|
304 | |
|
|
305 | return res; |
|
|
306 | } |
|
|
307 | |
|
|
308 | |