From 941f27c9a06b21183e977efea86503e7ed9ef995 Mon Sep 17 00:00:00 2001
From: curt <curt>
Date: Sun, 23 Jul 2000 21:32:59 +0000
Subject: [PATCH] Updates to move scenery initialization earlier in the
 initialization sequence so that the FDM can know the current ground altitude
 when it is initialized.

---
 src/GUI/gui.cxx           |  2 +
 src/Main/fg_init.cxx      | 80 ++++++++++++++++++-------------------
 src/Main/keyboard.cxx     |  7 ++--
 src/Main/main.cxx         |  4 +-
 src/Scenery/scenery.cxx   |  9 +----
 src/Scenery/tilecache.cxx |  2 +-
 src/Scenery/tilemgr.cxx   | 83 ++++++++++++++++++++++-----------------
 src/Scenery/tilemgr.hxx   | 10 ++---
 8 files changed, 101 insertions(+), 96 deletions(-)

diff --git a/src/GUI/gui.cxx b/src/GUI/gui.cxx
index 23464e22a..2a3d52254 100644
--- a/src/GUI/gui.cxx
+++ b/src/GUI/gui.cxx
@@ -1042,6 +1042,8 @@ void AptDialog_OK (puObject *)
         if ( airports.search( AptId, &a ) )
         {
             current_options.set_airport_id( AptId.c_str() );
+            current_options.set_altitude( -9999.0 );
+	    fgSetPosFromAirportID( AptId );
             BusyCursor(0);
             fgReInitSubsystems();
             BusyCursor(1);
diff --git a/src/Main/fg_init.cxx b/src/Main/fg_init.cxx
index 5fb58c971..313776de7 100644
--- a/src/Main/fg_init.cxx
+++ b/src/Main/fg_init.cxx
@@ -188,17 +188,14 @@ bool fgInitPosition( void ) {
     FGInterface *f = current_aircraft.fdm_state;
     string id = current_options.get_airport_id();
 
-    if ( id.length() ) {
-	// set initial position from airport id
-	if ( ! fgSetPosFromAirportID( id ) ) {
-	    exit(-1);
-	}
-    }
-
     // set initial position from default or command line coordinates
     f->set_Longitude( current_options.get_lon() * DEG_TO_RAD );
     f->set_Latitude( current_options.get_lat() * DEG_TO_RAD );
 
+    if ( scenery.cur_elev > current_options.get_altitude() - 2 ) {
+	current_options.set_altitude( scenery.cur_elev + 2 );
+    }
+
     FG_LOG( FG_GENERAL, FG_INFO,
 	    "starting altitude is = " << current_options.get_altitude() );
 
@@ -266,6 +263,36 @@ bool fgInitSubsystems( void ) {
     FG_LOG( FG_GENERAL, FG_INFO, "Initialize Subsystems");
     FG_LOG( FG_GENERAL, FG_INFO, "========== ==========");
 
+    // Initialize the material property lib
+    FGPath mpath( current_options.get_fg_root() );
+    mpath.append( "materials" );
+    if ( material_lib.load( mpath.str() ) ) {
+    } else {
+    	FG_LOG( FG_GENERAL, FG_ALERT, "Error loading material lib!" );
+	exit(-1);
+    }
+
+    // Initialize the Scenery Management subsystem
+    if ( fgSceneryInit() ) {
+	// Material lib initialized ok.
+    } else {
+    	FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
+	exit(-1);
+    }
+
+    if ( global_tile_mgr.init() ) {
+	// Load the local scenery data
+	global_tile_mgr.update( current_options.get_lon(),
+				current_options.get_lat() );
+    } else {
+    	FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
+	exit(-1);
+    }
+
+    FG_LOG( FG_GENERAL, FG_DEBUG,
+    	    "Current terrain elevation after tile mgr init " <<
+	    scenery.cur_elev );
+
     if ( current_options.get_flight_model() == FGInterface::FG_LARCSIM ) {
 	cur_fdm_state = new FGLaRCsim;
     } else if ( current_options.get_flight_model() == FGInterface::FG_JSBSIM ) {
@@ -292,36 +319,6 @@ bool fgInitSubsystems( void ) {
     // set the initial position
     fgInitPosition();
 
-    // Initialize the material property lib
-    FGPath mpath( current_options.get_fg_root() );
-    mpath.append( "materials" );
-    if ( material_lib.load( mpath.str() ) ) {
-    } else {
-    	FG_LOG( FG_GENERAL, FG_ALERT, "Error loading material lib!" );
-	exit(-1);
-    }
-
-    // Initialize the Scenery Management subsystem
-    if ( fgSceneryInit() ) {
-	// Material lib initialized ok.
-    } else {
-    	FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
-	exit(-1);
-    }
-
-    if( global_tile_mgr.init() ) {
-	// Load the local scenery data
-	global_tile_mgr.update( cur_fdm_state->get_Longitude(),
-				cur_fdm_state->get_Latitude() );
-    } else {
-    	FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
-	exit(-1);
-    }
-
-    FG_LOG( FG_GENERAL, FG_DEBUG,
-    	    "Current terrain elevation after tile mgr init " <<
-	    scenery.cur_elev );
-
     // Calculate ground elevation at starting point (we didn't have
     // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
     //
@@ -613,15 +610,18 @@ void fgReInitSubsystems( void )
     if( !freeze )
         globals->set_freeze( true );
     
-    fgInitPosition();
     if( global_tile_mgr.init() ) {
 	// Load the local scenery data
-	global_tile_mgr.update( cur_fdm_state->get_Longitude(),
-				cur_fdm_state->get_Latitude() );
+	global_tile_mgr.update( current_options.get_lon(),
+				current_options.get_lat() );
     } else {
     	FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
 		exit(-1);
     }
+
+    // cout << "current scenery elev = " << scenery.cur_elev << endl;
+
+    fgInitPosition();
     fgFDMSetGroundElevation( current_options.get_flight_model(), 
 			     scenery.cur_elev );
 
diff --git a/src/Main/keyboard.cxx b/src/Main/keyboard.cxx
index ddf52d44b..dc52097a2 100644
--- a/src/Main/keyboard.cxx
+++ b/src/Main/keyboard.cxx
@@ -476,10 +476,11 @@ void GLUTspecialkey(int k, int x, int y) {
 		if ( !freeze ) 
 		    globals->set_freeze( true );
 		BusyCursor(0);
-		if( global_tile_mgr.init() ) {
+		if ( global_tile_mgr.init() ) {
 		    // Load the local scenery data
-		    global_tile_mgr.update( cur_fdm_state->get_Longitude(),
-					    cur_fdm_state->get_Latitude() );
+		    global_tile_mgr.update( 
+		        cur_fdm_state->get_Longitude() * RAD_TO_DEG,
+			cur_fdm_state->get_Latitude() * RAD_TO_DEG );
 		} else {
 		    FG_LOG( FG_GENERAL, FG_ALERT, 
 			    "Error in Tile Manager initialization!" );
diff --git a/src/Main/main.cxx b/src/Main/main.cxx
index bbac07348..1afd19c32 100644
--- a/src/Main/main.cxx
+++ b/src/Main/main.cxx
@@ -879,8 +879,8 @@ static void fgMainLoop( void ) {
 #endif
 
     // see if we need to load any new scenery tiles
-    global_tile_mgr.update( cur_fdm_state->get_Longitude(),
-			    cur_fdm_state->get_Latitude() );
+    global_tile_mgr.update( cur_fdm_state->get_Longitude() * RAD_TO_DEG,
+			    cur_fdm_state->get_Latitude() * RAD_TO_DEG );
 
     // Process/manage pending events
     global_events.Process();
diff --git a/src/Scenery/scenery.cxx b/src/Scenery/scenery.cxx
index e74a2ad24..7f27a782d 100644
--- a/src/Scenery/scenery.cxx
+++ b/src/Scenery/scenery.cxx
@@ -52,18 +52,11 @@ struct fgSCENERY scenery;
 
 // Initialize the Scenery Management system
 int fgSceneryInit( void ) {
-    fgOPTIONS *o;
-    // char path[1024], fgpath[1024];
-    // GLubyte *texbuf;
-    // int width, height;
-
-    o = &current_options;
-
     FG_LOG( FG_TERRAIN, FG_INFO, "Initializing scenery subsystem" );
 
     scenery.cur_elev = -9999;
 
-    return(1);
+    return 1;
 }
 
 
diff --git a/src/Scenery/tilecache.cxx b/src/Scenery/tilecache.cxx
index b0f8d9be7..1bca11ee3 100644
--- a/src/Scenery/tilecache.cxx
+++ b/src/Scenery/tilecache.cxx
@@ -114,7 +114,7 @@ FGTileCache::init( void )
     // and ... just in case we missed something ... 
     terrain->removeAllKids();
 
-    FG_LOG( FG_TERRAIN, FG_DEBUG, "  done with init()"  );
+    FG_LOG( FG_TERRAIN, FG_INFO, "  done with init()"  );
 }
 
 
diff --git a/src/Scenery/tilemgr.cxx b/src/Scenery/tilemgr.cxx
index 56290a48f..7949a9b94 100644
--- a/src/Scenery/tilemgr.cxx
+++ b/src/Scenery/tilemgr.cxx
@@ -39,7 +39,7 @@
 #include <simgear/math/polar3d.hxx>
 #include <simgear/math/vector.hxx>
 
-#include <Aircraft/aircraft.hxx>
+// #include <Aircraft/aircraft.hxx>
 #include <Main/options.hxx>
 #include <Main/views.hxx>
 #include <Objects/obj.hxx>
@@ -88,7 +88,7 @@ int FGTileMgr::init( void ) {
 
     if ( state != Start ) {
 	FG_LOG( FG_TERRAIN, FG_INFO,
-		"ReInitializing the Tile Manager subsystem." );
+		"... Reinitializing." );
 
 	// This is necessay to keep bookeeping straight for the
 	// tile_cache   -- which actually handles all the
@@ -102,7 +102,7 @@ int FGTileMgr::init( void ) {
 	}
     } else {
 	FG_LOG( FG_TERRAIN, FG_INFO,
-		"Initializing Tile Manager subsystem." );
+		"... First time through." );
     }
 
     global_tile_cache.init();
@@ -304,13 +304,12 @@ void FGTileMgr::scroll( void )
 }
 
 
-void FGTileMgr::initialize_queue( void )
+void FGTileMgr::initialize_queue()
 {
     // First time through or we have teleported, initialize the
     // system and load all relavant tiles
 
     FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << current_bucket );
-    FG_LOG( FG_TERRAIN, FG_INFO, "  First time through ... " );
     FG_LOG( FG_TERRAIN, FG_INFO, "  Updating Tile list for " << current_bucket );
     FG_LOG( FG_TERRAIN, FG_INFO, "  Loading " 
             << tile_diameter * tile_diameter << " tiles" );
@@ -328,12 +327,6 @@ void FGTileMgr::initialize_queue( void )
     // "rings"
 
     sched_tile( current_bucket );
-    Point3D geod_view_center( current_bucket.get_center_lon(), 
-                              current_bucket.get_center_lat(), 
-                              cur_fdm_state->get_Altitude()*FEET_TO_METER + 3 );
-
-    current_view.abs_view_pos = fgGeodToCart( geod_view_center );
-    current_view.view_pos = current_view.abs_view_pos - scenery.next_center;
 
     for ( i = 3; i <= tile_diameter; i = i + 2 ) {
         int j;
@@ -372,16 +365,19 @@ void FGTileMgr::initialize_queue( void )
 }
 
 
-// given the current lon/lat, fill in the array of local chunks.  If
-// the chunk isn't already in the cache, then read it from disk.
-int FGTileMgr::update( double junk1, double junk2 ) {
+// given the current lon/lat (in degrees), fill in the array of local
+// chunks.  If the chunk isn't already in the cache, then read it from
+// disk.
+int FGTileMgr::update( double lon, double lat ) {
     // FG_LOG( FG_TERRAIN, FG_DEBUG, "FGTileMgr::update()" );
 
-    FGInterface *f = current_aircraft.fdm_state;
+    // FGInterface *f = current_aircraft.fdm_state;
 
     // lonlat for this update 
-    longitude = f->get_Longitude() * RAD_TO_DEG;
-    latitude = f->get_Latitude() * RAD_TO_DEG;
+    // longitude = f->get_Longitude() * RAD_TO_DEG;
+    // latitude = f->get_Latitude() * RAD_TO_DEG;
+    longitude = lon;
+    latitude = lat;
     // FG_LOG( FG_TERRAIN, FG_DEBUG, "lon "<< lonlat[LON] <<
     //      " lat " << lonlat[LAT] );
 
@@ -395,7 +391,7 @@ int FGTileMgr::update( double junk1, double junk2 ) {
         current_tile = global_tile_cache.get_tile(tile_index);
         scenery.next_center = current_tile->center;
     } else {
-        FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found" );
+        FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found (Ok if initializing)" );
     }
 
     if ( state == Running ) {
@@ -438,7 +434,7 @@ int FGTileMgr::update( double junk1, double junk2 ) {
 	    scroll();
 	}
 
-    } else if ( (state == Start) || (state == Inited) ) {
+    } else if ( state == Start || state == Inited ) {
 	initialize_queue();
 	state = Running;
     }
@@ -451,19 +447,25 @@ int FGTileMgr::update( double junk1, double junk2 ) {
 	load_tile( pending.b, pending.cache_index );
     }
 
-    // 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);
+    if ( scenery.center == Point3D(0.0) ) {
+	// initializing
+	// cout << "initializing ... " << endl;
+	Point3D geod_pos = Point3D( longitude * DEG_TO_RAD,
+				    latitude * DEG_TO_RAD,
+				    0.0);
+	Point3D tmp_abs_view_pos = fgGeodToCart( geod_pos );
+	scenery.center = tmp_abs_view_pos;
+	// cout << "abs_view_pos = " << tmp_abs_view_pos << endl;
+	prep_ssg_nodes();
+	current_elev_ssg( tmp_abs_view_pos,
+			  Point3D( 0.0 ) );
+    } else {
+	// cout << "abs view pos = " << current_view.abs_view_pos
+	//      << " view pos = " << current_view.view_pos << endl;
+	current_elev_ssg( current_view.abs_view_pos,
+			  current_view.view_pos );
+    }
 
-    // cout << "current elevation (old) == " 
-    //      << current_elev( f->get_Longitude(), f->get_Latitude(), 
-    //                       tmp_abs_view_pos ) 
-    //      << endl;
-
-    // set scenery.cur_elev and scenery.cur_radius
-
-    current_elev_ssg( current_view.abs_view_pos,
-                      current_view.view_pos );
     // cout << "current elevation (ssg) == " << scenery.cur_elev << endl;
 
     previous_bucket = current_bucket;
@@ -483,6 +485,18 @@ void FGTileMgr::prep_ssg_nodes( void ) {
     FGTileEntry *t;
     float ranges[2];
     ranges[0] = 0.0f;
+    double vis = 0.0;
+
+#ifndef FG_OLD_WEATHER
+    if ( WeatherDatabase != NULL ) {
+	vis = WeatherDatabase->getWeatherVisibility();
+    } else {
+	vis = 16000;
+    }
+#else
+    vis = current_weather.get_visibility();
+#endif
+    // cout << "visibility = " << vis << endl;
 
     // traverse the potentially viewable tile list and update range
     // selector and transform
@@ -493,12 +507,7 @@ void FGTileMgr::prep_ssg_nodes( void ) {
 	    // set range selector (LOD trick) to be distance to center
 	    // of tile + bounding radius
 
-#ifndef FG_OLD_WEATHER
-            ranges[1] = WeatherDatabase->getWeatherVisibility()
-		+ t->bounding_radius;
-#else
-            ranges[1] = current_weather.get_visibility()+t->bounding_radius;
-#endif
+            ranges[1] = vis + t->bounding_radius;
             t->range_ptr->setRanges( ranges, 2 );
 
             // calculate tile offset
diff --git a/src/Scenery/tilemgr.hxx b/src/Scenery/tilemgr.hxx
index 0d616bdb4..b99760fb7 100644
--- a/src/Scenery/tilemgr.hxx
+++ b/src/Scenery/tilemgr.hxx
@@ -94,7 +94,7 @@ private:
     list < FGLoadRec > load_queue;
 
     // initialize the cache
-    void initialize_queue( void );
+    void initialize_queue();
 
     FGBucket BucketOffset( int dx, int dy );
 
@@ -143,10 +143,10 @@ public:
     // Initialize the Tile Manager subsystem
     int init( void );
 
-    // given the current lon/lat, fill in the array of local chunks.
-    // If the chunk isn't already in the cache, then read it from
-    // disk.
-    int update( double junk1, double junk2 );
+    // given the current lon/lat (in degrees), fill in the array of
+    // local chunks.  If the chunk isn't already in the cache, then
+    // read it from disk.
+    int update( double lon, double lat );
 
     // Determine scenery altitude.  Normally this just happens when we
     // render the scene, but we'd also like to be able to do this