3 * fg_init.c -- Flight Gear top level initialization routines
5 * Written by Curtis Olson, started August 1997.
7 * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * (Log is kept at end of this file)
26 **************************************************************************/
36 #include <Include/fg_constants.h>
37 #include <Include/general.h>
39 #include <Aircraft/aircraft.h>
40 #include <Astro/moon.hxx>
41 #include <Astro/planets.hxx>
42 #include <Astro/sky.hxx>
43 #include <Astro/stars.hxx>
44 #include <Astro/sun.hxx>
45 #include <Autopilot/autopilot.h>
46 #include <Cockpit/cockpit.hxx>
47 #include <Debug/fg_debug.h>
48 #include <Joystick/joystick.h>
49 #include <Math/fg_random.h>
50 #include <Scenery/scenery.h>
51 #include <Scenery/tilemgr.hxx>
52 #include <Time/event.hxx>
53 #include <Time/fg_time.hxx>
54 #include <Time/light.hxx>
55 #include <Time/sunpos.hxx>
56 #include <Weather/weather.h>
58 #include "fg_init.hxx"
61 extern int show_hud; /* HUD state */
62 extern int displayInstruments;
63 extern const char *default_root;
65 /* General house keeping initializations */
67 int fgInitGeneral( void ) {
72 // determine the fg root path.
73 if( !(g->root_dir) ) {
74 // If not set by command line test for environmental var..
75 g->root_dir = getenv("FG_ROOT");
77 // No root path set? Then assume, we will exit if this is
78 // wrong when looking for support files.
79 fgPrintf( FG_GENERAL, FG_EXIT, "%s %s\n",
80 "Cannot continue without environment variable FG_ROOT",
84 fgPrintf( FG_GENERAL, FG_INFO, "FG_ROOT = %s\n\n", g->root_dir);
86 fgPrintf( FG_GENERAL, FG_INFO, "General Initialization\n" );
87 fgPrintf( FG_GENERAL, FG_INFO, "======= ==============\n" );
89 // seed the random number generater
96 // This is the top level init routine which calls all the other
97 // initialization routines. If you are adding a subsystem to flight
98 // gear, its initialization call should located in this routine.
99 // Returns non-zero if a problem encountered.
101 int fgInitSubsystems( void ) {
109 l = &cur_light_params;
110 t = &cur_time_params;
113 fgPrintf( FG_GENERAL, FG_INFO, "Initialize Subsystems\n");
114 fgPrintf( FG_GENERAL, FG_INFO, "========== ==========\n");
116 /****************************************************************
117 * The following section sets up the flight model EOM parameters and
118 * should really be read in from one or more files.
119 ****************************************************************/
121 // Must happen before any of the flight model or control
122 // parameters are set
124 fgAircraftInit(); // In the future this might not be the case.
125 f = current_aircraft.flight;
127 // Initial Position at (P13) Globe, AZ
128 FG_Longitude = ( -110.6642444 ) * DEG_TO_RAD;
129 FG_Latitude = ( 33.3528917 ) * DEG_TO_RAD;
130 FG_Runway_altitude = (3234.5 + 300);
131 FG_Altitude = FG_Runway_altitude + 3.758099;
133 // Initial Position at (E81) Superior, AZ
134 // FG_Longitude = ( -111.1270650 ) * DEG_TO_RAD;
135 // FG_Latitude = ( 33.2778339 ) * DEG_TO_RAD;
136 // FG_Runway_altitude = (2646 + 100);
137 // FG_Altitude = FG_Runway_altitude + 3.758099;
139 // Initial Position at (TUS) Tucson, AZ
140 // FG_Longitude = ( -110.9412597 ) * DEG_TO_RAD;
141 // FG_Latitude = ( 32.1162439 ) * DEG_TO_RAD;
142 // FG_Runway_altitude = (2641 + 0);
143 // FG_Altitude = FG_Runway_altitude + 3.758099;
145 // Initial Position at (SEZ) SEDONA airport
146 // FG_Longitude = (-111.7884614 + 0.01) * DEG_TO_RAD;
147 // FG_Latitude = ( 34.8486289 - 0.015) * DEG_TO_RAD;
148 // FG_Runway_altitude = (4827 + 450);
149 // FG_Altitude = FG_Runway_altitude + 3.758099;
151 // Initial Position at near Anchoraze, AK
152 // FG_Longitude = ( -152.00 ) * DEG_TO_RAD;
153 // FG_Latitude = ( 61.17 ) * DEG_TO_RAD;
154 // FG_Runway_altitude = (0);
155 // FG_Altitude = FG_Runway_altitude + 3.758099;
157 // Initial Position at (HSP) Hot Springs, VA
158 // FG_Longitude = (-79.8338964 + 0.01) * DEG_TO_RAD;
159 // FG_Latitude = ( 37.9514564 + 0.008) * DEG_TO_RAD;
160 // FG_Runway_altitude = (3792 + 1300);
161 // FG_Altitude = FG_Runway_altitude + 3.758099;
163 // Initial Position at (ANE) Anoka County airport
164 // FG_Longitude = -93.2113889 * DEG_TO_RAD;
165 // FG_Latitude = 45.145 * DEG_TO_RAD;
166 // FG_Runway_altitude = 912;
167 // FG_Altitude = FG_Runway_altitude + 3.758099;
169 // Initial Position north of the city of Globe
170 // FG_Longitude = ( -398673.28 / 3600.0 ) * DEG_TO_RAD;
171 // FG_Latitude = ( 120625.64 / 3600.0 ) * DEG_TO_RAD;
172 // FG_Longitude = ( -397867.44 / 3600.0 ) * DEG_TO_RAD;
173 // FG_Latitude = ( 119548.21 / 3600.0 ) * DEG_TO_RAD;
174 // FG_Altitude = 0.0 + 3.758099;
176 // Initial Position near where I used to live in Globe, AZ
177 // FG_Longitude = ( -398757.6 / 3600.0 ) * DEG_TO_RAD;
178 // FG_Latitude = ( 120160.0 / 3600.0 ) * DEG_TO_RAD;
179 // FG_Runway_altitude = 4000.0;
180 // FG_Altitude = FG_Runway_altitude + 3.758099;
182 // Initial Position: 10125 Jewell St. NE
183 // FG_Longitude = ( -93.15 ) * DEG_TO_RAD;
184 // FG_Latitude = ( 45.15 ) * DEG_TO_RAD;
185 // FG_Runway_altitude = 950.0;
186 // FG_Altitude = FG_Runway_altitude + 3.758099;
188 // Initial Position: Somewhere near the Grand Canyon
189 // FG_Longitude = ( -112.5 ) * DEG_TO_RAD;
190 // FG_Latitude = ( 36.5 ) * DEG_TO_RAD;
191 // FG_Runway_altitude = 8000.0;
192 // FG_Altitude = FG_Runway_altitude + 3.758099;
194 // Initial Position: (GCN) Grand Canyon Airport, AZ
195 // FG_Longitude = ( -112.1469647 ) * DEG_TO_RAD;
196 // FG_Latitude = ( 35.9523539 ) * DEG_TO_RAD;
197 // FG_Runway_altitude = 6606.0;
198 // FG_Altitude = FG_Runway_altitude + 3.758099;
200 // Initial Position: Jim Brennon's Kingmont Observatory
201 // FG_Longitude = ( -121.1131667 ) * DEG_TO_RAD;
202 // FG_Latitude = ( 38.8293917 ) * DEG_TO_RAD;
203 // FG_Runway_altitude = 920.0;
204 // FG_Altitude = FG_Runway_altitude + 3.758099;
206 // probably interesting for european team members
207 // That is: If I can get the scenery to work -;) (Durk)
209 // Initial Position: Groningen Airport Eelde, the netherlands
210 // FG_Longitude = ( 6.583333 ) * DEG_TO_RAD;
211 // FG_Latitude = ( 53.125 ) * DEG_TO_RAD;
212 // FG_Runway_altitude = 920.0;
213 // FG_Altitude = FG_Runway_altitude + 3.758099;
215 // Initial Position: Schiphol Amsterdam Airport, the netherlands
216 // FG_Longitude = ( -4.7641667 ) * DEG_TO_RAD;
217 // FG_Latitude = ( 52.308056 ) * DEG_TO_RAD;
218 // FG_Runway_altitude = 920.0;
219 // FG_Altitude = FG_Runway_altitude + 3.758099;
221 // Eclipse Watching w73.5 n10 (approx) 18:00 UT
222 // FG_Longitude = ( -73.5 ) * DEG_TO_RAD;
223 // FG_Latitude = ( 10.0 ) * DEG_TO_RAD;
224 // FG_Runway_altitude = 0.0;
225 // FG_Altitude = FG_Runway_altitude + 3.758099;
228 // FG_Longitude = ( -109.5 ) * DEG_TO_RAD;
229 // FG_Latitude = ( 32.5 ) * DEG_TO_RAD;
230 // FG_Runway_altitude = (2646 + 2000);
231 // FG_Altitude = FG_Runway_altitude + 3.758099;
233 fgPrintf( FG_GENERAL, FG_INFO,
234 "Initial position is: (%.4f, %.4f, %.2f)\n",
235 FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG,
236 FG_Altitude * FEET_TO_METER);
239 FG_V_north = 0.0 /* 7.287719E+00 */;
240 FG_V_east = 0.0 /* 1.521770E+03 */;
241 FG_V_down = 0.0 /* -1.265722E-05 */;
243 // Initial Orientation
244 FG_Phi = -2.658474E-06;
245 FG_Theta = 7.401790E-03;
246 FG_Psi = 270.0 * DEG_TO_RAD;
248 // Initial Angular B rates
249 FG_P_body = 7.206685E-05;
250 FG_Q_body = 0.000000E+00;
251 FG_R_body = 9.492658E-05;
253 FG_Earth_position_angle = 0.000000E+00;
255 // Mass properties and geometry values
256 FG_Mass = 8.547270E+01;
257 FG_I_xx = 1.048000E+03;
258 FG_I_yy = 3.000000E+03;
259 FG_I_zz = 3.530000E+03;
260 FG_I_xz = 0.000000E+00;
262 // CG position w.r.t. ref. point
263 FG_Dx_cg = 0.000000E+00;
264 FG_Dy_cg = 0.000000E+00;
265 FG_Dz_cg = 0.000000E+00;
267 // Set initial position and slew parameters
268 // fgSlewInit(-398391.3, 120070.41, 244, 3.1415); // GLOBE Airport
269 // fgSlewInit(-335340,162540, 15, 4.38);
270 // fgSlewInit(-398673.28,120625.64, 53, 4.38);
272 // Initialize the event manager
275 // Dump event stats every 60 seconds
276 fgEventRegister( "fgEventPrintStats()", fgEventPrintStats,
277 FG_EVENT_READY, 60000 );
283 // fgViewUpdate() needs the sun in the right place, while
284 // fgUpdateSunPos() needs to know the view position. I'll get
285 // around this interdependency for now by calling fgUpdateSunPos()
286 // once, then moving on with normal initialization.
289 // Initialize view parameters
291 fgViewUpdate(f, v, l);
293 // Initialize Lighting interpolation tables
296 // update the lighting parameters (based on sun angle)
297 fgEventRegister( "fgLightUpdate()", fgLightUpdate,
298 FG_EVENT_READY, 30000 );
300 // Initialize the weather modeling subsystem
303 // update the weather for our current position
304 fgEventRegister( "fgWeatherUpdate()", fgWeatherUpdate,
305 FG_EVENT_READY, 120000 );
307 // Initialize the Cockpit subsystem
308 if( fgCockpitInit( ¤t_aircraft )) {
309 // Cockpit initialized ok.
311 fgPrintf( FG_GENERAL, FG_EXIT, "Error in Cockpit initialization!\n" );
314 // Initialize the orbital elements of sun, moon and mayor planets
315 fgSolarSystemInit(*t);
317 // Initialize the Stars subsystem
318 if( fgStarsInit() ) {
319 // Stars initialized ok.
321 fgPrintf( FG_GENERAL, FG_EXIT, "Error in Stars initialization!\n" );
324 // Initialize the planetary subsystem
325 fgEventRegister("fgPlanetsInit()", fgPlanetsInit, FG_EVENT_READY, 600000);
327 // Initialize the sun's position
328 fgEventRegister("fgSunInit()", fgSunInit, FG_EVENT_READY, 30000 );
330 // Intialize the moon's position
331 fgEventRegister( "fgMoonInit()", fgMoonInit, FG_EVENT_READY, 600000 );
333 // Initialize the "sky"
336 // Initialize the Scenery Management subsystem
337 if ( fgSceneryInit() ) {
338 // Scenery initialized ok.
340 fgPrintf( FG_GENERAL, FG_EXIT, "Error in Scenery initialization!\n" );
344 if( fgTileMgrInit() ) {
345 // Load the local scenery data
348 fgPrintf( FG_GENERAL, FG_EXIT,
349 "Error in Tile Manager initialization!\n" );
352 // I'm just sticking this here for now, it should probably move
354 // cur_elev = mesh_altitude(FG_Longitude * RAD_TO_DEG * 3600.0,
355 // FG_Latitude * RAD_TO_DEG * 3600.0);
356 // fgPrintf( FG_GENERAL, FG_INFO,
357 // "True ground elevation is %.2f meters here.\n",
360 cur_elev = FG_Runway_altitude * FEET_TO_METER;
361 if ( cur_elev > -9990.0 ) {
362 FG_Runway_altitude = cur_elev * METER_TO_FEET;
365 if ( FG_Altitude < FG_Runway_altitude ) {
366 FG_Altitude = FG_Runway_altitude + 3.758099;
369 fgPrintf(FG_GENERAL, FG_INFO,
370 "Updated position (after elevation adj): (%.4f, %.4f, %.2f)\n",
371 FG_Latitude * RAD_TO_DEG, FG_Longitude * RAD_TO_DEG,
372 FG_Altitude * FEET_TO_METER);
374 // end of thing that I just stuck in that I should probably move
376 // Initialize the flight model subsystem data structures base on
379 fgFlightModelInit( FG_LARCSIM, f, 1.0 / DEFAULT_MODEL_HZ );
381 // To HUD or not to HUD - Now a command line issue
384 // Let's not show the instrument panel
385 displayInstruments = 0;
388 if (fgJoystickInit(0) ) {
389 // Joystick initialized ok.
391 fgPrintf( FG_GENERAL, FG_EXIT, "Error in Joystick initialization!\n" );
394 // Autopilot init added here, by Jeff Goeke-Smith
395 fgAPInit(¤t_aircraft);
397 // One more try here to get the sky synced up
400 fgPrintf(FG_GENERAL, FG_INFO,"\n");
407 /* Revision 1.2 1998/04/24 00:49:20 curt
408 /* Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
409 /* Trying out some different option parsing code.
410 /* Some code reorganization.
412 * Revision 1.1 1998/04/22 13:25:44 curt
413 * C++ - ifing the code.
414 * Starting a bit of reorganization of lighting code.
416 * Revision 1.56 1998/04/18 04:11:28 curt
417 * Moved fg_debug to it's own library, added zlib support.
419 * Revision 1.55 1998/04/14 02:21:03 curt
420 * Incorporated autopilot heading hold contributed by: Jeff Goeke-Smith
421 * <jgoeke@voyager.net>
423 * Revision 1.54 1998/04/08 23:35:36 curt
424 * Tweaks to Gnu automake/autoconf system.
426 * Revision 1.53 1998/04/03 22:09:06 curt
427 * Converting to Gnu autoconf system.
429 * Revision 1.52 1998/03/23 21:24:38 curt
430 * Source code formating tweaks.
432 * Revision 1.51 1998/03/14 00:31:22 curt
433 * Beginning initial terrain texturing experiments.
435 * Revision 1.50 1998/03/09 22:46:19 curt
438 * Revision 1.49 1998/02/23 19:07:59 curt
439 * Incorporated Durk's Astro/ tweaks. Includes unifying the sun position
440 * calculation code between sun display, and other FG sections that use this
441 * for things like lighting.
443 * Revision 1.48 1998/02/21 14:53:15 curt
444 * Added Charlie's HUD changes.
446 * Revision 1.47 1998/02/19 13:05:53 curt
447 * Incorporated some HUD tweaks from Michelle America.
448 * Tweaked the sky's sunset/rise colors.
449 * Other misc. tweaks.
451 * Revision 1.46 1998/02/18 15:07:06 curt
452 * Tweaks to build with SGI OpenGL (and therefor hopefully other accelerated
453 * drivers will work.)
455 * Revision 1.45 1998/02/16 13:39:43 curt
456 * Miscellaneous weekend tweaks. Fixed? a cache problem that caused whole
457 * tiles to occasionally be missing.
459 * Revision 1.44 1998/02/12 21:59:50 curt
460 * Incorporated code changes contributed by Charlie Hotchkiss
461 * <chotchkiss@namg.us.anritsu.com>
463 * Revision 1.43 1998/02/11 02:50:40 curt
466 * Revision 1.42 1998/02/09 22:56:58 curt
467 * Removed "depend" files from cvs control. Other minor make tweaks.
469 * Revision 1.41 1998/02/09 15:07:50 curt
472 * Revision 1.40 1998/02/07 15:29:44 curt
473 * Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
474 * <chotchkiss@namg.us.anritsu.com>
476 * Revision 1.39 1998/02/03 23:20:25 curt
477 * Lots of little tweaks to fix various consistency problems discovered by
478 * Solaris' CC. Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
479 * passed arguments along to the real printf(). Also incorporated HUD changes
480 * by Michele America.
482 * Revision 1.38 1998/02/02 20:53:58 curt
483 * Incorporated Durk's changes.
485 * Revision 1.37 1998/02/01 03:39:54 curt
488 * Revision 1.36 1998/01/31 00:43:13 curt
489 * Added MetroWorks patches from Carmen Volpe.
491 * Revision 1.35 1998/01/27 00:47:57 curt
492 * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
493 * system and commandline/config file processing code.
495 * Revision 1.34 1998/01/22 02:59:37 curt
496 * Changed #ifdef FILE_H to #ifdef _FILE_H
498 * Revision 1.33 1998/01/21 21:11:34 curt
501 * Revision 1.32 1998/01/19 19:27:08 curt
502 * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
503 * This should simplify things tremendously.
505 * Revision 1.31 1998/01/19 18:40:32 curt
506 * Tons of little changes to clean up the code and to remove fatal errors
507 * when building with the c++ compiler.
509 * Revision 1.30 1998/01/13 00:23:09 curt
510 * Initial changes to support loading and management of scenery tiles. Note,
511 * there's still a fair amount of work left to be done.
513 * Revision 1.29 1998/01/08 02:22:08 curt
514 * Beginning to integrate Tile management subsystem.
516 * Revision 1.28 1998/01/07 03:18:58 curt
517 * Moved astronomical stuff from .../Src/Scenery to .../Src/Astro/
519 * Revision 1.27 1998/01/05 18:44:35 curt
520 * Add an option to advance/decrease time from keyboard.
522 * Revision 1.26 1997/12/30 23:09:04 curt
523 * Tweaking initialization sequences.
525 * Revision 1.25 1997/12/30 22:22:33 curt
526 * Further integration of event manager.
528 * Revision 1.24 1997/12/30 20:47:44 curt
529 * Integrated new event manager with subsystem initializations.
531 * Revision 1.23 1997/12/30 16:36:50 curt
532 * Merged in Durk's changes ...
534 * Revision 1.22 1997/12/19 23:34:05 curt
535 * Lot's of tweaking with sky rendering and lighting.
537 * Revision 1.21 1997/12/19 16:45:00 curt
538 * Working on scene rendering order and options.
540 * Revision 1.20 1997/12/18 23:32:33 curt
541 * First stab at sky dome actually starting to look reasonable. :-)
543 * Revision 1.19 1997/12/17 23:13:36 curt
544 * Began working on rendering a sky.
546 * Revision 1.18 1997/12/15 23:54:49 curt
547 * Add xgl wrappers for debugging.
548 * Generate terrain normals on the fly.
550 * Revision 1.17 1997/12/15 20:59:09 curt
553 * Revision 1.16 1997/12/12 19:52:48 curt
554 * Working on lightling and material properties.
556 * Revision 1.15 1997/12/11 04:43:55 curt
557 * Fixed sun vector and lighting problems. I thing the moon is now lit
560 * Revision 1.14 1997/12/10 22:37:47 curt
561 * Prepended "fg" on the name of all global structures that didn't have it yet.
562 * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
564 * Revision 1.13 1997/11/25 19:25:32 curt
565 * Changes to integrate Durk's moon/sun code updates + clean up.
567 * Revision 1.12 1997/11/15 18:16:35 curt
570 * Revision 1.11 1997/10/30 12:38:42 curt
571 * Working on new scenery subsystem.
573 * Revision 1.10 1997/10/25 03:24:23 curt
574 * Incorporated sun, moon, and star positioning code contributed by Durk Talsma.
576 * Revision 1.9 1997/09/23 00:29:39 curt
577 * Tweaks to get things to compile with gcc-win32.
579 * Revision 1.8 1997/09/22 14:44:20 curt
580 * Continuing to try to align stars correctly.
582 * Revision 1.7 1997/09/16 15:50:30 curt
583 * Working on star alignment and time issues.
585 * Revision 1.6 1997/09/05 14:17:30 curt
586 * More tweaking with stars.
588 * Revision 1.5 1997/09/04 02:17:36 curt
591 * Revision 1.4 1997/08/27 21:32:26 curt
592 * Restructured view calculation code. Added stars.
594 * Revision 1.3 1997/08/27 03:30:19 curt
595 * Changed naming scheme of basic shared structures.
597 * Revision 1.2 1997/08/25 20:27:23 curt
598 * Merged in initial HUD and Joystick code.
600 * Revision 1.1 1997/08/23 01:46:20 curt