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