]> git.mxchange.org Git - flightgear.git/blobdiff - Scenery/tilemgr.cxx
Converted to new logstream debugging facility. This allows release
[flightgear.git] / Scenery / tilemgr.cxx
index 1d5740ac987edc9c011cca2a82c216bb88507f16..f3aa2d3b49be3c6a8dc8f3df0ea92859ba812270 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 <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.h>
+#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.h>
+#include <Weather/weather.hxx>
 
 #include "scenery.hxx"
+#include "tile.hxx"
 #include "tilecache.hxx"
 
 
@@ -77,7 +78,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();
@@ -92,16 +93,17 @@ void fgTileMgrLoadTile( fgBUCKET *p, int *index) {
 
     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->lon << " " << p->lat << " " << p->x << " " << p->y );
     
     // if not in cache, load tile into the next available slot
-    if ( (*index = c->Exists(p)) < 0 ) {
-       *index = c->NextAvail();
-       c->EntryFillIn(*index, p);
+    if ( (*index = c->exists(p)) < 0 ) {
+       *index = c->next_avail();
+       c->fill_in(*index, p);
     }
 
-    fgPrintf( FG_TERRAIN, FG_DEBUG, "Selected cache index of %d\n", *index);
+    FG_LOG( FG_TERRAIN, FG_DEBUG, "Selected cache index of " << *index);
 }
 
 
@@ -127,19 +129,21 @@ int fgTileMgrUpdate( void ) {
     if ( (p1.lon == p_last.lon) && (p1.lat == p_last.lat) &&
         (p1.x == p_last.x) && (p1.y == p_last.y) ) {
        // 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", 
-                 tile_diameter * tile_diameter);
+       FG_LOG( FG_TERRAIN, FG_INFO, "  First time through ... " );
+       FG_LOG( FG_TERRAIN, FG_INFO, 
+               "  Updating Tile list for "
+               << p1.lon << "," << p1.lat << " " << p1.x << "," << p1.y );
+       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 < tile_diameter; j++ ) {
@@ -156,13 +160,14 @@ 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.lon << "," << p1.lat << " " << p1.x << "," << p1.y );
 
        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"
-                     tile_diameter);
+           FG_LOG( FG_TERRAIN, FG_INFO
+                   "  Loading " << tile_diameter << "tiles" );
            for ( j = 0; j < tile_diameter; j++ ) {
                // scrolling East
                for ( i = 0; i < tile_diameter - 1; i++ ) {
@@ -176,8 +181,8 @@ int fgTileMgrUpdate( void ) {
            }
        } 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"
-                     tile_diameter);
+           FG_LOG( FG_TERRAIN, FG_INFO
+                   "  Loading " << tile_diameter << "tiles" );
            for ( j = 0; j < tile_diameter; j++ ) {
                // scrolling West
                for ( i = tile_diameter - 1; i > 0; i-- ) {
@@ -192,8 +197,8 @@ int fgTileMgrUpdate( void ) {
 
        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"
-                     tile_diameter);
+           FG_LOG( FG_TERRAIN, FG_INFO
+                   "  Loading " << tile_diameter << "tiles" );
            for ( i = 0; i < tile_diameter; i++ ) {
                // scrolling North
                for ( j = 0; j < tile_diameter - 1; j++ ) {
@@ -207,8 +212,8 @@ int fgTileMgrUpdate( void ) {
            }
        } 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"
-                     tile_diameter);
+           FG_LOG( FG_TERRAIN, FG_INFO
+                   "  Loading " << tile_diameter << "tiles" );
            for ( i = 0; i < tile_diameter; i++ ) {
                // scrolling South
                for ( j = tile_diameter - 1; j > 0; j-- ) {
@@ -230,14 +235,14 @@ int fgTileMgrUpdate( void ) {
 
 
 // Calculate shortest distance from point to line
-static double point_line_dist_squared( fgPoint3d *tc, fgPoint3d *vp, 
+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;
+    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);
 
@@ -248,7 +253,7 @@ static double point_line_dist_squared( fgPoint3d *tc, fgPoint3d *vp,
 
 
 // Calculate if point/radius is inside view frustum
-static int viewable( fgPoint3d *cp, double radius ) {
+static int viewable( const Point3D& cp, double radius ) {
     int viewable = 1; // start by assuming it's viewable
     double x1, y1;
 
@@ -260,9 +265,9 @@ static int viewable( fgPoint3d *cp, double radius ) {
     double *mat;
     double x, y, z;
 
-    x = cp->x;
-    y = cp->y;
-    z = cp->z;
+    x = cp.x();
+    y = cp.y();
+    z = cp.z();
        
     mat = (double *)(current_view.WORLD_TO_EYE);
        
@@ -375,16 +380,16 @@ static int viewable( fgPoint3d *cp, double radius ) {
 // the compiler should inline this for us
 
 static int
-inrange( const double radius, const fgPoint3d *center, const fgPoint3d *vp,
+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;
+    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;
@@ -404,51 +409,48 @@ inrange( const double radius, const fgPoint3d *center, const fgPoint3d *vp,
 // 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, fgPoint3d *abs_view_pos ) {
+double fgTileMgrCurElev( double lon, double lat, const Point3D& abs_view_pos ) {
     fgTILECACHE *c;
     fgTILE *t;
     // fgVIEW *v;
     fgFRAGMENT *frag_ptr;
     fgBUCKET p;
-    fgPoint3d earth_center, result;
-    fgPoint3d pp;
+    Point3D earth_center, result;
+    Point3D pp;
     MAT3vec local_up;
     list < fgFRAGMENT > :: iterator current;
     list < fgFRAGMENT > :: iterator last;
-    double dist, min_dist, lat_geod, alt, sea_level_r;
-    double x, y, z;
-    int index, tile_diameter, i;
+    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;
+    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 = c->Exists(&p);
-    t = c->GetTile(index);
-
-    scenery.next_center.x = t->center.x;
-    scenery.next_center.y = t->center.y;
-    scenery.next_center.z = t->center.z;
+    index = c->exists(&p);
+    t = c->get_tile(index);
 
-    earth_center.x = 0.0;
-    earth_center.y = 0.0;
-    earth_center.z = 0.0;
+    scenery.next_center = t->center;
+    
+    earth_center = Point3D(0.0, 0.0, 0.0);
 
-    fgPrintf( FG_TERRAIN, FG_DEBUG, 
-             "Pos = (%.2f, %.2f) Current bucket = %d %d %d %d  Index = %ld\n", 
-             lon * RAD_TO_DEG, lat * RAD_TO_DEG,
-             p.lon, p.lat, p.x, p.y, fgBucketGenIndex(&p) );
+    FG_LOG( FG_TERRAIN, FG_DEBUG, 
+           "Pos = (" << lon * RAD_TO_DEG << ", " << lat * RAD_TO_DEG
+           << "  Current bucket = " 
+           << p.lon << " " << p.lat << " " << p.x << " " << p.y
+           << "  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);
-
+    // 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.
@@ -457,8 +459,7 @@ double fgTileMgrCurElev( double lon, double lat, fgPoint3d *abs_view_pos ) {
        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 );
+    dist = point_line_dist_squared( t->center, abs_view_pos, local_up );
     if ( dist < FG_SQUARE(t->bounding_radius) ) {
 
        // traverse fragment list for tile
@@ -472,15 +473,16 @@ double fgTileMgrCurElev( double lon, double lat, fgPoint3d *abs_view_pos ) {
               &abs_view_pos), local_up),
               frag_ptr->bounding_radius); */
 
-           dist = point_line_dist_squared( &(frag_ptr->center), 
-                                           abs_view_pos, local_up);
+           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 ) ) {
+                                         earth_center, 0, result ) ) {
                    // compute geocentric coordinates of tile center
                    pp = fgCartToPolar3d(result);
                    // convert to geodetic coordinates
-                   fgGeocToGeod(pp.lat, pp.radius, &lat_geod, 
+                   fgGeocToGeod(pp.lat(), pp.radius(), &lat_geod, 
                                 &alt, &sea_level_r);
                    // printf("alt = %.2f\n", alt);
                    // exit since we found an intersection
@@ -506,9 +508,11 @@ update_tile_geometry( fgTILE *t, GLdouble *MODEL_VIEW)
     double x, y, z;
        
     // 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);
+    t->offset = t->center - scenery.center;
+
+    x = t->offset.x();
+    y = t->offset.y();
+    z = t->offset.z();
        
     m = t->model_view;
        
@@ -527,96 +531,51 @@ update_tile_geometry( fgTILE *t, GLdouble *MODEL_VIEW)
 
 // Render the local tiles
 void fgTileMgrRender( void ) {
-    fgTILECACHE *c;
     fgFLIGHT *f;
-    fgTILE *t, *last_tile_ptr;
+    fgTILECACHE *c;
+    fgTILE *t;
     fgVIEW *v;
-    fgBUCKET p;
-    fgPoint3d frag_offset, pp;
-    fgPoint3d earth_center, result;
+    Point3D frag_offset;
     fgFRAGMENT *frag_ptr;
     fgMATERIAL *mtl_ptr;
-    GLdouble *m;
-    double dist, min_dist, lat_geod, alt, sea_level_r;
-    double x, y, z;
     list < fgFRAGMENT > :: iterator current;
     list < fgFRAGMENT > :: iterator last;
-    int i, j, size;
-    int tile_diameter, textures;
+    int i;
+    int tile_diameter;
     int index;
     int culled = 0;
     int drawn = 0;
-    int total_faces = 0;
 
     c = &global_tile_cache;
     f = current_aircraft.flight;
     v = &current_view;
 
     tile_diameter = current_options.get_tile_diameter();
-    textures = current_options.get_textures();
-
-    // Find current translation offset
-    fgBucketFind(FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, &p);
-    index = c->Exists(&p);
-    t = c->GetTile(index);
-
-    scenery.next_center.x = t->center.x;
-    scenery.next_center.y = t->center.y;
-    scenery.next_center.z = t->center.z;
-
-    earth_center.x = 0.0;
-    earth_center.y = 0.0;
-    earth_center.z = 0.0;
-
-    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) );
 
+    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();
-    min_dist = 100000.0;
-
-    scenery.cur_elev = fgTileMgrCurElev( FG_Longitude, FG_Latitude, 
-                                        &(v->abs_view_pos) );
-    
+   
     // Pass 1
     // traverse the potentially viewable tile list
     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
-       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);
-
-       m = t->model_view;
-       for ( j = 0; j < 16; j++ ) {
-           m[j] = v->MODEL_VIEW[j];
-       }
-
-       // Calculate the model_view transformation matrix for this tile
-       // This is equivalent to doing a glTranslatef(x, y, z);
-       m[12] = m[0] * x + m[4] * y + m[8]  * z + m[12];
-       m[13] = m[1] * x + m[5] * y + m[9]  * z + m[13];
-       m[14] = m[2] * x + m[6] * y + m[10] * z + m[14];
-       m[15] = m[3] * x + m[7] * y + m[11] * z + m[15];
-
-#if defined( TEST_FOV_CLIP )
-       if( viewable(&(t->offset), t->bounding_radius) !=
-           viewable2(&(t->offset), t->bounding_radius) )
-       {
-           printf("FOV PROBLEM\n");
-           exit(10);
-       }
-#endif // defined( TEST_FOV_CLIP )
+       t->SetOffset( scenery.center );
 
        // Course (tile based) culling
-       if ( viewable(&(t->offset), t->bounding_radius) ) {
+       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->MODEL_VIEW );
+
            // xglPushMatrix();
            // xglTranslatef(t->offset.x, t->offset.y, t->offset.z);
 
@@ -629,20 +588,9 @@ void fgTileMgrRender( void ) {
                
                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;
-
-#if defined( TEST_FOV_CLIP )
-                   radius = frag_ptr->bounding_radius*2;
-                   if ( viewable(&frag_offset, radius) !=
-                        viewable2(&frag_offset, radius) ) {
-                       printf("FOV PROBLEM\n");
-                       exit(10);
-                   }
-#endif // defined( TEST_FOV_CLIP )
+                   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;
@@ -651,8 +599,8 @@ void fgTileMgrRender( void ) {
                        mtl_ptr = frag_ptr->material_ptr;
                        // printf(" lookup = %s\n", mtl_ptr->texture_name);
                        if ( ! mtl_ptr->append_sort_list( frag_ptr ) ) {
-                           fgPrintf( FG_TERRAIN, FG_ALERT,
-                                     "Overran material sorting array\n" );
+                           FG_LOG( FG_TERRAIN, FG_ALERT,
+                                   "Overran material sorting array" );
                        }
 
                        // xglCallList(frag_ptr->display_list);
@@ -690,6 +638,32 @@ void fgTileMgrRender( void ) {
 
 
 // $Log$
+// 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.