]> git.mxchange.org Git - flightgear.git/blobdiff - Scenery/tilemgr.cxx
Modifications to incorporate Jon S. Berndts flight model code.
[flightgear.git] / Scenery / tilemgr.cxx
index 92bc3d91ef571ff32d36855792fae73c8087c91a..858966c04fa5d9219457a59b8e5726a9c7613f57 100644 (file)
 #include <GL/glut.h>
 #include <XGL/xgl.h>
 
-#include <Aircraft/aircraft.h>
+#include <Aircraft/aircraft.hxx>
 
-#include <Bucket/bucketutils.h>
-#include <Debug/fg_debug.h>
+#include <Bucket/bucketutils.hxx>
+#include <Debug/logstream.hxx>
 #include <Include/fg_constants.h>
-#include <Include/fg_types.h>
 #include <Main/options.hxx>
 #include <Main/views.hxx>
+#include <Math/fg_geodesy.hxx>
 #include <Math/mat3.h>
+#include <Math/point3d.hxx>
+#include <Math/polar3d.hxx>
+#include <Math/vector.hxx>
+#include <Objects/material.hxx>
+#include <Objects/obj.hxx>
+#include <Weather/weather.hxx>
 
-#include "material.hxx"
-#include "obj.hxx"
 #include "scenery.hxx"
+#include "tile.hxx"
 #include "tilecache.hxx"
+#include "tilemgr.hxx"
+
+
+// to test clipping speedup in fgTileMgrRender()
+#if defined ( USE_FAST_FOV_CLIP )
+  // #define TEST_FOV_CLIP
+  // #define TEST_ELEV
+#endif
 
 
 #define FG_LOCAL_X_Y         81  // max(o->tile_diameter) ** 2
 
+#define FG_SQUARE( X ) ( (X) * (X) )
+
+#if defined(USE_MEM) || defined(WIN32)
+#  define FG_MEM_COPY(to,from,n)        memcpy(to, from, n)
+#else
+#  define FG_MEM_COPY(to,from,n)        bcopy(from, to, n)
+#endif
 
 // closest (potentially viewable) tiles, centered on current tile.
 // This is an array of pointers to cache indexes.
@@ -59,7 +79,7 @@ int tiles[FG_LOCAL_X_Y];
 
 // Initialize the Tile Manager subsystem
 int fgTileMgrInit( void ) {
-    fgPrintf( FG_TERRAIN, FG_INFO, "Initializing Tile Manager subsystem.\n");
+    FG_LOG( FG_TERRAIN, FG_INFO, "Initializing Tile Manager subsystem." );
 
     // load default material library
     material_mgr.load_lib();
@@ -69,21 +89,243 @@ int fgTileMgrInit( void ) {
 
 
 // load a tile
-void fgTileMgrLoadTile( struct fgBUCKET *p, int *index) {
+void fgTileMgrLoadTile( const fgBUCKET& p, int *index) {
     fgTILECACHE *c;
 
     c = &global_tile_cache;
 
-    fgPrintf( FG_TERRAIN, FG_DEBUG, "Updating for bucket %d %d %d %d\n", 
-          p->lon, p->lat, p->x, p->y);
+    FG_LOG( FG_TERRAIN, FG_DEBUG, "Updating for bucket " << p );
     
     // if not in cache, load tile into the next available slot
-    if ( (*index = c->Exists(p)) < 0 ) {
-       *index = c->NextAvail();
-       c->EntryFillIn(*index, p);
+    *index = c->exists(p);
+    if ( *index < 0 ) {
+       *index = c->next_avail();
+       c->fill_in(*index, p);
+    }
+
+    FG_LOG( FG_TERRAIN, FG_DEBUG, "Selected cache index: " << *index );
+}
+
+
+// Calculate shortest distance from point to line
+static double point_line_dist_squared( const Point3D& tc, const Point3D& vp, 
+                                      MAT3vec d )
+{
+    MAT3vec p, p0;
+    double dist;
+
+    p[0] = tc.x(); p[1] = tc.y(); p[2] = tc.z();
+    p0[0] = vp.x(); p0[1] = vp.y(); p0[2] = vp.z();
+
+    dist = fgPointLineSquared(p, p0, d);
+
+    // cout << "dist = " << dist << endl;
+
+    return(dist);
+}
+
+
+// Determine scenery altitude.  Normally this just happens when we
+// render the scene, but we'd also like to be able to do this
+// explicitely.  lat & lon are in radians.  abs_view_pos in meters.
+// Returns result in meters.
+double
+fgTileMgrCurElevNEW( const fgBUCKET& p ) {
+    fgTILE *t;
+    fgFRAGMENT *frag_ptr;
+    Point3D abs_view_pos = current_view.get_abs_view_pos();
+    Point3D earth_center(0.0);
+    Point3D result;
+    MAT3vec local_up;
+    double dist, lat_geod, alt, sea_level_r;
+    int index;
+
+    local_up[0] = abs_view_pos.x();
+    local_up[1] = abs_view_pos.y();
+    local_up[2] = abs_view_pos.z();
+
+    // Find current translation offset
+    // fgBucketFind(lon * RAD_TO_DEG, lat * RAD_TO_DEG, &p);
+    index = global_tile_cache.exists(p);
+    if ( index < 0 ) {
+       FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found" );
+       return 0.0;
+    }
+
+    t = global_tile_cache.get_tile(index);
+
+    scenery.next_center = t->center;
+    
+    FG_LOG( FG_TERRAIN, FG_DEBUG, 
+           "Current bucket = " << p << "  Index = " << fgBucketGenIndex(&p) );
+    FG_LOG( FG_TERRAIN, FG_DEBUG,
+           "abs_view_pos = " << abs_view_pos );
+
+    // calculate tile offset
+    // x = (t->offset.x = t->center.x - scenery.center.x);
+    // y = (t->offset.y = t->center.y - scenery.center.y);
+    // z = (t->offset.z = t->center.z - scenery.center.z);
+    
+    // calc current terrain elevation calculate distance from
+    // vertical tangent line at current position to center of
+    // tile.
+       
+    /* printf("distance squared = %.2f, bounding radius = %.2f\n", 
+       point_line_dist_squared(&(t->offset), &(v->view_pos), 
+       v->local_up), t->bounding_radius); */
+
+    dist = point_line_dist_squared( t->center, abs_view_pos, local_up );
+    if ( dist < FG_SQUARE(t->bounding_radius) ) {
+
+       // traverse fragment list for tile
+        fgTILE::FragmentIterator current = t->begin();
+        fgTILE::FragmentIterator last = t->end();
+
+       for ( ; current != last; ++current ) {
+           frag_ptr = &(*current);
+           /* printf("distance squared = %.2f, bounding radius = %.2f\n", 
+              point_line_dist_squared( &(frag_ptr->center), 
+              &abs_view_pos), local_up),
+              frag_ptr->bounding_radius); */
+
+           dist = point_line_dist_squared( frag_ptr->center,
+                                           abs_view_pos,
+                                           local_up);
+           if ( dist <= FG_SQUARE(frag_ptr->bounding_radius) ) {
+               if ( frag_ptr->intersect( abs_view_pos, 
+                                         earth_center, 0, result ) ) {
+                   FG_LOG( FG_TERRAIN, FG_DEBUG, "intersection point " <<
+                           result );
+                   // compute geocentric coordinates of tile center
+                   Point3D pp = fgCartToPolar3d(result);
+                   FG_LOG( FG_TERRAIN, FG_DEBUG, "  polar form = " << pp );
+                   // convert to geodetic coordinates
+                   fgGeocToGeod(pp.lat(), pp.radius(), &lat_geod, 
+                                &alt, &sea_level_r);
+
+                   // printf("alt = %.2f\n", alt);
+                   // exit since we found an intersection
+                   if ( alt > -9999.0 ) {
+                       // printf("returning alt\n");
+                       return alt;
+                   } else {
+                       // printf("returning 0\n");
+                       return 0.0;
+                   }
+               }
+           }
+       }
+    }
+
+    FG_LOG( FG_TERRAIN, FG_INFO, "(new) no terrain intersection found" );
+
+    return 0.0;
+}
+
+
+// Determine scenery altitude.  Normally this just happens when we
+// render the scene, but we'd also like to be able to do this
+// explicitely.  lat & lon are in radians.  abs_view_pos in meters.
+// Returns result in meters.
+double
+fgTileMgrCurElev( double lon, double lat, const Point3D& abs_view_pos ) {
+    fgTILECACHE *c;
+    fgTILE *t;
+    // fgVIEW *v;
+    fgFRAGMENT *frag_ptr;
+    fgBUCKET p;
+    Point3D earth_center(0.0);
+    Point3D result;
+    MAT3vec local_up;
+    double dist, lat_geod, alt, sea_level_r;
+    // double x, y, z;
+    int index;
+
+    c = &global_tile_cache;
+    // v = &current_view;
+
+    local_up[0] = abs_view_pos.x();
+    local_up[1] = abs_view_pos.y();
+    local_up[2] = abs_view_pos.z();
+
+    FG_LOG( FG_TERRAIN, FG_DEBUG, "Absolute view pos = " << abs_view_pos );
+
+    // Find current translation offset
+    fgBucketFind(lon * RAD_TO_DEG, lat * RAD_TO_DEG, &p);
+    index = c->exists(p);
+    if ( index < 0 ) {
+       FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found" );
+       return 0.0;
     }
 
-    fgPrintf( FG_TERRAIN, FG_DEBUG, "Selected cache index of %d\n", *index);
+    t = c->get_tile(index);
+
+    scenery.next_center = t->center;
+    
+    FG_LOG( FG_TERRAIN, FG_DEBUG, 
+           "Pos = (" << lon * RAD_TO_DEG << ", " << lat * RAD_TO_DEG
+           << ")  Current bucket = " << p 
+           << "  Index = " << fgBucketGenIndex(&p) );
+
+    // calculate tile offset
+    // x = (t->offset.x = t->center.x - scenery.center.x);
+    // y = (t->offset.y = t->center.y - scenery.center.y);
+    // z = (t->offset.z = t->center.z - scenery.center.z);
+    
+    // calc current terrain elevation calculate distance from
+    // vertical tangent line at current position to center of
+    // tile.
+       
+    /* printf("distance squared = %.2f, bounding radius = %.2f\n", 
+       point_line_dist_squared(&(t->offset), &(v->view_pos), 
+       v->local_up), t->bounding_radius); */
+
+    dist = point_line_dist_squared( t->center, abs_view_pos, local_up );
+    if ( dist < FG_SQUARE(t->bounding_radius) ) {
+
+       // traverse fragment list for tile
+        fgTILE::FragmentIterator current = t->begin();
+        fgTILE::FragmentIterator last = t->end();
+
+       for ( ; current != last; ++current ) {
+           frag_ptr = &(*current);
+           /* printf("distance squared = %.2f, bounding radius = %.2f\n", 
+              point_line_dist_squared( &(frag_ptr->center), 
+              &abs_view_pos), local_up),
+              frag_ptr->bounding_radius); */
+
+           dist = point_line_dist_squared( frag_ptr->center,
+                                           abs_view_pos,
+                                           local_up);
+           if ( dist <= FG_SQUARE(frag_ptr->bounding_radius) ) {
+               if ( frag_ptr->intersect( abs_view_pos, 
+                                         earth_center, 0, result ) ) {
+                   FG_LOG( FG_TERRAIN, FG_DEBUG, "intersection point " <<
+                           result );
+                   // compute geocentric coordinates of tile center
+                   Point3D pp = fgCartToPolar3d(result);
+                   FG_LOG( FG_TERRAIN, FG_DEBUG, "  polar form = " << pp );
+                   // convert to geodetic coordinates
+                   fgGeocToGeod(pp.lat(), pp.radius(), &lat_geod, 
+                                &alt, &sea_level_r);
+
+                   // printf("alt = %.2f\n", alt);
+                   // exit since we found an intersection
+                   if ( alt > -9999.0 ) {
+                       // printf("returning alt\n");
+                       return alt;
+                   } else {
+                       // printf("returning 0\n");
+                       return 0.0;
+                   }
+               }
+           }
+       }
+    }
+
+    FG_LOG( FG_TERRAIN, FG_INFO, "(old) no terrain intersection found" );
+
+    return 0.0;
 }
 
 
@@ -91,42 +333,43 @@ void fgTileMgrLoadTile( struct fgBUCKET *p, int *index) {
 // the chunk isn't already in the cache, then read it from disk.
 int fgTileMgrUpdate( void ) {
     fgTILECACHE *c;
-    fgFLIGHT *f;
-    fgOPTIONS *o;
-    struct fgBUCKET p1, p2;
-    static struct fgBUCKET p_last = {-1000, 0, 0, 0};
+    FGInterface *f;
+    fgBUCKET p1, p2;
+    static fgBUCKET p_last = {-1000, 0, 0, 0};
+    int tile_diameter;
     int i, j, dw, dh;
 
     c = &global_tile_cache;
-    f = current_aircraft.flight;
-    o = &current_options;
+    f = current_aircraft.fdm_state;
+
+    tile_diameter = current_options.get_tile_diameter();
 
-    fgBucketFind(FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, &p1);
-    dw = o->tile_diameter / 2;
-    dh = o->tile_diameter / 2;
+    fgBucketFind( f->get_Longitude() * RAD_TO_DEG,
+                 f->get_Latitude() * RAD_TO_DEG, &p1);
+    dw = tile_diameter / 2;
+    dh = tile_diameter / 2;
 
-    if ( (p1.lon == p_last.lon) && (p1.lat == p_last.lat) &&
-        (p1.x == p_last.x) && (p1.y == p_last.y) ) {
+    if ( p1 == p_last ) {
        // same bucket as last time
-       fgPrintf( FG_TERRAIN, FG_DEBUG, "Same bucket as last time\n");
+       FG_LOG( FG_TERRAIN, FG_DEBUG, "Same bucket as last time" );
     } else if ( p_last.lon == -1000 ) {
        // First time through, initialize the system and load all
        // relavant tiles
 
-       fgPrintf( FG_TERRAIN, FG_INFO, "  First time through ... ");
-       fgPrintf( FG_TERRAIN, FG_INFO, "  Updating Tile list for %d,%d %d,%d\n",
-                 p1.lon, p1.lat, p1.x, p1.y);
-       fgPrintf( FG_TERRAIN, FG_INFO, "  Loading %d tiles\n", 
-                 o->tile_diameter * o->tile_diameter);
+       FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << p1 );
+       FG_LOG( FG_TERRAIN, FG_INFO, "  First time through ... " );
+       FG_LOG( FG_TERRAIN, FG_INFO, "  Updating Tile list for " << p1 );
+       FG_LOG( FG_TERRAIN, FG_INFO, "  Loading " 
+               << tile_diameter * tile_diameter << " tiles" );
 
        // wipe/initialize tile cache
-       c->Init();
+       c->init();
 
        // build the local area list and update cache
-       for ( j = 0; j < o->tile_diameter; j++ ) {
-           for ( i = 0; i < o->tile_diameter; i++ ) {
+       for ( j = 0; j < tile_diameter; j++ ) {
+           for ( i = 0; i < tile_diameter; i++ ) {
                fgBucketOffset(&p1, &p2, i - dw, j - dh);
-               fgTileMgrLoadTile(&p2, &tiles[(j*o->tile_diameter) + i]);
+               fgTileMgrLoadTile( p2, &tiles[(j*tile_diameter) + i]);
            }
        }
     } else {
@@ -137,95 +380,163 @@ int fgTileMgrUpdate( void ) {
        // AT ULTRA HIGH SPEEDS THIS ASSUMPTION MAY NOT BE VALID IF
        // THE AIRCRAFT CAN SKIP A TILE IN A SINGLE ITERATION.
 
-       fgPrintf( FG_TERRAIN, FG_INFO, "Updating Tile list for %d,%d %d,%d\n",
-                 p1.lon, p1.lat, p1.x, p1.y);
+       FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << p1 );
 
        if ( (p1.lon > p_last.lon) ||
             ( (p1.lon == p_last.lon) && (p1.x > p_last.x) ) ) {
-           fgPrintf( FG_TERRAIN, FG_INFO, "  Loading %d tiles\n"
-                     o->tile_diameter);
-           for ( j = 0; j < o->tile_diameter; j++ ) {
+           FG_LOG( FG_TERRAIN, FG_INFO
+                   "  Loading " << tile_diameter << "tiles" );
+           for ( j = 0; j < tile_diameter; j++ ) {
                // scrolling East
-               for ( i = 0; i < o->tile_diameter - 1; i++ ) {
-                   tiles[(j*o->tile_diameter) + i] = 
-                       tiles[(j*o->tile_diameter) + i + 1];
+               for ( i = 0; i < tile_diameter - 1; i++ ) {
+                   tiles[(j*tile_diameter) + i] = 
+                       tiles[(j*tile_diameter) + i + 1];
                }
                // load in new column
                fgBucketOffset(&p_last, &p2, dw + 1, j - dh);
-               fgTileMgrLoadTile(&p2, &tiles[(j*o->tile_diameter) + 
-                                            o->tile_diameter - 1]);
+               fgTileMgrLoadTile( p2, &tiles[(j*tile_diameter) + 
+                                            tile_diameter - 1]);
            }
        } else if ( (p1.lon < p_last.lon) ||
                    ( (p1.lon == p_last.lon) && (p1.x < p_last.x) ) ) {
-           fgPrintf( FG_TERRAIN, FG_INFO, "  Loading %d tiles\n"
-                     o->tile_diameter);
-           for ( j = 0; j < o->tile_diameter; j++ ) {
+           FG_LOG( FG_TERRAIN, FG_INFO
+                   "  Loading " << tile_diameter << "tiles" );
+           for ( j = 0; j < tile_diameter; j++ ) {
                // scrolling West
-               for ( i = o->tile_diameter - 1; i > 0; i-- ) {
-                   tiles[(j*o->tile_diameter) + i] = 
-                       tiles[(j*o->tile_diameter) + i - 1];
+               for ( i = tile_diameter - 1; i > 0; i-- ) {
+                   tiles[(j*tile_diameter) + i] = 
+                       tiles[(j*tile_diameter) + i - 1];
                }
                // load in new column
                fgBucketOffset(&p_last, &p2, -dw - 1, j - dh);
-               fgTileMgrLoadTile(&p2, &tiles[(j*o->tile_diameter) + 0]);
+               fgTileMgrLoadTile( p2, &tiles[(j*tile_diameter) + 0]);
            }
        }
 
        if ( (p1.lat > p_last.lat) ||
             ( (p1.lat == p_last.lat) && (p1.y > p_last.y) ) ) {
-           fgPrintf( FG_TERRAIN, FG_INFO, "  Loading %d tiles\n"
-                     o->tile_diameter);
-           for ( i = 0; i < o->tile_diameter; i++ ) {
+           FG_LOG( FG_TERRAIN, FG_INFO
+                   "  Loading " << tile_diameter << "tiles" );
+           for ( i = 0; i < tile_diameter; i++ ) {
                // scrolling North
-               for ( j = 0; j < o->tile_diameter - 1; j++ ) {
-                   tiles[(j * o->tile_diameter) + i] =
-                       tiles[((j+1) * o->tile_diameter) + i];
+               for ( j = 0; j < tile_diameter - 1; j++ ) {
+                   tiles[(j * tile_diameter) + i] =
+                       tiles[((j+1) * tile_diameter) + i];
                }
                // load in new column
                fgBucketOffset(&p_last, &p2, i - dw, dh + 1);
-               fgTileMgrLoadTile(&p2, &tiles[((o->tile_diameter-1) * 
-                                              o->tile_diameter) + i]);
+               fgTileMgrLoadTile( p2, &tiles[((tile_diameter-1) * 
+                                              tile_diameter) + i]);
            }
        } else if ( (p1.lat < p_last.lat) ||
                    ( (p1.lat == p_last.lat) && (p1.y < p_last.y) ) ) {
-           fgPrintf( FG_TERRAIN, FG_INFO, "  Loading %d tiles\n"
-                     o->tile_diameter);
-           for ( i = 0; i < o->tile_diameter; i++ ) {
+           FG_LOG( FG_TERRAIN, FG_INFO
+                   "  Loading " << tile_diameter << "tiles" );
+           for ( i = 0; i < tile_diameter; i++ ) {
                // scrolling South
-               for ( j = o->tile_diameter - 1; j > 0; j-- ) {
-                   tiles[(j * o->tile_diameter) + i] = 
-                       tiles[((j-1) * o->tile_diameter) + i];
+               for ( j = tile_diameter - 1; j > 0; j-- ) {
+                   tiles[(j * tile_diameter) + i] = 
+                       tiles[((j-1) * tile_diameter) + i];
                }
                // load in new column
                fgBucketOffset(&p_last, &p2, i - dw, -dh - 1);
-               fgTileMgrLoadTile(&p2, &tiles[0 + i]);
+               fgTileMgrLoadTile( p2, &tiles[0 + i]);
            }
        }
     }
+
+    // find our current elevation (feed in the current bucket to save work)
+    Point3D geod_pos = Point3D( f->get_Longitude(), f->get_Latitude(), 0.0);
+    Point3D tmp_abs_view_pos = fgGeodToCart(geod_pos);
+
+    scenery.cur_elev = 
+       fgTileMgrCurElev( f->get_Longitude(), f->get_Latitude(), 
+                         tmp_abs_view_pos );
+
     p_last.lon = p1.lon;
     p_last.lat = p1.lat;
     p_last.x = p1.x;
     p_last.y = p1.y;
+
     return 1;
 }
 
 
 // Calculate if point/radius is inside view frustum
-static int viewable( fgCartesianPoint3d *cp, double radius ) {
+static int viewable( const Point3D& cp, double radius ) {
+    int viewable = 1; // start by assuming it's viewable
+    double x1, y1;
+
+/********************************/
+#if defined( USE_FAST_FOV_CLIP ) // views.hxx
+/********************************/
+       
+    MAT3vec eye;       
+    double *mat;
+    double x, y, z;
+
+    x = cp.x();
+    y = cp.y();
+    z = cp.z();
+       
+    mat = (double *)(current_view.get_WORLD_TO_EYE());
+       
+    eye[2] =  x*mat[2] + y*mat[6] + z*mat[10] + mat[14];
+       
+    // Check near and far clip plane
+    if( ( eye[2] > radius ) ||
+       ( eye[2] + radius + current_weather.get_visibility() < 0) )
+    {
+       return(0);
+    }
+       
+    eye[0] = (x*mat[0] + y*mat[4] + z*mat[8] + mat[12])
+       * current_view.get_slope_x();
+
+    // check right and left clip plane (from eye perspective)
+    x1 = radius * current_view.get_fov_x_clip();
+    if( (eye[2] > -(eye[0]+x1)) || (eye[2] > (eye[0]-x1)) )
+    {
+       return(0);
+    }
+       
+    eye[1] = (x*mat[1] + y*mat[5] + z*mat[9] + mat[13]) 
+       * current_view.get_slope_y();
+
+    // check bottom and top clip plane (from eye perspective)
+    y1 = radius * current_view.get_fov_y_clip();
+    if( (eye[2] > -(eye[1]+y1)) || (eye[2] > (eye[1]-y1)) )
+    {
+       return(0);
+    }
+
+/********************************/     
+#else // DO NOT USE_FAST_FOV_CLIP
+/********************************/     
+
     fgVIEW *v;
     MAT3hvec world, eye;
-    int viewable = 1; // start by assuming it's viewable
-    double x0, x1, y1, slope;
+    double x0, slope;
 
     v = &current_view;
 
     MAT3_SET_HVEC(world, cp->x, cp->y, cp->z, 1.0);
-    MAT3mult_vec(eye, world, v->WORLD_TO_EYE);
+    // MAT3mult_vec(eye, world, v->WORLD_TO_EYE);
     // printf( "\nworld -> eye = %.2f %.2f %.2f  radius = %.2f\n", 
     //         eye[0], eye[1], eye[2], radius);
 
+    // Use lazy evaluation for calculating eye hvec.
+#define vec world
+#define mat v->WORLD_TO_EYE
+    eye[2] = vec[0]*mat[0][2]+vec[1]*mat[1][2]+vec[2]*mat[2][2]+mat[3][2];
+
     // Check near clip plane
-    if ( eye[2] - radius > 0.0 ) {
+    if ( eye[2] > radius ) {
+       return(0);
+    }
+
+    // Check far clip plane
+    if ( eye[2] + radius < -current_weather.get_visibility() ) {
        return(0);
     }
 
@@ -234,158 +545,182 @@ static int viewable( fgCartesianPoint3d *cp, double radius ) {
     x1 = v->cos_fov_x * radius;
     y1 = v->sin_fov_x * radius;
     slope = v->slope_x;
-    x0 = x1 - y1 / slope;
+    eye[0] = vec[0]*mat[0][0]+vec[1]*mat[1][0]+vec[2]*mat[2][0]+mat[3][0];
 
-    // printf("(r) x1 = %.2f  y1 = %.2f\n", x1, y1);
-    // printf("eye[0] = %.2f  eye[2] = %.2f\n", eye[0], eye[2]);
-    // printf("(r) x0 = %.2f  slope_x = %.2f  radius = %.2f\n", 
-    //        x0, slope, radius);
-
-    if ( eye[2] > slope * (eye[0] - x0) ) {
-       return(0);
+    if ( eye[2] > ((slope * (eye[0] - x1)) + y1) ) {
+       return( false );
     }
 
     // check left clip plane (from eye perspective)
-    x1 = -x1;
-    slope = -slope;
-    x0 = x1 - y1 / slope;
-
-    // printf("(r) x1 = %.2f  y1 = %.2f\n", x1, y1);
-    // printf("eye[0] = %.2f  eye[2] = %.2f\n", eye[0], eye[2]);
-    // printf("(r) x0 = %.2f  slope_x = %.2f  radius = %.2f\n", 
-    //        x0, slope, radius);
-
-    if ( eye[2] > slope * (eye[0] - x0) ) {
-       return(0);
+    if ( eye[2] > -((slope * (eye[0] + x1)) - y1) ) {
+       return( false );
     }
 
     // check bottom clip plane (from eye perspective)
     x1 = -(v->cos_fov_y) * radius;
     y1 = v->sin_fov_y * radius;
     slope = v->slope_y;
-    x0 = x1 - y1 / slope;
+    eye[1] = vec[0]*mat[0][1]+vec[1]*mat[1][1]+vec[2]*mat[2][1]+mat[3][1];
+#undef vec
+#undef mat
 
-    // printf("(r) x1 = %.2f  y1 = %.2f\n", x1, y1);
-    // printf("eye[1] = %.2f  eye[2] = %.2f\n", eye[1], eye[2]);
-    // printf("(r) x0 = %.2f  slope_y = %.2f  radius = %.2f\n", 
-    //       x0, slope, radius);
-
-    if ( eye[2] > slope * (eye[1] - x0) ) {
-       return(0);
+    if ( eye[2] > ((slope * (eye[1] - x1)) + y1) ) {
+       return( false );
     }
 
     // check top clip plane (from eye perspective)
-    x1 = -x1;
-    slope = -slope;
-    x0 = x1 - y1 / slope;
-
-    // printf("(r) x1 = %.2f  y1 = %.2f\n", x1, y1);
-    // printf("eye[1] = %.2f  eye[2] = %.2f\n", eye[1], eye[2]);
-    // printf("(r) x0 = %.2f  slope_y = %.2f  radius = %.2f\n", 
-    //        x0, slope, radius);
-
-    if ( eye[2] > slope * (eye[1] - x0) ) {
-       return(0);
+    if ( eye[2] > -((slope * (eye[1] + x1)) - y1) ) {
+       return( false );
     }
 
+#endif // defined( USE_FAST_FOV_CLIP )
+       
     return(viewable);
 }
 
 
+// NEW 
+
+// inrange() IS THIS POINT WITHIN POSSIBLE VIEWING RANGE ?
+//     calculate distance from vertical tangent line at
+//     current position to center of object.
+//     this is equivalent to
+//     dist = point_line_dist_squared( &(t->center), &(v->abs_view_pos), 
+//                                     v->local_up );
+//     if ( dist < FG_SQUARE(t->bounding_radius) ) {
+//
+// the compiler should inline this for us
+
+static int
+inrange( const double radius, const Point3D& center, const Point3D& vp,
+        const MAT3vec up)
+{
+    MAT3vec u, u1, v;
+    // double tmp;
+       
+    // u = p - p0
+    u[0] = center.x() - vp.x();
+    u[1] = center.y() - vp.y();
+    u[2] = center.z() - vp.z();
+       
+    // calculate the projection, u1, of u along d.
+    // u1 = ( dot_prod(u, d) / dot_prod(d, d) ) * d;
+       
+    MAT3_SCALE_VEC(u1, up,
+                  (MAT3_DOT_PRODUCT(u, up) / MAT3_DOT_PRODUCT(up, up)) );
+    
+    // v = u - u1 = vector from closest point on line, p1, to the
+    // original point, p.
+    MAT3_SUB_VEC(v, u, u1);
+       
+    return( FG_SQUARE(radius) >= MAT3_DOT_PRODUCT(v, v));
+}
+
+
+// NEW for legibility
+
+// update this tile's geometry for current view
+// The Compiler should inline this
+static void
+update_tile_geometry( fgTILE *t, GLdouble *MODEL_VIEW)
+{
+    GLdouble *m;
+    double x, y, z;
+       
+    // calculate tile offset
+    t->offset = t->center - scenery.center;
+
+    x = t->offset.x();
+    y = t->offset.y();
+    z = t->offset.z();
+       
+    m = t->model_view;
+       
+    // Calculate the model_view transformation matrix for this tile
+    FG_MEM_COPY( m, MODEL_VIEW, 16*sizeof(GLdouble) );
+    
+    // This is equivalent to doing a glTranslatef(x, y, z);
+    m[12] += (m[0]*x + m[4]*y + m[8] *z);
+    m[13] += (m[1]*x + m[5]*y + m[9] *z);
+    m[14] += (m[2]*x + m[6]*y + m[10]*z);
+    // m[15] += (m[3]*x + m[7]*y + m[11]*z);
+    // m[3] m7[] m[11] are 0.0 see LookAt() in views.cxx
+    // so m[15] is unchanged
+}
+
+
 // Render the local tiles
 void fgTileMgrRender( void ) {
+    FGInterface *f;
     fgTILECACHE *c;
-    fgFLIGHT *f;
-    fgOPTIONS *o;
     fgTILE *t;
-    fgVIEW *v;
-    struct fgBUCKET p;
-    fgCartesianPoint3d frag_offset, last_offset;
+    FGView *v;
+    Point3D frag_offset;
     fgFRAGMENT *frag_ptr;
     fgMATERIAL *mtl_ptr;
-    list < fgFRAGMENT > :: iterator current;
-    list < fgFRAGMENT > :: iterator last;
-    int i, size;
+    int i;
+    int tile_diameter;
     int index;
     int culled = 0;
     int drawn = 0;
 
     c = &global_tile_cache;
-    f = current_aircraft.flight;
-    o = &current_options;
+    f = current_aircraft.fdm_state;
     v = &current_view;
 
-    // Find current translation offset
-    fgBucketFind(FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, &p);
-    index = c->Exists(&p);
-    t = c->GetTile(index);
-
-    fgPrintf( FG_TERRAIN, FG_DEBUG, 
-             "Pos = (%.2f, %.2f) Current bucket = %d %d %d %d  Index = %ld\n", 
-             FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG,
-             p.lon, p.lat, p.x, p.y, fgBucketGenIndex(&p) );
+    tile_diameter = current_options.get_tile_diameter();
 
+    // moved to fgTileMgrUpdate, right after we check if we need to
+    // load additional tiles:
+    // scenery.cur_elev = fgTileMgrCurElev( FG_Longitude, FG_Latitude, 
+    //                                      v->abs_view_pos );
     // initialize the transient per-material fragment lists
     material_mgr.init_transient_material_lists();
-
+   
     // Pass 1
     // traverse the potentially viewable tile list
-    for ( i = 0; i < (o->tile_diameter * o->tile_diameter); i++ ) {
+    for ( i = 0; i < (tile_diameter * tile_diameter); i++ ) {
        index = tiles[i];
        // fgPrintf( FG_TERRAIN, FG_DEBUG, "Index = %d\n", index);
-       t = c->GetTile(index);
+       t = c->get_tile(index);
 
        // calculate tile offset
-       t->offset.x = t->center.x - scenery.center.x;
-       t->offset.y = t->center.y - scenery.center.y;
-       t->offset.z = t->center.z - scenery.center.z;
+       t->SetOffset( scenery.center );
 
        // Course (tile based) culling
-       if ( viewable(&(t->offset), t->bounding_radius) ) {
-           // at least a portion of this tile is viewable
+       if ( viewable(t->offset, t->bounding_radius) ) {
+           // at least a portion of this tile could be viewable
            
+           // Calculate the model_view transformation matrix for this tile
+           // This is equivalent to doing a glTranslatef(x, y, z);
+           t->UpdateViewMatrix( v->get_MODEL_VIEW() );
+
            // xglPushMatrix();
            // xglTranslatef(t->offset.x, t->offset.y, t->offset.z);
 
            // traverse fragment list for tile
-           current = t->fragment_list.begin();
-           last = t->fragment_list.end();
+            fgTILE::FragmentIterator current = t->begin();
+            fgTILE::FragmentIterator last = t->end();
 
-           while ( current != last ) {
+           for ( ; current != last; ++current ) {
                frag_ptr = &(*current);
-               current++;
                
                if ( frag_ptr->display_list >= 0 ) {
                    // Fine (fragment based) culling
-                   frag_offset.x = frag_ptr->center.x - scenery.center.x;
-                   frag_offset.y = frag_ptr->center.y - scenery.center.y;
-                   frag_offset.z = frag_ptr->center.z - scenery.center.z;
+                   frag_offset = frag_ptr->center - scenery.center;
 
-                   if ( viewable(&frag_offset, frag_ptr->bounding_radius*2) ) {
+                   if ( viewable(frag_offset, frag_ptr->bounding_radius*2) ) {
                        // add to transient per-material property fragment list
-                       frag_ptr->tile_offset.x = t->offset.x;
-                       frag_ptr->tile_offset.y = t->offset.y;
-                       frag_ptr->tile_offset.z = t->offset.z;
-
-                       /*
-                       frag_ptr->matrix[12] = t->offset.x;
-                       frag_ptr->matrix[13] = t->offset.y;
-                       frag_ptr->matrix[14] = t->offset.z;
-
-                       xglGetFloatv(GL_MODELVIEW_MATRIX, frag_ptr->matrix);
-                       for ( j = 1; j < 16; j++ ) {
-                           printf(" a%d = %f\n", j, frag_ptr->matrix[j]);
-                       }
-                       */
+                       // frag_ptr->tile_offset.x = t->offset.x;
+                       // frag_ptr->tile_offset.y = t->offset.y;
+                       // frag_ptr->tile_offset.z = t->offset.z;
 
-                       mtl_ptr = (fgMATERIAL *)(frag_ptr->material_ptr);
+                       mtl_ptr = frag_ptr->material_ptr;
                        // printf(" lookup = %s\n", mtl_ptr->texture_name);
-                       if ( mtl_ptr->list_size < FG_MAX_MATERIAL_FRAGS ) {
-                           mtl_ptr->list[mtl_ptr->list_size] = frag_ptr;
-                           (mtl_ptr->list_size)++;
-                       } else {
-                           fgPrintf( FG_TERRAIN, FG_ALERT,
-                                     "Overran material sorting array\n" );
+                       if ( ! mtl_ptr->append_sort_list( frag_ptr ) ) {
+                           FG_LOG( FG_TERRAIN, FG_ALERT,
+                                   "Overran material sorting array" );
                        }
 
                        // xglCallList(frag_ptr->display_list);
@@ -406,9 +741,9 @@ void fgTileMgrRender( void ) {
     }
 
     if ( (drawn + culled) > 0 ) {
-       v->vfc_ratio = (double)culled / (double)(drawn + culled);
+       v->set_vfc_ratio( (double)culled / (double)(drawn + culled) );
     } else {
-       v->vfc_ratio = 0.0;
+       v->set_vfc_ratio( 0.0 );
     }
     // printf("drawn = %d  culled = %d  saved = %.2f\n", drawn, culled, 
     //        v->vfc_ratio);
@@ -416,70 +751,192 @@ void fgTileMgrRender( void ) {
     // Pass 2
     // traverse the transient per-material fragment lists and render
     // out all fragments for each material property.
-    map < string, fgMATERIAL, less<string> > :: iterator mapcurrent = 
-       material_mgr.material_map.begin();
-    map < string, fgMATERIAL, less<string> > :: iterator maplast = 
-       material_mgr.material_map.end();
-
     xglPushMatrix();
-
-    while ( mapcurrent != maplast ) {
-        // (char *)key = (*mapcurrent).first;
-        // (fgMATERIAL)value = (*mapcurrent).second;
-       mtl_ptr = &(*mapcurrent).second;
-
-       last_offset.x = last_offset.y = last_offset.z = -99999999.0;
-
-       size = mtl_ptr->list_size;
-       if ( size > 0 ) {
-           if ( o->textures ) {
-#ifdef GL_VERSION_1_1
-               xglBindTexture(GL_TEXTURE_2D, mtl_ptr->texture_id);
-#elif GL_EXT_texture_object
-               xglBindTextureEXT(GL_TEXTURE_2D, mtl_ptr->texture_id);
-#else
-#  error port me
-#endif
-           } else {
-               xglMaterialfv (GL_FRONT, GL_AMBIENT, mtl_ptr->ambient);
-               xglMaterialfv (GL_FRONT, GL_DIFFUSE, mtl_ptr->diffuse);
-           }
-
-           // printf("traversing = %s, size = %d\n", 
-           //       mtl_ptr->texture_name, size);
-           for ( i = 0; i < size; i++ ) {
-               frag_ptr = mtl_ptr->list[i];
-               
-               if ( (frag_ptr->tile_offset.x == last_offset.x) && 
-                    (frag_ptr->tile_offset.y == last_offset.y) &&
-                    (frag_ptr->tile_offset.z == last_offset.z) ) {
-                   // same tile as last time, no transform necessary
-               } else {
-                   // new tile, new translate
-                   // xglLoadMatrixf( frag_ptr->matrix );
-                   xglPopMatrix();
-                   xglPushMatrix();
-                   xglTranslatef( frag_ptr->tile_offset.x,  
-                                  frag_ptr->tile_offset.y, 
-                                  frag_ptr->tile_offset.z ); 
-               }
-           
-               // Woohoo!!!  We finally get to draw something!
-               // printf("  display_list = %d\n", frag_ptr->display_list);
-               xglCallList(frag_ptr->display_list);
-
-               last_offset = frag_ptr->tile_offset;
-           }
-       }
-
-       xglPopMatrix();
-
-        *mapcurrent++;
-    }
+    material_mgr.render_fragments();
+    xglPopMatrix();
 }
 
 
 // $Log$
+// Revision 1.53  1999/02/05 21:29:16  curt
+// Modifications to incorporate Jon S. Berndts flight model code.
+//
+// Revision 1.52  1999/01/27 04:49:48  curt
+// Fixes so that the sim can start out at an airport below sea level.
+//
+// Revision 1.51  1998/12/09 18:50:33  curt
+// Converted "class fgVIEW" to "class FGView" and updated to make data
+// members private and make required accessor functions.
+//
+// Revision 1.50  1998/12/06 13:51:25  curt
+// Turned "struct fgWEATHER" into "class FGWeather".
+//
+// Revision 1.49  1998/12/05 15:54:26  curt
+// Renamed class fgFLIGHT to class FGState as per request by JSB.
+//
+// Revision 1.48  1998/12/05 14:20:21  curt
+// Looking into a terrain intersection problem.
+//
+// Revision 1.47  1998/12/05 14:11:19  curt
+// Sun portability tweak.
+//
+// Revision 1.46  1998/12/03 14:15:24  curt
+// Actually set the current scenery elevation based on scenery intersection point
+// rather than calculating the intesection point and throwing it away.
+//
+// Revision 1.45  1998/12/03 01:18:18  curt
+// Converted fgFLIGHT to a class.
+// Tweaks for Sun Portability.
+// Tweaked current terrain elevation code as per NHV.
+//
+// Revision 1.44  1998/11/23 21:49:48  curt
+// minor tweaks.
+//
+// Revision 1.43  1998/11/09 23:40:52  curt
+// Bernie Bright <bbright@c031.aone.net.au> writes:
+// I've made some changes to the Scenery handling.  Basically just tidy ups.
+// The main difference is in tile.[ch]xx where I've changed list<fgFRAGMENT> to
+// vector<fgFRAGMENT>.  Studying our usage patterns this seems reasonable.
+// Lists are good if you need to insert/delete elements randomly but we
+// don't do that.  All access seems to be sequential.  Two additional
+// benefits are smaller memory usage - each list element requires pointers
+// to the next and previous elements, and faster access - vector iterators
+// are smaller and faster than list iterators.  This should also help
+// Charlie Hotchkiss' problem when compiling with Borland and STLport.
+//
+// ./Lib/Bucket/bucketutils.hxx
+//   Convenience functions for fgBUCKET.
+//
+// ./Simulator/Scenery/tile.cxx
+// ./Simulator/Scenery/tile.hxx
+//   Changed fragment list to a vector.
+//   Added some convenience member functions.
+//
+// ./Simulator/Scenery/tilecache.cxx
+// ./Simulator/Scenery/tilecache.hxx
+//   use const fgBUCKET& instead of fgBUCKET* where appropriate.
+//
+// ./Simulator/Scenery/tilemgr.cxx
+// ./Simulator/Scenery/tilemgr.hxx
+//   uses all the new convenience functions.
+//
+// Revision 1.42  1998/11/06 21:18:23  curt
+// Converted to new logstream debugging facility.  This allows release
+// builds with no messages at all (and no performance impact) by using
+// the -DFG_NDEBUG flag.
+//
+// Revision 1.41  1998/10/18 01:17:23  curt
+// Point3D tweaks.
+//
+// Revision 1.40  1998/10/17 01:34:28  curt
+// C++ ifying ...
+//
+// Revision 1.39  1998/10/16 00:55:50  curt
+// Converted to Point3D class.
+//
+// Revision 1.38  1998/09/17 18:36:18  curt
+// Tweaks and optimizations by Norman Vine.
+//
+// Revision 1.37  1998/09/15 01:36:45  curt
+// cleaned up my fragment.num_faces hack :-) to use the STL (no need in
+// duplicating work.)
+// Tweaked fgTileMgrRender() do not calc tile matrix unless necessary.
+// removed some unneeded stuff from fgTileMgrCurElev()
+//
+// Revision 1.36  1998/09/14 12:45:26  curt
+// minor tweaks.
+//
+// Revision 1.35  1998/09/10 19:07:16  curt
+// /Simulator/Objects/fragment.hxx
+//   Nested fgFACE inside fgFRAGMENT since its not used anywhere else.
+//
+// ./Simulator/Objects/material.cxx
+// ./Simulator/Objects/material.hxx
+//   Made fgMATERIAL and fgMATERIAL_MGR bona fide classes with private
+//   data members - that should keep the rabble happy :)
+//
+// ./Simulator/Scenery/tilemgr.cxx
+//   In viewable() delay evaluation of eye[0] and eye[1] in until they're
+//   actually needed.
+//   Change to fgTileMgrRender() to call fgMATERIAL_MGR::render_fragments()
+//   method.
+//
+// ./Include/fg_stl_config.h
+// ./Include/auto_ptr.hxx
+//   Added support for g++ 2.7.
+//   Further changes to other files are forthcoming.
+//
+// Brief summary of changes required for g++ 2.7.
+//   operator->() not supported by iterators: use (*i).x instead of i->x
+//   default template arguments not supported,
+//   <functional> doesn't have mem_fun_ref() needed by callbacks.
+//   some std include files have different names.
+//   template member functions not supported.
+//
+// Revision 1.34  1998/09/09 20:58:09  curt
+// Tweaks to loop constructs with STL usage.
+//
+// Revision 1.33  1998/09/08 15:05:10  curt
+// Optimization by Norman Vine.
+//
+// Revision 1.32  1998/08/25 16:52:44  curt
+// material.cxx material.hxx obj.cxx obj.hxx texload.c texload.h moved to
+//   ../Objects
+//
+// Revision 1.31  1998/08/24 20:11:40  curt
+// Tweaks ...
+//
+// Revision 1.30  1998/08/22  14:49:59  curt
+// Attempting to iron out seg faults and crashes.
+// Did some shuffling to fix a initialization order problem between view
+// position, scenery elevation.
+//
+// Revision 1.29  1998/08/20 15:12:06  curt
+// Used a forward declaration of classes fgTILE and fgMATERIAL to eliminate
+// the need for "void" pointers and casts.
+// Quick hack to count the number of scenery polygons that are being drawn.
+//
+// Revision 1.28  1998/08/12 21:13:06  curt
+// material.cxx: don't load textures if they are disabled
+// obj.cxx: optimizations from Norman Vine
+// tile.cxx: minor tweaks
+// tile.hxx: addition of num_faces
+// tilemgr.cxx: minor tweaks
+//
+// Revision 1.27  1998/07/24 21:42:09  curt
+// material.cxx: whups, double method declaration with no definition.
+// obj.cxx: tweaks to avoid errors in SGI's CC.
+// tile.cxx: optimizations by Norman Vine.
+// tilemgr.cxx: optimizations by Norman Vine.
+//
+// Revision 1.26  1998/07/20 12:51:26  curt
+// Added far clip plane to fragment clipping calculations and tie this to
+// weather->visibility.  This way you can increase framerates by increasing
+// for and lowering visibility.
+//
+// Revision 1.25  1998/07/13 21:02:01  curt
+// Wrote access functions for current fgOPTIONS.
+//
+// Revision 1.24  1998/07/12 03:18:29  curt
+// Added ground collision detection.  This involved:
+// - saving the entire vertex list for each tile with the tile records.
+// - saving the face list for each fragment with the fragment records.
+// - code to intersect the current vertical line with the proper face in
+//   an efficient manner as possible.
+// Fixed a bug where the tiles weren't being shifted to "near" (0,0,0)
+//
+// Revision 1.23  1998/07/08 14:47:23  curt
+// Fix GL_MODULATE vs. GL_DECAL problem introduced by splash screen.
+// polare3d.h renamed to polar3d.hxx
+// fg{Cartesian,Polar}Point3d consolodated.
+// Added some initial support for calculating local current ground elevation.
+//
+// Revision 1.22  1998/07/04 00:54:31  curt
+// Added automatic mipmap generation.
+//
+// When rendering fragments, use saved model view matrix from associated tile
+// rather than recalculating it with push() translate() pop().
+//
 // Revision 1.21  1998/06/27 16:54:59  curt
 // Check for GL_VERSION_1_1 or GL_EXT_texture_object to decide whether to use
 //   "EXT" versions of texture management routines.