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

Comparing libgender/view.C (file contents):
Revision 1.37 by root, Sat Oct 9 16:36:31 2004 UTC vs.
Revision 1.79 by root, Wed Nov 10 01:57:16 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines