]> git.mxchange.org Git - flightgear.git/blob - Simulator/Main/fg_init.cxx
Removed in-src cvs logs.
[flightgear.git] / Simulator / 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 #include <GL/glut.h>
31 #include <XGL/xgl.h>
32
33 #include <stdio.h>
34 #include <stdlib.h>
35
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.
38 #ifdef linux
39 #  define _G_NO_EXTERN_TEMPLATES
40 #endif
41
42 #include <Include/compiler.h>
43
44 #include STL_STRING
45
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>
68
69 #include "fg_init.hxx"
70 #include "options.hxx"
71 #include "views.hxx"
72 #include "fg_serial.hxx"
73
74 #if defined(FX) && defined(XMESA)
75 #include <GL/xmesa.h>
76 #endif
77
78 FG_USING_STD(string);
79
80 extern const char *default_root;
81
82
83 // Set initial position and orientation
84 int fgInitPosition( void ) {
85     string id;
86     FGInterface *f;
87
88     f = current_aircraft.fdm_state;
89
90     id = current_options.get_airport_id();
91     if ( id.length() ) {
92         // set initial position from airport id
93
94         fgAIRPORTS airports;
95         fgAIRPORT a;
96
97         FG_LOG( FG_GENERAL, FG_INFO,
98                 "Attempting to set starting position from airport code "
99                 << id );
100
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." );
105             exit(-1);
106         } else {
107             f->set_Longitude( a.longitude * DEG_TO_RAD );
108             f->set_Latitude( a.latitude * DEG_TO_RAD );
109         }
110     } else {
111         // set initial position from default or command line coordinates
112
113         f->set_Longitude( current_options.get_lon() * DEG_TO_RAD );
114         f->set_Latitude( current_options.get_lat() * DEG_TO_RAD );
115     }
116
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);
119
120     FG_LOG( FG_GENERAL, FG_INFO,
121             "starting altitude is = " << current_options.get_altitude() );
122
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 );
126
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) << ")" );
132
133     return(1);
134 }
135
136
137 // General house keeping initializations
138 int fgInitGeneral( void ) {
139     string root;
140     char *mesa_win_state;
141
142     FG_LOG( FG_GENERAL, FG_INFO, "General Initialization" );
143     FG_LOG( FG_GENERAL, FG_INFO, "======= ==============" );
144
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." );
151         exit(-1);
152     }
153     FG_LOG( FG_GENERAL, FG_INFO, "FG_ROOT = " << root << endl );
154
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;
165             }
166         }
167     }
168 #endif
169
170     return ( 1 );
171 }
172
173
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 )
179 {
180     FGInterface *f; // assigned later
181     fgLIGHT *l = &cur_light_params;
182     fgTIME *t = &cur_time_params;
183     FGView *v = &current_view;
184
185     FG_LOG( FG_GENERAL, FG_INFO, "Initialize Subsystems");
186     FG_LOG( FG_GENERAL, FG_INFO, "========== ==========");
187
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;
192
193     // set the initial position
194     fgInitPosition();
195
196     // Initialize the Scenery Management subsystem
197     if ( fgSceneryInit() ) {
198         // Scenery initialized ok.
199     } else {
200         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
201         exit(-1);
202     }
203
204     if( fgTileMgrInit() ) {
205         // Load the local scenery data
206         fgTileMgrUpdate();
207     } else {
208         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
209         exit(-1);
210     }
211
212     FG_LOG( FG_GENERAL, FG_DEBUG,
213             "Current terrain elevation after tile mgr init " <<
214             scenery.cur_elev );
215
216     // Calculate ground elevation at starting point (we didn't have
217     // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
218     //
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
222     // yet :-)
223
224     // now handled inside of the fgTileMgrUpdate()
225
226     /*
227     geod_pos = Point3D( f->get_Longitude(), f->get_Latitude(), 0.0);
228     tmp_abs_view_pos = fgGeodToCart(geod_pos);
229
230     FG_LOG( FG_GENERAL, FG_DEBUG,
231             "Initial abs_view_pos = " << tmp_abs_view_pos );
232     scenery.cur_elev =
233         fgTileMgrCurElev( f->get_Longitude(), f->get_Latitude(),
234                           tmp_abs_view_pos );
235     FG_LOG( FG_GENERAL, FG_DEBUG,
236             "Altitude after update " << scenery.cur_elev );
237     */
238
239     fgFDMSetGroundElevation( current_options.get_flight_model(),
240                              scenery.cur_elev );
241
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() );
246
247     if ( f->get_Altitude() < f->get_Runway_altitude() + 3.758099) {
248         f->set_Altitude( f->get_Runway_altitude() + 3.758099 );
249     }
250
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) << ")" );
256
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.
260
261     double sea_level_radius_meters;
262     double lat_geoc;
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(),
267                                 f->get_Altitude() +
268                                 (sea_level_radius_meters * METER_TO_FEET) );
269     f->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
270
271     // The following section sets up the flight model EOM parameters
272     // and should really be read in from one or more files.
273
274     // Initial Velocity
275     f->set_Velocities_Local( 0.0, 0.0, 0.0 );
276
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 );
281
282     // Initial Angular Body rates
283     f->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
284
285     f->set_Earth_position_angle( 0.0 );
286
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 );
290
291     // CG position w.r.t. ref. point
292     f->set_CG_Position( 0.0, 0.0, 0.0 );
293
294     // Initialize the event manager
295     global_events.Init();
296
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 );
302
303     // Initialize the time dependent variables
304     fgTimeInit(t);
305     fgTimeUpdate(f, t);
306
307     // Initialize view parameters
308     FG_LOG( FG_GENERAL, FG_DEBUG, "Before v->init()");
309     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);
314
315     // Build the solar system
316     //fgSolarSystemInit(*t);
317     FG_LOG(FG_GENERAL, FG_INFO, "Building SolarSystem");
318     SolarSystem::theSolarSystem = new SolarSystem(t);
319
320     // Initialize the Stars subsystem
321     if( fgStarsInit() ) {
322         // Stars initialized ok.
323     } else {
324         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Stars initialization!" );
325         exit(-1);
326     }
327
328     // Initialize the planetary subsystem
329     // global_events.Register( "fgPlanetsInit()", fgPlanetsInit,
330     //                      fgEVENT::FG_EVENT_READY, 600000);
331
332     // Initialize the sun's position
333     // global_events.Register( "fgSunInit()", fgSunInit,
334     //                      fgEVENT::FG_EVENT_READY, 30000 );
335
336     // Intialize the moon's position
337     // global_events.Register( "fgMoonInit()", fgMoonInit,
338     //                      fgEVENT::FG_EVENT_READY, 600000 );
339
340     // register the periodic update of Sun, moon, and planets
341     global_events.Register( "ssolsysUpdate", solarSystemRebuild,
342                             fgEVENT::FG_EVENT_READY, 600000);
343
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.
347     fgUpdateSunPos();
348     fgUpdateMoonPos();
349     global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
350                             fgEVENT::FG_EVENT_READY, 60000);
351     global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
352                             fgEVENT::FG_EVENT_READY, 60000);
353
354     // Initialize Lighting interpolation tables
355     l->Init();
356
357     // update the lighting parameters (based on sun angle)
358     global_events.Register( "fgLight::Update()",
359                             fgMethodCallback<fgLIGHT>( &cur_light_params,
360                                                        &fgLIGHT::Update),
361                             fgEVENT::FG_EVENT_READY, 30000 );
362
363     // Initialize the weather modeling subsystem
364     current_weather.Init();
365
366     // Initialize the Cockpit subsystem
367     if( fgCockpitInit( &current_aircraft )) {
368         // Cockpit initialized ok.
369     } else {
370         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Cockpit initialization!" );
371         exit(-1);
372     }
373
374     // Initialize the "sky"
375     fgSkyInit();
376
377     // Initialize the flight model subsystem data structures base on
378     // above values
379
380     fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
381                        1.0 / DEFAULT_MODEL_HZ );
382
383     // I'm just sticking this here for now, it should probably move
384     // eventually
385     scenery.cur_elev = f->get_Runway_altitude() * FEET_TO_METER;
386
387     if ( f->get_Altitude() < f->get_Runway_altitude() + 3.758099) {
388         f->set_Altitude( f->get_Runway_altitude() + 3.758099 );
389     }
390
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
397
398     // Joystick support
399     if ( fgJoystickInit() ) {
400         // Joystick initialized ok.
401     } else {
402         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Joystick initialization!" );
403     }
404
405     // Autopilot init added here, by Jeff Goeke-Smith
406     fgAPInit(&current_aircraft);
407
408     // Initialize serial ports
409     fgSerialInit();
410
411     FG_LOG( FG_GENERAL, FG_INFO, endl);
412
413     return(1);
414 }
415
416