]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/main.cxx
Use a pointer to a structure instead of a large number of variables when calling...
[flightgear.git] / src / Main / main.cxx
index 1ff665716e2ec52ebd0a8e04ba452a5703b47df4..c1ad6e9b399e7c65aa2400ca7537c22d766f8d86 100644 (file)
@@ -39,6 +39,9 @@ SG_USING_STD(endl);
 #include <simgear/misc/exception.hxx>
 #include <simgear/ephemeris/ephemeris.hxx>
 #include <simgear/route/route.hxx>
+#include <simgear/screen/extensions.hxx>
+
+#include <Environment/environment_mgr.hxx>
 
 #ifdef SG_MATH_EXCEPTION_CLASH
 #  include <math.h>
@@ -77,11 +80,16 @@ SG_USING_STD(endl);
 #include <simgear/math/polar3d.hxx>
 #include <simgear/math/sg_random.h>
 #include <simgear/misc/sg_path.hxx>
+#include <simgear/scene/material/matlib.hxx>
+#include <simgear/scene/model/animation.hxx>
+#include <simgear/scene/model/location.hxx>
+#include <simgear/scene/model/model.hxx>
+#include <simgear/scene/model/modellib.hxx>
 #ifdef FG_USE_CLOUDS_3D
-#  include <simgear/sky/clouds3d/SkySceneLoader.hpp>
-#  include <simgear/sky/clouds3d/SkyUtil.hpp>
+#  include <simgear/scene/sky/clouds3d/SkySceneLoader.hpp>
+#  include <simgear/scene/sky/clouds3d/SkyUtil.hpp>
 #endif
-#include <simgear/sky/sky.hxx>
+#include <simgear/scene/sky/sky.hxx>
 #include <simgear/timing/sg_time.hxx>
 
 #include <Include/general.hxx>
@@ -92,7 +100,6 @@ SG_USING_STD(endl);
 #include <ATC/ATCdisplay.hxx>
 #include <ATC/AIMgr.hxx>
 
-
 #include <Autopilot/newauto.hxx>
 
 #include <Cockpit/hud.hxx>
@@ -102,10 +109,7 @@ SG_USING_STD(endl);
 #include <FDM/UIUCModel/uiuc_aircraftdir.h>
 #include <GUI/gui.h>
 #include <Model/acmodel.hxx>
-#include <Model/loader.hxx>
-#include <Model/model.hxx>
 #include <Model/modelmgr.hxx>
-#include <Main/location.hxx>
 #include <Model/panelnode.hxx>
 #ifdef FG_NETWORK_OLK
 #include <NetworkOLK/network.h>
@@ -116,11 +120,10 @@ SG_USING_STD(endl);
 #include <MultiPlayer/multiplayrxmgr.hxx>
 #endif
 
-#include <Objects/matlib.hxx>
+#include <Replay/replay.hxx>
 #include <Scenery/scenery.hxx>
 #include <Scenery/tilemgr.hxx>
 #ifdef ENABLE_AUDIO_SUPPORT
-#  include <Sound/soundmgr.hxx>
 #  include <Sound/fg_fx.hxx>
 #  include <Sound/morse.hxx>
 #endif
@@ -141,25 +144,9 @@ SG_USING_STD(endl);
 
 #include "fg_commands.hxx"
 
-#ifdef FG_EXPERIMENTAL_POINT_LIGHTING
-#ifdef WIN32
-  typedef void (APIENTRY * PFNGLPOINTPARAMETERFEXTPROC)(GLenum pname,
-                                                        GLfloat param);
-  typedef void (APIENTRY * PFNGLPOINTPARAMETERFVEXTPROC)(GLenum pname,
-                                                         const GLfloat *params);
-
-  PFNGLPOINTPARAMETERFEXTPROC glPointParameterfEXT = 0;
-  PFNGLPOINTPARAMETERFVEXTPROC glPointParameterfvEXT = 0;
-#elif linux
-  #include <GL/glx.h>
-
-  typedef void (* OpenGLFuncExt)(GLenum pname, GLfloat param);
-  typedef void (* OpenGLFuncExtv)(GLenum pname, const GLfloat *params);
-
-  OpenGLFuncExt glPointParameterfEXT = 0;
-  OpenGLFuncExtv glPointParameterfvEXT = 0;
-#endif
-#endif
+glPointParameterfProc glPointParameterfPtr = 0;
+glPointParameterfvProc glPointParameterfvPtr = 0;
+bool glPointParameterIsSupported = false;
 
 float default_attenuation[3] = {1.0, 0.0, 0.0};
 //Required for using GL_extensions
@@ -179,13 +166,11 @@ sgVec3 rway_ols;
 float scene_nearplane = 0.5f;
 float scene_farplane = 120000.0f;
 
-static double delta_time_sec = 0;
+static double delta_time_sec = 0.0;
 
 
 #ifdef FG_WEATHERCM
 #  include <WeatherCM/FGLocalWeatherDatabase.h>
-#else
-#  include <Environment/environment_mgr.hxx>
 #endif
 
 #include "version.h"
@@ -438,6 +423,10 @@ void trRenderFrame( void ) {
 
 // Update all Visuals (redraws anything graphics related)
 void fgRenderFrame() {
+    bool draw_otw = fgGetBool("/sim/rendering/draw-otw");
+    bool skyblend = fgGetBool("/sim/rendering/skyblend");
+    bool enhanced_lighting = fgGetBool("/sim/rendering/enhanced-lighting");
+    bool distance_attenuation = fgGetBool("/sim/rendering/distance-attenuation");
 
     GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
     GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
@@ -445,12 +434,12 @@ void fgRenderFrame() {
     // Process/manage pending events
     global_events.update( delta_time_sec );
 
-    static const SGPropertyNode *longitude
-        = fgGetNode("/position/longitude-deg");
-    static const SGPropertyNode *latitude
-        = fgGetNode("/position/latitude-deg");
-    static const SGPropertyNode *altitude
-        = fgGetNode("/position/altitude-ft");
+    // static const SGPropertyNode *longitude
+    //     = fgGetNode("/position/longitude-deg");
+    // static const SGPropertyNode *latitude
+    //     = fgGetNode("/position/latitude-deg");
+    // static const SGPropertyNode *altitude
+    //     = fgGetNode("/position/altitude-ft");
     static const SGPropertyNode *groundlevel_nearplane
         = fgGetNode("/sim/current-view/ground-level-nearplane-m");
 
@@ -497,6 +486,7 @@ void fgRenderFrame() {
         }
         // Keep resetting sim time while the sim is initializing
         globals->set_sim_time_sec( 0.0 );
+        SGAnimation::set_sim_time_sec( 0.0 );
     } else {
         // idle_state is now 1000 meaning we've finished all our
         // initializations and are running the main loop, so this will
@@ -507,7 +497,7 @@ void fgRenderFrame() {
 
         // update view port
         fgReshape( fgGetInt("/sim/startup/xsize"),
-               fgGetInt("/sim/startup/ysize") );
+                   fgGetInt("/sim/startup/ysize") );
 
         if ( fgGetBool("/sim/rendering/clouds3d") ) {
             glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
@@ -535,7 +525,7 @@ void fgRenderFrame() {
             clear_mask |= GL_COLOR_BUFFER_BIT;
         }
 
-        if ( fgGetBool("/sim/rendering/skyblend") ) {
+        if ( skyblend ) {
             if ( fgGetBool("/sim/rendering/textures") ) {
             // glClearColor(black[0], black[1], black[2], black[3]);
             glClearColor(l->adj_fog_color[0], l->adj_fog_color[1],
@@ -567,14 +557,14 @@ void fgRenderFrame() {
         thesky->set_visibility(visibility_meters);
 
         thesky->modify_vis( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER,
-                        ( global_multi_loop * fgGetInt("/sim/speed-up") )
+                            ( global_multi_loop * fgGetInt("/sim/speed-up") )
                             / (double)fgGetInt("/sim/model-hz") );
 
         // Set correct opengl fog density
         glFogf (GL_FOG_DENSITY, fog_exp2_density);
 
         // update the sky dome
-        if ( fgGetBool("/sim/rendering/skyblend") ) {
+        if ( skyblend ) {
             /*
              SG_LOG( SG_GENERAL, SG_BULK, "thesky->repaint() sky_color = "
              << cur_light_params.sky_color[0] << " "
@@ -591,37 +581,19 @@ void fgRenderFrame() {
              << "    moon_angle = " << cur_light_params.moon_angle );
             */
 
-            // Some insane defaults.
-            static int init = 0;
-            static double sun_pos_angle = 9999.0;
-            static double sun_pos_rotation = 9999.0;
-            static double lat_curr = 9999.0;
-            static double long_curr = 9999.0;
+            static SGSkyColor scolor;
 
-            if ((fabs(sun_pos_rotation - cur_light_params.sun_rotation) > 5e-3)
-                || (fabs(sun_pos_angle - cur_light_params.sun_angle) > 5e-3)
-                || (init < 100)
-#if 0
-                || (fabs(lat_curr - current__view->getLongitude_deg()) > 0.5)
-                || (fabs(long_curr - current__view->getLatitude_deg()) > 0.5))
-#else
-                )
-#endif
-            {
-                if (init < 200)
-                   init++;
-
-                sun_pos_angle = cur_light_params.sun_angle;
-                sun_pos_rotation = cur_light_params.sun_rotation;
+            scolor.sky_color   = cur_light_params.sky_color;
+            scolor.fog_color   = cur_light_params.adj_fog_color;
+            scolor.cloud_color = cur_light_params.cloud_color;
+            scolor.sun_angle   = cur_light_params.sun_angle;
+            scolor.moon_angle  = cur_light_params.moon_angle;
+            scolor.nplanets    = globals->get_ephem()->getNumPlanets();
+            scolor.nstars      = globals->get_ephem()->getNumStars();
+            scolor.planet_data = globals->get_ephem()->getPlanets();
+            scolor.star_data   = globals->get_ephem()->getStars();
 
-                thesky->repaint( cur_light_params.sky_color,
-                             cur_light_params.adj_fog_color,
-                             cur_light_params.sun_angle,
-                             cur_light_params.moon_angle,
-                             globals->get_ephem()->getNumPlanets(),
-                             globals->get_ephem()->getPlanets(),
-                             globals->get_ephem()->getNumStars(),
-                             globals->get_ephem()->getStars() );
+            thesky->repaint( scolor );
 
             /*
             SG_LOG( SG_GENERAL, SG_BULK,
@@ -642,26 +614,40 @@ void fgRenderFrame() {
               << " moon dec = " << globals->get_ephem()->getMoonDeclination() );
             */
 
-                lat_curr = current__view->getLongitude_deg();
-                long_curr = current__view->getLatitude_deg();
-                thesky->reposition( current__view->get_view_pos(),
-                            current__view->get_zero_elev(),
-                            current__view->get_world_up(),
-                            current__view->getLongitude_deg()
-                               * SGD_DEGREES_TO_RADIANS,
-                            current__view->getLatitude_deg()
-                               * SGD_DEGREES_TO_RADIANS,
-                            current__view->getAltitudeASL_ft()
-                               * SG_FEET_TO_METER,
-                            cur_light_params.sun_rotation,
-                            globals->get_time_params()->getGst(),
-                            globals->get_ephem()->getSunRightAscension(),
-                            globals->get_ephem()->getSunDeclination(),
-                            50000.0,
-                            globals->get_ephem()->getMoonRightAscension(),
-                            globals->get_ephem()->getMoonDeclination(),
-                            50000.0 );
+            // The sun and moon distances are scaled down versions
+            // of the actual distance to get both the moon and the sun
+            // within the range of the far clip plane.
+            // Moon distance:    384,467 kilometers
+            // Sun distance: 150,000,000 kilometers
+            double sun_horiz_eff, moon_horiz_eff;
+            if (fgGetBool("/sim/rendering/horizon-effect")) {
+            sun_horiz_eff = 0.67+pow(0.5+cos(cur_light_params.sun_angle*2)/2,0.33)/3;
+            moon_horiz_eff = 0.67+pow(0.5+cos(cur_light_params.moon_angle*2)/2,0.33)/3;
+            } else {
+               sun_horiz_eff = moon_horiz_eff = 1.0;
             }
+
+            static SGSkyState sstate;
+
+            sstate.view_pos  = current__view->get_view_pos();
+            sstate.zero_elev = current__view->get_zero_elev();
+            sstate.view_up   = current__view->get_world_up();
+            sstate.lon       = current__view->getLongitude_deg()
+                                * SGD_DEGREES_TO_RADIANS;
+            sstate.lat       = current__view->getLatitude_deg()
+                                * SGD_DEGREES_TO_RADIANS;
+            sstate.alt       = current__view->getAltitudeASL_ft()
+                                * SG_FEET_TO_METER;
+            sstate.spin      = cur_light_params.sun_rotation;
+            sstate.gst       = globals->get_time_params()->getGst();
+            sstate.sun_ra    = globals->get_ephem()->getSunRightAscension();
+            sstate.sun_dec   = globals->get_ephem()->getSunDeclination();
+            sstate.sun_dist  = 50000.0 * sun_horiz_eff;
+            sstate.moon_ra   = globals->get_ephem()->getMoonRightAscension();
+            sstate.moon_dec  = globals->get_ephem()->getMoonDeclination();
+            sstate.moon_dist = 40000.0 * moon_horiz_eff;
+
+            thesky->reposition( sstate );
         }
 
         glEnable( GL_DEPTH_TEST );
@@ -692,7 +678,7 @@ void fgRenderFrame() {
         // glMatrixMode( GL_PROJECTION );
         // glLoadIdentity();
         ssgSetFOV( current__view->get_h_fov(),
-               current__view->get_v_fov() );
+                   current__view->get_v_fov() );
 
         double agl =
             current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER
@@ -739,8 +725,7 @@ void fgRenderFrame() {
         globals->get_multiplayer_rx_mgr()->Update();
 #endif
 
-        if ( fgGetBool("/sim/rendering/skyblend") &&
-             fgGetBool("/sim/rendering/draw-otw") )
+        if ( draw_otw && skyblend )
         {
             // draw the sky backdrop
 
@@ -762,7 +747,7 @@ void fgRenderFrame() {
             // draw wire frame
             glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
         }
-        if ( fgGetBool("/sim/rendering/draw-otw") ) {
+        if ( draw_otw ) {
             ssgCullAndDraw( globals->get_scenery()->get_scene_graph() );
         }
 
@@ -792,23 +777,27 @@ void fgRenderFrame() {
         glFogf (GL_FOG_DENSITY, rwy_exp2_punch_through);
         ssgSetNearFar( scene_nearplane, scene_farplane );
 
-#ifdef FG_EXPERIMENTAL_POINT_LIGHTING
-        // Enable states for drawing points with GL_extension
-        glEnable(GL_POINT_SMOOTH);
+        if ( enhanced_lighting ) {
 
-        if ( fgGetBool("/sim/rendering/distance-attenuation")
-             && glutExtensionSupported("GL_EXT_point_parameters") )
-        {
-            float quadratic[3] = {1.0, 0.001, 0.0000001};
-            // makes the points fade as they move away
-            glPointParameterfvEXT(GL_DISTANCE_ATTENUATION_EXT, quadratic);
-            glPointParameterfEXT(GL_POINT_SIZE_MIN_EXT, 1.0); 
-        }
-        glPointSize(4.0);
+            // Enable states for drawing points with GL_extension
+            glEnable(GL_POINT_SMOOTH);
 
-        // blending function for runway lights
-        glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) ;
-#endif
+            if ( distance_attenuation && glPointParameterIsSupported )
+            {
+                // Enable states for drawing points with GL_extension
+                glEnable(GL_POINT_SMOOTH);
+
+                float quadratic[3] = {1.0, 0.001, 0.0000001};
+                // makes the points fade as they move away
+                glPointParameterfvPtr(GL_DISTANCE_ATTENUATION_EXT, quadratic);
+                glPointParameterfPtr(GL_POINT_SIZE_MIN_EXT, 1.0); 
+            }
+
+            glPointSize(4.0);
+
+            // blending function for runway lights
+            glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) ;
+        }
 
         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
         glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
@@ -817,7 +806,7 @@ void fgRenderFrame() {
         glPolygonMode(GL_FRONT, GL_POINT);
 
         // draw runway lighting
-        if ( fgGetBool("/sim/rendering/draw-otw") ) {
+        if ( draw_otw ) {
             ssgCullAndDraw( globals->get_scenery()->get_rwy_lights_root() );
         }
 
@@ -826,7 +815,7 @@ void fgRenderFrame() {
         // sgVec3 taxi_fog;
         // sgSetVec3( taxi_fog, 0.0, 0.0, 0.0 );
         // glFogfv ( GL_FOG_COLOR, taxi_fog );
-        if ( fgGetBool("/sim/rendering/draw-otw") ) {
+        if ( draw_otw ) {
             ssgCullAndDraw( globals->get_scenery()->get_taxi_lights_root() );
         }
 
@@ -845,36 +834,33 @@ void fgRenderFrame() {
         //_frame_count++;
 
 
-#ifdef FG_EXPERIMENTAL_POINT_LIGHTING
-        if ( fgGetBool("/sim/rendering/distance-attenuation")
-             && glutExtensionSupported("GL_EXT_point_parameters") )
-        {
-            glPointParameterfvEXT(GL_DISTANCE_ATTENUATION_EXT,
-                                  default_attenuation);
-        }
+        if ( enhanced_lighting ) {
+            if ( distance_attenuation && glPointParameterIsSupported )
+            {
+                glPointParameterfvPtr(GL_DISTANCE_ATTENUATION_EXT,
+                                      default_attenuation);
+            }
 
-        glPointSize(1.0);
-        glDisable(GL_POINT_SMOOTH);
-#endif
+            glPointSize(1.0);
+            glDisable(GL_POINT_SMOOTH);
+        }
 
         // draw ground lighting
         glFogf (GL_FOG_DENSITY, ground_exp2_punch_through);
-        if ( fgGetBool("/sim/rendering/draw-otw") ) {
+        if ( draw_otw ) {
             ssgCullAndDraw( globals->get_scenery()->get_gnd_lights_root() );
         }
 
-        if ( fgGetBool("/sim/rendering/skyblend") ) {
+        if ( skyblend ) {
             // draw the sky cloud layers
-            if ( fgGetBool("/environment/clouds/status") &&
-                 fgGetBool("/sim/rendering/draw-otw") )
+            if ( draw_otw && fgGetBool("/environment/clouds/status") )
             {
                 thesky->postDraw( cur_fdm_state->get_Altitude()
                                   * SG_FEET_TO_METER );
             }
         }
 
-        if ( fgGetBool("/sim/rendering/clouds3d") &&
-             fgGetBool("/sim/rendering/draw-otw") )
+        if ( draw_otw && fgGetBool("/sim/rendering/clouds3d") )
         {
             glDisable( GL_FOG );
             glDisable( GL_LIGHTING );
@@ -901,7 +887,7 @@ void fgRenderFrame() {
             glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
         }
 
-        if ( fgGetBool("/sim/rendering/draw-otw") ) {
+        if ( draw_otw ) {
             globals->get_model_mgr()->draw();
             globals->get_aircraft_model()->draw();
         }
@@ -957,6 +943,13 @@ void fgRenderFrame() {
 void fgUpdateTimeDepCalcs() {
     static bool inited = false;
 
+    static const SGPropertyNode *replay_master
+        = fgGetNode( "/sim/freeze/replay", true );
+    static SGPropertyNode *replay_time
+        = fgGetNode( "/sim/replay/time", true );
+    static const SGPropertyNode *replay_end_time
+        = fgGetNode( "/sim/replay/end-time", true );
+
     //SG_LOG(SG_FLIGHT,SG_INFO, "Updating time dep calcs()");
 
     fgLIGHT *l = &cur_light_params;
@@ -978,23 +971,27 @@ void fgUpdateTimeDepCalcs() {
         cur_fdm_state->bind();
     }
 
-#ifndef FG_WEATHERCM
-    globals->get_environment_mgr()->update(delta_time_sec);
-#endif
-
     // conceptually, the following block could be done for each fdm
     // instance ...
-    if ( !cur_fdm_state->get_inited() ) {
-        // do nothing, fdm isn't inited yet
-    } else {
+    if ( cur_fdm_state->get_inited() ) {
         // we have been inited, and  we are good to go ...
 
         if ( !inited ) {
             inited = true;
         }
 
-        globals->get_autopilot()->update(delta_time_sec);
-        cur_fdm_state->update(delta_time_sec);
+        if ( ! replay_master->getBoolValue() ) {
+            globals->get_autopilot()->update( delta_time_sec );
+            cur_fdm_state->update( delta_time_sec );
+        } else {
+            FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
+            r->replay( replay_time->getDoubleValue() );
+            replay_time->setDoubleValue( replay_time->getDoubleValue()
+                                         + ( delta_time_sec
+                                             * fgGetInt("/sim/speed-up") ) );
+        }
+    } else {
+        // do nothing, fdm isn't inited yet
     }
 
     globals->get_model_mgr()->update(delta_time_sec);
@@ -1027,6 +1024,7 @@ static const double alt_adjust_m = alt_adjust_ft * SG_FEET_TO_METER;
 // What should we do when we have nothing else to do?  Let's get ready
 // for the next move and update the display?
 static void fgMainLoop( void ) {
+    int model_hz = fgGetInt("/sim/model-hz");
 
     static const SGPropertyNode *longitude
         = fgGetNode("/position/longitude-deg");
@@ -1038,6 +1036,8 @@ static void fgMainLoop( void ) {
         = fgGetNode("/sim/freeze/clock", true);
     static const SGPropertyNode *cur_time_override
         = fgGetNode("/sim/time/cur-time-override", true);
+    static const SGPropertyNode *replay_master
+        = fgGetNode("/sim/freeze/replay", true);
 
     // Update the elapsed time.
     static bool first_time = true;
@@ -1045,12 +1045,27 @@ static void fgMainLoop( void ) {
         last_time_stamp.stamp();
         first_time = false;
     }
-    current_time_stamp.stamp();
+
+    double throttle_hz = fgGetDouble("/sim/frame-rate-throttle-hz", 0.0);
+    if ( throttle_hz > 0.0 ) {
+        // simple frame rate throttle
+        double dt = 1000000.0 / throttle_hz;
+        current_time_stamp.stamp();
+        while ( current_time_stamp - last_time_stamp < dt ) {
+            current_time_stamp.stamp();
+        }
+    } else {
+        // run as fast as the app will go
+        current_time_stamp.stamp();
+    }
+
     delta_time_sec = double(current_time_stamp - last_time_stamp) / 1000000.0;
-    if (clock_freeze->getBoolValue())
+    if ( clock_freeze->getBoolValue() ) {
         delta_time_sec = 0;
+    } 
     last_time_stamp = current_time_stamp;
     globals->inc_sim_time_sec( delta_time_sec );
+    SGAnimation::set_sim_time_sec( globals->get_sim_time_sec() );
 
     static long remainder = 0;
     long elapsed;
@@ -1065,10 +1080,11 @@ static void fgMainLoop( void ) {
     SGTime *t = globals->get_time_params();
 
     sglog().setLogLevels( SG_ALL, (sgDebugPriority)fgGetInt("/sim/log-level") );
+    sglog().setLogLevels( SG_ALL, SG_INFO );
 
-    FGLocation * acmodel_location = 0;
+    SGLocation * acmodel_location = 0;
     if(cur_fdm_state->getACModel() != 0) {
-      acmodel_location = (FGLocation *)  cur_fdm_state->getACModel()->get3DModel()->getFGLocation();
+      acmodel_location = (SGLocation *)  cur_fdm_state->getACModel()->get3DModel()->getSGLocation();
     }
 
     SG_LOG( SG_ALL, SG_DEBUG, "Running Main Loop");
@@ -1104,15 +1120,20 @@ static void fgMainLoop( void ) {
            cur_fdm_state->get_Runway_altitude() * SG_FEET_TO_METER,
            cur_fdm_state->get_Altitude() * SG_FEET_TO_METER); */
 
-// Curt is this code used?  I don't see any problems when I comment it out.
+#if 0
+// This code is used by LaRCsim/UIUC to position the aircraft at a proper
+// place. This code should eventually go into the LaRCsim directory.
+// Commented out at Jul 27 2003 because tests show it is not needed.
+// Can probbaly be safely removed after Spet. 2003 - EMH
+
     if ( acmodel_location != 0 ) {
       if ( acmodel_location->get_cur_elev_m() > -9990 && cur_fdm_state->get_inited() ) {
         if ( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER < 
-             (acmodel_location->get_cur_elev_m() + alt_adjust_m - 3.0) ) {
+             (acmodel_location->get_cur_elev_m() + alt_adjust_m - 130.0) ) {
             // now set aircraft altitude above ground
             printf("(*) Current Altitude = %.2f < %.2f forcing to %.2f\n", 
                cur_fdm_state->get_Altitude() * SG_FEET_TO_METER,
-               acmodel_location->get_cur_elev_m() + alt_adjust_m - 3.0,
+               acmodel_location->get_cur_elev_m() + alt_adjust_m - 130.0,
                acmodel_location->get_cur_elev_m() + alt_adjust_m );
             cur_fdm_state->set_Altitude( (acmodel_location->get_cur_elev_m() 
                                                 + alt_adjust_m) * SG_METER_TO_FEET );
@@ -1124,6 +1145,7 @@ static void fgMainLoop( void ) {
       }
     }
 // End of code in question. (see Curt is this code used? above)
+#endif
 
     /* printf("Adjustment - ground = %.2f  runway = %.2f  alt = %.2f\n",
            scenery.get_cur_elev(),
@@ -1225,18 +1247,16 @@ static void fgMainLoop( void ) {
     // Calculate model iterations needed for next frame
     elapsed += remainder;
 
-    global_multi_loop = (long)(((double)elapsed * 0.000001) * 
-                          fgGetInt("/sim/model-hz"));
-    remainder = elapsed - ( (global_multi_loop*1000000) / 
-                        fgGetInt("/sim/model-hz") );
+    global_multi_loop = (long)(((double)elapsed * 0.000001) * model_hz );
+    remainder = elapsed - ( (global_multi_loop*1000000) / model_hz );
     SG_LOG( SG_ALL, SG_DEBUG, 
             "Model iterations needed = " << global_multi_loop
             << ", new remainder = " << remainder );
         
     // chop max interations to something reasonable if the sim was
     // delayed for an excesive amount of time
-    if ( global_multi_loop > 2.0 * fgGetInt("/sim/model-hz") ) {
-        global_multi_loop = (int)(2.0 * fgGetInt("/sim/model-hz") );
+    if ( global_multi_loop > 2.0 * model_hz ) {
+        global_multi_loop = (int)(2.0 * model_hz );
         remainder = 0;
     }
 
@@ -1252,7 +1272,7 @@ static void fgMainLoop( void ) {
     globals->get_io()->update( delta_time_sec );
 
     // see if we need to load any deferred-load textures
-    material_lib.load_next_deferred();
+    globals->get_matlib()->load_next_deferred();
 
     // Run audio scheduler
 #ifdef ENABLE_AUDIO_SUPPORT
@@ -1274,14 +1294,14 @@ static void fgMainLoop( void ) {
 
     // update tile manager for FDM...
     // ...only if location is different than the viewer (to avoid duplicating effort)
-    if( acmodel_location != current_view->getFGLocation() ) {
+    if( acmodel_location != current_view->getSGLocation() ) {
       if( acmodel_location != 0 ) {
         globals->get_tile_mgr()->prep_ssg_nodes( acmodel_location,
                                                  visibility_meters );
         globals->get_tile_mgr()->
             update( acmodel_location, visibility_meters,
-                    acmodel_location->get_absolute_view_pos() );
-        // save results of update in FGLocation for fdm...
+                    acmodel_location->get_absolute_view_pos(globals->get_scenery()->get_center()) );
+        // save results of update in SGLocation for fdm...
         if ( globals->get_scenery()->get_cur_elev() > -9990 ) {
           acmodel_location->
               set_cur_elev_m( globals->get_scenery()->get_cur_elev() );
@@ -1291,24 +1311,24 @@ static void fgMainLoop( void ) {
       }
     }
 
-    globals->get_tile_mgr()->prep_ssg_nodes( current_view->getFGLocation(),
+    globals->get_tile_mgr()->prep_ssg_nodes( current_view->getSGLocation(),
                                              visibility_meters );
     // update tile manager for view...
     // IMPORTANT!!! the tilemgr update for view location _must_ be done last 
     // after the FDM's until all of Flight Gear code references the viewer's location
     // for elevation instead of the "scenery's" current elevation.
-    FGLocation *view_location = globals->get_current_view()->getFGLocation();
+    SGLocation *view_location = globals->get_current_view()->getSGLocation();
     globals->get_tile_mgr()->update( view_location, visibility_meters,
                                      current_view->get_absolute_view_pos() );
-    // save results of update in FGLocation for fdm...
+    // save results of update in SGLocation for fdm...
     if ( globals->get_scenery()->get_cur_elev() > -9990 ) {
-      current_view->getFGLocation()->set_cur_elev_m( globals->get_scenery()->get_cur_elev() );
+      current_view->getSGLocation()->set_cur_elev_m( globals->get_scenery()->get_cur_elev() );
     }
-    current_view->getFGLocation()->set_tile_center( globals->get_scenery()->get_next_center() );
+    current_view->getSGLocation()->set_tile_center( globals->get_scenery()->get_next_center() );
 
     // If fdm location is same as viewer's then we didn't do the update for fdm location 
-    //   above so we need to save the viewer results in the fdm FGLocation as well...
-    if( acmodel_location == current_view->getFGLocation() ) {
+    //   above so we need to save the viewer results in the fdm SGLocation as well...
+    if( acmodel_location == current_view->getSGLocation() ) {
       if( acmodel_location != 0 ) {
         if ( globals->get_scenery()->get_cur_elev() > -9990 ) {
           acmodel_location->set_cur_elev_m( globals->get_scenery()->get_cur_elev() );
@@ -1592,10 +1612,6 @@ static bool fgMainInit( int argc, char **argv ) {
 
     globals = new FGGlobals;
 
-#ifndef FG_WEATHERCM
-    globals->set_environment_mgr(new FGEnvironmentMgr);
-#endif
-
     // seed the random number generater
     sg_srandom_time();
 
@@ -1613,7 +1629,7 @@ static bool fgMainInit( int argc, char **argv ) {
     fgInitFGRoot(argc, argv);
 
     // Check for the correct base package version
-    static char required_version[] = "0.9.1";
+    static char required_version[] = "0.9.2";
     string base_version = fgBasePackageVersion();
     if ( !(base_version == required_version) ) {
         // tell the operator how to use this application
@@ -1644,7 +1660,7 @@ static bool fgMainInit( int argc, char **argv ) {
 
     // Initialize the various GLUT Event Handlers.
     if( !fgGlutInitEvents() ) {
- SG_LOG( SG_GENERAL, SG_ALERT, 
       SG_LOG( SG_GENERAL, SG_ALERT, 
                "GLUT event handler initialization failed ..." );
         exit(-1);
     }
@@ -1668,23 +1684,25 @@ static bool fgMainInit( int argc, char **argv ) {
     glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, -0.5 ) ;
 #endif
 
-#ifdef FG_EXPERIMENTAL_POINT_LIGHTING
             // get the address of our OpenGL extensions
     if ( fgGetBool("/sim/rendering/distance-attenuation") )
     {
-#ifdef WIN32
-        glPointParameterfEXT = (PFNGLPOINTPARAMETERFEXTPROC) 
-            wglGetProcAddress("glPointParameterfEXT");
-        glPointParameterfvEXT = (PFNGLPOINTPARAMETERFVEXTPROC) 
-            wglGetProcAddress("glPointParameterfvEXT");
-#elif linux
-        glPointParameterfEXT = (OpenGLFuncExt) 
-            glXGetProcAddressARB((GLubyte *)"glPointParameterfEXT");
-        glPointParameterfvEXT = (OpenGLFuncExtv) 
-            glXGetProcAddressARB((GLubyte *)"glPointParameterfvEXT");
-#endif
+        if (SGIsOpenGLExtensionSupported("GL_EXT_point_parameters") ) {
+            glPointParameterIsSupported = true;
+            glPointParameterfPtr = (glPointParameterfProc)
+                                   SGLookupFunction("glPointParameterfEXT");
+            glPointParameterfvPtr = (glPointParameterfvProc)
+                                    SGLookupFunction("glPointParameterfvEXT");
+
+        } else if ( SGIsOpenGLExtensionSupported("GL_ARB_point_parameters") ) {
+            glPointParameterIsSupported = true;
+            glPointParameterfPtr = (glPointParameterfProc)
+                                   SGLookupFunction("glPointParameterfARB");
+            glPointParameterfvPtr = (glPointParameterfvProc)
+                                    SGLookupFunction("glPointParameterfvARB");
+        } else
+            glPointParameterIsSupported = false;
    }
-#endif
 
     // based on the requested presets, calculate the true starting
     // lon, lat
@@ -1704,33 +1722,37 @@ static bool fgMainInit( int argc, char **argv ) {
     SGPath modelpath( globals->get_fg_root() );
     ssgModelPath( (char *)modelpath.c_str() );
 
-    // Initialize the global scenery manager
-    globals->set_scenery( new FGScenery );
-    globals->get_scenery()->init();
-    globals->get_scenery()->bind();
-
-    // Initialize the global tile manager
-    globals->set_tile_mgr( new FGTileMgr );
-
     ////////////////////////////////////////////////////////////////////
     // Initialize the property-based built-in commands
     ////////////////////////////////////////////////////////////////////
     fgInitCommands();
 
     ////////////////////////////////////////////////////////////////////
-    // Initialize the general model subsystem.
+    // Initialize the material manager
     ////////////////////////////////////////////////////////////////////
+    globals->set_matlib( new SGMaterialLib );
+
+    globals->set_model_lib(new SGModelLib);
 
-    globals->set_model_loader(new FGModelLoader);
-    globals->set_texture_loader(new FGTextureLoader);
+    ////////////////////////////////////////////////////////////////////
+    // Initialize the TG scenery subsystem.
+    ////////////////////////////////////////////////////////////////////
+    globals->set_scenery( new FGScenery );
+    globals->get_scenery()->init();
+    globals->get_scenery()->bind();
+    globals->set_tile_mgr( new FGTileMgr );
+
+    ////////////////////////////////////////////////////////////////////
+    // Initialize the general model subsystem.
+    ////////////////////////////////////////////////////////////////////
     globals->set_model_mgr(new FGModelMgr);
     globals->get_model_mgr()->init();
     globals->get_model_mgr()->bind();
 
     ////////////////////////////////////////////////////////////////////
-    // Initialize the 3D aircraft model subsystem.
+    // Initialize the 3D aircraft model subsystem (has a dependency on
+    // the scenery subsystem.)
     ////////////////////////////////////////////////////////////////////
-
     globals->set_aircraft_model(new FGAircraftModel);
     globals->get_aircraft_model()->init();
     globals->get_aircraft_model()->bind();
@@ -1738,7 +1760,6 @@ static bool fgMainInit( int argc, char **argv ) {
     ////////////////////////////////////////////////////////////////////
     // Initialize the view manager subsystem.
     ////////////////////////////////////////////////////////////////////
-
     FGViewMgr *viewmgr = new FGViewMgr;
     globals->set_viewmgr( viewmgr );
     viewmgr->init();
@@ -1770,16 +1791,37 @@ static bool fgMainInit( int argc, char **argv ) {
     sky_tex_path.append( "Sky" );
     thesky->texture_path( sky_tex_path.str() );
 
-    thesky->build( 550.0, 550.0,
-               globals->get_ephem()->getNumPlanets(), 
-               globals->get_ephem()->getPlanets(), 60000.0,
-               globals->get_ephem()->getNumStars(),
-               globals->get_ephem()->getStars(), 60000.0 );
+    // The sun and moon diameters are scaled down numbers of the
+    // actual diameters. This was needed to fit bot the sun and the
+    // moon within the distance to the far clip plane.
+    // Moon diameter:    3,476 kilometers
+    // Sun diameter: 1,390,000 kilometers
+    thesky->build( 80000.0, 80000.0,
+                   463.3, 361.8,
+                   globals->get_ephem()->getNumPlanets(), 
+                   globals->get_ephem()->getPlanets(),
+                   globals->get_ephem()->getNumStars(),
+                   globals->get_ephem()->getStars() );
 
     // Initialize MagVar model
     SGMagVar *magvar = new SGMagVar();
     globals->set_mag( magvar );
 
+
+                                // kludge to initialize mag compass
+                                // (should only be done for in-flight
+                                // startup)
+    // update magvar model
+    globals->get_mag()->update( fgGetDouble("/position/longitude-deg")
+                                * SGD_DEGREES_TO_RADIANS,
+                                fgGetDouble("/position/latitude-deg")
+                                * SGD_DEGREES_TO_RADIANS,
+                                fgGetDouble("/position/altitude-ft")
+                                * SG_FEET_TO_METER,
+                                globals->get_time_params()->getJD() );
+    double var = globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES;
+    fgSetDouble("/instrumentation/heading-indicator/offset-deg", -var);
+
     // airport = new ssgBranch;
     // airport->setName( "Airport Lighting" );
     // lighting->addKid( airport );
@@ -1958,7 +2000,7 @@ void fgLoadDCS(void) {
 
                 if ( strcmp(obj_filename,"repeat") != 0) {
                     ship_obj =
-                        globals->get_model_loader()->load_model( obj_filename );
+                      globals->get_model_lib()->load_model( globals->get_fg_root(), obj_filename, globals->get_props(), globals->get_sim_time_sec() );
                 }
       
                 if ( ship_obj != NULL ) {
@@ -1979,11 +2021,11 @@ void fgLoadDCS(void) {
                             // temporary hack for deck lights - ultimately should move to PLib (when??)
                             //const char *extn = file_extension ( obj_filename ) ;
                             if ( objc == 1 ){
-                                ssgVertexArray *lights = new ssgVertexArray( 100 );
+                                // ssgVertexArray *lights = new ssgVertexArray( 100 );
                                ssgVertexArray *lightpoints = new ssgVertexArray( 100 );
                                ssgVertexArray *lightnormals = new ssgVertexArray( 100 );
                                ssgVertexArray *lightdir = new ssgVertexArray( 100 );
-                               int ltype[500], light_type;
+                               int ltype[500], light_type = 0;
                                static int ltcount = 0;
                                 string token;
                                sgVec3 rway_dir,rway_normal,lightpt;
@@ -2048,7 +2090,8 @@ void fgLoadDCS(void) {
                                                        if (in1.eof()) break;
                                                } 
                                        }  //while
-        
+
+#if 0        
                                        if ( lightpoints->getNum() ) {
                                                ssgBranch *lightpoints_branch;
                                                long int dummy = -999;
@@ -2067,6 +2110,7 @@ void fgLoadDCS(void) {
                                                lightpoints_transform->ref();
                                                globals->get_scenery()->get_gnd_lights_root()->addKid( lightpoints_transform );
                                        } 
+#endif
                                } //if in1 
                 } //if objc
                             // end hack for deck lights
@@ -2194,7 +2238,7 @@ void fgUpdateDCS (void) {
 //             if ((ref_elev < -0.17) & (ref_elev >= -0.51)) sel = 0x28;
 //             if (ref_elev < -0.51) sel = 0x30;
 // DO NOT DELETE THIS CODE - This is to compare a discrete FLOLS (without LOD) with analog FLOLS
-            dummy_tile->lightmaps_sequence->select(sel);
+            // dummy_tile->lightmaps_sequence->select(sel);
 
             sgVec3 up;
             sgCopyVec3 (up, ship_up);
@@ -2202,7 +2246,7 @@ void fgUpdateDCS (void) {
                     sgScaleVec3 (up, 4.0*ref_elev*dist/750.0);
             else
                     sgScaleVec3 (up, 4.0*ref_elev);
-            dummy_tile->ols_transform->setTransform(up);
+            // dummy_tile->ols_transform->setTransform(up);
             //cout << "ref_elev  " << ref_elev << endl;
         }
     // end hack for deck lights