… | |
… | |
2 | |
2 | |
3 | #include "opengl.h" |
3 | #include "opengl.h" |
4 | |
4 | |
5 | #include "view.h" |
5 | #include "view.h" |
6 | #include "oct.h" |
6 | #include "oct.h" |
|
|
7 | #include "material.h" |
7 | |
8 | |
8 | using namespace gl; |
9 | using namespace gl; |
|
|
10 | |
|
|
11 | pass_data pass_depth (0, DEPTH); |
|
|
12 | pass_data pass_postdepth (0, POSTDEPTH); |
9 | |
13 | |
10 | vector<GLuint> occ_query_objects; |
14 | vector<GLuint> occ_query_objects; |
11 | |
15 | |
12 | static GLuint begin_occ_query () |
16 | static GLuint begin_occ_query () |
13 | { |
17 | { |
… | |
… | |
55 | } |
59 | } |
56 | |
60 | |
57 | void view::end_occ_query () |
61 | void view::end_occ_query () |
58 | { |
62 | { |
59 | ::end_occ_query (); |
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 |
60 | } |
82 | } |
61 | |
83 | |
62 | bool view::may_draw (const entity *e) |
84 | bool view::may_draw (const entity *e) |
63 | { |
85 | { |
64 | if (drawn.find (e) != drawn.end ()) |
86 | if (drawn.find (e) != drawn.end ()) |
… | |
… | |
106 | GLdouble aspect = (GLdouble)w/h; |
128 | GLdouble aspect = (GLdouble)w/h; |
107 | GLdouble ftan = tan (fov * (.5 * M_PI / 180.)); |
129 | GLdouble ftan = tan (fov * (.5 * M_PI / 180.)); |
108 | GLdouble ymax = z_near * ftan; |
130 | GLdouble ymax = z_near * ftan; |
109 | GLdouble xmax = ymax * aspect; |
131 | GLdouble xmax = ymax * aspect; |
110 | |
132 | |
|
|
133 | matrix perspective; |
|
|
134 | #if 1 |
|
|
135 | { |
|
|
136 | matrix &m = perspective; |
|
|
137 | |
|
|
138 | m(0,0) = 1.F / xmax; m(0,1) = 0; m(0,2) = 0; m(0,3) = 0; |
|
|
139 | m(1,0) = 0; m(1,1) = 1.F / ymax; m(1,2) = 0; m(1,3) = 0; |
|
|
140 | m(2,0) = 0; m(2,1) = 0; m(2,2) = -1e10F / z_far; m(2,3) = 1; |
|
|
141 | m(3,0) = 0; m(3,1) = 0; m(3,2) = -1; m(3,3) = 0; |
|
|
142 | |
|
|
143 | glLoadMatrixf (perspective); |
|
|
144 | } |
|
|
145 | #else |
111 | glFrustum (-xmax, xmax, -ymax, ymax, z_near, z_far); |
146 | glFrustum (-xmax, xmax, -ymax, ymax, z_near, z_far); |
|
|
147 | #endif |
|
|
148 | |
|
|
149 | glGetFloatv (GL_PROJECTION_MATRIX, perspective); |
112 | |
150 | |
113 | perspfact = z_near / ymax * 0.5F * h; |
151 | perspfact = z_near / ymax * 0.5F * h; |
|
|
152 | |
|
|
153 | glMatrixMode (GL_MODELVIEW); |
|
|
154 | glLoadIdentity (); |
|
|
155 | matrix &m = projection; |
114 | |
156 | |
115 | d = normalize (d);//D |
157 | d = normalize (d);//D |
116 | u = normalize (u);//D |
158 | u = normalize (u);//D |
117 | |
159 | |
118 | vec3 rz = -d; |
160 | vec3 rz = -d; |
119 | vec3 rx = cross (u, rz); |
161 | vec3 rx = cross (u, rz); |
120 | vec3 ry = cross (rz, rx); |
162 | vec3 ry = cross (rz, rx); |
121 | |
163 | |
122 | matrix &m = projection; |
|
|
123 | m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = 0; |
164 | m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = 0; |
124 | m(1,0) = ry.x; m(1,1) = ry.y; m(1,2) = ry.z; m(1,3) = 0; |
165 | m(1,0) = ry.x; m(1,1) = ry.y; m(1,2) = ry.z; m(1,3) = 0; |
125 | m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = 0; |
166 | m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = 0; |
126 | m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1; |
167 | m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1; |
127 | |
168 | |
128 | diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z); |
169 | diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z); |
129 | //printf ("diagfact = %f\n", diagfact); |
170 | //printf ("diagfact = %f\n", diagfact); |
130 | diagfact = sqrtf (3.);//D WHY??? |
171 | diagfact = sqrtf (3.);//D WHY??? |
131 | |
172 | |
132 | glMultMatrixf (m); |
173 | glMultMatrixf (m); |
133 | glTranslatef (-p.x, -p.y, -p.z); |
|
|
134 | |
174 | |
135 | glGetFloatv (GL_PROJECTION_MATRIX, m); |
175 | glGetFloatv (GL_MODELVIEW_MATRIX, m); |
|
|
176 | |
|
|
177 | m = perspective * m; |
136 | |
178 | |
137 | 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) ); |
179 | 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.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) ); |
180 | 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.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) ); |
181 | 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.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) ); |
182 | 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) ); |
… | |
… | |
155 | GLdouble depth = h / ftan; |
197 | GLdouble depth = h / ftan; |
156 | |
198 | |
157 | frustum.c = cone (p, d, atan (sqrt (GLdouble (w * w + h * h)) * ftan / h)); |
199 | frustum.c = cone (p, d, atan (sqrt (GLdouble (w * w + h * h)) * ftan / h)); |
158 | } |
200 | } |
159 | |
201 | |
160 | glMatrixMode (GL_MODELVIEW); |
202 | glTranslatef (-p.x, -p.y, -p.z); |
161 | glLoadIdentity (); |
|
|
162 | } |
203 | } |
163 | |
204 | |
164 | void view::begin () |
205 | void view::begin () |
165 | { |
206 | { |
166 | generation++; |
207 | generation++; |
… | |
… | |
182 | vislist.clear (); |
223 | vislist.clear (); |
183 | } |
224 | } |
184 | |
225 | |
185 | #define DEPTH_OFFSET (1. / (GLdouble)(1L << 16)) |
226 | #define DEPTH_OFFSET (1. / (GLdouble)(1L << 16)) |
186 | |
227 | |
187 | void view::render (enum pass p) |
228 | void view::render (pass_data &pass) |
188 | { |
229 | { |
189 | pass = p; |
230 | this->pass = &pass; |
190 | |
231 | |
191 | switch (pass) |
232 | switch (pass.type) |
192 | { |
233 | { |
193 | case DEPTH: |
234 | case DEPTH: |
194 | glColorMask (1, 1, 1, 1); |
235 | glColorMask (1, 1, 1, 1); |
195 | glDepthMask (1); |
236 | glDepthMask (1); |
|
|
237 | glDisable (GL_BLEND); |
196 | |
238 | |
197 | glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
239 | //glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
|
|
240 | glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
198 | |
241 | |
199 | //glEnable (GL_STENCIL_TEST); // for depth-passes |
242 | //glEnable (GL_STENCIL_TEST); // for depth-passes |
200 | //glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE); |
243 | //glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE); |
201 | //glStencilFunc (GL_LESS, 1, 255); |
244 | //glStencilFunc (GL_LESS, 1, 255); |
202 | |
245 | |
203 | //glEnable (GL_POLYGON_OFFSET_FILL); |
246 | //glEnable (GL_POLYGON_OFFSET_FILL); |
204 | //glPolygonOffset (0, 5); |
247 | //glPolygonOffset (0, 5); |
|
|
248 | glEnable (GL_CULL_FACE); |
205 | glDisable (GL_MINMAX); |
249 | glDisable (GL_MINMAX); |
206 | glDepthRange (DEPTH_OFFSET, 1.); |
250 | glDepthRange (DEPTH_OFFSET, 1.); |
207 | glDepthFunc (GL_LESS); |
251 | glDepthFunc (GL_LESS); |
208 | glEnable (GL_DEPTH_TEST); |
252 | glEnable (GL_DEPTH_TEST); |
209 | glColorMask (0, 0, 0, 0); |
253 | glColorMask (0, 0, 0, 0); |
… | |
… | |
218 | glDepthFunc (GL_LESS); |
262 | glDepthFunc (GL_LESS); |
219 | glColorMask (0, 0, 0, 0); |
263 | glColorMask (0, 0, 0, 0); |
220 | glDepthMask (0); |
264 | glDepthMask (0); |
221 | glEnable (GL_DEPTH_CLAMP_NV); |
265 | glEnable (GL_DEPTH_CLAMP_NV); |
222 | glDisable (GL_STENCIL_TEST); |
266 | glDisable (GL_STENCIL_TEST); |
|
|
267 | glDisable (GL_CULL_FACE); |
223 | |
268 | |
|
|
269 | occ_query_material.enable (*this); |
|
|
270 | |
224 | // check occlusion queries |
271 | // check occlusion queries |
225 | for (vector<oq_data>::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i) |
272 | for (vector<oq_data>::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i) |
226 | { |
273 | { |
227 | occ_query oq(*this, i->data, ::occ_query_result (i->id)); |
274 | occ_query oq(*this, i->data, ::occ_query_result (i->id)); |
228 | i->recv->event (oq); |
275 | i->recv->event (oq); |
229 | } |
276 | } |
230 | |
277 | |
|
|
278 | occ_query_material.disable (*this); |
|
|
279 | |
231 | occ_queries.clear (); |
280 | occ_queries.clear (); |
232 | |
281 | |
233 | break; |
282 | break; |
234 | |
283 | |
235 | case LIGHTED: |
284 | case LIGHTED: |
|
|
285 | glEnable (GL_CULL_FACE); |
|
|
286 | //glEnable (GL_BLEND);//TODO, only when not first lighted pass |
|
|
287 | glBlendFunc (GL_ONE, GL_ONE); |
236 | glClear (GL_STENCIL_BUFFER_BIT); |
288 | //glClear (GL_STENCIL_BUFFER_BIT); |
237 | //glEnable (GL_STENCIL_TEST); |
289 | //glEnable (GL_STENCIL_TEST); |
238 | glEnable (GL_MINMAX); |
290 | glEnable (GL_MINMAX); |
239 | glDepthRange (0., 1. - DEPTH_OFFSET); |
291 | glDepthRange (0., 1. - DEPTH_OFFSET); |
240 | glDepthFunc (GL_LESS); |
292 | glDepthFunc (GL_LESS); |
241 | glColorMask (1, 1, 1, 1); |
293 | glColorMask (1, 1, 1, 1); |
… | |
… | |
246 | } |
298 | } |
247 | |
299 | |
248 | for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i) |
300 | for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i) |
249 | (*i)->display (*this); |
301 | (*i)->display (*this); |
250 | |
302 | |
251 | if (pass == LIGHTED) |
303 | if (pass.type == DEPTH) |
252 | 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 |
304 | 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 |
253 | |
305 | |
254 | drawn.clear (); |
306 | drawn.clear (); |
255 | |
307 | |
256 | #if 0 |
308 | #if 0 |
… | |
… | |
270 | glDisable (GL_DEPTH_CLAMP_NV); |
322 | glDisable (GL_DEPTH_CLAMP_NV); |
271 | } |
323 | } |
272 | #endif |
324 | #endif |
273 | } |
325 | } |
274 | |
326 | |
|
|
327 | void light::enable () |
|
|
328 | { |
|
|
329 | lightpos->set (p); |
|
|
330 | } |
275 | |
331 | |
|
|
332 | void light::disable () |
|
|
333 | { |
|
|
334 | } |
|
|
335 | |
|
|
336 | static shader::varying_1f camdist; |
|
|
337 | static shader::varying_3f lightvec; |
|
|
338 | |
|
|
339 | void linear_light::vsh () |
|
|
340 | { |
|
|
341 | using namespace shader::compile; |
|
|
342 | |
|
|
343 | lightvec = xyz (lightpos - model_view_matrix * vin.vertex); |
|
|
344 | camdist = max (1 - length (lightvec) / radius, 0); |
|
|
345 | |
|
|
346 | fsh (); |
|
|
347 | } |
|
|
348 | |
|
|
349 | void linear_light::fsh () |
|
|
350 | { |
|
|
351 | using namespace shader::compile; |
|
|
352 | |
|
|
353 | sh_lightvec = lightvec; |
|
|
354 | sh_colour = float3 (c.r / 255.F, c.g / 255.F, c.b / 255.F) * (min (intensity * camdist * 100.F + 0.9F, 1.F)); |
|
|
355 | } |
|
|
356 | |
|
|
357 | |