]> git.mxchange.org Git - flightgear.git/blob - Scenery/tilemgr.cxx
cleaned up my fragment.num_faces hack :-) to use the STL (no need in
[flightgear.git] / Scenery / tilemgr.cxx
1 // tilemgr.cxx -- routines to handle dynamic management of scenery tiles
2 //
3 // Written by Curtis Olson, started January 1998.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22 // (Log is kept at end of this file)
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #ifdef HAVE_WINDOWS_H
30 #  include <windows.h>
31 #endif
32
33 #include <GL/glut.h>
34 #include <XGL/xgl.h>
35
36 #include <Aircraft/aircraft.h>
37
38 #include <Bucket/bucketutils.h>
39 #include <Debug/fg_debug.h>
40 #include <Include/fg_constants.h>
41 #include <Include/fg_types.h>
42 #include <Main/options.hxx>
43 #include <Main/views.hxx>
44 #include <Math/fg_geodesy.h>
45 #include <Math/mat3.h>
46 #include <Math/polar3d.hxx>
47 #include <Math/vector.hxx>
48 #include <Objects/material.hxx>
49 #include <Objects/obj.hxx>
50 #include <Weather/weather.h>
51
52 #include "scenery.hxx"
53 #include "tilecache.hxx"
54
55
56 // to test clipping speedup in fgTileMgrRender()
57 #if defined ( USE_FAST_FOV_CLIP )
58   // #define TEST_FOV_CLIP
59   // #define TEST_ELEV
60 #endif
61
62
63 #define FG_LOCAL_X_Y         81  // max(o->tile_diameter) ** 2
64
65 #define FG_SQUARE( X ) ( (X) * (X) )
66
67 #ifdef WIN32
68 #  define FG_MEM_COPY(to,from,n)        memcpy(to, from, n)
69 #else
70 #  define FG_MEM_COPY(to,from,n)        bcopy(from, to, n)
71 #endif
72
73 // closest (potentially viewable) tiles, centered on current tile.
74 // This is an array of pointers to cache indexes.
75 int tiles[FG_LOCAL_X_Y];
76
77
78 // Initialize the Tile Manager subsystem
79 int fgTileMgrInit( void ) {
80     fgPrintf( FG_TERRAIN, FG_INFO, "Initializing Tile Manager subsystem.\n");
81
82     // load default material library
83     material_mgr.load_lib();
84
85     return 1;
86 }
87
88
89 // load a tile
90 void fgTileMgrLoadTile( fgBUCKET *p, int *index) {
91     fgTILECACHE *c;
92
93     c = &global_tile_cache;
94
95     fgPrintf( FG_TERRAIN, FG_DEBUG, "Updating for bucket %d %d %d %d\n", 
96            p->lon, p->lat, p->x, p->y);
97     
98     // if not in cache, load tile into the next available slot
99     if ( (*index = c->exists(p)) < 0 ) {
100         *index = c->next_avail();
101         c->fill_in(*index, p);
102     }
103
104     fgPrintf( FG_TERRAIN, FG_DEBUG, "Selected cache index of %d\n", *index);
105 }
106
107
108 // given the current lon/lat, fill in the array of local chunks.  If
109 // the chunk isn't already in the cache, then read it from disk.
110 int fgTileMgrUpdate( void ) {
111     fgTILECACHE *c;
112     fgFLIGHT *f;
113     fgBUCKET p1, p2;
114     static fgBUCKET p_last = {-1000, 0, 0, 0};
115     int tile_diameter;
116     int i, j, dw, dh;
117
118     c = &global_tile_cache;
119     f = current_aircraft.flight;
120
121     tile_diameter = current_options.get_tile_diameter();
122
123     fgBucketFind(FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, &p1);
124     dw = tile_diameter / 2;
125     dh = tile_diameter / 2;
126
127     if ( (p1.lon == p_last.lon) && (p1.lat == p_last.lat) &&
128          (p1.x == p_last.x) && (p1.y == p_last.y) ) {
129         // same bucket as last time
130         fgPrintf( FG_TERRAIN, FG_DEBUG, "Same bucket as last time\n");
131     } else if ( p_last.lon == -1000 ) {
132         // First time through, initialize the system and load all
133         // relavant tiles
134
135         fgPrintf( FG_TERRAIN, FG_INFO, "  First time through ... ");
136         fgPrintf( FG_TERRAIN, FG_INFO, "  Updating Tile list for %d,%d %d,%d\n",
137                   p1.lon, p1.lat, p1.x, p1.y);
138         fgPrintf( FG_TERRAIN, FG_INFO, "  Loading %d tiles\n", 
139                   tile_diameter * tile_diameter);
140
141         // wipe/initialize tile cache
142         c->init();
143
144         // build the local area list and update cache
145         for ( j = 0; j < tile_diameter; j++ ) {
146             for ( i = 0; i < tile_diameter; i++ ) {
147                 fgBucketOffset(&p1, &p2, i - dw, j - dh);
148                 fgTileMgrLoadTile(&p2, &tiles[(j*tile_diameter) + i]);
149             }
150         }
151     } else {
152         // We've moved to a new bucket, we need to scroll our
153         // structures, and load in the new tiles
154
155         // CURRENTLY THIS ASSUMES WE CAN ONLY MOVE TO ADJACENT TILES.
156         // AT ULTRA HIGH SPEEDS THIS ASSUMPTION MAY NOT BE VALID IF
157         // THE AIRCRAFT CAN SKIP A TILE IN A SINGLE ITERATION.
158
159         fgPrintf( FG_TERRAIN, FG_INFO, "Updating Tile list for %d,%d %d,%d\n",
160                   p1.lon, p1.lat, p1.x, p1.y);
161
162         if ( (p1.lon > p_last.lon) ||
163              ( (p1.lon == p_last.lon) && (p1.x > p_last.x) ) ) {
164             fgPrintf( FG_TERRAIN, FG_INFO, "  Loading %d tiles\n", 
165                       tile_diameter);
166             for ( j = 0; j < tile_diameter; j++ ) {
167                 // scrolling East
168                 for ( i = 0; i < tile_diameter - 1; i++ ) {
169                     tiles[(j*tile_diameter) + i] = 
170                         tiles[(j*tile_diameter) + i + 1];
171                 }
172                 // load in new column
173                 fgBucketOffset(&p_last, &p2, dw + 1, j - dh);
174                 fgTileMgrLoadTile(&p2, &tiles[(j*tile_diameter) + 
175                                              tile_diameter - 1]);
176             }
177         } else if ( (p1.lon < p_last.lon) ||
178                     ( (p1.lon == p_last.lon) && (p1.x < p_last.x) ) ) {
179             fgPrintf( FG_TERRAIN, FG_INFO, "  Loading %d tiles\n", 
180                       tile_diameter);
181             for ( j = 0; j < tile_diameter; j++ ) {
182                 // scrolling West
183                 for ( i = tile_diameter - 1; i > 0; i-- ) {
184                     tiles[(j*tile_diameter) + i] = 
185                         tiles[(j*tile_diameter) + i - 1];
186                 }
187                 // load in new column
188                 fgBucketOffset(&p_last, &p2, -dw - 1, j - dh);
189                 fgTileMgrLoadTile(&p2, &tiles[(j*tile_diameter) + 0]);
190             }
191         }
192
193         if ( (p1.lat > p_last.lat) ||
194              ( (p1.lat == p_last.lat) && (p1.y > p_last.y) ) ) {
195             fgPrintf( FG_TERRAIN, FG_INFO, "  Loading %d tiles\n", 
196                       tile_diameter);
197             for ( i = 0; i < tile_diameter; i++ ) {
198                 // scrolling North
199                 for ( j = 0; j < tile_diameter - 1; j++ ) {
200                     tiles[(j * tile_diameter) + i] =
201                         tiles[((j+1) * tile_diameter) + i];
202                 }
203                 // load in new column
204                 fgBucketOffset(&p_last, &p2, i - dw, dh + 1);
205                 fgTileMgrLoadTile(&p2, &tiles[((tile_diameter-1) * 
206                                                tile_diameter) + i]);
207             }
208         } else if ( (p1.lat < p_last.lat) ||
209                     ( (p1.lat == p_last.lat) && (p1.y < p_last.y) ) ) {
210             fgPrintf( FG_TERRAIN, FG_INFO, "  Loading %d tiles\n", 
211                       tile_diameter);
212             for ( i = 0; i < tile_diameter; i++ ) {
213                 // scrolling South
214                 for ( j = tile_diameter - 1; j > 0; j-- ) {
215                     tiles[(j * tile_diameter) + i] = 
216                         tiles[((j-1) * tile_diameter) + i];
217                 }
218                 // load in new column
219                 fgBucketOffset(&p_last, &p2, i - dw, -dh - 1);
220                 fgTileMgrLoadTile(&p2, &tiles[0 + i]);
221             }
222         }
223     }
224     p_last.lon = p1.lon;
225     p_last.lat = p1.lat;
226     p_last.x = p1.x;
227     p_last.y = p1.y;
228     return 1;
229 }
230
231
232 // Calculate shortest distance from point to line
233 static double point_line_dist_squared( fgPoint3d *tc, fgPoint3d *vp, 
234                                        MAT3vec d )
235 {
236     MAT3vec p, p0;
237     double dist;
238
239     p[0] = tc->x; p[1] = tc->y; p[2] = tc->z;
240     p0[0] = vp->x; p0[1] = vp->y; p0[2] = vp->z;
241
242     dist = fgPointLineSquared(p, p0, d);
243
244     // printf("dist = %.2f\n", dist);
245
246     return(dist);
247 }
248
249
250 // Calculate if point/radius is inside view frustum
251 static int viewable( fgPoint3d *cp, double radius ) {
252     int viewable = 1; // start by assuming it's viewable
253     double x1, y1;
254
255 /********************************/
256 #if defined( USE_FAST_FOV_CLIP ) // views.hxx
257 /********************************/
258         
259     MAT3vec eye;        
260     double *mat;
261     double x, y, z;
262
263     x = cp->x;
264     y = cp->y;
265     z = cp->z;
266         
267     mat = (double *)(current_view.WORLD_TO_EYE);
268         
269     eye[2] =  x*mat[2] + y*mat[6] + z*mat[10] + mat[14];
270         
271     // Check near and far clip plane
272     if( ( eye[2] > radius ) ||
273         ( eye[2] + radius + current_weather.visibility < 0) )
274     {
275         return(0);
276     }
277         
278     eye[0] = (x*mat[0] + y*mat[4] + z*mat[8] + mat[12]) * current_view.slope_x;
279
280     // check right and left clip plane (from eye perspective)
281     x1 = radius * current_view.fov_x_clip;
282     if( (eye[2] > -(eye[0]+x1)) || (eye[2] > (eye[0]-x1)) )
283     {
284         return(0);
285     }
286         
287     eye[1] = (x*mat[1] + y*mat[5] + z*mat[9] + mat[13]) * current_view.slope_y;
288
289     // check bottom and top clip plane (from eye perspective)
290     y1 = radius * current_view.fov_y_clip;
291     if( (eye[2] > -(eye[1]+y1)) || (eye[2] > (eye[1]-y1)) )
292     {
293         return(0);
294     }
295
296 /********************************/      
297 #else // DO NOT USE_FAST_FOV_CLIP
298 /********************************/      
299
300     fgVIEW *v;
301     MAT3hvec world, eye;
302     double x0, slope;
303
304     v = &current_view;
305
306     MAT3_SET_HVEC(world, cp->x, cp->y, cp->z, 1.0);
307     // MAT3mult_vec(eye, world, v->WORLD_TO_EYE);
308     // printf( "\nworld -> eye = %.2f %.2f %.2f  radius = %.2f\n", 
309     //         eye[0], eye[1], eye[2], radius);
310
311     // Use lazy evaluation for calculating eye hvec.
312 #define vec world
313 #define mat v->WORLD_TO_EYE
314     eye[2] = vec[0]*mat[0][2]+vec[1]*mat[1][2]+vec[2]*mat[2][2]+mat[3][2];
315
316     // Check near clip plane
317     if ( eye[2] > radius ) {
318         return(0);
319     }
320
321     // Check far clip plane
322     if ( eye[2] + radius < -current_weather.visibility ) {
323         return(0);
324     }
325
326     // check right clip plane (from eye perspective)
327     // y = m * (x - x0) = equation of a line intercepting X axis at x0
328     x1 = v->cos_fov_x * radius;
329     y1 = v->sin_fov_x * radius;
330     slope = v->slope_x;
331     eye[0] = vec[0]*mat[0][0]+vec[1]*mat[1][0]+vec[2]*mat[2][0]+mat[3][0];
332
333     if ( eye[2] > ((slope * (eye[0] - x1)) + y1) ) {
334         return( false );
335     }
336
337     // check left clip plane (from eye perspective)
338     if ( eye[2] > -((slope * (eye[0] + x1)) - y1) ) {
339         return( false );
340     }
341
342     // check bottom clip plane (from eye perspective)
343     x1 = -(v->cos_fov_y) * radius;
344     y1 = v->sin_fov_y * radius;
345     slope = v->slope_y;
346     eye[1] = vec[0]*mat[0][1]+vec[1]*mat[1][1]+vec[2]*mat[2][1]+mat[3][1];
347 #undef vec
348 #undef mat
349
350     if ( eye[2] > ((slope * (eye[1] - x1)) + y1) ) {
351         return( false );
352     }
353
354     // check top clip plane (from eye perspective)
355     if ( eye[2] > -((slope * (eye[1] + x1)) - y1) ) {
356         return( false );
357     }
358
359 #endif // defined( USE_FAST_FOV_CLIP )
360         
361     return(viewable);
362 }
363
364
365 // NEW 
366
367 // inrange() IS THIS POINT WITHIN POSSIBLE VIEWING RANGE ?
368 //      calculate distance from vertical tangent line at
369 //      current position to center of object.
370 //      this is equivalent to
371 //      dist = point_line_dist_squared( &(t->center), &(v->abs_view_pos), 
372 //                                      v->local_up );
373 //      if ( dist < FG_SQUARE(t->bounding_radius) ) {
374 //
375 // the compiler should inline this for us
376
377 static int
378 inrange( const double radius, const fgPoint3d *center, const fgPoint3d *vp,
379          const MAT3vec up)
380 {
381     MAT3vec u, u1, v;
382     //  double tmp;
383         
384     // u = p - p0
385     u[0] = center->x - vp->x;
386     u[1] = center->y - vp->y;
387     u[2] = center->z - vp->z;
388         
389     // calculate the projection, u1, of u along d.
390     // u1 = ( dot_prod(u, d) / dot_prod(d, d) ) * d;
391         
392     MAT3_SCALE_VEC(u1, up,
393                    (MAT3_DOT_PRODUCT(u, up) / MAT3_DOT_PRODUCT(up, up)) );
394     
395     // v = u - u1 = vector from closest point on line, p1, to the
396     // original point, p.
397     MAT3_SUB_VEC(v, u, u1);
398         
399     return( FG_SQUARE(radius) >= MAT3_DOT_PRODUCT(v, v));
400 }
401
402
403 // Determine scenery altitude.  Normally this just happens when we
404 // render the scene, but we'd also like to be able to do this
405 // explicitely.  lat & lon are in radians.  abs_view_pos in meters.
406 // Returns result in meters.
407 double fgTileMgrCurElev( double lon, double lat, fgPoint3d *abs_view_pos ) {
408     fgTILECACHE *c;
409     fgTILE *t;
410     // fgVIEW *v;
411     fgFRAGMENT *frag_ptr;
412     fgBUCKET p;
413     fgPoint3d earth_center, result;
414     fgPoint3d pp;
415     MAT3vec local_up;
416     list < fgFRAGMENT > :: iterator current;
417     list < fgFRAGMENT > :: iterator last;
418     double dist, min_dist, lat_geod, alt, sea_level_r;
419     // double x, y, z;
420     int index, tile_diameter, i;
421
422     c = &global_tile_cache;
423     // v = &current_view;
424
425     local_up[0] = abs_view_pos->x;
426     local_up[1] = abs_view_pos->y;
427     local_up[2] = abs_view_pos->z;
428
429     // Find current translation offset
430     fgBucketFind(lon * RAD_TO_DEG, lat * RAD_TO_DEG, &p);
431     index = c->exists(&p);
432     t = c->get_tile(index);
433
434     // scenery.next_center.x = t->center.x;
435     // scenery.next_center.y = t->center.y;
436     // scenery.next_center.z = t->center.z;
437     
438     earth_center.x = 0.0;
439     earth_center.y = 0.0;
440     earth_center.z = 0.0;
441
442     fgPrintf( FG_TERRAIN, FG_DEBUG, 
443               "Pos = (%.2f, %.2f) Current bucket = %d %d %d %d  Index = %ld\n", 
444               lon * RAD_TO_DEG, lat * RAD_TO_DEG,
445               p.lon, p.lat, p.x, p.y, fgBucketGenIndex(&p) );
446
447     // calculate tile offset
448     // x = (t->offset.x = t->center.x - scenery.center.x);
449     // y = (t->offset.y = t->center.y - scenery.center.y);
450     // z = (t->offset.z = t->center.z - scenery.center.z);
451     
452     // calc current terrain elevation calculate distance from
453     // vertical tangent line at current position to center of
454     // tile.
455         
456     /* printf("distance squared = %.2f, bounding radius = %.2f\n", 
457        point_line_dist_squared(&(t->offset), &(v->view_pos), 
458        v->local_up), t->bounding_radius); */
459
460     dist = point_line_dist_squared( &(t->center), abs_view_pos, 
461                                         local_up );
462     if ( dist < FG_SQUARE(t->bounding_radius) ) {
463
464         // traverse fragment list for tile
465         current = t->fragment_list.begin();
466         last = t->fragment_list.end();
467
468         for ( ; current != last; ++current ) {
469             frag_ptr = &(*current);
470             /* printf("distance squared = %.2f, bounding radius = %.2f\n", 
471                point_line_dist_squared( &(frag_ptr->center), 
472                &abs_view_pos), local_up),
473                frag_ptr->bounding_radius); */
474
475             dist = point_line_dist_squared( &(frag_ptr->center), 
476                                             abs_view_pos, local_up);
477             if ( dist <= FG_SQUARE(frag_ptr->bounding_radius) ) {
478                 if ( frag_ptr->intersect( abs_view_pos, 
479                                           &earth_center, 0, &result ) ) {
480                     // compute geocentric coordinates of tile center
481                     pp = fgCartToPolar3d(result);
482                     // convert to geodetic coordinates
483                     fgGeocToGeod(pp.lat, pp.radius, &lat_geod, 
484                                  &alt, &sea_level_r);
485                     // printf("alt = %.2f\n", alt);
486                     // exit since we found an intersection
487                     return(alt);
488                 }
489             }
490         }
491     }
492
493     printf("no terrain intersection found\n");
494     return(0);
495 }
496
497
498 // NEW for legibility
499
500 // update this tile's geometry for current view
501 // The Compiler should inline this
502 static void
503 update_tile_geometry( fgTILE *t, GLdouble *MODEL_VIEW)
504 {
505     GLdouble *m;
506     double x, y, z;
507         
508     // calculate tile offset
509     x = (t->offset.x = t->center.x - scenery.center.x);
510     y = (t->offset.y = t->center.y - scenery.center.y);
511     z = (t->offset.z = t->center.z - scenery.center.z);
512         
513     m = t->model_view;
514         
515     // Calculate the model_view transformation matrix for this tile
516     FG_MEM_COPY( m, MODEL_VIEW, 16*sizeof(GLdouble) );
517     
518     // This is equivalent to doing a glTranslatef(x, y, z);
519     m[12] += (m[0]*x + m[4]*y + m[8] *z);
520     m[13] += (m[1]*x + m[5]*y + m[9] *z);
521     m[14] += (m[2]*x + m[6]*y + m[10]*z);
522     // m[15] += (m[3]*x + m[7]*y + m[11]*z);
523     // m[3] m7[] m[11] are 0.0 see LookAt() in views.cxx
524     // so m[15] is unchanged
525 }
526
527
528 // Render the local tiles
529 void fgTileMgrRender( void ) {
530     fgTILECACHE *c;
531     fgFLIGHT *f;
532     fgTILE *t, *last_tile_ptr;
533     fgVIEW *v;
534     fgBUCKET p;
535     fgPoint3d frag_offset, pp;
536     fgPoint3d earth_center, result;
537     fgFRAGMENT *frag_ptr;
538     fgMATERIAL *mtl_ptr;
539     GLdouble *m;
540     double dist, min_dist, lat_geod, alt, sea_level_r;
541     double x, y, z;
542     list < fgFRAGMENT > :: iterator current;
543     list < fgFRAGMENT > :: iterator last;
544     int i, j, size;
545     int tile_diameter, textures;
546     int index;
547     int culled = 0;
548     int drawn = 0;
549     int total_faces = 0;
550
551     c = &global_tile_cache;
552     f = current_aircraft.flight;
553     v = &current_view;
554
555     tile_diameter = current_options.get_tile_diameter();
556     textures = current_options.get_textures();
557
558     // Find current translation offset
559     fgBucketFind(FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, &p);
560     index = c->exists(&p);
561     t = c->get_tile(index);
562
563     scenery.next_center.x = t->center.x;
564     scenery.next_center.y = t->center.y;
565     scenery.next_center.z = t->center.z;
566
567     earth_center.x = 0.0;
568     earth_center.y = 0.0;
569     earth_center.z = 0.0;
570
571     fgPrintf( FG_TERRAIN, FG_DEBUG, 
572               "Pos = (%.2f, %.2f) Current bucket = %d %d %d %d  Index = %ld\n", 
573               FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG,
574               p.lon, p.lat, p.x, p.y, fgBucketGenIndex(&p) );
575
576     // initialize the transient per-material fragment lists
577     material_mgr.init_transient_material_lists();
578     min_dist = 100000.0;
579
580     scenery.cur_elev = fgTileMgrCurElev( FG_Longitude, FG_Latitude, 
581                                          &(v->abs_view_pos) );
582     
583     // Pass 1
584     // traverse the potentially viewable tile list
585     for ( i = 0; i < (tile_diameter * tile_diameter); i++ ) {
586         index = tiles[i];
587         // fgPrintf( FG_TERRAIN, FG_DEBUG, "Index = %d\n", index);
588         t = c->get_tile(index);
589
590         // calculate tile offset
591         x = (t->offset.x = t->center.x - scenery.center.x);
592         y = (t->offset.y = t->center.y - scenery.center.y);
593         z = (t->offset.z = t->center.z - scenery.center.z);
594
595 #if defined( TEST_FOV_CLIP )
596         if( viewable(&(t->offset), t->bounding_radius) !=
597             viewable2(&(t->offset), t->bounding_radius) )
598         {
599             printf("FOV PROBLEM\n");
600             exit(10);
601         }
602 #endif // defined( TEST_FOV_CLIP )
603
604         // Course (tile based) culling
605         if ( viewable(&(t->offset), t->bounding_radius) ) {
606             // at least a portion of this tile could be viewable
607             
608             m = t->model_view;
609             for ( j = 0; j < 16; j++ ) {
610                 m[j] = v->MODEL_VIEW[j];
611             }
612             
613             // Calculate the model_view transformation matrix for this tile
614             // This is equivalent to doing a glTranslatef(x, y, z);
615             m[12] = m[0] * x + m[4] * y + m[8]  * z + m[12];
616             m[13] = m[1] * x + m[5] * y + m[9]  * z + m[13];
617             m[14] = m[2] * x + m[6] * y + m[10] * z + m[14];
618             m[15] = m[3] * x + m[7] * y + m[11] * z + m[15];
619
620             // xglPushMatrix();
621             // xglTranslatef(t->offset.x, t->offset.y, t->offset.z);
622
623             // traverse fragment list for tile
624             current = t->fragment_list.begin();
625             last = t->fragment_list.end();
626
627             for ( ; current != last; ++current ) {
628                 frag_ptr = &(*current);
629                 
630                 if ( frag_ptr->display_list >= 0 ) {
631                     // Fine (fragment based) culling
632                     frag_offset.x = frag_ptr->center.x - scenery.center.x;
633                     frag_offset.y = frag_ptr->center.y - scenery.center.y;
634                     frag_offset.z = frag_ptr->center.z - scenery.center.z;
635
636 #if defined( TEST_FOV_CLIP )
637                     radius = frag_ptr->bounding_radius*2;
638                     if ( viewable(&frag_offset, radius) !=
639                          viewable2(&frag_offset, radius) ) {
640                         printf("FOV PROBLEM\n");
641                         exit(10);
642                     }
643 #endif // defined( TEST_FOV_CLIP )
644
645                     if ( viewable(&frag_offset, frag_ptr->bounding_radius*2) ) {
646                         // add to transient per-material property fragment list
647                         // frag_ptr->tile_offset.x = t->offset.x;
648                         // frag_ptr->tile_offset.y = t->offset.y;
649                         // frag_ptr->tile_offset.z = t->offset.z;
650
651                         mtl_ptr = frag_ptr->material_ptr;
652                         // printf(" lookup = %s\n", mtl_ptr->texture_name);
653                         if ( ! mtl_ptr->append_sort_list( frag_ptr ) ) {
654                             fgPrintf( FG_TERRAIN, FG_ALERT,
655                                       "Overran material sorting array\n" );
656                         }
657
658                         // xglCallList(frag_ptr->display_list);
659                         drawn++;
660                     } else {
661                         // printf("Culled a fragment %.2f %.2f %.2f %.2f\n",
662                         //        frag_ptr->center.x, frag_ptr->center.y,
663                         //        frag_ptr->center.z, frag_ptr->bounding_radius);
664                         culled++;
665                     }
666                 }
667             }
668
669             // xglPopMatrix();
670         } else {
671             culled += t->fragment_list.size();
672         }
673     }
674
675     if ( (drawn + culled) > 0 ) {
676         v->vfc_ratio = (double)culled / (double)(drawn + culled);
677     } else {
678         v->vfc_ratio = 0.0;
679     }
680     // printf("drawn = %d  culled = %d  saved = %.2f\n", drawn, culled, 
681     //        v->vfc_ratio);
682
683     // Pass 2
684     // traverse the transient per-material fragment lists and render
685     // out all fragments for each material property.
686     xglPushMatrix();
687     material_mgr.render_fragments();
688     xglPopMatrix();
689 }
690
691
692 // $Log$
693 // Revision 1.37  1998/09/15 01:36:45  curt
694 // cleaned up my fragment.num_faces hack :-) to use the STL (no need in
695 // duplicating work.)
696 // Tweaked fgTileMgrRender() do not calc tile matrix unless necessary.
697 // removed some unneeded stuff from fgTileMgrCurElev()
698 //
699 // Revision 1.36  1998/09/14 12:45:26  curt
700 // minor tweaks.
701 //
702 // Revision 1.35  1998/09/10 19:07:16  curt
703 // /Simulator/Objects/fragment.hxx
704 //   Nested fgFACE inside fgFRAGMENT since its not used anywhere else.
705 //
706 // ./Simulator/Objects/material.cxx
707 // ./Simulator/Objects/material.hxx
708 //   Made fgMATERIAL and fgMATERIAL_MGR bona fide classes with private
709 //   data members - that should keep the rabble happy :)
710 //
711 // ./Simulator/Scenery/tilemgr.cxx
712 //   In viewable() delay evaluation of eye[0] and eye[1] in until they're
713 //   actually needed.
714 //   Change to fgTileMgrRender() to call fgMATERIAL_MGR::render_fragments()
715 //   method.
716 //
717 // ./Include/fg_stl_config.h
718 // ./Include/auto_ptr.hxx
719 //   Added support for g++ 2.7.
720 //   Further changes to other files are forthcoming.
721 //
722 // Brief summary of changes required for g++ 2.7.
723 //   operator->() not supported by iterators: use (*i).x instead of i->x
724 //   default template arguments not supported,
725 //   <functional> doesn't have mem_fun_ref() needed by callbacks.
726 //   some std include files have different names.
727 //   template member functions not supported.
728 //
729 // Revision 1.34  1998/09/09 20:58:09  curt
730 // Tweaks to loop constructs with STL usage.
731 //
732 // Revision 1.33  1998/09/08 15:05:10  curt
733 // Optimization by Norman Vine.
734 //
735 // Revision 1.32  1998/08/25 16:52:44  curt
736 // material.cxx material.hxx obj.cxx obj.hxx texload.c texload.h moved to
737 //   ../Objects
738 //
739 // Revision 1.31  1998/08/24 20:11:40  curt
740 // Tweaks ...
741 //
742 // Revision 1.30  1998/08/22  14:49:59  curt
743 // Attempting to iron out seg faults and crashes.
744 // Did some shuffling to fix a initialization order problem between view
745 // position, scenery elevation.
746 //
747 // Revision 1.29  1998/08/20 15:12:06  curt
748 // Used a forward declaration of classes fgTILE and fgMATERIAL to eliminate
749 // the need for "void" pointers and casts.
750 // Quick hack to count the number of scenery polygons that are being drawn.
751 //
752 // Revision 1.28  1998/08/12 21:13:06  curt
753 // material.cxx: don't load textures if they are disabled
754 // obj.cxx: optimizations from Norman Vine
755 // tile.cxx: minor tweaks
756 // tile.hxx: addition of num_faces
757 // tilemgr.cxx: minor tweaks
758 //
759 // Revision 1.27  1998/07/24 21:42:09  curt
760 // material.cxx: whups, double method declaration with no definition.
761 // obj.cxx: tweaks to avoid errors in SGI's CC.
762 // tile.cxx: optimizations by Norman Vine.
763 // tilemgr.cxx: optimizations by Norman Vine.
764 //
765 // Revision 1.26  1998/07/20 12:51:26  curt
766 // Added far clip plane to fragment clipping calculations and tie this to
767 // weather->visibility.  This way you can increase framerates by increasing
768 // for and lowering visibility.
769 //
770 // Revision 1.25  1998/07/13 21:02:01  curt
771 // Wrote access functions for current fgOPTIONS.
772 //
773 // Revision 1.24  1998/07/12 03:18:29  curt
774 // Added ground collision detection.  This involved:
775 // - saving the entire vertex list for each tile with the tile records.
776 // - saving the face list for each fragment with the fragment records.
777 // - code to intersect the current vertical line with the proper face in
778 //   an efficient manner as possible.
779 // Fixed a bug where the tiles weren't being shifted to "near" (0,0,0)
780 //
781 // Revision 1.23  1998/07/08 14:47:23  curt
782 // Fix GL_MODULATE vs. GL_DECAL problem introduced by splash screen.
783 // polare3d.h renamed to polar3d.hxx
784 // fg{Cartesian,Polar}Point3d consolodated.
785 // Added some initial support for calculating local current ground elevation.
786 //
787 // Revision 1.22  1998/07/04 00:54:31  curt
788 // Added automatic mipmap generation.
789 //
790 // When rendering fragments, use saved model view matrix from associated tile
791 // rather than recalculating it with push() translate() pop().
792 //
793 // Revision 1.21  1998/06/27 16:54:59  curt
794 // Check for GL_VERSION_1_1 or GL_EXT_texture_object to decide whether to use
795 //   "EXT" versions of texture management routines.
796 //
797 // Revision 1.20  1998/06/17 21:36:42  curt
798 // Load and manage multiple textures defined in the Materials library.
799 // Boost max material fagments for each material property to 800.
800 // Multiple texture support when rendering.
801 //
802 // Revision 1.19  1998/06/08 17:57:54  curt
803 // Working first pass at material proporty sorting.
804 //
805 // Revision 1.18  1998/06/06 01:09:32  curt
806 // I goofed on the log message in the last commit ... now fixed.
807 //
808 // Revision 1.17  1998/06/06 01:07:18  curt
809 // Increased per material fragment list size from 100 to 400.
810 // Now correctly draw viewable fragments in per material order.
811 //
812 // Revision 1.16  1998/06/05 22:39:55  curt
813 // Working on sorting by, and rendering by material properties.
814 //
815 // Revision 1.15  1998/06/03 00:47:51  curt
816 // No .h for STL includes.
817 // Minor view culling optimizations.
818 //
819 // Revision 1.14  1998/06/01 17:56:20  curt
820 // Incremental additions to material.cxx (not fully functional)
821 // Tweaked vfc_ratio math to avoid divide by zero.
822 //
823 // Revision 1.13  1998/05/24 02:49:10  curt
824 // Implimented fragment level view frustum culling.
825 //
826 // Revision 1.12  1998/05/23 14:09:23  curt
827 // Added tile.cxx and tile.hxx.
828 // Working on rewriting the tile management system so a tile is just a list
829 // fragments, and the fragment record contains the display list for that fragment.
830 //
831 // Revision 1.11  1998/05/20 20:53:55  curt
832 // Moved global ref point and radius (bounding sphere info, and offset) to
833 // data file rather than calculating it on the fly.
834 // Fixed polygon winding problem in scenery generation stage rather than
835 // compensating for it on the fly.
836 // Made a fgTILECACHE class.
837 //
838 // Revision 1.10  1998/05/17 16:59:34  curt
839 // Frist pass at view frustum culling now operational.
840 //
841 // Revision 1.9  1998/05/16 13:09:58  curt
842 // Beginning to add support for view frustum culling.
843 // Added some temporary code to calculate bouding radius, until the
844 //   scenery generation tools and scenery can be updated.
845 //
846 // Revision 1.8  1998/05/07 23:15:21  curt
847 // Fixed a glTexImage2D() usage bug where width and height were mis-swapped.
848 // Added support for --tile-radius=n option.
849 //
850 // Revision 1.7  1998/05/06 03:16:42  curt
851 // Added an option to control square tile radius.
852 //
853 // Revision 1.6  1998/05/02 01:52:18  curt
854 // Playing around with texture coordinates.
855 //
856 // Revision 1.5  1998/04/30 12:35:32  curt
857 // Added a command line rendering option specify smooth/flat shading.
858 //
859 // Revision 1.4  1998/04/27 03:30:14  curt
860 // Minor transformation adjustments to try to keep scenery tiles closer to
861 // (0, 0, 0)  GLfloats run out of precision at the distances we need to model
862 // the earth, but we can do a bunch of pre-transformations using double math
863 // and then cast to GLfloat once everything is close in where we have less
864 // precision problems.
865 //
866 // Revision 1.3  1998/04/25 22:06:32  curt
867 // Edited cvs log messages in source files ... bad bad bad!
868 //
869 // Revision 1.2  1998/04/24 00:51:09  curt
870 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
871 // Tweaked the scenery file extentions to be "file.obj" (uncompressed)
872 // or "file.obz" (compressed.)
873 //
874 // Revision 1.1  1998/04/22 13:22:48  curt
875 // C++ - ifing the code a bit.
876 //
877 // Revision 1.25  1998/04/18 04:14:07  curt
878 // Moved fg_debug.c to it's own library.
879 //
880 // Revision 1.24  1998/04/14 02:23:18  curt
881 // Code reorganizations.  Added a Lib/ directory for more general libraries.
882 //
883 // Revision 1.23  1998/04/08 23:30:08  curt
884 // Adopted Gnu automake/autoconf system.
885 //
886 // Revision 1.22  1998/04/03 22:11:38  curt
887 // Converting to Gnu autoconf system.
888 //
889 // Revision 1.21  1998/03/23 21:23:05  curt
890 // Debugging output tweaks.
891 //
892 // Revision 1.20  1998/03/14 00:30:51  curt
893 // Beginning initial terrain texturing experiments.
894 //
895 // Revision 1.19  1998/02/20 00:16:25  curt
896 // Thursday's tweaks.
897 //
898 // Revision 1.18  1998/02/19 13:05:54  curt
899 // Incorporated some HUD tweaks from Michelle America.
900 // Tweaked the sky's sunset/rise colors.
901 // Other misc. tweaks.
902 //
903 // Revision 1.17  1998/02/16 13:39:46  curt
904 // Miscellaneous weekend tweaks.  Fixed? a cache problem that caused whole
905 // tiles to occasionally be missing.
906 //
907 // Revision 1.16  1998/02/12 21:59:53  curt
908 // Incorporated code changes contributed by Charlie Hotchkiss
909 // <chotchkiss@namg.us.anritsu.com>
910 //
911 // Revision 1.14  1998/02/09 21:30:19  curt
912 // Fixed a nagging problem with terrain tiles not "quite" matching up perfectly.
913 //
914 // Revision 1.13  1998/02/07 15:29:46  curt
915 // Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
916 // <chotchkiss@namg.us.anritsu.com>
917 //
918 // Revision 1.12  1998/02/01 03:39:55  curt
919 // Minor tweaks.
920 //
921 // Revision 1.11  1998/01/31 00:43:27  curt
922 // Added MetroWorks patches from Carmen Volpe.
923 //
924 // Revision 1.10  1998/01/29 00:51:40  curt
925 // First pass at tile cache, dynamic tile loading and tile unloading now works.
926 //
927 // Revision 1.9  1998/01/27 03:26:44  curt
928 // Playing with new fgPrintf command.
929 //
930 // Revision 1.8  1998/01/27 00:48:04  curt
931 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
932 // system and commandline/config file processing code.
933 //
934 // Revision 1.7  1998/01/26 15:55:25  curt
935 // Progressing on building dynamic scenery system.
936 //
937 // Revision 1.6  1998/01/24 00:03:30  curt
938 // Initial revision.
939 //
940 // Revision 1.5  1998/01/19 19:27:18  curt
941 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
942 // This should simplify things tremendously.
943 //
944 // Revision 1.4  1998/01/19 18:40:38  curt
945 // Tons of little changes to clean up the code and to remove fatal errors
946 // when building with the c++ compiler.
947 //
948 // Revision 1.3  1998/01/13 00:23:11  curt
949 // Initial changes to support loading and management of scenery tiles.  Note,
950 // there's still a fair amount of work left to be done.
951 //
952 // Revision 1.2  1998/01/08 02:22:27  curt
953 // Continue working on basic features.
954 //
955 // Revision 1.1  1998/01/07 23:50:51  curt
956 // "area" renamed to "tile"
957 //
958 // Revision 1.2  1998/01/07 03:29:29  curt
959 // Given an arbitrary lat/lon, we can now:
960 //   generate a unique index for the chunk containing the lat/lon
961 //   generate a path name to the chunk file
962 //   build a list of the indexes of all the nearby areas.
963 //
964 // Revision 1.1  1998/01/07 02:05:48  curt
965 // Initial revision.