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>
44 #include <Autopilot/autopilot.h>
45 #include <Cockpit/cockpit.hxx>
46 #include <Debug/fg_debug.h>
47 #include <Joystick/joystick.h>
48 #include <Math/fg_random.h>
49 #include <Scenery/scenery.hxx>
50 #include <Scenery/tilemgr.hxx>
51 #include <Time/event.hxx>
52 #include <Time/fg_time.hxx>
53 #include <Time/light.hxx>
54 #include <Time/sunpos.hxx>
55 #include <Weather/weather.h>
57 #include "airports.hxx"
58 #include "fg_init.hxx"
59 #include "options.hxx"
62 // extern int show_hud; // HUD state
63 extern int displayInstruments;
64 extern const char *default_root;
67 // Set initial position
68 int fgInitPosition( void ) {
72 f = current_aircraft.flight;
75 // If nothing else is specified, default initial position is
77 FG_Longitude = ( -110.6642444 ) * DEG_TO_RAD;
78 FG_Latitude = ( 33.3528917 ) * DEG_TO_RAD;
79 FG_Runway_altitude = (3234.5 + 300);
80 FG_Altitude = FG_Runway_altitude + 3.758099;
82 // Initial Position north of the city of Globe
83 // FG_Longitude = ( -398673.28 / 3600.0 ) * DEG_TO_RAD;
84 // FG_Latitude = ( 120625.64 / 3600.0 ) * DEG_TO_RAD;
85 // FG_Longitude = ( -397867.44 / 3600.0 ) * DEG_TO_RAD;
86 // FG_Latitude = ( 119548.21 / 3600.0 ) * DEG_TO_RAD;
87 // FG_Altitude = 0.0 + 3.758099;
89 // Initial Position near where I used to live in Globe, AZ
90 // FG_Longitude = ( -398757.6 / 3600.0 ) * DEG_TO_RAD;
91 // FG_Latitude = ( 120160.0 / 3600.0 ) * DEG_TO_RAD;
92 // FG_Runway_altitude = 4000.0;
93 // FG_Altitude = FG_Runway_altitude + 3.758099;
95 // Initial Position: 10125 Jewell St. NE
96 // FG_Longitude = ( -93.15 ) * DEG_TO_RAD;
97 // FG_Latitude = ( 45.15 ) * DEG_TO_RAD;
98 // FG_Runway_altitude = 950.0;
99 // FG_Altitude = FG_Runway_altitude + 3.758099;
101 // Initial Position near KHSP (Hot Springs, VA)
102 // FG_Longitude = (-79.8338964 + 0.01) * DEG_TO_RAD;
103 // FG_Latitude = ( 37.9514564 + 0.008) * DEG_TO_RAD;
104 // FG_Runway_altitude = (3792 + 2800);
105 // FG_Altitude = FG_Runway_altitude + 3.758099;
107 // Initial Position at (SEZ) SEDONA airport
108 // FG_Longitude = (-111.7884614 + 0.01) * DEG_TO_RAD;
109 // FG_Latitude = ( 34.8486289 - 0.015) * DEG_TO_RAD;
110 // FG_Runway_altitude = (4827 + 450);
111 // FG_Altitude = FG_Runway_altitude + 3.758099;
113 // Initial Position: Somewhere near the Grand Canyon
114 // FG_Longitude = ( -112.5 ) * DEG_TO_RAD;
115 // FG_Latitude = ( 36.5 ) * DEG_TO_RAD;
116 // FG_Runway_altitude = 8000.0;
117 // FG_Altitude = FG_Runway_altitude + 3.758099;
119 // Initial Position: Jim Brennon's Kingmont Observatory
120 // FG_Longitude = ( -121.1131667 ) * DEG_TO_RAD;
121 // FG_Latitude = ( 38.8293917 ) * DEG_TO_RAD;
122 // FG_Runway_altitude = 920.0;
123 // FG_Altitude = FG_Runway_altitude + 3.758099;
125 // probably interesting for european team members
126 // That is: If I can get the scenery to work -;) (Durk)
128 // Eclipse Watching w73.5 n10 (approx) 18:00 UT
129 // FG_Longitude = ( -73.5 ) * DEG_TO_RAD;
130 // FG_Latitude = ( 10.0 ) * DEG_TO_RAD;
131 // FG_Runway_altitude = 0.0;
132 // FG_Altitude = FG_Runway_altitude + 3.758099;
135 // FG_Longitude = ( 8.5 ) * DEG_TO_RAD;
136 // FG_Latitude = ( 47.5 ) * DEG_TO_RAD;
137 FG_Runway_altitude = ( 6000 );
138 FG_Altitude = FG_Runway_altitude + 3.758099;
140 if ( strlen(o->airport_id) ) {
144 fgPrintf( FG_GENERAL, FG_INFO,
145 "Attempting to set starting position from airport code %s.\n",
148 airports.load("Airports");
149 a = airports.search(o->airport_id);
150 if ( (fabs(a.longitude) < FG_EPSILON) &&
151 (fabs(a.latitude) < FG_EPSILON) &&
152 (fabs(a.elevation) < FG_EPSILON) ) {
153 fgPrintf( FG_GENERAL, FG_EXIT,
154 "Failed to find %s in database.\n", o->airport_id);
156 FG_Longitude = ( a.longitude ) * DEG_TO_RAD;
157 FG_Latitude = ( a.latitude ) * DEG_TO_RAD;
158 FG_Runway_altitude = ( a.elevation + 300 );
159 FG_Altitude = FG_Runway_altitude + 3.758099;
163 fgPrintf( FG_GENERAL, FG_INFO,
164 "Initial position is: (%.4f, %.4f, %.2f)\n",
165 FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG,
166 FG_Altitude * FEET_TO_METER);
172 // General house keeping initializations
173 int fgInitGeneral( void ) {
179 o = ¤t_options;
181 fgPrintf( FG_GENERAL, FG_INFO, "General Initialization\n" );
182 fgPrintf( FG_GENERAL, FG_INFO, "======= ==============\n" );
184 if ( !strlen(o->fg_root) ) {
185 // No root path set? Then assume, we will exit if this is
186 // wrong when looking for support files.
187 fgPrintf( FG_GENERAL, FG_EXIT, "%s %s\n",
188 "Cannot continue without environment variable FG_ROOT",
191 fgPrintf( FG_GENERAL, FG_INFO, "FG_ROOT = %s\n\n", o->fg_root);
193 // prime the frame rate counter pump
194 for ( i = 0; i < FG_FRAME_RATE_HISTORY; i++ ) {
202 // This is the top level init routine which calls all the other
203 // initialization routines. If you are adding a subsystem to flight
204 // gear, its initialization call should located in this routine.
205 // Returns non-zero if a problem encountered.
206 int fgInitSubsystems( void ) {
214 l = &cur_light_params;
215 t = &cur_time_params;
218 fgPrintf( FG_GENERAL, FG_INFO, "Initialize Subsystems\n");
219 fgPrintf( FG_GENERAL, FG_INFO, "========== ==========\n");
221 // seed the random number generater
224 // The following section sets up the flight model EOM parameters
225 // and should really be read in from one or more files.
227 // Must happen before any of the flight model or control
228 // parameters are set
230 fgAircraftInit(); // In the future this might not be the case.
231 f = current_aircraft.flight;
233 // set the initial position
237 FG_V_north = 0.0; // 7.287719E+00
238 FG_V_east = 0.0; // 1.521770E+03
239 FG_V_down = 0.0; // -1.265722E-05
241 // Initial Orientation
242 FG_Phi = -2.658474E-06;
243 FG_Theta = 7.401790E-03;
244 FG_Psi = 270.0 * DEG_TO_RAD;
246 // Initial Angular B rates
247 FG_P_body = 7.206685E-05;
248 FG_Q_body = 0.000000E+00;
249 FG_R_body = 9.492658E-05;
251 FG_Earth_position_angle = 0.000000E+00;
253 // Mass properties and geometry values
254 FG_Mass = 8.547270E+01;
255 FG_I_xx = 1.048000E+03;
256 FG_I_yy = 3.000000E+03;
257 FG_I_zz = 3.530000E+03;
258 FG_I_xz = 0.000000E+00;
260 // CG position w.r.t. ref. point
261 FG_Dx_cg = 0.000000E+00;
262 FG_Dy_cg = 0.000000E+00;
263 FG_Dz_cg = 0.000000E+00;
265 // Initialize the event manager
266 global_events.Init();
268 // Output event stats every 60 seconds
269 global_events.Register( "fgEventPrintStats()", fgEventPrintStats,
270 FG_EVENT_READY, 60000 );
272 // Initialize the time dependent variables
276 // Initialize view parameters
279 v->UpdateWorldToEye(f);
281 // Initialize the orbital elements of sun, moon and mayor planets
282 fgSolarSystemInit(*t);
284 // Initialize the Stars subsystem
285 if( fgStarsInit() ) {
286 // Stars initialized ok.
288 fgPrintf( FG_GENERAL, FG_EXIT, "Error in Stars initialization!\n" );
291 // Initialize the planetary subsystem
292 global_events.Register( "fgPlanetsInit()", fgPlanetsInit,
293 FG_EVENT_READY, 600000);
295 // Initialize the sun's position
296 global_events.Register( "fgSunInit()", fgSunInit,
297 FG_EVENT_READY, 30000 );
299 // Intialize the moon's position
300 global_events.Register( "fgMoonInit()", fgMoonInit,
301 FG_EVENT_READY, 600000 );
303 // fgUpdateSunPos() needs a few position and view parameters set
304 // so it can calculate local relative sun angle and a few other
305 // things for correctly orienting the sky.
308 // Initialize Lighting interpolation tables
311 // update the lighting parameters (based on sun angle)
312 global_events.Register( "fgLightUpdate()", fgLightUpdate,
313 FG_EVENT_READY, 30000 );
315 // Initialize the weather modeling subsystem
318 // update the weather for our current position
319 global_events.Register( "fgWeatherUpdate()", fgWeatherUpdate,
320 FG_EVENT_READY, 120000 );
322 // Initialize the Cockpit subsystem
323 if( fgCockpitInit( ¤t_aircraft )) {
324 // Cockpit initialized ok.
326 fgPrintf( FG_GENERAL, FG_EXIT, "Error in Cockpit initialization!\n" );
329 // Initialize the "sky"
332 // Initialize the Scenery Management subsystem
333 if ( fgSceneryInit() ) {
334 // Scenery initialized ok.
336 fgPrintf( FG_GENERAL, FG_EXIT, "Error in Scenery initialization!\n" );
340 if( fgTileMgrInit() ) {
341 // Load the local scenery data
344 fgPrintf( FG_GENERAL, FG_EXIT,
345 "Error in Tile Manager initialization!\n" );
348 // I'm just sticking this here for now, it should probably move
350 cur_elev = FG_Runway_altitude * FEET_TO_METER;
351 if ( cur_elev > -9990.0 ) {
352 FG_Runway_altitude = cur_elev * METER_TO_FEET;
355 if ( FG_Altitude < FG_Runway_altitude ) {
356 FG_Altitude = FG_Runway_altitude + 3.758099;
359 fgPrintf( FG_GENERAL, FG_INFO,
360 "Updated position (after elevation adj): (%.4f, %.4f, %.2f)\n",
361 FG_Latitude * RAD_TO_DEG, FG_Longitude * RAD_TO_DEG,
362 FG_Altitude * FEET_TO_METER);
363 // end of thing that I just stuck in that I should probably move
365 // Initialize the flight model subsystem data structures base on
368 fgFlightModelInit( FG_LARCSIM, f, 1.0 / DEFAULT_MODEL_HZ );
370 // Let's not show the instrument panel
371 displayInstruments = 0;
374 if (fgJoystickInit(0) ) {
375 // Joystick initialized ok.
377 fgPrintf( FG_GENERAL, FG_EXIT, "Error in Joystick initialization!\n" );
380 // Autopilot init added here, by Jeff Goeke-Smith
381 fgAPInit(¤t_aircraft);
383 fgPrintf(FG_GENERAL, FG_INFO,"\n");
390 // Revision 1.17 1998/06/01 17:54:42 curt
391 // Added Linux audio support.
392 // avoid glClear( COLOR_BUFFER_BIT ) when not using it to set the sky color.
395 // Revision 1.16 1998/05/29 20:37:24 curt
396 // Tweaked material properties & lighting a bit in GLUTmain.cxx.
397 // Read airport list into a "map" STL for dynamic list sizing and fast tree
400 // Revision 1.15 1998/05/22 21:28:53 curt
401 // Modifications to use the new fgEVENT_MGR class.
403 // Revision 1.14 1998/05/20 20:51:35 curt
404 // Tweaked smooth shaded texture lighting properties.
405 // Converted fgLIGHT to a C++ class.
407 // Revision 1.13 1998/05/16 13:08:35 curt
408 // C++ - ified views.[ch]xx
409 // Shuffled some additional view parameters into the fgVIEW class.
410 // Changed tile-radius to tile-diameter because it is a much better
412 // Added a WORLD_TO_EYE transformation to views.cxx. This allows us
413 // to transform world space to eye space for view frustum culling.
415 // Revision 1.12 1998/05/13 18:29:58 curt
416 // Added a keyboard binding to dynamically adjust field of view.
417 // Added a command line option to specify fov.
418 // Adjusted terrain color.
419 // Root path info moved to fgOPTIONS.
420 // Added ability to parse options out of a config file.
422 // Revision 1.11 1998/05/07 23:14:15 curt
423 // Added "D" key binding to set autopilot heading.
424 // Made frame rate calculation average out over last 10 frames.
425 // Borland C++ floating point exception workaround.
426 // Added a --tile-radius=n option.
428 // Revision 1.10 1998/05/06 03:16:24 curt
429 // Added an averaged global frame rate counter.
430 // Added an option to control tile radius.
432 // Revision 1.9 1998/05/03 00:47:31 curt
433 // Added an option to enable/disable full-screen mode.
435 // Revision 1.8 1998/04/30 12:34:18 curt
436 // Added command line rendering options:
437 // enable/disable fog/haze
438 // specify smooth/flat shading
439 // disable sky blending and just use a solid color
440 // enable wireframe drawing mode
442 // Revision 1.7 1998/04/28 01:20:22 curt
443 // Type-ified fgTIME and fgVIEW.
444 // Added a command line option to disable textures.
446 // Revision 1.6 1998/04/26 05:10:03 curt
447 // "struct fgLIGHT" -> "fgLIGHT" because fgLIGHT is typedef'd.
449 // Revision 1.5 1998/04/25 22:06:30 curt
450 // Edited cvs log messages in source files ... bad bad bad!
452 // Revision 1.4 1998/04/25 20:24:01 curt
453 // Cleaned up initialization sequence to eliminate interdependencies
454 // between sun position, lighting, and view position. This creates a
455 // valid single pass initialization path.
457 // Revision 1.3 1998/04/25 15:11:11 curt
458 // Added an command line option to set starting position based on airport ID.
460 // Revision 1.2 1998/04/24 00:49:20 curt
461 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
462 // Trying out some different option parsing code.
463 // Some code reorganization.
465 // Revision 1.1 1998/04/22 13:25:44 curt
466 // C++ - ifing the code.
467 // Starting a bit of reorganization of lighting code.
469 // Revision 1.56 1998/04/18 04:11:28 curt
470 // Moved fg_debug to it's own library, added zlib support.
472 // Revision 1.55 1998/04/14 02:21:03 curt
473 // Incorporated autopilot heading hold contributed by: Jeff Goeke-Smith
474 // <jgoeke@voyager.net>
476 // Revision 1.54 1998/04/08 23:35:36 curt
477 // Tweaks to Gnu automake/autoconf system.
479 // Revision 1.53 1998/04/03 22:09:06 curt
480 // Converting to Gnu autoconf system.
482 // Revision 1.52 1998/03/23 21:24:38 curt
483 // Source code formating tweaks.
485 // Revision 1.51 1998/03/14 00:31:22 curt
486 // Beginning initial terrain texturing experiments.
488 // Revision 1.50 1998/03/09 22:46:19 curt
491 // Revision 1.49 1998/02/23 19:07:59 curt
492 // Incorporated Durk's Astro/ tweaks. Includes unifying the sun position
493 // calculation code between sun display, and other FG sections that use this
494 // for things like lighting.
496 // Revision 1.48 1998/02/21 14:53:15 curt
497 // Added Charlie's HUD changes.
499 // Revision 1.47 1998/02/19 13:05:53 curt
500 // Incorporated some HUD tweaks from Michelle America.
501 // Tweaked the sky's sunset/rise colors.
502 // Other misc. tweaks.
504 // Revision 1.46 1998/02/18 15:07:06 curt
505 // Tweaks to build with SGI OpenGL (and therefor hopefully other accelerated
506 // drivers will work.)
508 // Revision 1.45 1998/02/16 13:39:43 curt
509 // Miscellaneous weekend tweaks. Fixed? a cache problem that caused whole
510 // tiles to occasionally be missing.
512 // Revision 1.44 1998/02/12 21:59:50 curt
513 // Incorporated code changes contributed by Charlie Hotchkiss
514 // <chotchkiss@namg.us.anritsu.com>
516 // Revision 1.43 1998/02/11 02:50:40 curt
519 // Revision 1.42 1998/02/09 22:56:58 curt
520 // Removed "depend" files from cvs control. Other minor make tweaks.
522 // Revision 1.41 1998/02/09 15:07:50 curt
525 // Revision 1.40 1998/02/07 15:29:44 curt
526 // Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
527 // <chotchkiss@namg.us.anritsu.com>
529 // Revision 1.39 1998/02/03 23:20:25 curt
530 // Lots of little tweaks to fix various consistency problems discovered by
531 // Solaris' CC. Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
532 // passed arguments along to the real printf(). Also incorporated HUD changes
533 // by Michele America.
535 // Revision 1.38 1998/02/02 20:53:58 curt
536 // Incorporated Durk's changes.
538 // Revision 1.37 1998/02/01 03:39:54 curt
541 // Revision 1.36 1998/01/31 00:43:13 curt
542 // Added MetroWorks patches from Carmen Volpe.
544 // Revision 1.35 1998/01/27 00:47:57 curt
545 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
546 // system and commandline/config file processing code.
548 // Revision 1.34 1998/01/22 02:59:37 curt
549 // Changed #ifdef FILE_H to #ifdef _FILE_H
551 // Revision 1.33 1998/01/21 21:11:34 curt
554 // Revision 1.32 1998/01/19 19:27:08 curt
555 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
556 // This should simplify things tremendously.
558 // Revision 1.31 1998/01/19 18:40:32 curt
559 // Tons of little changes to clean up the code and to remove fatal errors
560 // when building with the c++ compiler.
562 // Revision 1.30 1998/01/13 00:23:09 curt
563 // Initial changes to support loading and management of scenery tiles. Note,
564 // there's still a fair amount of work left to be done.
566 // Revision 1.29 1998/01/08 02:22:08 curt
567 // Beginning to integrate Tile management subsystem.
569 // Revision 1.28 1998/01/07 03:18:58 curt
570 // Moved astronomical stuff from .../Src/Scenery to .../Src/Astro/
572 // Revision 1.27 1998/01/05 18:44:35 curt
573 // Add an option to advance/decrease time from keyboard.
575 // Revision 1.26 1997/12/30 23:09:04 curt
576 // Tweaking initialization sequences.
578 // Revision 1.25 1997/12/30 22:22:33 curt
579 // Further integration of event manager.
581 // Revision 1.24 1997/12/30 20:47:44 curt
582 // Integrated new event manager with subsystem initializations.
584 // Revision 1.23 1997/12/30 16:36:50 curt
585 // Merged in Durk's changes ...
587 // Revision 1.22 1997/12/19 23:34:05 curt
588 // Lot's of tweaking with sky rendering and lighting.
590 // Revision 1.21 1997/12/19 16:45:00 curt
591 // Working on scene rendering order and options.
593 // Revision 1.20 1997/12/18 23:32:33 curt
594 // First stab at sky dome actually starting to look reasonable. :-)
596 // Revision 1.19 1997/12/17 23:13:36 curt
597 // Began working on rendering a sky.
599 // Revision 1.18 1997/12/15 23:54:49 curt
600 // Add xgl wrappers for debugging.
601 // Generate terrain normals on the fly.
603 // Revision 1.17 1997/12/15 20:59:09 curt
606 // Revision 1.16 1997/12/12 19:52:48 curt
607 // Working on lightling and material properties.
609 // Revision 1.15 1997/12/11 04:43:55 curt
610 // Fixed sun vector and lighting problems. I thing the moon is now lit
613 // Revision 1.14 1997/12/10 22:37:47 curt
614 // Prepended "fg" on the name of all global structures that didn't have it yet.
615 // i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
617 // Revision 1.13 1997/11/25 19:25:32 curt
618 // Changes to integrate Durk's moon/sun code updates + clean up.
620 // Revision 1.12 1997/11/15 18:16:35 curt
623 // Revision 1.11 1997/10/30 12:38:42 curt
624 // Working on new scenery subsystem.
626 // Revision 1.10 1997/10/25 03:24:23 curt
627 // Incorporated sun, moon, and star positioning code contributed by Durk Talsma.
629 // Revision 1.9 1997/09/23 00:29:39 curt
630 // Tweaks to get things to compile with gcc-win32.
632 // Revision 1.8 1997/09/22 14:44:20 curt
633 // Continuing to try to align stars correctly.
635 // Revision 1.7 1997/09/16 15:50:30 curt
636 // Working on star alignment and time issues.
638 // Revision 1.6 1997/09/05 14:17:30 curt
639 // More tweaking with stars.
641 // Revision 1.5 1997/09/04 02:17:36 curt
644 // Revision 1.4 1997/08/27 21:32:26 curt
645 // Restructured view calculation code. Added stars.
647 // Revision 1.3 1997/08/27 03:30:19 curt
648 // Changed naming scheme of basic shared structures.
650 // Revision 1.2 1997/08/25 20:27:23 curt
651 // Merged in initial HUD and Joystick code.
653 // Revision 1.1 1997/08/23 01:46:20 curt