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.
24 // (Log is kept at end of this file)
34 #include <Include/fg_constants.h>
35 #include <Include/general.h>
37 #include <Aircraft/aircraft.h>
38 #include <Astro/moon.hxx>
39 #include <Astro/planets.hxx>
40 #include <Astro/sky.hxx>
41 #include <Astro/stars.hxx>
42 #include <Astro/sun.hxx>
43 #include <Autopilot/autopilot.h>
44 #include <Cockpit/cockpit.hxx>
45 #include <Debug/fg_debug.h>
46 #include <Joystick/joystick.h>
47 #include <Math/fg_random.h>
48 #include <Scenery/scenery.h>
49 #include <Scenery/tilemgr.hxx>
50 #include <Time/event.hxx>
51 #include <Time/fg_time.hxx>
52 #include <Time/light.hxx>
53 #include <Time/sunpos.hxx>
54 #include <Weather/weather.h>
56 #include "airports.hxx"
57 #include "fg_init.hxx"
58 #include "options.hxx"
61 // extern int show_hud; // HUD state
62 extern int displayInstruments;
63 extern const char *default_root;
66 // Set initial position
67 int fgInitPosition( void ) {
71 f = current_aircraft.flight;
74 // Initial Position at (P13) Globe, AZ
75 FG_Longitude = ( -110.6642444 ) * DEG_TO_RAD;
76 FG_Latitude = ( 33.3528917 ) * DEG_TO_RAD;
77 FG_Runway_altitude = (3234.5 + 300);
78 FG_Altitude = FG_Runway_altitude + 3.758099;
80 // Initial Position at (E81) Superior, AZ
81 // FG_Longitude = ( -111.1270650 ) * DEG_TO_RAD;
82 // FG_Latitude = ( 33.2778339 ) * DEG_TO_RAD;
83 // FG_Runway_altitude = (2646 + 100);
84 // FG_Altitude = FG_Runway_altitude + 3.758099;
86 // Initial Position at (TUS) Tucson, AZ
87 // FG_Longitude = ( -110.9412597 ) * DEG_TO_RAD;
88 // FG_Latitude = ( 32.1162439 ) * DEG_TO_RAD;
89 // FG_Runway_altitude = (2641 + 0);
90 // FG_Altitude = FG_Runway_altitude + 3.758099;
92 // Initial Position at (SEZ) SEDONA airport
93 // FG_Longitude = (-111.7884614 + 0.01) * DEG_TO_RAD;
94 // FG_Latitude = ( 34.8486289 - 0.015) * DEG_TO_RAD;
95 // FG_Runway_altitude = (4827 + 450);
96 // FG_Altitude = FG_Runway_altitude + 3.758099;
98 // Initial Position at near Anchoraze, AK
99 // FG_Longitude = ( -152.00 ) * DEG_TO_RAD;
100 // FG_Latitude = ( 61.17 ) * DEG_TO_RAD;
101 // FG_Runway_altitude = (0);
102 // FG_Altitude = FG_Runway_altitude + 3.758099;
104 // Initial Position at (HSP) Hot Springs, VA
105 // FG_Longitude = (-79.8338964 + 0.01) * DEG_TO_RAD;
106 // FG_Latitude = ( 37.9514564 + 0.008) * DEG_TO_RAD;
107 // FG_Runway_altitude = (3792 + 1300);
108 // FG_Altitude = FG_Runway_altitude + 3.758099;
110 // Initial Position at (ANE) Anoka County airport
111 // FG_Longitude = -93.2113889 * DEG_TO_RAD;
112 // FG_Latitude = 45.145 * DEG_TO_RAD;
113 // FG_Runway_altitude = 912;
114 // FG_Altitude = FG_Runway_altitude + 3.758099;
116 // Initial Position north of the city of Globe
117 // FG_Longitude = ( -398673.28 / 3600.0 ) * DEG_TO_RAD;
118 // FG_Latitude = ( 120625.64 / 3600.0 ) * DEG_TO_RAD;
119 // FG_Longitude = ( -397867.44 / 3600.0 ) * DEG_TO_RAD;
120 // FG_Latitude = ( 119548.21 / 3600.0 ) * DEG_TO_RAD;
121 // FG_Altitude = 0.0 + 3.758099;
123 // Initial Position near where I used to live in Globe, AZ
124 // FG_Longitude = ( -398757.6 / 3600.0 ) * DEG_TO_RAD;
125 // FG_Latitude = ( 120160.0 / 3600.0 ) * DEG_TO_RAD;
126 // FG_Runway_altitude = 4000.0;
127 // FG_Altitude = FG_Runway_altitude + 3.758099;
129 // Initial Position: 10125 Jewell St. NE
130 // FG_Longitude = ( -93.15 ) * DEG_TO_RAD;
131 // FG_Latitude = ( 45.15 ) * DEG_TO_RAD;
132 // FG_Runway_altitude = 950.0;
133 // FG_Altitude = FG_Runway_altitude + 3.758099;
135 // Initial Position: Somewhere near the Grand Canyon
136 // FG_Longitude = ( -112.5 ) * DEG_TO_RAD;
137 // FG_Latitude = ( 36.5 ) * DEG_TO_RAD;
138 // FG_Runway_altitude = 8000.0;
139 // FG_Altitude = FG_Runway_altitude + 3.758099;
141 // Initial Position: (GCN) Grand Canyon Airport, AZ
142 // FG_Longitude = ( -112.1469647 ) * DEG_TO_RAD;
143 // FG_Latitude = ( 35.9523539 ) * DEG_TO_RAD;
144 // FG_Runway_altitude = 6606.0;
145 // FG_Altitude = FG_Runway_altitude + 3.758099;
147 // Initial Position: Jim Brennon's Kingmont Observatory
148 // FG_Longitude = ( -121.1131667 ) * DEG_TO_RAD;
149 // FG_Latitude = ( 38.8293917 ) * DEG_TO_RAD;
150 // FG_Runway_altitude = 920.0;
151 // FG_Altitude = FG_Runway_altitude + 3.758099;
153 // probably interesting for european team members
154 // That is: If I can get the scenery to work -;) (Durk)
156 // Initial Position: Groningen Airport Eelde, the netherlands
157 // FG_Longitude = ( 6.583333 ) * DEG_TO_RAD;
158 // FG_Latitude = ( 53.125 ) * DEG_TO_RAD;
159 // FG_Runway_altitude = 920.0;
160 // FG_Altitude = FG_Runway_altitude + 3.758099;
162 // Initial Position: Schiphol Amsterdam Airport, the netherlands
163 // FG_Longitude = ( -4.7641667 ) * DEG_TO_RAD;
164 // FG_Latitude = ( 52.308056 ) * DEG_TO_RAD;
165 // FG_Runway_altitude = 920.0;
166 // FG_Altitude = FG_Runway_altitude + 3.758099;
168 // Eclipse Watching w73.5 n10 (approx) 18:00 UT
169 // FG_Longitude = ( -73.5 ) * DEG_TO_RAD;
170 // FG_Latitude = ( 10.0 ) * DEG_TO_RAD;
171 // FG_Runway_altitude = 0.0;
172 // FG_Altitude = FG_Runway_altitude + 3.758099;
175 // FG_Longitude = ( -109.5 ) * DEG_TO_RAD;
176 // FG_Latitude = ( 32.5 ) * DEG_TO_RAD;
177 // FG_Runway_altitude = (2646 + 2000);
178 // FG_Altitude = FG_Runway_altitude + 3.758099;
180 if ( strlen(o->airport_id) ) {
184 fgPrintf( FG_GENERAL, FG_INFO,
185 "Attempting to set starting position from airport code %s.\n",
188 airports.load("Airports");
189 a = airports.search(o->airport_id);
190 if ( strcmp(a.id, "none") == 0 ) {
191 fgPrintf( FG_GENERAL, FG_INFO,
192 "Failed to find %s in database.\n", o->airport_id);
194 FG_Longitude = ( a.longitude ) * DEG_TO_RAD;
195 FG_Latitude = ( a.latitude ) * DEG_TO_RAD;
196 FG_Runway_altitude = ( a.elevation + 300 );
197 FG_Altitude = FG_Runway_altitude + 3.758099;
201 fgPrintf( FG_GENERAL, FG_INFO,
202 "Initial position is: (%.4f, %.4f, %.2f)\n",
203 FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG,
204 FG_Altitude * FEET_TO_METER);
208 // General house keeping initializations
209 int fgInitGeneral( void ) {
214 // determine the fg root path.
215 if( !(g->root_dir) ) {
216 // If not set by command line test for environmental var..
217 g->root_dir = getenv("FG_ROOT");
218 if ( !g->root_dir ) {
219 // No root path set? Then assume, we will exit if this is
220 // wrong when looking for support files.
221 fgPrintf( FG_GENERAL, FG_EXIT, "%s %s\n",
222 "Cannot continue without environment variable FG_ROOT",
226 fgPrintf( FG_GENERAL, FG_INFO, "FG_ROOT = %s\n\n", g->root_dir);
228 fgPrintf( FG_GENERAL, FG_INFO, "General Initialization\n" );
229 fgPrintf( FG_GENERAL, FG_INFO, "======= ==============\n" );
231 // seed the random number generater
238 // This is the top level init routine which calls all the other
239 // initialization routines. If you are adding a subsystem to flight
240 // gear, its initialization call should located in this routine.
241 // Returns non-zero if a problem encountered.
242 int fgInitSubsystems( void ) {
250 l = &cur_light_params;
251 t = &cur_time_params;
254 fgPrintf( FG_GENERAL, FG_INFO, "Initialize Subsystems\n");
255 fgPrintf( FG_GENERAL, FG_INFO, "========== ==========\n");
257 // The following section sets up the flight model EOM parameters
258 // and should really be read in from one or more files.
260 // Must happen before any of the flight model or control
261 // parameters are set
263 fgAircraftInit(); // In the future this might not be the case.
264 f = current_aircraft.flight;
266 // set the initial position
270 FG_V_north = 0.0; // 7.287719E+00
271 FG_V_east = 0.0; // 1.521770E+03
272 FG_V_down = 0.0; // -1.265722E-05
274 // Initial Orientation
275 FG_Phi = -2.658474E-06;
276 FG_Theta = 7.401790E-03;
277 FG_Psi = 270.0 * DEG_TO_RAD;
279 // Initial Angular B rates
280 FG_P_body = 7.206685E-05;
281 FG_Q_body = 0.000000E+00;
282 FG_R_body = 9.492658E-05;
284 FG_Earth_position_angle = 0.000000E+00;
286 // Mass properties and geometry values
287 FG_Mass = 8.547270E+01;
288 FG_I_xx = 1.048000E+03;
289 FG_I_yy = 3.000000E+03;
290 FG_I_zz = 3.530000E+03;
291 FG_I_xz = 0.000000E+00;
293 // CG position w.r.t. ref. point
294 FG_Dx_cg = 0.000000E+00;
295 FG_Dy_cg = 0.000000E+00;
296 FG_Dz_cg = 0.000000E+00;
298 // Set initial position and slew parameters
299 // fgSlewInit(-398391.3, 120070.41, 244, 3.1415); // GLOBE Airport
300 // fgSlewInit(-335340,162540, 15, 4.38);
301 // fgSlewInit(-398673.28,120625.64, 53, 4.38);
303 // Initialize the event manager
306 // Dump event stats every 60 seconds
307 fgEventRegister( "fgEventPrintStats()", fgEventPrintStats,
308 FG_EVENT_READY, 60000 );
314 // fgViewUpdate() needs the sun in the right place, while
315 // fgUpdateSunPos() needs to know the view position. I'll get
316 // around this interdependency for now by calling fgUpdateSunPos()
317 // once, then moving on with normal initialization.
320 // Initialize view parameters
322 fgViewUpdate(f, v, l);
324 // Initialize Lighting interpolation tables
327 // update the lighting parameters (based on sun angle)
328 fgEventRegister( "fgLightUpdate()", fgLightUpdate,
329 FG_EVENT_READY, 30000 );
331 // Initialize the weather modeling subsystem
334 // update the weather for our current position
335 fgEventRegister( "fgWeatherUpdate()", fgWeatherUpdate,
336 FG_EVENT_READY, 120000 );
338 // Initialize the Cockpit subsystem
339 if( fgCockpitInit( ¤t_aircraft )) {
340 // Cockpit initialized ok.
342 fgPrintf( FG_GENERAL, FG_EXIT, "Error in Cockpit initialization!\n" );
345 // Initialize the orbital elements of sun, moon and mayor planets
346 fgSolarSystemInit(*t);
348 // Initialize the Stars subsystem
349 if( fgStarsInit() ) {
350 // Stars initialized ok.
352 fgPrintf( FG_GENERAL, FG_EXIT, "Error in Stars initialization!\n" );
355 // Initialize the planetary subsystem
356 fgEventRegister("fgPlanetsInit()", fgPlanetsInit, FG_EVENT_READY, 600000);
358 // Initialize the sun's position
359 fgEventRegister("fgSunInit()", fgSunInit, FG_EVENT_READY, 30000 );
361 // Intialize the moon's position
362 fgEventRegister( "fgMoonInit()", fgMoonInit, FG_EVENT_READY, 600000 );
364 // Initialize the "sky"
367 // Initialize the Scenery Management subsystem
368 if ( fgSceneryInit() ) {
369 // Scenery initialized ok.
371 fgPrintf( FG_GENERAL, FG_EXIT, "Error in Scenery initialization!\n" );
375 if( fgTileMgrInit() ) {
376 // Load the local scenery data
379 fgPrintf( FG_GENERAL, FG_EXIT,
380 "Error in Tile Manager initialization!\n" );
383 // I'm just sticking this here for now, it should probably move
385 // cur_elev = mesh_altitude(FG_Longitude * RAD_TO_DEG * 3600.0,
386 // FG_Latitude * RAD_TO_DEG * 3600.0);
387 // fgPrintf( FG_GENERAL, FG_INFO,
388 // "True ground elevation is %.2f meters here.\n",
391 cur_elev = FG_Runway_altitude * FEET_TO_METER;
392 if ( cur_elev > -9990.0 ) {
393 FG_Runway_altitude = cur_elev * METER_TO_FEET;
396 if ( FG_Altitude < FG_Runway_altitude ) {
397 FG_Altitude = FG_Runway_altitude + 3.758099;
400 fgPrintf(FG_GENERAL, FG_INFO,
401 "Updated position (after elevation adj): (%.4f, %.4f, %.2f)\n",
402 FG_Latitude * RAD_TO_DEG, FG_Longitude * RAD_TO_DEG,
403 FG_Altitude * FEET_TO_METER);
405 // end of thing that I just stuck in that I should probably move
407 // Initialize the flight model subsystem data structures base on
410 fgFlightModelInit( FG_LARCSIM, f, 1.0 / DEFAULT_MODEL_HZ );
412 // To HUD or not to HUD - Now a command line issue
415 // Let's not show the instrument panel
416 displayInstruments = 0;
419 if (fgJoystickInit(0) ) {
420 // Joystick initialized ok.
422 fgPrintf( FG_GENERAL, FG_EXIT, "Error in Joystick initialization!\n" );
425 // Autopilot init added here, by Jeff Goeke-Smith
426 fgAPInit(¤t_aircraft);
428 // One more try here to get the sky synced up
431 fgPrintf(FG_GENERAL, FG_INFO,"\n");
438 // Revision 1.3 1998/04/25 15:11:11 curt
439 // Added an command line option to set starting position based on airport ID.
441 // Revision 1.2 1998/04/24 00:49:20 curt
442 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
443 // Trying out some different option parsing code.
444 // Some code reorganization.
446 // Revision 1.1 1998/04/22 13:25:44 curt
447 // C++ - ifing the code.
448 // Starting a bit of reorganization of lighting code.
450 // Revision 1.56 1998/04/18 04:11:28 curt
451 // Moved fg_debug to it's own library, added zlib support.
453 // Revision 1.55 1998/04/14 02:21:03 curt
454 // Incorporated autopilot heading hold contributed by: Jeff Goeke-Smith
455 // <jgoeke@voyager.net>
457 // Revision 1.54 1998/04/08 23:35:36 curt
458 // Tweaks to Gnu automake/autoconf system.
460 // Revision 1.53 1998/04/03 22:09:06 curt
461 // Converting to Gnu autoconf system.
463 // Revision 1.52 1998/03/23 21:24:38 curt
464 // Source code formating tweaks.
466 // Revision 1.51 1998/03/14 00:31:22 curt
467 // Beginning initial terrain texturing experiments.
469 // Revision 1.50 1998/03/09 22:46:19 curt
472 // Revision 1.49 1998/02/23 19:07:59 curt
473 // Incorporated Durk's Astro/ tweaks. Includes unifying the sun position
474 // calculation code between sun display, and other FG sections that use this
475 // for things like lighting.
477 // Revision 1.48 1998/02/21 14:53:15 curt
478 // Added Charlie's HUD changes.
480 // Revision 1.47 1998/02/19 13:05:53 curt
481 // Incorporated some HUD tweaks from Michelle America.
482 // Tweaked the sky's sunset/rise colors.
483 // Other misc. tweaks.
485 // Revision 1.46 1998/02/18 15:07:06 curt
486 // Tweaks to build with SGI OpenGL (and therefor hopefully other accelerated
487 // drivers will work.)
489 // Revision 1.45 1998/02/16 13:39:43 curt
490 // Miscellaneous weekend tweaks. Fixed? a cache problem that caused whole
491 // tiles to occasionally be missing.
493 // Revision 1.44 1998/02/12 21:59:50 curt
494 // Incorporated code changes contributed by Charlie Hotchkiss
495 // <chotchkiss@namg.us.anritsu.com>
497 // Revision 1.43 1998/02/11 02:50:40 curt
500 // Revision 1.42 1998/02/09 22:56:58 curt
501 // Removed "depend" files from cvs control. Other minor make tweaks.
503 // Revision 1.41 1998/02/09 15:07:50 curt
506 // Revision 1.40 1998/02/07 15:29:44 curt
507 // Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
508 // <chotchkiss@namg.us.anritsu.com>
510 // Revision 1.39 1998/02/03 23:20:25 curt
511 // Lots of little tweaks to fix various consistency problems discovered by
512 // Solaris' CC. Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
513 // passed arguments along to the real printf(). Also incorporated HUD changes
514 // by Michele America.
516 // Revision 1.38 1998/02/02 20:53:58 curt
517 // Incorporated Durk's changes.
519 // Revision 1.37 1998/02/01 03:39:54 curt
522 // Revision 1.36 1998/01/31 00:43:13 curt
523 // Added MetroWorks patches from Carmen Volpe.
525 // Revision 1.35 1998/01/27 00:47:57 curt
526 // Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
527 // system and commandline/config file processing code.
529 // Revision 1.34 1998/01/22 02:59:37 curt
530 // Changed #ifdef FILE_H to #ifdef _FILE_H
532 // Revision 1.33 1998/01/21 21:11:34 curt
535 // Revision 1.32 1998/01/19 19:27:08 curt
536 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
537 // This should simplify things tremendously.
539 // Revision 1.31 1998/01/19 18:40:32 curt
540 // Tons of little changes to clean up the code and to remove fatal errors
541 // when building with the c++ compiler.
543 // Revision 1.30 1998/01/13 00:23:09 curt
544 // Initial changes to support loading and management of scenery tiles. Note,
545 // there's still a fair amount of work left to be done.
547 // Revision 1.29 1998/01/08 02:22:08 curt
548 // Beginning to integrate Tile management subsystem.
550 // Revision 1.28 1998/01/07 03:18:58 curt
551 // Moved astronomical stuff from .../Src/Scenery to .../Src/Astro/
553 // Revision 1.27 1998/01/05 18:44:35 curt
554 // Add an option to advance/decrease time from keyboard.
556 // Revision 1.26 1997/12/30 23:09:04 curt
557 // Tweaking initialization sequences.
559 // Revision 1.25 1997/12/30 22:22:33 curt
560 // Further integration of event manager.
562 // Revision 1.24 1997/12/30 20:47:44 curt
563 // Integrated new event manager with subsystem initializations.
565 // Revision 1.23 1997/12/30 16:36:50 curt
566 // Merged in Durk's changes ...
568 // Revision 1.22 1997/12/19 23:34:05 curt
569 // Lot's of tweaking with sky rendering and lighting.
571 // Revision 1.21 1997/12/19 16:45:00 curt
572 // Working on scene rendering order and options.
574 // Revision 1.20 1997/12/18 23:32:33 curt
575 // First stab at sky dome actually starting to look reasonable. :-)
577 // Revision 1.19 1997/12/17 23:13:36 curt
578 // Began working on rendering a sky.
580 // Revision 1.18 1997/12/15 23:54:49 curt
581 // Add xgl wrappers for debugging.
582 // Generate terrain normals on the fly.
584 // Revision 1.17 1997/12/15 20:59:09 curt
587 // Revision 1.16 1997/12/12 19:52:48 curt
588 // Working on lightling and material properties.
590 // Revision 1.15 1997/12/11 04:43:55 curt
591 // Fixed sun vector and lighting problems. I thing the moon is now lit
594 // Revision 1.14 1997/12/10 22:37:47 curt
595 // Prepended "fg" on the name of all global structures that didn't have it yet.
596 // i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
598 // Revision 1.13 1997/11/25 19:25:32 curt
599 // Changes to integrate Durk's moon/sun code updates + clean up.
601 // Revision 1.12 1997/11/15 18:16:35 curt
604 // Revision 1.11 1997/10/30 12:38:42 curt
605 // Working on new scenery subsystem.
607 // Revision 1.10 1997/10/25 03:24:23 curt
608 // Incorporated sun, moon, and star positioning code contributed by Durk Talsma.
610 // Revision 1.9 1997/09/23 00:29:39 curt
611 // Tweaks to get things to compile with gcc-win32.
613 // Revision 1.8 1997/09/22 14:44:20 curt
614 // Continuing to try to align stars correctly.
616 // Revision 1.7 1997/09/16 15:50:30 curt
617 // Working on star alignment and time issues.
619 // Revision 1.6 1997/09/05 14:17:30 curt
620 // More tweaking with stars.
622 // Revision 1.5 1997/09/04 02:17:36 curt
625 // Revision 1.4 1997/08/27 21:32:26 curt
626 // Restructured view calculation code. Added stars.
628 // Revision 1.3 1997/08/27 03:30:19 curt
629 // Changed naming scheme of basic shared structures.
631 // Revision 1.2 1997/08/25 20:27:23 curt
632 // Merged in initial HUD and Joystick code.
634 // Revision 1.1 1997/08/23 01:46:20 curt