ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libgender/view.C
Revision: 1.76
Committed: Sun Nov 7 03:28:20 2004 UTC (21 years, 10 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.75: +29 -6 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #include <cmath>
2    
3 root 1.45 #include "opengl.h"
4 root 1.26
5 root 1.37 #include "view.h"
6 root 1.1 #include "oct.h"
7 root 1.76 #include "material.h"
8 root 1.1
9 root 1.55 using namespace gl;
10    
11 root 1.76 pass_data pass_depth (0, DEPTH);
12     pass_data pass_postdepth (0, POSTDEPTH);
13 root 1.68
14 root 1.37 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 root 1.66 glGenQueries (1, &id);
27 root 1.37
28 root 1.66 glBeginQuery (GL_SAMPLES_PASSED, id);
29 root 1.37 return id;
30     }
31    
32     inline void end_occ_query ()
33     {
34 root 1.66 glEndQuery (GL_SAMPLES_PASSED);
35 root 1.37 }
36    
37     static GLuint occ_query_result (GLuint id)
38     {
39     GLuint count;
40    
41 root 1.66 glGetQueryObjectuiv (id, GL_QUERY_RESULT, &count);
42 root 1.37 occ_query_objects.push_back (id);
43    
44     return count;
45     }
46    
47 root 1.17 view::view ()
48 root 1.60 : gamma(1.0), nz_near (1.F), nz_far (2.F)
49 root 1.1 {
50     }
51    
52 root 1.17 view::~view ()
53 root 1.1 {
54     }
55    
56 root 1.51 void view::begin_occ_query (recv_occ_query &recv, void *id)
57 root 1.37 {
58 root 1.51 occ_queries.push_back (oq_data (&recv, ::begin_occ_query (), id));
59 root 1.37 }
60    
61     void view::end_occ_query ()
62     {
63     ::end_occ_query ();
64     }
65    
66 root 1.76 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
82     }
83    
84 root 1.34 bool view::may_draw (const entity *e)
85 root 1.1 {
86     if (drawn.find (e) != drawn.end ())
87     return false;
88    
89     drawn.insert (e);
90     return true;
91     }
92    
93 root 1.51 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 (vs->generation + 1 != ctx.generation)
110     clear_visibility (vs);
111    
112     vs->generation = ctx.generation;
113     }
114     }
115    
116     return vs;
117     }
118    
119 root 1.18 void view::reset_projection ()
120 root 1.1 {
121 root 1.42 renormalize (orig, p);
122 root 1.3
123 root 1.1 glViewport (0, 0, w, h);
124    
125     glMatrixMode (GL_PROJECTION);
126     glLoadIdentity ();
127    
128     GLdouble aspect = (GLdouble)w/h;
129 root 1.59 GLdouble ftan = tan (fov * (.5 * M_PI / 180.));
130     GLdouble ymax = z_near * ftan;
131     GLdouble xmax = ymax * aspect;
132 root 1.1
133 root 1.72 matrix perspective;
134 root 1.74 #if 1
135 root 1.72 {
136     matrix &m = perspective;
137    
138 root 1.75 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 root 1.74
143     glLoadMatrixf (perspective);
144 root 1.72 }
145     #else
146 root 1.59 glFrustum (-xmax, xmax, -ymax, ymax, z_near, z_far);
147 root 1.72 #endif
148    
149     glGetFloatv (GL_PROJECTION_MATRIX, perspective);
150 root 1.1
151 root 1.59 perspfact = z_near / ymax * 0.5F * h;
152 root 1.48
153 root 1.72 glMatrixMode (GL_MODELVIEW);
154     glLoadIdentity ();
155     matrix &m = projection;
156    
157 root 1.6 d = normalize (d);//D
158     u = normalize (u);//D
159 root 1.41
160 root 1.1 vec3 rz = -d;
161     vec3 rx = cross (u, rz);
162     vec3 ry = cross (rz, rx);
163    
164 root 1.16 m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = 0;
165     m(1,0) = ry.x; m(1,1) = ry.y; m(1,2) = ry.z; m(1,3) = 0;
166     m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = 0;
167 root 1.3 m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1;
168 root 1.37
169 root 1.38 diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z);
170 root 1.48 //printf ("diagfact = %f\n", diagfact);
171 root 1.41 diagfact = sqrtf (3.);//D WHY???
172 root 1.3
173 root 1.38 glMultMatrixf (m);
174 root 1.63
175 root 1.72 glGetFloatv (GL_MODELVIEW_MATRIX, m);
176    
177     m = perspective * m;
178 root 1.5
179 root 1.17 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) );
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) );
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) );
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) );
183     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) );
184     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) );
185 root 1.1
186 root 1.59 {
187     GLdouble frustlen = z_far - z_near;
188     GLdouble fheight = frustlen * ftan;
189     GLdouble fwidth = fheight * w / h;
190     point half (0, 0, z_near + frustlen * .5);
191     point corner (fwidth, fheight, frustlen);
192    
193     frustum.s = sphere (p + d * (.5 * frustlen), length (corner - half));
194     }
195    
196     {
197     GLdouble depth = h / ftan;
198    
199     frustum.c = cone (p, d, atan (sqrt (GLdouble (w * w + h * h)) * ftan / h));
200     }
201    
202 root 1.67 glTranslatef (-p.x, -p.y, -p.z);
203 root 1.18 }
204    
205     void view::begin ()
206     {
207 root 1.53 generation++;
208    
209 root 1.19 vislist.clear ();
210 root 1.18
211 root 1.60 z_near = max (nz_near, 1.F);
212     z_far = max (nz_far, z_near * 2.F);
213 root 1.43 c_far = nc_far;
214 root 1.48
215 root 1.43 reset_projection ();
216    
217 root 1.65 nz_near = 100.;
218 root 1.60 nc_far = nz_far = 1.F;
219 root 1.18 }
220    
221     void view::end ()
222     {
223 root 1.40 vislist.clear ();
224 root 1.18 }
225    
226 root 1.50 #define DEPTH_OFFSET (1. / (GLdouble)(1L << 16))
227    
228 root 1.76 void view::render (pass_data &pass)
229 root 1.18 {
230 root 1.76 this->pass = &pass;
231 root 1.18
232 root 1.76 switch (pass.type)
233 root 1.18 {
234 root 1.52 case DEPTH:
235 root 1.63 glColorMask (1, 1, 1, 1);
236     glDepthMask (1);
237 root 1.75 glDisable (GL_BLEND);
238 root 1.63
239 root 1.75 //glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
240     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
241 root 1.63
242     //glEnable (GL_STENCIL_TEST); // for depth-passes
243     //glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE);
244     //glStencilFunc (GL_LESS, 1, 255);
245    
246 root 1.52 //glEnable (GL_POLYGON_OFFSET_FILL);
247     //glPolygonOffset (0, 5);
248 root 1.75 glEnable (GL_CULL_FACE);
249 root 1.52 glDisable (GL_MINMAX);
250     glDepthRange (DEPTH_OFFSET, 1.);
251     glDepthFunc (GL_LESS);
252     glEnable (GL_DEPTH_TEST);
253     glColorMask (0, 0, 0, 0);
254 root 1.59 glDisable (GL_DEPTH_CLAMP_NV);
255 root 1.54
256 root 1.61 world.detect_visibility (*this);
257 root 1.54
258 root 1.52 break;
259    
260     case POSTDEPTH:
261 root 1.59 glDepthRange (0., 1. - DEPTH_OFFSET);
262 root 1.57 glDepthFunc (GL_LESS);
263 root 1.52 glColorMask (0, 0, 0, 0);
264     glDepthMask (0);
265 root 1.59 glEnable (GL_DEPTH_CLAMP_NV);
266 root 1.63 glDisable (GL_STENCIL_TEST);
267 root 1.75 glDisable (GL_CULL_FACE);
268 root 1.53
269 root 1.76 occ_query_material.enable (*this);
270    
271 root 1.53 // check occlusion queries
272     for (vector<oq_data>::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i)
273     {
274     occ_query oq(*this, i->data, ::occ_query_result (i->id));
275     i->recv->event (oq);
276     }
277    
278 root 1.76 occ_query_material.disable (*this);
279    
280 root 1.53 occ_queries.clear ();
281    
282 root 1.52 break;
283    
284     case LIGHTED:
285 root 1.75 glEnable (GL_CULL_FACE);
286     //glEnable (GL_BLEND);//TODO, only when not first lighted pass
287     glBlendFunc (GL_ONE, GL_ONE);
288     //glClear (GL_STENCIL_BUFFER_BIT);
289 root 1.63 //glEnable (GL_STENCIL_TEST);
290 root 1.52 glEnable (GL_MINMAX);
291     glDepthRange (0., 1. - DEPTH_OFFSET);
292     glDepthFunc (GL_LESS);
293 root 1.53 glColorMask (1, 1, 1, 1);
294 root 1.52 glDepthMask (0);
295 root 1.59 glDisable (GL_DEPTH_CLAMP_NV);
296 root 1.54
297 root 1.52 break;
298 root 1.18 }
299    
300 root 1.61 for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i)
301     (*i)->display (*this);
302 root 1.62
303 root 1.76 if (pass.type == DEPTH)
304 root 1.62 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
305 root 1.61
306 root 1.56 drawn.clear ();
307    
308 root 1.52 #if 0
309 root 1.56 if (pass == view::DEPTH)
310 root 1.25 {
311 root 1.27 glEnable (GL_DEPTH_CLAMP_NV);
312 root 1.42 glDepthMask (0);
313 root 1.30 glDepthFunc (GL_LESS);
314 root 1.25
315 root 1.37 for (vector<octant *>::iterator i = farlist.begin (); i != farlist.end (); ++i)
316     {
317 root 1.48 begin_occ_query (**i);
318 root 1.37 (*i)->draw_bbox (*this);
319 root 1.48 end_occ_query ();
320 root 1.30 }
321 root 1.27
322     glDisable (GL_DEPTH_CLAMP_NV);
323 root 1.25 }
324 root 1.52 #endif
325 root 1.1 }
326 root 1.6
327 root 1.69 void light::enable ()
328     {
329     lightpos->set (p);
330     }
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 root 1.73
346     fsh ();
347 root 1.69 }
348    
349 root 1.73 void linear_light::fsh ()
350 root 1.69 {
351     using namespace shader::compile;
352    
353 root 1.73 sh_lightvec = lightvec;
354 root 1.75 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 root 1.69 }
356    
357 root 1.1