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