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