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