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