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.
36 // work around a stdc++ lib bug in some versions of linux, but doesn't
37 // seem to hurt to have this here for all versions of Linux.
39 # define _G_NO_EXTERN_TEMPLATES
42 #include <Include/compiler.h>
46 #include <Debug/logstream.hxx>
47 #include <Aircraft/aircraft.hxx>
48 #include <Airports/simple.hxx>
49 #include <Astro/sky.hxx>
50 #include <Astro/stars.hxx>
51 #include <Astro/solarsystem.hxx>
52 #include <Autopilot/autopilot.hxx>
53 #include <Cockpit/cockpit.hxx>
54 #include <Include/fg_constants.h>
55 #include <Include/general.hxx>
56 #include <Joystick/joystick.hxx>
57 #include <Math/fg_geodesy.hxx>
58 #include <Math/point3d.hxx>
59 #include <Math/polar3d.hxx>
60 #include <Scenery/scenery.hxx>
61 #include <Scenery/tilemgr.hxx>
62 #include <Time/event.hxx>
63 #include <Time/fg_time.hxx>
64 #include <Time/light.hxx>
65 #include <Time/sunpos.hxx>
66 #include <Time/moonpos.hxx>
67 #include <Weather/weather.hxx>
69 #include "fg_init.hxx"
70 #include "options.hxx"
72 #include "fg_serial.hxx"
74 #if defined(FX) && defined(XMESA)
80 extern const char *default_root;
83 // Set initial position and orientation
84 int fgInitPosition( void ) {
88 f = current_aircraft.fdm_state;
90 id = current_options.get_airport_id();
92 // set initial position from airport id
97 FG_LOG( FG_GENERAL, FG_INFO,
98 "Attempting to set starting position from airport code "
101 airports.load("apt_simple");
102 if ( ! airports.search( id, &a ) ) {
103 FG_LOG( FG_GENERAL, FG_ALERT,
104 "Failed to find " << id << " in database." );
107 f->set_Longitude( a.longitude * DEG_TO_RAD );
108 f->set_Latitude( a.latitude * DEG_TO_RAD );
111 // set initial position from default or command line coordinates
113 f->set_Longitude( current_options.get_lon() * DEG_TO_RAD );
114 f->set_Latitude( current_options.get_lat() * DEG_TO_RAD );
117 f->set_sin_cos_longitude(current_options.get_lon() * DEG_TO_RAD);
118 f->set_sin_cos_latitude(current_options.get_lat() * DEG_TO_RAD);
120 FG_LOG( FG_GENERAL, FG_INFO,
121 "starting altitude is = " << current_options.get_altitude() );
123 f->set_Altitude( current_options.get_altitude() * METER_TO_FEET );
124 fgFDMSetGroundElevation( current_options.get_flight_model(),
125 (f->get_Altitude() - 3.758099) * FEET_TO_METER );
127 FG_LOG( FG_GENERAL, FG_INFO,
128 "Initial position is: ("
129 << (f->get_Longitude() * RAD_TO_DEG) << ", "
130 << (f->get_Latitude() * RAD_TO_DEG) << ", "
131 << (f->get_Altitude() * FEET_TO_METER) << ")" );
137 // General house keeping initializations
138 int fgInitGeneral( void ) {
140 char *mesa_win_state;
142 FG_LOG( FG_GENERAL, FG_INFO, "General Initialization" );
143 FG_LOG( FG_GENERAL, FG_INFO, "======= ==============" );
145 root = current_options.get_fg_root();
146 if ( ! root.length() ) {
147 // No root path set? Then bail ...
148 FG_LOG( FG_GENERAL, FG_ALERT,
149 "Cannot continue without environment variable FG_ROOT"
150 << "being defined." );
153 FG_LOG( FG_GENERAL, FG_INFO, "FG_ROOT = " << root << endl );
155 #if defined(FX) && defined(XMESA)
156 // initialize full screen flag
157 global_fullscreen = false;
158 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
159 // Test for the MESA_GLX_FX env variable
160 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
161 // test if we are fullscreen mesa/glide
162 if ( (mesa_win_state[0] == 'f') ||
163 (mesa_win_state[0] == 'F') ) {
164 global_fullscreen = true;
174 // This is the top level init routine which calls all the other
175 // initialization routines. If you are adding a subsystem to flight
176 // gear, its initialization call should located in this routine.
177 // Returns non-zero if a problem encountered.
178 int fgInitSubsystems( void )
180 FGInterface *f; // assigned later
181 fgLIGHT *l = &cur_light_params;
182 fgTIME *t = &cur_time_params;
183 FGView *v = ¤t_view;
185 FG_LOG( FG_GENERAL, FG_INFO, "Initialize Subsystems");
186 FG_LOG( FG_GENERAL, FG_INFO, "========== ==========");
188 // allocates structures so must happen before any of the flight
189 // model or control parameters are set
190 fgAircraftInit(); // In the future this might not be the case.
191 f = current_aircraft.fdm_state;
193 // set the initial position
196 // Initialize the Scenery Management subsystem
197 if ( fgSceneryInit() ) {
198 // Scenery initialized ok.
200 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
204 if( fgTileMgrInit() ) {
205 // Load the local scenery data
208 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
212 FG_LOG( FG_GENERAL, FG_DEBUG,
213 "Current terrain elevation after tile mgr init " <<
216 // Calculate ground elevation at starting point (we didn't have
217 // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
219 // calculalate a cartesian point somewhere along the line between
220 // the center of the earth and our view position. Doesn't have to
221 // be the exact elevation (this is good because we don't know it
224 // now handled inside of the fgTileMgrUpdate()
227 geod_pos = Point3D( f->get_Longitude(), f->get_Latitude(), 0.0);
228 tmp_abs_view_pos = fgGeodToCart(geod_pos);
230 FG_LOG( FG_GENERAL, FG_DEBUG,
231 "Initial abs_view_pos = " << tmp_abs_view_pos );
233 fgTileMgrCurElev( f->get_Longitude(), f->get_Latitude(),
235 FG_LOG( FG_GENERAL, FG_DEBUG,
236 "Altitude after update " << scenery.cur_elev );
239 fgFDMSetGroundElevation( current_options.get_flight_model(),
242 // Reset our altitude if we are below ground
243 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << f->get_Altitude() );
244 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
245 f->get_Runway_altitude() );
247 if ( f->get_Altitude() < f->get_Runway_altitude() + 3.758099) {
248 f->set_Altitude( f->get_Runway_altitude() + 3.758099 );
251 FG_LOG( FG_GENERAL, FG_INFO,
252 "Updated position (after elevation adj): ("
253 << (f->get_Latitude() * RAD_TO_DEG) << ", "
254 << (f->get_Longitude() * RAD_TO_DEG) << ", "
255 << (f->get_Altitude() * FEET_TO_METER) << ")" );
257 // We need to calculate a few more values here that would normally
258 // be calculated by the FDM so that the v->UpdateViewMath()
259 // routine doesn't get hosed.
261 double sea_level_radius_meters;
263 // Set the FG variables first
264 fgGeodToGeoc( f->get_Latitude(), f->get_Altitude(),
265 &sea_level_radius_meters, &lat_geoc);
266 f->set_Geocentric_Position( lat_geoc, f->get_Longitude(),
268 (sea_level_radius_meters * METER_TO_FEET) );
269 f->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
271 // The following section sets up the flight model EOM parameters
272 // and should really be read in from one or more files.
275 f->set_Velocities_Local( 0.0, 0.0, 0.0 );
277 // Initial Orientation
278 f->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
279 current_options.get_pitch() * DEG_TO_RAD,
280 current_options.get_heading() * DEG_TO_RAD );
282 // Initial Angular Body rates
283 f->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
285 f->set_Earth_position_angle( 0.0 );
287 // Mass properties and geometry values
288 f->set_Inertias( 8.547270E+01,
289 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
291 // CG position w.r.t. ref. point
292 f->set_CG_Position( 0.0, 0.0, 0.0 );
294 // Initialize the event manager
295 global_events.Init();
297 // Output event stats every 60 seconds
298 global_events.Register( "fgEVENT_MGR::PrintStats()",
299 fgMethodCallback<fgEVENT_MGR>( &global_events,
300 &fgEVENT_MGR::PrintStats),
301 fgEVENT::FG_EVENT_READY, 60000 );
303 // Initialize the time dependent variables
307 // Initialize view parameters
308 FG_LOG( FG_GENERAL, FG_DEBUG, "Before v->init()");
310 FG_LOG( FG_GENERAL, FG_DEBUG, "After v->init()");
311 v->UpdateViewMath(f);
312 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = " << v->get_abs_view_pos());
313 v->UpdateWorldToEye(f);
315 // Build the solar system
316 //fgSolarSystemInit(*t);
317 FG_LOG(FG_GENERAL, FG_INFO, "Building SolarSystem");
318 SolarSystem::theSolarSystem = new SolarSystem(t);
320 // Initialize the Stars subsystem
321 if( fgStarsInit() ) {
322 // Stars initialized ok.
324 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Stars initialization!" );
328 // Initialize the planetary subsystem
329 // global_events.Register( "fgPlanetsInit()", fgPlanetsInit,
330 // fgEVENT::FG_EVENT_READY, 600000);
332 // Initialize the sun's position
333 // global_events.Register( "fgSunInit()", fgSunInit,
334 // fgEVENT::FG_EVENT_READY, 30000 );
336 // Intialize the moon's position
337 // global_events.Register( "fgMoonInit()", fgMoonInit,
338 // fgEVENT::FG_EVENT_READY, 600000 );
340 // register the periodic update of Sun, moon, and planets
341 global_events.Register( "ssolsysUpdate", solarSystemRebuild,
342 fgEVENT::FG_EVENT_READY, 600000);
344 // fgUpdateSunPos() needs a few position and view parameters set
345 // so it can calculate local relative sun angle and a few other
346 // things for correctly orienting the sky.
349 global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
350 fgEVENT::FG_EVENT_READY, 60000);
351 global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
352 fgEVENT::FG_EVENT_READY, 60000);
354 // Initialize Lighting interpolation tables
357 // update the lighting parameters (based on sun angle)
358 global_events.Register( "fgLight::Update()",
359 fgMethodCallback<fgLIGHT>( &cur_light_params,
361 fgEVENT::FG_EVENT_READY, 30000 );
363 // Initialize the weather modeling subsystem
364 current_weather.Init();
366 // Initialize the Cockpit subsystem
367 if( fgCockpitInit( ¤t_aircraft )) {
368 // Cockpit initialized ok.
370 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Cockpit initialization!" );
374 // Initialize the "sky"
377 // Initialize the flight model subsystem data structures base on
380 fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
381 1.0 / DEFAULT_MODEL_HZ );
383 // I'm just sticking this here for now, it should probably move
385 scenery.cur_elev = f->get_Runway_altitude() * FEET_TO_METER;
387 if ( f->get_Altitude() < f->get_Runway_altitude() + 3.758099) {
388 f->set_Altitude( f->get_Runway_altitude() + 3.758099 );
391 FG_LOG( FG_GENERAL, FG_INFO,
392 "Updated position (after elevation adj): ("
393 << (f->get_Latitude() * RAD_TO_DEG) << ", "
394 << (f->get_Longitude() * RAD_TO_DEG) << ", "
395 << (f->get_Altitude() * FEET_TO_METER) << ")" );
396 // end of thing that I just stuck in that I should probably move
399 if ( fgJoystickInit() ) {
400 // Joystick initialized ok.
402 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Joystick initialization!" );
405 // Autopilot init added here, by Jeff Goeke-Smith
406 fgAPInit(¤t_aircraft);
408 // Initialize serial ports
411 FG_LOG( FG_GENERAL, FG_INFO, endl);