2 // fg_init.cxx -- Flight Gear top level initialization routines
4 // Written by Curtis Olson, started August 1997.
6 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
30 // For BC 5.01 this must be included before OpenGL includes.
31 #ifdef FG_MATH_EXCEPTION_CLASH
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.
44 # define _G_NO_EXTERN_TEMPLATES
47 #include <Include/compiler.h>
51 #include <Debug/logstream.hxx>
52 #include <Aircraft/aircraft.hxx>
53 #include <Airports/simple.hxx>
54 #include <Astro/sky.hxx>
55 #include <Astro/stars.hxx>
56 #include <Astro/solarsystem.hxx>
57 #include <Autopilot/autopilot.hxx>
58 #include <Cockpit/cockpit.hxx>
59 #include <FDM/Balloon.h>
60 #include <FDM/External.hxx>
61 #include <FDM/JSBsim.hxx>
62 #include <FDM/LaRCsim.hxx>
63 #include <FDM/MagicCarpet.hxx>
64 #include <Include/fg_constants.h>
65 #include <Include/general.hxx>
66 #include <Joystick/joystick.hxx>
67 #include <Math/fg_geodesy.hxx>
68 #include <Math/point3d.hxx>
69 #include <Math/polar3d.hxx>
70 #include <Misc/fgpath.hxx>
71 #include <Scenery/scenery.hxx>
72 #include <Scenery/tilemgr.hxx>
73 #include <Time/event.hxx>
74 #include <Time/fg_time.hxx>
75 #include <Time/light.hxx>
76 #include <Time/sunpos.hxx>
77 #include <Time/moonpos.hxx>
79 #ifndef FG_OLD_WEATHER
80 # include <WeatherCM/FGLocalWeatherDatabase.h>
82 # include <Weather/weather.hxx>
85 #include "fg_init.hxx"
87 #include "options.hxx"
90 #if defined(FX) && defined(XMESA)
96 extern const char *default_root;
99 // Read in configuration (file and command line)
100 bool fgInitConfig ( int argc, char **argv ) {
101 // Attempt to locate and parse a config file
102 // First check fg_root
103 FGPath config( current_options.get_fg_root() );
104 config.append( "system.fgfsrc" );
105 current_options.parse_config_file( config.str() );
107 // Next check home directory
108 char* envp = ::getenv( "HOME" );
109 if ( envp != NULL ) {
111 config.append( ".fgfsrc" );
112 current_options.parse_config_file( config.str() );
115 // Parse remaining command line options
116 // These will override anything specified in a config file
117 if ( current_options.parse_command_line(argc, argv) !=
118 fgOPTIONS::FG_OPTIONS_OK )
120 // Something must have gone horribly wrong with the command
121 // line parsing or maybe the user just requested help ... :-)
122 current_options.usage();
123 FG_LOG( FG_GENERAL, FG_ALERT, "\nExiting ...");
131 // Set initial position and orientation
132 bool fgInitPosition( void ) {
136 f = current_aircraft.fdm_state;
138 id = current_options.get_airport_id();
140 // set initial position from airport id
145 FG_LOG( FG_GENERAL, FG_INFO,
146 "Attempting to set starting position from airport code "
149 airports.load("apt_simple");
150 if ( ! airports.search( id, &a ) ) {
151 FG_LOG( FG_GENERAL, FG_ALERT,
152 "Failed to find " << id << " in database." );
155 f->set_Longitude( a.longitude * DEG_TO_RAD );
156 f->set_Latitude( a.latitude * DEG_TO_RAD );
159 // set initial position from default or command line coordinates
161 f->set_Longitude( current_options.get_lon() * DEG_TO_RAD );
162 f->set_Latitude( current_options.get_lat() * DEG_TO_RAD );
165 FG_LOG( FG_GENERAL, FG_INFO,
166 "starting altitude is = " << current_options.get_altitude() );
168 f->set_Altitude( current_options.get_altitude() * METER_TO_FEET );
169 fgFDMSetGroundElevation( current_options.get_flight_model(),
170 (f->get_Altitude() - 3.758099) * FEET_TO_METER );
172 FG_LOG( FG_GENERAL, FG_INFO,
173 "Initial position is: ("
174 << (f->get_Longitude() * RAD_TO_DEG) << ", "
175 << (f->get_Latitude() * RAD_TO_DEG) << ", "
176 << (f->get_Altitude() * FEET_TO_METER) << ")" );
182 // General house keeping initializations
183 bool fgInitGeneral( void ) {
186 #if defined(FX) && defined(XMESA)
187 char *mesa_win_state;
190 FG_LOG( FG_GENERAL, FG_INFO, "General Initialization" );
191 FG_LOG( FG_GENERAL, FG_INFO, "======= ==============" );
193 root = current_options.get_fg_root();
194 if ( ! root.length() ) {
195 // No root path set? Then bail ...
196 FG_LOG( FG_GENERAL, FG_ALERT,
197 "Cannot continue without environment variable FG_ROOT"
198 << "being defined." );
201 FG_LOG( FG_GENERAL, FG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
203 #if defined(FX) && defined(XMESA)
204 // initialize full screen flag
205 global_fullscreen = false;
206 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
207 // Test for the MESA_GLX_FX env variable
208 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
209 // test if we are fullscreen mesa/glide
210 if ( (mesa_win_state[0] == 'f') ||
211 (mesa_win_state[0] == 'F') ) {
212 global_fullscreen = true;
222 // This is the top level init routine which calls all the other
223 // initialization routines. If you are adding a subsystem to flight
224 // gear, its initialization call should located in this routine.
225 // Returns non-zero if a problem encountered.
226 bool fgInitSubsystems( void ) {
227 FGTime::cur_time_params = new FGTime();
229 fgLIGHT *l = &cur_light_params;
230 FGTime *t = FGTime::cur_time_params;
232 FG_LOG( FG_GENERAL, FG_INFO, "Initialize Subsystems");
233 FG_LOG( FG_GENERAL, FG_INFO, "========== ==========");
235 if ( current_options.get_flight_model() == FGInterface::FG_LARCSIM ) {
236 cur_fdm_state = new FGLaRCsim;
237 } else if ( current_options.get_flight_model() == FGInterface::FG_JSBSIM ) {
238 cur_fdm_state = new FGJSBsim;
239 } else if ( current_options.get_flight_model() ==
240 FGInterface::FG_BALLOONSIM ) {
241 cur_fdm_state = new FGBalloonSim;
242 } else if ( current_options.get_flight_model() ==
243 FGInterface::FG_MAGICCARPET ) {
244 cur_fdm_state = new FGMagicCarpet;
245 } else if ( current_options.get_flight_model() ==
246 FGInterface::FG_EXTERNAL ) {
247 cur_fdm_state = new FGExternal;
249 FG_LOG( FG_GENERAL, FG_ALERT,
250 "No flight model, can't init aircraft" );
254 // allocates structures so must happen before any of the flight
255 // model or control parameters are set
256 fgAircraftInit(); // In the future this might not be the case.
258 // set the initial position
261 // Initialize the Scenery Management subsystem
262 if ( fgSceneryInit() ) {
263 // Scenery initialized ok.
265 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
269 if( global_tile_mgr.init() ) {
270 // Load the local scenery data
271 global_tile_mgr.update();
273 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
277 FG_LOG( FG_GENERAL, FG_DEBUG,
278 "Current terrain elevation after tile mgr init " <<
281 // Calculate ground elevation at starting point (we didn't have
282 // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
284 // calculalate a cartesian point somewhere along the line between
285 // the center of the earth and our view position. Doesn't have to
286 // be the exact elevation (this is good because we don't know it
289 // now handled inside of the fgTileMgrUpdate()
292 geod_pos = Point3D( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(), 0.0);
293 tmp_abs_view_pos = fgGeodToCart(geod_pos);
295 FG_LOG( FG_GENERAL, FG_DEBUG,
296 "Initial abs_view_pos = " << tmp_abs_view_pos );
298 fgTileMgrCurElev( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(),
300 FG_LOG( FG_GENERAL, FG_DEBUG,
301 "Altitude after update " << scenery.cur_elev );
304 fgFDMSetGroundElevation( current_options.get_flight_model(),
307 // Reset our altitude if we are below ground
308 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
309 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
310 cur_fdm_state->get_Runway_altitude() );
312 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
313 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
316 FG_LOG( FG_GENERAL, FG_INFO,
317 "Updated position (after elevation adj): ("
318 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
319 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
320 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
322 // We need to calculate a few more values here that would normally
323 // be calculated by the FDM so that the current_view.UpdateViewMath()
324 // routine doesn't get hosed.
326 double sea_level_radius_meters;
328 // Set the FG variables first
329 fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
330 &sea_level_radius_meters, &lat_geoc);
331 cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
332 cur_fdm_state->get_Altitude() +
333 (sea_level_radius_meters * METER_TO_FEET) );
334 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
336 cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
337 cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
339 cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
340 cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
342 // The following section sets up the flight model EOM parameters
343 // and should really be read in from one or more files.
346 cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
347 current_options.get_vBody(),
348 current_options.get_wBody());
350 // Initial Orientation
351 cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
352 current_options.get_pitch() * DEG_TO_RAD,
353 current_options.get_heading() * DEG_TO_RAD );
355 // Initial Angular Body rates
356 cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
358 cur_fdm_state->set_Earth_position_angle( 0.0 );
360 // Mass properties and geometry values
361 cur_fdm_state->set_Inertias( 8.547270E+01,
362 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
364 // CG position w.r.t. ref. point
365 cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
367 // Initialize the event manager
368 global_events.Init();
370 // Output event stats every 60 seconds
371 global_events.Register( "fgEVENT_MGR::PrintStats()",
372 fgMethodCallback<fgEVENT_MGR>( &global_events,
373 &fgEVENT_MGR::PrintStats),
374 fgEVENT::FG_EVENT_READY, 60000 );
376 // Initialize the time dependent variables
377 t->init(*cur_fdm_state);
378 t->update(*cur_fdm_state);
380 // Initialize view parameters
381 FG_LOG( FG_GENERAL, FG_DEBUG, "Before current_view.init()");
384 FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
385 current_view.UpdateViewMath(*cur_fdm_state);
386 pilot_view.UpdateViewMath(*cur_fdm_state);
387 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = " << current_view.get_abs_view_pos());
388 // current_view.UpdateWorldToEye(f);
390 // Build the solar system
391 //fgSolarSystemInit(*t);
392 FG_LOG(FG_GENERAL, FG_INFO, "Building SolarSystem");
393 SolarSystem::theSolarSystem = new SolarSystem(t);
395 // Initialize the Stars subsystem
396 if( fgStarsInit() ) {
397 // Stars initialized ok.
399 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Stars initialization!" );
403 // Initialize the planetary subsystem
404 // global_events.Register( "fgPlanetsInit()", fgPlanetsInit,
405 // fgEVENT::FG_EVENT_READY, 600000);
407 // Initialize the sun's position
408 // global_events.Register( "fgSunInit()", fgSunInit,
409 // fgEVENT::FG_EVENT_READY, 30000 );
411 // Intialize the moon's position
412 // global_events.Register( "fgMoonInit()", fgMoonInit,
413 // fgEVENT::FG_EVENT_READY, 600000 );
415 // register the periodic update of Sun, moon, and planets
416 global_events.Register( "ssolsysUpdate", solarSystemRebuild,
417 fgEVENT::FG_EVENT_READY, 600000);
419 // fgUpdateSunPos() needs a few position and view parameters set
420 // so it can calculate local relative sun angle and a few other
421 // things for correctly orienting the sky.
424 global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
425 fgEVENT::FG_EVENT_READY, 60000);
426 global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
427 fgEVENT::FG_EVENT_READY, 60000);
429 // Initialize Lighting interpolation tables
432 // update the lighting parameters (based on sun angle)
433 global_events.Register( "fgLight::Update()",
434 fgMethodCallback<fgLIGHT>( &cur_light_params,
436 fgEVENT::FG_EVENT_READY, 30000 );
437 // update the current timezone each 30 minutes
438 global_events.Register( "fgTIME::updateLocal()",
439 fgMethodCallback<FGTime>(FGTime::cur_time_params,
440 &FGTime::updateLocal),
441 fgEVENT::FG_EVENT_READY, 1800000);
443 // Initialize the weather modeling subsystem
444 #ifndef FG_OLD_WEATHER
445 // Initialize the WeatherDatabase
446 FG_LOG(FG_GENERAL, FG_INFO, "Creating LocalWeatherDatabase");
448 sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
449 current_aircraft.fdm_state->get_Longitude(),
450 current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER );
451 FGLocalWeatherDatabase::theFGLocalWeatherDatabase =
452 new FGLocalWeatherDatabase( position );
453 // cout << theFGLocalWeatherDatabase << endl;
454 // cout << "visibility = "
455 // << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
457 WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
459 // register the periodic update of the weather
460 global_events.Register( "weather update", fgUpdateWeatherDatabase,
461 fgEVENT::FG_EVENT_READY, 30000);
463 current_weather.Init();
466 // Initialize the Cockpit subsystem
467 if( fgCockpitInit( ¤t_aircraft )) {
468 // Cockpit initialized ok.
470 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Cockpit initialization!" );
474 // Initialize the "sky"
477 // Initialize the flight model subsystem data structures base on
480 // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
481 // 1.0 / current_options.get_model_hz() );
482 cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
484 // I'm just sticking this here for now, it should probably move
486 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
488 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
489 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
492 FG_LOG( FG_GENERAL, FG_INFO,
493 "Updated position (after elevation adj): ("
494 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
495 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
496 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
497 // end of thing that I just stuck in that I should probably move
500 if ( fgJoystickInit() ) {
501 // Joystick initialized ok.
503 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Joystick initialization!" );
506 // Autopilot init added here, by Jeff Goeke-Smith
507 fgAPInit(¤t_aircraft);
509 // Initialize I/O channels
510 #if ! defined( MACOS )
514 FG_LOG( FG_GENERAL, FG_INFO, endl);
520 void fgReInitSubsystems( void )
522 FGTime *t = FGTime::cur_time_params;
524 int toggle_pause = t->getPause();
527 t->togglePauseMode();
530 if( global_tile_mgr.init() ) {
531 // Load the local scenery data
532 global_tile_mgr.update();
534 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
537 fgFDMSetGroundElevation( current_options.get_flight_model(),
540 // Reset our altitude if we are below ground
541 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
542 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
543 cur_fdm_state->get_Runway_altitude() );
545 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
546 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
548 double sea_level_radius_meters;
550 // Set the FG variables first
551 fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
552 &sea_level_radius_meters, &lat_geoc);
553 cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
554 cur_fdm_state->get_Altitude() +
555 (sea_level_radius_meters * METER_TO_FEET) );
556 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
558 cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
559 cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
561 cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
562 cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
564 // The following section sets up the flight model EOM parameters
565 // and should really be read in from one or more files.
568 cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
569 current_options.get_vBody(),
570 current_options.get_wBody());
572 // Initial Orientation
573 cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
574 current_options.get_pitch() * DEG_TO_RAD,
575 current_options.get_heading() * DEG_TO_RAD );
577 // Initial Angular Body rates
578 cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
580 cur_fdm_state->set_Earth_position_angle( 0.0 );
582 // Mass properties and geometry values
583 cur_fdm_state->set_Inertias( 8.547270E+01,
584 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
586 // CG position w.r.t. ref. point
587 cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
589 // Initialize view parameters
590 current_view.set_view_offset( 0.0 );
591 current_view.set_goal_view_offset( 0.0 );
592 pilot_view.set_view_offset( 0.0 );
593 pilot_view.set_goal_view_offset( 0.0 );
595 FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
596 current_view.UpdateViewMath(*cur_fdm_state);
597 pilot_view.UpdateViewMath(*cur_fdm_state);
598 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = " << current_view.get_abs_view_pos());
600 // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
601 // 1.0 / current_options.get_model_hz() );
602 cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
604 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
606 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
607 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
610 controls.reset_all();
614 t->togglePauseMode();