]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_init.cxx
Moved sky code over to simgear.
[flightgear.git] / src / Main / fg_init.cxx
1 //
2 // fg_init.cxx -- Flight Gear top level initialization routines
3 //
4 // Written by Curtis Olson, started August 1997.
5 //
6 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 //
22 //
23 // $Id$
24
25
26 #ifdef HAVE_CONFIG_H
27 #  include <config.h>
28 #endif
29
30 // For BC 5.01 this must be included before OpenGL includes.
31 #ifdef FG_MATH_EXCEPTION_CLASH
32 #  include <math.h>
33 #endif
34
35 #include <GL/glut.h>
36 #include <simgear/xgl/xgl.h>
37
38 #include <stdio.h>
39 #include <stdlib.h>
40
41 // work around a stdc++ lib bug in some versions of linux, but doesn't
42 // seem to hurt to have this here for all versions of Linux.
43 #ifdef linux
44 #  define _G_NO_EXTERN_TEMPLATES
45 #endif
46
47 #include <simgear/compiler.h>
48
49 #include STL_STRING
50
51 #include <simgear/constants.h>
52 #include <simgear/debug/logstream.hxx>
53 #include <simgear/math/fg_geodesy.hxx>
54 #include <simgear/math/point3d.hxx>
55 #include <simgear/math/polar3d.hxx>
56 #include <simgear/misc/fgpath.hxx>
57
58 #include <Aircraft/aircraft.hxx>
59 #include <Airports/simple.hxx>
60 #include <Autopilot/auto_gui.hxx>
61 #include <Autopilot/newauto.hxx>
62 #include <Cockpit/cockpit.hxx>
63 #include <Cockpit/radiostack.hxx>
64 #include <Cockpit/panel.hxx>
65 #include <Cockpit/sp_panel.hxx>
66 #include <FDM/Balloon.h>
67 #include <FDM/External.hxx>
68 #include <FDM/JSBsim.hxx>
69 #include <FDM/LaRCsim.hxx>
70 #include <FDM/MagicCarpet.hxx>
71 #include <Include/general.hxx>
72 #include <Joystick/joystick.hxx>
73 #include <Objects/matlib.hxx>
74 #include <Navaids/fixlist.hxx>
75 #include <Navaids/ilslist.hxx>
76 #include <Navaids/navlist.hxx>
77 #include <Scenery/scenery.hxx>
78 #include <Scenery/tilemgr.hxx>
79 #include <Time/event.hxx>
80 #include <Time/fg_time.hxx>
81 #include <Time/light.hxx>
82 #include <Time/sunpos.hxx>
83 #include <Time/moonpos.hxx>
84
85 #ifndef FG_OLD_WEATHER
86 #  include <WeatherCM/FGLocalWeatherDatabase.h>
87 #else
88 #  include <Weather/weather.hxx>
89 #endif
90
91 #include "fg_init.hxx"
92 #include "fg_io.hxx"
93 #include "options.hxx"
94 #include "views.hxx"
95 #include "bfi.hxx"
96
97 #if defined(FX) && defined(XMESA)
98 #include <GL/xmesa.h>
99 #endif
100
101 FG_USING_STD(string);
102
103 extern const char *default_root;
104
105
106 // Read in configuration (file and command line)
107 bool fgInitConfig ( int argc, char **argv ) {
108     // Attempt to locate and parse a config file
109     // First check fg_root
110     FGPath config( current_options.get_fg_root() );
111     config.append( "system.fgfsrc" );
112     current_options.parse_config_file( config.str() );
113
114     // Next check home directory
115     char* envp = ::getenv( "HOME" );
116     if ( envp != NULL ) {
117         config.set( envp );
118         config.append( ".fgfsrc" );
119         current_options.parse_config_file( config.str() );
120     }
121
122     // Parse remaining command line options
123     // These will override anything specified in a config file
124     if ( current_options.parse_command_line(argc, argv) !=
125          fgOPTIONS::FG_OPTIONS_OK )
126     {
127         // Something must have gone horribly wrong with the command
128         // line parsing or maybe the user just requested help ... :-)
129         current_options.usage();
130         FG_LOG( FG_GENERAL, FG_ALERT, "\nExiting ...");
131         return false;
132     }
133
134    return true;
135 }
136
137
138 // Set initial position and orientation
139 bool fgInitPosition( void ) {
140     string id;
141     FGInterface *f;
142
143     f = current_aircraft.fdm_state;
144
145     id = current_options.get_airport_id();
146     if ( id.length() ) {
147         // set initial position from airport id
148
149         FGPath path( current_options.get_fg_root() );
150         path.append( "Airports" );
151         path.append( "simple.mk4" );
152         FGAirports airports( path.c_str() );
153         FGAirport a;
154
155         FG_LOG( FG_GENERAL, FG_INFO,
156                 "Attempting to set starting position from airport code "
157                 << id );
158
159         // FGPath inpath( current_options.get_fg_root() );
160         // inpath.append( "Airports" );
161         // inpath.append( "apt_simple" );
162         // airports.load( inpath.c_str() );
163
164         // FGPath outpath( current_options.get_fg_root() );
165         // outpath.append( "Airports" );
166         // outpath.append( "simple.gdbm" );
167         // airports.dump_gdbm( outpath.c_str() );
168
169         if ( ! airports.search( id, &a ) ) {
170             FG_LOG( FG_GENERAL, FG_ALERT,
171                     "Failed to find " << id << " in database." );
172             exit(-1);
173         } else {
174             f->set_Longitude( a.longitude * DEG_TO_RAD );
175             f->set_Latitude( a.latitude * DEG_TO_RAD );
176         }
177     } else {
178         // set initial position from default or command line coordinates
179
180         f->set_Longitude( current_options.get_lon() * DEG_TO_RAD );
181         f->set_Latitude( current_options.get_lat() * DEG_TO_RAD );
182     }
183
184     FG_LOG( FG_GENERAL, FG_INFO,
185             "starting altitude is = " << current_options.get_altitude() );
186
187     f->set_Altitude( current_options.get_altitude() * METER_TO_FEET );
188     fgFDMSetGroundElevation( current_options.get_flight_model(),
189                              (f->get_Altitude() - 3.758099) * FEET_TO_METER );
190
191     FG_LOG( FG_GENERAL, FG_INFO,
192             "Initial position is: ("
193             << (f->get_Longitude() * RAD_TO_DEG) << ", "
194             << (f->get_Latitude() * RAD_TO_DEG) << ", "
195             << (f->get_Altitude() * FEET_TO_METER) << ")" );
196
197     return true;
198 }
199
200
201 // General house keeping initializations
202 bool fgInitGeneral( void ) {
203     string root;
204
205 #if defined(FX) && defined(XMESA)
206     char *mesa_win_state;
207 #endif
208
209     FG_LOG( FG_GENERAL, FG_INFO, "General Initialization" );
210     FG_LOG( FG_GENERAL, FG_INFO, "======= ==============" );
211
212     root = current_options.get_fg_root();
213     if ( ! root.length() ) {
214         // No root path set? Then bail ...
215         FG_LOG( FG_GENERAL, FG_ALERT,
216                 "Cannot continue without environment variable FG_ROOT"
217                 << "being defined." );
218         exit(-1);
219     }
220     FG_LOG( FG_GENERAL, FG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
221
222 #if defined(FX) && defined(XMESA)
223     // initialize full screen flag
224     global_fullscreen = false;
225     if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
226         // Test for the MESA_GLX_FX env variable
227         if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
228             // test if we are fullscreen mesa/glide
229             if ( (mesa_win_state[0] == 'f') ||
230                  (mesa_win_state[0] == 'F') ) {
231                 global_fullscreen = true;
232             }
233         }
234     }
235 #endif
236
237     return true;
238 }
239
240
241 // This is the top level init routine which calls all the other
242 // initialization routines.  If you are adding a subsystem to flight
243 // gear, its initialization call should located in this routine.
244 // Returns non-zero if a problem encountered.
245 bool fgInitSubsystems( void ) {
246     fgLIGHT *l = &cur_light_params;
247     FGTime *t = FGTime::cur_time_params;
248
249     FG_LOG( FG_GENERAL, FG_INFO, "Initialize Subsystems");
250     FG_LOG( FG_GENERAL, FG_INFO, "========== ==========");
251
252     if ( current_options.get_flight_model() == FGInterface::FG_LARCSIM ) {
253         cur_fdm_state = new FGLaRCsim;
254     } else if ( current_options.get_flight_model() == FGInterface::FG_JSBSIM ) {
255         cur_fdm_state = new FGJSBsim;
256     } else if ( current_options.get_flight_model() == 
257                 FGInterface::FG_BALLOONSIM ) {
258         cur_fdm_state = new FGBalloonSim;
259     } else if ( current_options.get_flight_model() == 
260                 FGInterface::FG_MAGICCARPET ) {
261         cur_fdm_state = new FGMagicCarpet;
262     } else if ( current_options.get_flight_model() == 
263                 FGInterface::FG_EXTERNAL ) {
264         cur_fdm_state = new FGExternal;
265     } else {
266         FG_LOG( FG_GENERAL, FG_ALERT,
267                 "No flight model, can't init aircraft" );
268         exit(-1);
269     }
270
271     // allocates structures so must happen before any of the flight
272     // model or control parameters are set
273     fgAircraftInit();   // In the future this might not be the case.
274
275     // set the initial position
276     fgInitPosition();
277
278     // Initialize the material property lib
279     FGPath mpath( current_options.get_fg_root() );
280     mpath.append( "materials" );
281     if ( material_lib.load( mpath.str() ) ) {
282     } else {
283         FG_LOG( FG_GENERAL, FG_ALERT, "Error loading material lib!" );
284         exit(-1);
285     }
286
287     // Initialize the Scenery Management subsystem
288     if ( fgSceneryInit() ) {
289         // Material lib initialized ok.
290     } else {
291         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
292         exit(-1);
293     }
294
295     if( global_tile_mgr.init() ) {
296         // Load the local scenery data
297         global_tile_mgr.update( cur_fdm_state->get_Longitude(),
298                                 cur_fdm_state->get_Latitude() );
299     } else {
300         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
301         exit(-1);
302     }
303
304     FG_LOG( FG_GENERAL, FG_DEBUG,
305             "Current terrain elevation after tile mgr init " <<
306             scenery.cur_elev );
307
308     // Calculate ground elevation at starting point (we didn't have
309     // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
310     //
311     // calculalate a cartesian point somewhere along the line between
312     // the center of the earth and our view position.  Doesn't have to
313     // be the exact elevation (this is good because we don't know it
314     // yet :-)
315
316     // now handled inside of the fgTileMgrUpdate()
317
318     /*
319     geod_pos = Point3D( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(), 0.0);
320     tmp_abs_view_pos = fgGeodToCart(geod_pos);
321
322     FG_LOG( FG_GENERAL, FG_DEBUG,
323             "Initial abs_view_pos = " << tmp_abs_view_pos );
324     scenery.cur_elev =
325         fgTileMgrCurElev( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(),
326                           tmp_abs_view_pos );
327     FG_LOG( FG_GENERAL, FG_DEBUG,
328             "Altitude after update " << scenery.cur_elev );
329     */
330
331     fgFDMSetGroundElevation( current_options.get_flight_model(),
332                              scenery.cur_elev );
333
334     // Reset our altitude if we are below ground
335     FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
336     FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
337             cur_fdm_state->get_Runway_altitude() );
338
339     if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
340         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
341     }
342
343     FG_LOG( FG_GENERAL, FG_INFO,
344             "Updated position (after elevation adj): ("
345             << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
346             << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
347             << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
348
349     // We need to calculate a few more values here that would normally
350     // be calculated by the FDM so that the current_view.UpdateViewMath()
351     // routine doesn't get hosed.
352
353     double sea_level_radius_meters;
354     double lat_geoc;
355     // Set the FG variables first
356     fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
357                   &sea_level_radius_meters, &lat_geoc);
358     cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
359                                 cur_fdm_state->get_Altitude() +
360                                 (sea_level_radius_meters * METER_TO_FEET) );
361     cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
362
363     cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
364     cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
365         
366     cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
367     cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
368
369     // The following section sets up the flight model EOM parameters
370     // and should really be read in from one or more files.
371
372     // Initial Velocity
373     cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
374                              current_options.get_vBody(),
375                              current_options.get_wBody());
376
377     // Initial Orientation
378     cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
379                          current_options.get_pitch() * DEG_TO_RAD,
380                          current_options.get_heading() * DEG_TO_RAD );
381
382     // Initial Angular Body rates
383     cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
384
385     cur_fdm_state->set_Earth_position_angle( 0.0 );
386
387     // Mass properties and geometry values
388     cur_fdm_state->set_Inertias( 8.547270E+01,
389                      1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
390
391     // CG position w.r.t. ref. point
392     cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
393
394     // Initialize the event manager
395     global_events.Init();
396
397     // Output event stats every 60 seconds
398     global_events.Register( "fgEVENT_MGR::PrintStats()",
399                             fgMethodCallback<fgEVENT_MGR>( &global_events,
400                                                    &fgEVENT_MGR::PrintStats),
401                             fgEVENT::FG_EVENT_READY, 60000 );
402
403     // Initialize view parameters
404     FG_LOG( FG_GENERAL, FG_DEBUG, "Before current_view.init()");
405     current_view.Init();
406     pilot_view.Init();
407     FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
408     current_view.UpdateViewMath(*cur_fdm_state);
409     pilot_view.UpdateViewMath(*cur_fdm_state);
410     FG_LOG( FG_GENERAL, FG_DEBUG, "  abs_view_pos = " << current_view.get_abs_view_pos());
411     // current_view.UpdateWorldToEye(f);
412
413     // Initialize the planetary subsystem
414     // global_events.Register( "fgPlanetsInit()", fgPlanetsInit,
415     //                      fgEVENT::FG_EVENT_READY, 600000);
416
417     // Initialize the sun's position
418     // global_events.Register( "fgSunInit()", fgSunInit,
419     //                      fgEVENT::FG_EVENT_READY, 30000 );
420
421     // Intialize the moon's position
422     // global_events.Register( "fgMoonInit()", fgMoonInit,
423     //                      fgEVENT::FG_EVENT_READY, 600000 );
424
425     // fgUpdateSunPos() needs a few position and view parameters set
426     // so it can calculate local relative sun angle and a few other
427     // things for correctly orienting the sky.
428     fgUpdateSunPos();
429     fgUpdateMoonPos();
430     global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
431                             fgEVENT::FG_EVENT_READY, 60000);
432     global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
433                             fgEVENT::FG_EVENT_READY, 60000);
434
435     // Initialize Lighting interpolation tables
436     l->Init();
437
438     // update the lighting parameters (based on sun angle)
439     global_events.Register( "fgLight::Update()",
440                             fgMethodCallback<fgLIGHT>( &cur_light_params,
441                                                        &fgLIGHT::Update),
442                             fgEVENT::FG_EVENT_READY, 30000 );
443     // update the current timezone each 30 minutes
444     global_events.Register( "fgTIME::updateLocal()",
445                             fgMethodCallback<FGTime>(FGTime::cur_time_params, 
446                                                      &FGTime::updateLocal),
447                             fgEVENT::FG_EVENT_READY, 1800000);
448
449     // Initialize the weather modeling subsystem
450 #ifndef FG_OLD_WEATHER
451     // Initialize the WeatherDatabase
452     FG_LOG(FG_GENERAL, FG_INFO, "Creating LocalWeatherDatabase");
453     sgVec3 position;
454     sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
455                current_aircraft.fdm_state->get_Longitude(),
456                current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER );
457     FGLocalWeatherDatabase::theFGLocalWeatherDatabase = 
458         new FGLocalWeatherDatabase( position );
459     // cout << theFGLocalWeatherDatabase << endl;
460     // cout << "visibility = " 
461     //      << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
462
463     WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
464      
465     // register the periodic update of the weather
466     global_events.Register( "weather update", fgUpdateWeatherDatabase,
467                             fgEVENT::FG_EVENT_READY, 30000);
468 #else
469     current_weather.Init();
470 #endif
471
472     // Initialize vor/ndb/ils/fix list management and query systems
473     FG_LOG(FG_GENERAL, FG_INFO, "Loading Navaids");
474
475     FG_LOG(FG_GENERAL, FG_INFO, "  VOR/NDB");
476     current_navlist = new FGNavList;
477     FGPath p_nav( current_options.get_fg_root() );
478     p_nav.append( "Navaids/default.nav" );
479     current_navlist->init( p_nav );
480
481     FG_LOG(FG_GENERAL, FG_INFO, "  ILS");
482     current_ilslist = new FGILSList;
483     FGPath p_ils( current_options.get_fg_root() );
484     p_ils.append( "Navaids/default.ils" );
485     current_ilslist->init( p_ils );
486
487     FG_LOG(FG_GENERAL, FG_INFO, "  Fixes");
488     current_fixlist = new FGFixList;
489     FGPath p_fix( current_options.get_fg_root() );
490     p_fix.append( "Navaids/default.fix" );
491     current_fixlist->init( p_fix );
492
493     // Initialize the underlying radio stack model
494     current_radiostack = new FGRadioStack;
495
496     current_radiostack->set_nav1_freq( 117.30 );
497     current_radiostack->set_nav1_alt_freq( 110.30 );
498     current_radiostack->set_nav1_sel_radial( 119.0 );
499
500     current_radiostack->set_nav2_freq( 111.80 );
501     current_radiostack->set_nav2_alt_freq( 115.70 );
502     current_radiostack->set_nav2_sel_radial( 029.0 );
503
504     current_radiostack->set_adf_freq( 266.0 );
505
506 #if 0
507     // This block of settings are Alex's defaults for San Diego
508     current_radiostack->set_nav1_freq( 111.70 );
509     current_radiostack->set_nav1_alt_freq( 115.30 );
510     current_radiostack->set_nav1_sel_radial( 280.0 );
511     current_radiostack->set_nav2_freq( 117.80 );
512     current_radiostack->set_nav2_alt_freq( 114.00 );
513     current_radiostack->set_nav2_sel_radial( 68.0 );
514     current_radiostack->set_adf_freq( 210.0 );
515     // End of Alex's custom settings
516 #endif
517
518     current_radiostack->search( cur_fdm_state->get_Longitude(),
519                                 cur_fdm_state->get_Latitude(),
520                                 cur_fdm_state->get_Altitude() * FEET_TO_METER );
521
522     current_radiostack->update( cur_fdm_state->get_Longitude(),
523                                 cur_fdm_state->get_Latitude(),
524                                 cur_fdm_state->get_Altitude() * FEET_TO_METER );
525
526     // Search radio database once per second
527     global_events.Register( "fgRadioSearch()", fgRadioSearch,
528                             fgEVENT::FG_EVENT_READY, 1000);
529
530
531     // Initialize the Cockpit subsystem
532     if( fgCockpitInit( &current_aircraft )) {
533         // Cockpit initialized ok.
534     } else {
535         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Cockpit initialization!" );
536         exit(-1);
537     }
538
539     // Initialize the flight model subsystem data structures base on
540     // above values
541
542     // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
543     //            1.0 / current_options.get_model_hz() );
544     cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
545
546     // I'm just sticking this here for now, it should probably move
547     // eventually
548     scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
549
550     if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
551         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
552     }
553
554     FG_LOG( FG_GENERAL, FG_INFO,
555             "Updated position (after elevation adj): ("
556             << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
557             << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
558             << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
559     // end of thing that I just stuck in that I should probably move
560
561     // Joystick support
562     if ( fgJoystickInit() ) {
563         // Joystick initialized ok.
564     } else {
565         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Joystick initialization!" );
566     }
567
568     // Autopilot init
569     current_autopilot = new FGAutopilot;
570     current_autopilot->init();
571
572     // initialize the gui parts of the autopilot
573     NewTgtAirportInit();
574     fgAPAdjustInit() ;
575     NewHeadingInit();
576     NewAltitudeInit();
577
578     // Initialize I/O channels
579 #if ! defined( MACOS )
580     fgIOInit();
581 #endif
582
583     // Initialize the 2D panel.
584     current_panel = fgCreateSmallSinglePropPanel(0, 0, 1024, 768);
585
586     // Initialize the BFI
587     FGBFI::init();
588
589     FG_LOG( FG_GENERAL, FG_INFO, endl);
590
591     return true;
592 }
593
594
595 void fgReInitSubsystems( void )
596 {
597     FGTime *t = FGTime::cur_time_params;
598     
599     int toggle_pause = t->getPause();
600     
601     if( !toggle_pause )
602         t->togglePauseMode();
603     
604     fgInitPosition();
605     if( global_tile_mgr.init() ) {
606         // Load the local scenery data
607         global_tile_mgr.update( cur_fdm_state->get_Longitude(),
608                                 cur_fdm_state->get_Latitude() );
609     } else {
610         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
611                 exit(-1);
612     }
613     fgFDMSetGroundElevation( current_options.get_flight_model(), 
614                              scenery.cur_elev );
615
616     // Reset our altitude if we are below ground
617     FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
618     FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " << 
619             cur_fdm_state->get_Runway_altitude() );
620
621     if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
622         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
623     }
624     double sea_level_radius_meters;
625     double lat_geoc;
626     // Set the FG variables first
627     fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(), 
628                   &sea_level_radius_meters, &lat_geoc);
629     cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(), 
630                                 cur_fdm_state->get_Altitude() + 
631                                 (sea_level_radius_meters * METER_TO_FEET) );
632     cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
633         
634     cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
635     cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
636         
637     cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
638     cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
639
640     // The following section sets up the flight model EOM parameters
641     // and should really be read in from one or more files.
642
643     // Initial Velocity
644     cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
645                              current_options.get_vBody(),
646                              current_options.get_wBody());
647
648     // Initial Orientation
649     cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
650                          current_options.get_pitch() * DEG_TO_RAD,
651                          current_options.get_heading() * DEG_TO_RAD );
652
653     // Initial Angular Body rates
654     cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
655
656     cur_fdm_state->set_Earth_position_angle( 0.0 );
657
658     // Mass properties and geometry values
659     cur_fdm_state->set_Inertias( 8.547270E+01, 
660                      1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
661
662     // CG position w.r.t. ref. point
663     cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
664
665     // Initialize view parameters
666     current_view.set_view_offset( 0.0 );
667     current_view.set_goal_view_offset( 0.0 );
668     pilot_view.set_view_offset( 0.0 );
669     pilot_view.set_goal_view_offset( 0.0 );
670
671     FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
672     current_view.UpdateViewMath(*cur_fdm_state);
673     pilot_view.UpdateViewMath(*cur_fdm_state);
674     FG_LOG( FG_GENERAL, FG_DEBUG, "  abs_view_pos = " << current_view.get_abs_view_pos());
675
676     // fgFDMInit( current_options.get_flight_model(), cur_fdm_state, 
677     //            1.0 / current_options.get_model_hz() );
678     cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
679
680     scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
681
682     if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
683         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
684     }
685
686     controls.reset_all();
687     current_autopilot->reset();
688
689     if( !toggle_pause )
690         t->togglePauseMode();
691 }