1 | #include <cmath> |
1 | #include <cmath> |
2 | |
2 | |
|
|
3 | #include "opengl.h" |
|
|
4 | |
|
|
5 | #include "view.h" |
3 | #include "oct.h" |
6 | #include "oct.h" |
4 | #include "view.h" |
7 | |
|
|
8 | using namespace gl; |
|
|
9 | |
|
|
10 | pass pass_depth (0); |
|
|
11 | |
|
|
12 | vector<GLuint> occ_query_objects; |
|
|
13 | |
|
|
14 | static GLuint begin_occ_query () |
|
|
15 | { |
|
|
16 | GLuint id; |
|
|
17 | |
|
|
18 | if (occ_query_objects.size ()) |
|
|
19 | { |
|
|
20 | id = *(occ_query_objects.end () - 1); |
|
|
21 | occ_query_objects.pop_back (); |
|
|
22 | } |
|
|
23 | else |
|
|
24 | glGenQueries (1, &id); |
|
|
25 | |
|
|
26 | glBeginQuery (GL_SAMPLES_PASSED, id); |
|
|
27 | return id; |
|
|
28 | } |
|
|
29 | |
|
|
30 | inline void end_occ_query () |
|
|
31 | { |
|
|
32 | glEndQuery (GL_SAMPLES_PASSED); |
|
|
33 | } |
|
|
34 | |
|
|
35 | static GLuint occ_query_result (GLuint id) |
|
|
36 | { |
|
|
37 | GLuint count; |
|
|
38 | |
|
|
39 | glGetQueryObjectuiv (id, GL_QUERY_RESULT, &count); |
|
|
40 | occ_query_objects.push_back (id); |
|
|
41 | |
|
|
42 | return count; |
|
|
43 | } |
5 | |
44 | |
6 | view::view () |
45 | view::view () |
7 | : gamma(1.0) |
46 | : gamma(1.0), nz_near (1.F), nz_far (2.F) |
8 | { |
47 | { |
9 | } |
48 | } |
10 | |
49 | |
11 | view::~view () |
50 | view::~view () |
12 | { |
51 | { |
13 | } |
52 | } |
14 | |
53 | |
|
|
54 | void view::begin_occ_query (recv_occ_query &recv, void *id) |
|
|
55 | { |
|
|
56 | occ_queries.push_back (oq_data (&recv, ::begin_occ_query (), id)); |
|
|
57 | } |
|
|
58 | |
|
|
59 | void view::end_occ_query () |
|
|
60 | { |
|
|
61 | ::end_occ_query (); |
|
|
62 | } |
|
|
63 | |
15 | bool view::may_draw (entity_base *e) |
64 | bool view::may_draw (const entity *e) |
16 | { |
65 | { |
17 | if (drawn.find (e) != drawn.end ()) |
66 | if (drawn.find (e) != drawn.end ()) |
18 | return false; |
67 | return false; |
19 | |
68 | |
20 | drawn.insert (e); |
69 | drawn.insert (e); |
21 | return true; |
70 | return true; |
22 | } |
71 | } |
23 | |
72 | |
|
|
73 | visibility_base *visible::get_visibility (view &ctx) |
|
|
74 | { |
|
|
75 | view::visibility_map::iterator i = ctx.vismap.find (this); |
|
|
76 | visibility_base *vs; |
|
|
77 | |
|
|
78 | if (i == ctx.vismap.end ()) |
|
|
79 | { |
|
|
80 | vs = new_visibility (); |
|
|
81 | ctx.vismap.insert (view::visibility_map::value_type (this, vs)); |
|
|
82 | } |
|
|
83 | else |
|
|
84 | { |
|
|
85 | vs = i->second; |
|
|
86 | |
|
|
87 | if (vs->generation != ctx.generation) |
|
|
88 | { |
|
|
89 | if (vs->generation + 1 != ctx.generation) |
|
|
90 | clear_visibility (vs); |
|
|
91 | |
|
|
92 | vs->generation = ctx.generation; |
|
|
93 | } |
|
|
94 | } |
|
|
95 | |
|
|
96 | return vs; |
|
|
97 | } |
|
|
98 | |
24 | void view::reset_projection () |
99 | void view::reset_projection () |
25 | { |
100 | { |
26 | renormalize (orig, p); |
101 | renormalize (orig, p); |
27 | |
102 | |
28 | glViewport (0, 0, w, h); |
103 | glViewport (0, 0, w, h); |
29 | |
104 | |
30 | glMatrixMode (GL_PROJECTION); |
105 | glMatrixMode (GL_PROJECTION); |
31 | glLoadIdentity (); |
106 | glLoadIdentity (); |
32 | |
107 | |
33 | GLdouble aspect = (GLdouble)w/h; |
108 | GLdouble aspect = (GLdouble)w/h; |
34 | GLdouble zNear = 0.1; |
109 | GLdouble ftan = tan (fov * (.5 * M_PI / 180.)); |
35 | GLdouble zFar = 50.; |
110 | GLdouble ymax = z_near * ftan; |
|
|
111 | GLdouble xmax = ymax * aspect; |
36 | |
112 | |
37 | GLdouble ymax = zNear * tan (fov * (M_PI / 360.0)); |
113 | matrix perspective; |
|
|
114 | #if 1 |
|
|
115 | { |
|
|
116 | matrix &m = perspective; |
|
|
117 | |
|
|
118 | m(0,0) = 1.F / xmax; m(0,1) = 0; m(0,2) = 0; m(0,3) = 0; |
|
|
119 | m(1,0) = 0; m(1,1) = 1.F / ymax; m(1,2) = 0; m(1,3) = 0; |
|
|
120 | m(2,0) = 0; m(2,1) = 0; m(2,2) = -1e10F / z_far; m(2,3) = 1; |
|
|
121 | m(3,0) = 0; m(3,1) = 0; m(3,2) = -1; m(3,3) = 0; |
|
|
122 | |
|
|
123 | glLoadMatrixf (perspective); |
|
|
124 | } |
|
|
125 | #else |
38 | glFrustum (-ymax * aspect, ymax * aspect, -ymax, ymax, zNear, zFar); |
126 | glFrustum (-xmax, xmax, -ymax, ymax, z_near, z_far); |
|
|
127 | #endif |
|
|
128 | |
|
|
129 | glGetFloatv (GL_PROJECTION_MATRIX, perspective); |
|
|
130 | |
|
|
131 | perspfact = z_near / ymax * 0.5F * h; |
|
|
132 | |
|
|
133 | glMatrixMode (GL_MODELVIEW); |
|
|
134 | glLoadIdentity (); |
|
|
135 | matrix &m = projection; |
39 | |
136 | |
40 | d = normalize (d);//D |
137 | d = normalize (d);//D |
41 | u = normalize (u);//D |
138 | u = normalize (u);//D |
|
|
139 | |
42 | vec3 rz = -d; |
140 | vec3 rz = -d; |
43 | vec3 rx = cross (u, rz); |
141 | vec3 rx = cross (u, rz); |
44 | vec3 ry = cross (rz, rx); |
142 | vec3 ry = cross (rz, rx); |
45 | |
143 | |
46 | matrix &m = projection; |
|
|
47 | m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = 0; |
144 | m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = 0; |
48 | m(1,0) = ry.x; m(1,1) = ry.y; m(1,2) = ry.z; m(1,3) = 0; |
145 | m(1,0) = ry.x; m(1,1) = ry.y; m(1,2) = ry.z; m(1,3) = 0; |
49 | m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = 0; |
146 | m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = 0; |
50 | m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1; |
147 | m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1; |
51 | |
148 | |
52 | glMultMatrixf ((GLfloat *)m.data); |
149 | diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z); |
|
|
150 | //printf ("diagfact = %f\n", diagfact); |
|
|
151 | diagfact = sqrtf (3.);//D WHY??? |
53 | |
152 | |
54 | glGetFloatv (GL_PROJECTION_MATRIX, (GLfloat *)&m); |
153 | glMultMatrixf (m); |
|
|
154 | |
|
|
155 | glGetFloatv (GL_MODELVIEW_MATRIX, m); |
|
|
156 | |
|
|
157 | m = perspective * m; |
55 | |
158 | |
56 | 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) ); |
159 | 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) ); |
57 | 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) ); |
160 | 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) ); |
58 | 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) ); |
161 | 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) ); |
59 | frustum.t = 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) ); |
162 | frustum.t = 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) ); |
60 | frustum.n = plane ( m(3,0) + m(2,0), m(3,1) + m(2,1), m(3,2) + m(2,2), m(3,3) + m(2,3) ); |
163 | frustum.n = plane ( m(3,0) + m(2,0), m(3,1) + m(2,1), m(3,2) + m(2,2), m(3,3) + m(2,3) ); |
61 | frustum.f = plane ( m(3,0) - m(2,0), m(3,1) - m(2,1), m(3,2) - m(2,2), m(3,3) - m(2,3) ); |
164 | frustum.f = plane ( m(3,0) - m(2,0), m(3,1) - m(2,1), m(3,2) - m(2,2), m(3,3) - m(2,3) ); |
62 | |
165 | |
63 | glMatrixMode (GL_MODELVIEW); |
166 | { |
64 | glLoadIdentity (); |
167 | GLdouble frustlen = z_far - z_near; |
|
|
168 | GLdouble fheight = frustlen * ftan; |
|
|
169 | GLdouble fwidth = fheight * w / h; |
|
|
170 | point half (0, 0, z_near + frustlen * .5); |
|
|
171 | point corner (fwidth, fheight, frustlen); |
|
|
172 | |
|
|
173 | frustum.s = sphere (p + d * (.5 * frustlen), length (corner - half)); |
|
|
174 | } |
|
|
175 | |
|
|
176 | { |
|
|
177 | GLdouble depth = h / ftan; |
|
|
178 | |
|
|
179 | frustum.c = cone (p, d, atan (sqrt (GLdouble (w * w + h * h)) * ftan / h)); |
|
|
180 | } |
|
|
181 | |
65 | glTranslatef (-p.x, -p.y, -p.z); |
182 | glTranslatef (-p.x, -p.y, -p.z); |
66 | } |
183 | } |
67 | |
184 | |
68 | void view::begin () |
185 | void view::begin () |
69 | { |
186 | { |
70 | vismap.clear (); |
187 | generation++; |
|
|
188 | |
71 | vislist.clear (); |
189 | vislist.clear (); |
72 | |
190 | |
73 | generation++; |
191 | z_near = max (nz_near, 1.F); |
|
|
192 | z_far = max (nz_far, z_near * 2.F); |
|
|
193 | c_far = nc_far; |
74 | |
194 | |
75 | reset_projection (); |
195 | reset_projection (); |
76 | |
196 | |
77 | // check occlusion queries here |
197 | nz_near = 100.; |
78 | for (vector<octant *>::iterator i = checklist.begin (); i != checklist.end (); ++i) |
198 | nc_far = nz_far = 1.F; |
79 | //printf ("%p %d\n", *i, occ_query_result ((*i)->occ_query)); |
|
|
80 | occ_query_result ((*i)->occ_query); |
|
|
81 | |
|
|
82 | checklist.clear (); |
|
|
83 | |
|
|
84 | world.detect_visibility (*this); |
|
|
85 | } |
199 | } |
86 | |
200 | |
87 | void view::end () |
201 | void view::end () |
88 | { |
202 | { |
89 | vismap.clear (); |
203 | vislist.clear (); |
90 | } |
204 | } |
91 | |
205 | |
92 | void view::pass (enum mode m) |
206 | #define DEPTH_OFFSET (1. / (GLdouble)(1L << 16)) |
93 | { |
|
|
94 | mode = m; |
|
|
95 | |
207 | |
96 | glDisable (GL_ALPHA_TEST); |
208 | void view::render (enum pass_type p, pass &data) |
|
|
209 | { |
|
|
210 | pass_type = p; |
|
|
211 | pass_data = &data; |
|
|
212 | |
|
|
213 | switch (pass_type) |
|
|
214 | { |
|
|
215 | case DEPTH: |
|
|
216 | glColorMask (1, 1, 1, 1); |
|
|
217 | glDepthMask (1); |
97 | glDisable (GL_BLEND); |
218 | glDisable (GL_BLEND); |
98 | |
219 | |
99 | if (mode == view::DEPTH) |
220 | //glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
100 | { |
221 | glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
222 | |
|
|
223 | //glEnable (GL_STENCIL_TEST); // for depth-passes |
|
|
224 | //glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE); |
|
|
225 | //glStencilFunc (GL_LESS, 1, 255); |
|
|
226 | |
101 | glEnable (GL_POLYGON_OFFSET_FILL); |
227 | //glEnable (GL_POLYGON_OFFSET_FILL); |
102 | glPolygonOffset (0, 1); |
228 | //glPolygonOffset (0, 5); |
|
|
229 | glEnable (GL_CULL_FACE); |
|
|
230 | glDisable (GL_MINMAX); |
|
|
231 | glDepthRange (DEPTH_OFFSET, 1.); |
103 | glDepthFunc (GL_LESS); |
232 | glDepthFunc (GL_LESS); |
104 | glDisable (GL_LIGHTING); |
233 | glEnable (GL_DEPTH_TEST); |
105 | glColorMask (0, 0, 0, 0); |
234 | glColorMask (0, 0, 0, 0); |
106 | //cgGLDisableProfile (vsh_profile); |
235 | glDisable (GL_DEPTH_CLAMP_NV); |
107 | cgGLDisableProfile (fsh_profile); |
236 | |
108 | } |
237 | world.detect_visibility (*this); |
109 | else |
238 | |
110 | { |
239 | break; |
111 | #if 0 |
240 | |
112 | glEnable (GL_MINMAX); |
241 | case POSTDEPTH: |
113 | #endif |
242 | glDepthRange (0., 1. - DEPTH_OFFSET); |
114 | glDisable (GL_POLYGON_OFFSET_FILL); |
|
|
115 | glDepthFunc (GL_LESS); |
243 | glDepthFunc (GL_LESS); |
|
|
244 | glColorMask (0, 0, 0, 0); |
116 | glDepthMask (0); |
245 | glDepthMask (0); |
117 | cgGLEnableProfile (vsh_profile); |
246 | glEnable (GL_DEPTH_CLAMP_NV); |
118 | cgGLEnableProfile (fsh_profile); |
247 | glDisable (GL_STENCIL_TEST); |
|
|
248 | glDisable (GL_CULL_FACE); |
|
|
249 | |
|
|
250 | // check occlusion queries |
|
|
251 | for (vector<oq_data>::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i) |
|
|
252 | { |
|
|
253 | occ_query oq(*this, i->data, ::occ_query_result (i->id)); |
|
|
254 | i->recv->event (oq); |
|
|
255 | } |
|
|
256 | |
|
|
257 | occ_queries.clear (); |
|
|
258 | |
|
|
259 | break; |
|
|
260 | |
|
|
261 | case LIGHTED: |
|
|
262 | glEnable (GL_CULL_FACE); |
|
|
263 | //glEnable (GL_BLEND);//TODO, only when not first lighted pass |
|
|
264 | glBlendFunc (GL_ONE, GL_ONE); |
|
|
265 | //glClear (GL_STENCIL_BUFFER_BIT); |
|
|
266 | //glEnable (GL_STENCIL_TEST); |
|
|
267 | glEnable (GL_MINMAX); |
|
|
268 | glDepthRange (0., 1. - DEPTH_OFFSET); |
|
|
269 | glDepthFunc (GL_LESS); |
|
|
270 | glColorMask (1, 1, 1, 1); |
|
|
271 | glDepthMask (0); |
|
|
272 | glDisable (GL_DEPTH_CLAMP_NV); |
|
|
273 | |
|
|
274 | break; |
119 | } |
275 | } |
120 | |
276 | |
121 | for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i) |
277 | for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i) |
122 | (*i)->display (*this); |
278 | (*i)->display (*this); |
123 | |
279 | |
|
|
280 | if (pass_type == DEPTH) |
|
|
281 | 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 |
|
|
282 | |
124 | drawn.clear (); |
283 | drawn.clear (); |
125 | |
284 | |
126 | glColorMask (1, 1, 1, 0); |
285 | #if 0 |
|
|
286 | if (pass == view::DEPTH) |
|
|
287 | { |
|
|
288 | glEnable (GL_DEPTH_CLAMP_NV); |
127 | glDepthMask (1); |
289 | glDepthMask (0); |
128 | } |
290 | glDepthFunc (GL_LESS); |
129 | |
291 | |
|
|
292 | for (vector<octant *>::iterator i = farlist.begin (); i != farlist.end (); ++i) |
|
|
293 | { |
|
|
294 | begin_occ_query (**i); |
|
|
295 | (*i)->draw_bbox (*this); |
|
|
296 | end_occ_query (); |
|
|
297 | } |
130 | |
298 | |
|
|
299 | glDisable (GL_DEPTH_CLAMP_NV); |
|
|
300 | } |
|
|
301 | #endif |
|
|
302 | } |
|
|
303 | |
|
|
304 | void light::enable () |
|
|
305 | { |
|
|
306 | lightpos->set (p); |
|
|
307 | } |
|
|
308 | |
|
|
309 | void light::disable () |
|
|
310 | { |
|
|
311 | } |
|
|
312 | |
|
|
313 | static shader::varying_1f camdist; |
|
|
314 | static shader::varying_3f lightvec; |
|
|
315 | |
|
|
316 | void linear_light::vsh () |
|
|
317 | { |
|
|
318 | using namespace shader::compile; |
|
|
319 | |
|
|
320 | lightvec = xyz (lightpos - model_view_matrix * vin.vertex); |
|
|
321 | camdist = max (1 - length (lightvec) / radius, 0); |
|
|
322 | |
|
|
323 | fsh (); |
|
|
324 | } |
|
|
325 | |
|
|
326 | void linear_light::fsh () |
|
|
327 | { |
|
|
328 | using namespace shader::compile; |
|
|
329 | |
|
|
330 | sh_lightvec = lightvec; |
|
|
331 | sh_colour = float3 (c.r / 255.F, c.g / 255.F, c.b / 255.F) * (min (intensity * camdist * 100.F + 0.9F, 1.F)); |
|
|
332 | } |
|
|
333 | |
|
|
334 | |