ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/view.C
(Generate patch)

Comparing libgender/view.C (file contents):
Revision 1.30 by root, Wed Oct 6 16:19:14 2004 UTC vs.
Revision 1.80 by root, Fri Nov 26 02:38:17 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines