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

Comparing libgender/view.C (file contents):
Revision 1.79 by root, Wed Nov 10 01:57:16 2004 UTC vs.
Revision 1.92 by root, Thu Aug 11 19:28:45 2005 UTC

4 4
5#include "view.h" 5#include "view.h"
6#include "oct.h" 6#include "oct.h"
7#include "material.h" 7#include "material.h"
8 8
9#include "entity.h"
10
9using namespace gl; 11using namespace gl;
12
13skybox *world_skybox;
10 14
11pass_data pass_depth (0, DEPTH); 15pass_data pass_depth (0, DEPTH);
12pass_data pass_postdepth (0, POSTDEPTH); 16pass_data pass_postdepth (0, POSTDEPTH);
13 17
14vector<GLuint> occ_query_objects; 18vector<GLuint> occ_query_objects;
51 55
52view::~view () 56view::~view ()
53{ 57{
54} 58}
55 59
56void view::begin_occ_query (recv_occ_query &recv, void *id) 60void view::begin_occ_query (int &res)
57{ 61{
58 occ_queries.push_back (oq_data (&recv, ::begin_occ_query (), id)); 62 occ_queries.push_back (oq_data (::begin_occ_query (), res));
59} 63}
60 64
61void view::end_occ_query () 65void view::end_occ_query ()
62{ 66{
63 ::end_occ_query (); 67 ::end_occ_query ();
104 { 108 {
105 vs = i->second; 109 vs = i->second;
106 110
107 if (vs->generation != ctx.generation) 111 if (vs->generation != ctx.generation)
108 { 112 {
109 if (vs->generation + 1 != ctx.generation) 113 if (ctx.generation - vs->generation > 2)
110 clear_visibility (vs); 114 clear_visibility (vs);
111 115
112 vs->generation = ctx.generation; 116 vs->generation = ctx.generation;
113 } 117 }
114 } 118 }
119void view::reset_projection () 123void view::reset_projection ()
120{ 124{
121 renormalize (orig, p); 125 renormalize (orig, p);
122 126
123 glViewport (0, 0, w, h); 127 glViewport (0, 0, w, h);
124
125 glMatrixMode (GL_PROJECTION);
126 128
127 GLdouble aspect = (GLdouble)w/h; 129 GLdouble aspect = (GLdouble)w/h;
128 GLdouble ftan = tanf (fov * GLfloat (.5 * M_PI / 180.)); 130 GLdouble ftan = tanf (fov * GLfloat (.5 * M_PI / 180.));
129 GLdouble ymax = ftan; 131 GLdouble ymax = ftan;
130 GLdouble xmax = ymax * aspect; 132 GLdouble xmax = ymax * aspect;
133 { 135 {
134 matrix &m = perspective; 136 matrix &m = perspective;
135 137
136 m(0,0) = 1.F / xmax; m(0,1) = 0; m(0,2) = 0; m(0,3) = 0; 138 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; 139 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; 140 m(2,0) = 0; m(2,1) = 0; m(2,2) = -1e30F / z_far; m(2,3) = 0;
139 m(3,0) = 0; m(3,1) = 0; m(3,2) = -1; m(3,3) = 0; 141 m(3,0) = 0; m(3,1) = 0; m(3,2) = -1.F; m(3,3) = 0;
140 142
141 glLoadMatrixf (perspective); 143 glLoadMatrixf (perspective);
142 } 144 }
143 //glFrustum (-xmax, xmax, -ymax, ymax, z_near, z_far); 145 //glFrustum (-xmax, xmax, -ymax, ymax, z_near, z_far);
144 146
145 glGetFloatv (GL_PROJECTION_MATRIX, perspective); 147 //glGetFloatv (GL_PROJECTION_MATRIX, perspective);
146 148
147 perspfact = z_near / ymax * 0.5F * h; 149 perspfact = z_near / ymax * 0.5F * h;
148 150
149 glMatrixMode (GL_MODELVIEW); 151 matrix view;
150 glLoadIdentity (); 152 {
151 matrix &m = projection; 153 matrix &m = view;
152 154
153 d = normalize (d);//D 155 d = normalize (d);//D
154 u = normalize (u);//D 156 u = normalize (u);//D
155 157
156 vec3 rz = -d; 158 vec3 rz = -d;
157 vec3 rx = cross (u, rz); 159 vec3 rx = cross (u, rz);
158 vec3 ry = cross (rz, rx); 160 vec3 ry = cross (rz, rx);
159 161
160 m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = 0; 162 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; 163 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; 164 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; 165 m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1.F;
164 166
165 diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z); 167 //diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z);
168 diagfact = sqrtf (3.F);
169 }
170
166 //printf ("diagfact = %f\n", diagfact); 171 //printf ("diagfact = %f\n", diagfact);
167 diagfact = sqrtf (3.);//D WHY??? 172 //diagfact = sqrtf (3.);//D WHY???
173 //
174 view = view * matrix::translation (-p);
168 175
169 glMultMatrixf (m); 176 matrix projection = perspective * view;
177
170 178 {
171 glTranslatef (-p.x, -p.y, -p.z); 179 matrix &m = projection;
172 180
173 glGetFloatv (GL_MODELVIEW_MATRIX, m);
174
175 m = perspective * m;
176
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) ); 181 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) ); 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) );
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) ); 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) );
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) ); 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) );
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) ); 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) );
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) ); 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 }
183 188
184#if 0 189#if 0
185 { 190 {
186 GLdouble frustlen = z_far - z_near; 191 GLdouble frustlen = z_far - z_near;
187 GLdouble fheight = frustlen * ftan; 192 GLdouble fheight = frustlen * ftan;
191 196
192 frustum.s = sphere (p + d * (.5 * frustlen), length (corner - half)); 197 frustum.s = sphere (p + d * (.5 * frustlen), length (corner - half));
193 } 198 }
194#endif 199#endif
195 200
196 {
197 GLdouble depth = h / ftan; 201 GLdouble depth = h / ftan;
198
199 frustum.c = cone (p, d, atan (sqrt (GLdouble (w * w + h * h)) * ftan / h)); 202 frustum.c = cone (p, d, atan (sqrt (GLdouble (w * w + h * h)) * ftan));
200 } 203
204 glMatrixMode (GL_PROJECTION);
205 glLoadMatrixf (projection);
206
207 glMatrixMode (GL_MODELVIEW);
208 glLoadIdentity ();
201} 209}
202 210
203void view::begin () 211void view::begin ()
204{ 212{
205 generation++; 213 generation++;
206 214
215 stat1 = stat2 = 0; //D
216
207 vislist.clear (); 217 vislist.clear ();
218 postdepthlist.clear ();
208 219
209 z_near = max (nz_near, 1.F); 220 z_near = max (nz_near, 1.F);
210 z_far = max (nz_far, z_near * 2.F); 221 z_far = max (nz_far, 1E10F);//D
211 c_far = nc_far; 222 c_far = nc_far;
212 223
213 reset_projection (); 224 reset_projection ();
214 225
215 nz_near = 100.; 226 nz_near = 100.;
218 first_lighted = false; 229 first_lighted = false;
219} 230}
220 231
221void view::end () 232void view::end ()
222{ 233{
234 printf ("\rfps %f NF %f:%f vis %d,%d (%d,%d) CAM (%ld,%ld,%ld) ",
235 timer.fps, z_near, z_far, vislist.size (), drawn.size (), stat1, stat2, orig.x, orig.y, orig.z);//D
236
223 vislist.clear (); 237 vislist.clear ();
224} 238}
225 239
226#define DEPTH_OFFSET (1. / (GLdouble)(1L << 16)) 240#define DEPTH_OFFSET (1. / (GLdouble)(1L << 16))
227 241
228void view::render (pass_data &pass) 242void view::render (pass_data &pass)
229{ 243{
230 this->pass = &pass; 244 this->pass = &pass;
231
232 stat1 = stat2 = 0; //D
233 245
234 switch (pass.type) 246 switch (pass.type)
235 { 247 {
236 case DEPTH: 248 case DEPTH:
237 glColorMask (1, 1, 1, 1);
238 glDepthMask (1);
239 glDisable (GL_BLEND);
240
241 //glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
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
248 //glEnable (GL_POLYGON_OFFSET_FILL);
249 //glPolygonOffset (0, 5);
250 glEnable (GL_CULL_FACE); 249 glEnable (GL_CULL_FACE);
251 glDisable (GL_MINMAX); 250 glDisable (GL_MINMAX);
252 glDepthRange (DEPTH_OFFSET, 1.); 251 glDepthRange (DEPTH_OFFSET, 1.);
253 glDepthFunc (GL_LESS); 252 glDepthFunc (GL_LESS);
253 glColorMask (1, 1, 1, 1);
254 glDisable (GL_BLEND);
255 glDepthMask (1);
256
257 glClear (GL_DEPTH_BUFFER_BIT | (world_skybox ? 0 : GL_COLOR_BUFFER_BIT));
258
259 //glEnable (GL_STENCIL_TEST); // for depth-passes
260 //glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE);
261 //glStencilFunc (GL_LESS, 1, 255);
262
263 //glEnable (GL_POLYGON_OFFSET_FILL);
264 //glPolygonOffset (0, 5);
254 glEnable (GL_DEPTH_TEST); 265 glEnable (GL_DEPTH_TEST);
255 glColorMask (0, 0, 0, 0); 266 glColorMask (0, 0, 0, 0);
256 glDisable (GL_DEPTH_CLAMP_NV); 267
268 // check occlusion queries
269 printf ("OC1 %d\n", occ_queries.size ());//D
270 for (vector<oq_data>::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i)
271 *(i->res) = ::occ_query_result (i->id);
272
273 occ_queries.clear ();
257 274
258 world.detect_visibility (*this); 275 world.detect_visibility (*this);
259 276
260 for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i) 277 for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i)
261 (*i)->draw_depth (*this); 278 (*i)->draw_depth (*this);
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 279
267 break; 280 break;
268 281
269 case POSTDEPTH: 282 case POSTDEPTH:
270 glDepthRange (0., 1. - DEPTH_OFFSET); 283 glDepthRange (0., 1. - DEPTH_OFFSET);
271 glDepthFunc (GL_LESS); 284 glDepthFunc (GL_LESS);
272 glColorMask (0, 0, 0, 0); 285 glColorMask (0, 0, 0, 0);
273 glDepthMask (0); 286 glDepthMask (0);
274 glEnable (GL_DEPTH_CLAMP_NV);
275 glDisable (GL_STENCIL_TEST); 287 glDisable (GL_STENCIL_TEST);
276 glDisable (GL_CULL_FACE); 288 glDisable (GL_CULL_FACE);
289
290 glEnable (GL_DEPTH_CLAMP_NV);
277 291
278 occ_query_material.enable (*this); 292 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) 293 for (vector<octant *>::iterator i = postdepthlist.begin (); i != postdepthlist.end (); ++i)
282 { 294 (*i)->draw_postdepth (*this);
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); 295 occ_query_material.disable (*this);
288 296
289 occ_queries.clear (); 297 glDisable (GL_DEPTH_CLAMP_NV);
290
291 for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i)
292 (*i)->draw_postdepth (*this);
293 298
294 first_lighted = true; 299 first_lighted = true;
295 300
296 break; 301 break;
297 302
310 glEnable (GL_MINMAX); 315 glEnable (GL_MINMAX);
311 glDepthRange (0., 1. - DEPTH_OFFSET); 316 glDepthRange (0., 1. - DEPTH_OFFSET);
312 glDepthFunc (GL_LESS); 317 glDepthFunc (GL_LESS);
313 glColorMask (1, 1, 1, 1); 318 glColorMask (1, 1, 1, 1);
314 glDepthMask (0); 319 glDepthMask (0);
320
321 if (world_skybox)
322 {
315 glDisable (GL_DEPTH_CLAMP_NV); 323 glDisable (GL_DEPTH_TEST);
324 world_skybox->draw (*this);//
325 glEnable (GL_DEPTH_TEST);
326 }
316 327
317 for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i) 328 for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i)
318 (*i)->draw_lighted (*this); 329 (*i)->draw_lighted (*this);
319 330
320 first_lighted = false; 331 first_lighted = false;
323 } 334 }
324 335
325 drawn.clear (); 336 drawn.clear ();
326} 337}
327 338
328void light::enable () 339void light::enable (view &ctx)
329{ 340{
330 lightpos->set (p); 341 lightpos->set (p + (orig - ctx.orig));
331} 342}
332 343
333void light::disable () 344void light::disable (view &ctx)
334{ 345{
335} 346}
336 347
337static shader::varying_1f camdist; 348void linear_light::enable (view &ctx)
338static shader::varying_3f lightvec; 349{
350 light::enable (ctx);
351}
352
353void linear_light::disable (view &ctx)
354{
355 light::disable (ctx);
356}
339 357
340void linear_light::vsh () 358void linear_light::vsh ()
341{ 359{
342 using namespace shader::compile; 360 using namespace shader::compile;
361 temp_1f camdist;
343 362
344 lightvec = xyz (lightpos - model_view_matrix * vin.vertex); 363 lightvec = xyz (lightpos - model_view_matrix * vin.vertex);
345 camdist = max (1 - length (lightvec) / radius, 0); 364 camdist = max (1 - length (lightvec) / radius, 0);
346 365
347 fsh (); 366 fsh ();
350void linear_light::fsh () 369void linear_light::fsh ()
351{ 370{
352 using namespace shader::compile; 371 using namespace shader::compile;
353 372
354 sh_lightvec = lightvec; 373 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)); 374 sh_colour = float3 (c.r / 255.F, c.g / 255.F, c.b / 255.F) * (min (camdist * (intensity * 100.F) + 0.9F, 1.F));
356} 375}
357 376
358 377

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines