ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/view.C
Revision: 1.81
Committed: Fri Nov 26 03:48:40 2004 UTC (19 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.80: +13 -9 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 root 1.80 if (ctx.generation - vs->generation > 2)
110 root 1.51 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    
127     GLdouble aspect = (GLdouble)w/h;
128 root 1.77 GLdouble ftan = tanf (fov * GLfloat (.5 * M_PI / 180.));
129     GLdouble ymax = ftan;
130 root 1.59 GLdouble xmax = ymax * aspect;
131 root 1.1
132 root 1.72 matrix perspective;
133     {
134     matrix &m = perspective;
135    
136 root 1.75 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 root 1.81 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 root 1.74
141     glLoadMatrixf (perspective);
142 root 1.72 }
143 root 1.77 //glFrustum (-xmax, xmax, -ymax, ymax, z_near, z_far);
144 root 1.72
145     glGetFloatv (GL_PROJECTION_MATRIX, perspective);
146 root 1.1
147 root 1.59 perspfact = z_near / ymax * 0.5F * h;
148 root 1.48
149 root 1.72 glMatrixMode (GL_MODELVIEW);
150     glLoadIdentity ();
151     matrix &m = projection;
152    
153 root 1.6 d = normalize (d);//D
154     u = normalize (u);//D
155 root 1.41
156 root 1.1 vec3 rz = -d;
157     vec3 rx = cross (u, rz);
158     vec3 ry = cross (rz, rx);
159    
160 root 1.81 m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = 0;
161     m(1,0) = ry.x; m(1,1) = ry.y; m(1,2) = ry.z; m(1,3) = 0;
162     m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = 0;
163     m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1.F;
164 root 1.37
165 root 1.38 diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z);
166 root 1.48 //printf ("diagfact = %f\n", diagfact);
167 root 1.81 //diagfact = sqrtf (3.);//D WHY???
168 root 1.3
169 root 1.38 glMultMatrixf (m);
170 root 1.81
171     glTranslatef (-p.x, -p.y, -p.z);
172 root 1.63
173 root 1.72 glGetFloatv (GL_MODELVIEW_MATRIX, m);
174    
175     m = perspective * m;
176 root 1.5
177 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) );
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) );
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) );
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) );
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) );
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) );
183 root 1.1
184 root 1.77 #if 0
185 root 1.59 {
186     GLdouble frustlen = z_far - z_near;
187     GLdouble fheight = frustlen * ftan;
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 root 1.77 #endif
195 root 1.59
196     {
197     GLdouble depth = h / ftan;
198    
199     frustum.c = cone (p, d, atan (sqrt (GLdouble (w * w + h * h)) * ftan / h));
200     }
201 root 1.18 }
202    
203     void view::begin ()
204     {
205 root 1.53 generation++;
206    
207 root 1.19 vislist.clear ();
208 root 1.80 postdepthlist.clear ();
209 root 1.18
210 root 1.60 z_near = max (nz_near, 1.F);
211     z_far = max (nz_far, z_near * 2.F);
212 root 1.43 c_far = nc_far;
213 root 1.48
214 root 1.43 reset_projection ();
215    
216 root 1.65 nz_near = 100.;
217 root 1.60 nc_far = nz_far = 1.F;
218 root 1.77
219     first_lighted = false;
220 root 1.18 }
221    
222     void view::end ()
223     {
224 root 1.40 vislist.clear ();
225 root 1.18 }
226    
227 root 1.50 #define DEPTH_OFFSET (1. / (GLdouble)(1L << 16))
228    
229 root 1.76 void view::render (pass_data &pass)
230 root 1.18 {
231 root 1.76 this->pass = &pass;
232 root 1.18
233 root 1.78 stat1 = stat2 = 0; //D
234    
235 root 1.76 switch (pass.type)
236 root 1.18 {
237 root 1.52 case DEPTH:
238 root 1.63 glColorMask (1, 1, 1, 1);
239     glDepthMask (1);
240 root 1.75 glDisable (GL_BLEND);
241 root 1.63
242 root 1.75 //glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
243     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
244 root 1.63
245     //glEnable (GL_STENCIL_TEST); // for depth-passes
246     //glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE);
247     //glStencilFunc (GL_LESS, 1, 255);
248    
249 root 1.52 //glEnable (GL_POLYGON_OFFSET_FILL);
250     //glPolygonOffset (0, 5);
251 root 1.75 glEnable (GL_CULL_FACE);
252 root 1.52 glDisable (GL_MINMAX);
253     glDepthRange (DEPTH_OFFSET, 1.);
254     glDepthFunc (GL_LESS);
255     glEnable (GL_DEPTH_TEST);
256     glColorMask (0, 0, 0, 0);
257 root 1.54
258 root 1.61 world.detect_visibility (*this);
259 root 1.54
260 root 1.77 for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i)
261     (*i)->draw_depth (*this);
262    
263     if (pass.type == DEPTH)
264 root 1.79 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 root 1.77
267 root 1.52 break;
268    
269     case POSTDEPTH:
270 root 1.59 glDepthRange (0., 1. - DEPTH_OFFSET);
271 root 1.57 glDepthFunc (GL_LESS);
272 root 1.52 glColorMask (0, 0, 0, 0);
273     glDepthMask (0);
274 root 1.63 glDisable (GL_STENCIL_TEST);
275 root 1.75 glDisable (GL_CULL_FACE);
276 root 1.81
277     glEnable (GL_DEPTH_CLAMP_NV);
278 root 1.53
279 root 1.76 occ_query_material.enable (*this);
280    
281 root 1.53 // 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 root 1.76 occ_query_material.disable (*this);
289    
290 root 1.53 occ_queries.clear ();
291    
292 root 1.80 for (vector<octant *>::iterator i = postdepthlist.begin (); i != postdepthlist.end (); ++i)
293 root 1.77 (*i)->draw_postdepth (*this);
294 root 1.81
295     glDisable (GL_DEPTH_CLAMP_NV);
296 root 1.77
297     first_lighted = true;
298    
299 root 1.52 break;
300    
301     case LIGHTED:
302 root 1.77 if (first_lighted)
303     glDisable (GL_BLEND);
304     else
305     {
306     glBlendFunc (GL_ONE, GL_ONE);
307     glEnable (GL_BLEND);
308     }
309    
310 root 1.75 //glClear (GL_STENCIL_BUFFER_BIT);
311 root 1.63 //glEnable (GL_STENCIL_TEST);
312 root 1.77 glEnable (GL_CULL_FACE);
313 root 1.52 glEnable (GL_MINMAX);
314     glDepthRange (0., 1. - DEPTH_OFFSET);
315     glDepthFunc (GL_LESS);
316 root 1.53 glColorMask (1, 1, 1, 1);
317 root 1.52 glDepthMask (0);
318 root 1.54
319 root 1.77 for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i)
320     (*i)->draw_lighted (*this);
321    
322     first_lighted = false;
323    
324 root 1.52 break;
325 root 1.18 }
326    
327 root 1.56 drawn.clear ();
328 root 1.1 }
329 root 1.6
330 root 1.69 void light::enable ()
331     {
332     lightpos->set (p);
333     }
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 root 1.73
349     fsh ();
350 root 1.69 }
351    
352 root 1.73 void linear_light::fsh ()
353 root 1.69 {
354     using namespace shader::compile;
355    
356 root 1.73 sh_lightvec = lightvec;
357 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));
358 root 1.69 }
359    
360 root 1.1