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 **************************************************************************/
32 #include <Main/fg_init.h>
33 #include <Main/views.h>
35 #include <Include/cmdargs.h>
36 #include <Include/fg_constants.h>
37 #include <Include/general.h>
39 #include <Aircraft/aircraft.h>
40 #include <Astro/moon.h>
41 #include <Astro/planets.h>
42 #include <Astro/sky.h>
43 #include <Astro/stars.h>
44 #include <Astro/sun.h>
45 #include <Autopilot/autopilot.h>
46 #include <Cockpit/cockpit.h>
47 #include <Joystick/joystick.h>
48 #include <Math/fg_random.h>
49 #include <Scenery/scenery.h>
50 #include <Scenery/tilemgr.h>
51 #include <Time/event.h>
52 #include <Time/fg_time.h>
53 #include <Time/sunpos.h>
54 #include <Weather/weather.h>
55 #include <Main/fg_debug.h>
57 extern int show_hud; /* HUD state */
58 extern int displayInstruments;
59 extern const char *default_root;
61 /* General house keeping initializations */
63 int fgInitGeneral( void ) {
70 fgPrintf( FG_GENERAL, FG_INFO, "General Initialization\n" );
71 fgPrintf( FG_GENERAL, FG_INFO, "======= ==============\n" );
73 /* seed the random number generater */
76 // determine the fg root path. The command line parser getargs() will
77 // fill in a root directory if the option was used.
79 if( !(g->root_dir) ) {
80 // If not set by command line test for environmental var..
81 g->root_dir = getenv("FG_ROOT");
83 // No root path set? Then assume, we will exit if this is
84 // wrong when looking for support files.
85 g->root_dir = (char *)DefaultRootDir;
88 fgPrintf( FG_GENERAL, FG_INFO, "FG_ROOT = %s\n\n", g->root_dir);
90 // Dummy value can be changed if future initializations
91 // fail a critical task.
92 return ( 0 /* FALSE */ );
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 ) {
104 // Ok will be flagged only if we get EVERYTHING done.
105 int ret_val = 1 /* TRUE */;
112 l = &cur_light_params;
113 t = &cur_time_params;
116 fgPrintf( FG_GENERAL, FG_INFO, "Initialize Subsystems\n");
117 fgPrintf( FG_GENERAL, FG_INFO, "========== ==========\n");
119 /****************************************************************
120 * The following section sets up the flight model EOM parameters and
121 * should really be read in from one or more files.
122 ****************************************************************/
124 /* Must happen before any of the flight model or control
125 * parameters are set */
127 fgAircraftInit(); // In the future this might not be the case.
128 f = current_aircraft.flight;
130 // Initial Position at (P13) Globe, AZ
131 FG_Longitude = ( -110.6642444 ) * DEG_TO_RAD;
132 FG_Latitude = ( 33.3528917 ) * DEG_TO_RAD;
133 FG_Runway_altitude = (3234.5 + 300);
134 FG_Altitude = FG_Runway_altitude + 3.758099;
136 // Initial Position at (E81) Superior, AZ
137 // FG_Longitude = ( -111.1270650 ) * DEG_TO_RAD;
138 // FG_Latitude = ( 33.2778339 ) * DEG_TO_RAD;
139 // FG_Runway_altitude = (2646 + 100);
140 // FG_Altitude = FG_Runway_altitude + 3.758099;
142 // Initial Position at (TUS) Tucson, AZ
143 // FG_Longitude = ( -110.9412597 ) * DEG_TO_RAD;
144 // FG_Latitude = ( 32.1162439 ) * DEG_TO_RAD;
145 // FG_Runway_altitude = (2641 + 0);
146 // FG_Altitude = FG_Runway_altitude + 3.758099;
148 // Initial Position at (SEZ) SEDONA airport
149 // FG_Longitude = (-111.7884614 + 0.01) * DEG_TO_RAD;
150 // FG_Latitude = ( 34.8486289 - 0.015) * DEG_TO_RAD;
151 // FG_Runway_altitude = (4827 + 450);
152 // FG_Altitude = FG_Runway_altitude + 3.758099;
154 // Initial Position at near Anchoraze, AK
155 // FG_Longitude = ( -152.00 ) * DEG_TO_RAD;
156 // FG_Latitude = ( 61.17 ) * DEG_TO_RAD;
157 // FG_Runway_altitude = (0);
158 // FG_Altitude = FG_Runway_altitude + 3.758099;
160 // Initial Position at (HSP) Hot Springs, VA
161 // FG_Longitude = (-79.8338964 + 0.01) * DEG_TO_RAD;
162 // FG_Latitude = ( 37.9514564 + 0.008) * DEG_TO_RAD;
163 // FG_Runway_altitude = (3792 + 1300);
164 // FG_Altitude = FG_Runway_altitude + 3.758099;
166 // Initial Position at (ANE) Anoka County airport
167 // FG_Longitude = -93.2113889 * DEG_TO_RAD;
168 // FG_Latitude = 45.145 * DEG_TO_RAD;
169 // FG_Runway_altitude = 912;
170 // FG_Altitude = FG_Runway_altitude + 3.758099;
172 // Initial Position north of the city of Globe
173 // FG_Longitude = ( -398673.28 / 3600.0 ) * DEG_TO_RAD;
174 // FG_Latitude = ( 120625.64 / 3600.0 ) * DEG_TO_RAD;
175 // FG_Longitude = ( -397867.44 / 3600.0 ) * DEG_TO_RAD;
176 // FG_Latitude = ( 119548.21 / 3600.0 ) * DEG_TO_RAD;
177 // FG_Altitude = 0.0 + 3.758099;
179 // Initial Position near where I used to live in Globe, AZ
180 // FG_Longitude = ( -398757.6 / 3600.0 ) * DEG_TO_RAD;
181 // FG_Latitude = ( 120160.0 / 3600.0 ) * DEG_TO_RAD;
182 // FG_Runway_altitude = 4000.0;
183 // FG_Altitude = FG_Runway_altitude + 3.758099;
185 // Initial Position: 10125 Jewell St. NE
186 // FG_Longitude = ( -93.15 ) * DEG_TO_RAD;
187 // FG_Latitude = ( 45.15 ) * DEG_TO_RAD;
188 // FG_Runway_altitude = 950.0;
189 // FG_Altitude = FG_Runway_altitude + 3.758099;
191 // Initial Position: Somewhere near the Grand Canyon
192 // FG_Longitude = ( -112.5 ) * DEG_TO_RAD;
193 // FG_Latitude = ( 36.5 ) * DEG_TO_RAD;
194 // FG_Runway_altitude = 8000.0;
195 // FG_Altitude = FG_Runway_altitude + 3.758099;
197 // Initial Position: (GCN) Grand Canyon Airport, AZ
198 // FG_Longitude = ( -112.1469647 ) * DEG_TO_RAD;
199 // FG_Latitude = ( 35.9523539 ) * DEG_TO_RAD;
200 // FG_Runway_altitude = 6606.0;
201 // FG_Altitude = FG_Runway_altitude + 3.758099;
203 // Initial Position: Jim Brennon's Kingmont Observatory
204 // FG_Longitude = ( -121.1131667 ) * DEG_TO_RAD;
205 // FG_Latitude = ( 38.8293917 ) * DEG_TO_RAD;
206 // FG_Runway_altitude = 920.0;
207 // FG_Altitude = FG_Runway_altitude + 3.758099;
209 // probably interesting for european team members
210 // That is: If I can get the scenery to work -;) (Durk)
212 // Initial Position: Groningen Airport Eelde, the netherlands
213 // FG_Longitude = ( 6.583333 ) * DEG_TO_RAD;
214 // FG_Latitude = ( 53.125 ) * DEG_TO_RAD;
215 // FG_Runway_altitude = 920.0;
216 // FG_Altitude = FG_Runway_altitude + 3.758099;
218 // Initial Position: Schiphol Amsterdam Airport, the netherlands
219 // FG_Longitude = ( -4.7641667 ) * DEG_TO_RAD;
220 // FG_Latitude = ( 52.308056 ) * DEG_TO_RAD;
221 // FG_Runway_altitude = 920.0;
222 // FG_Altitude = FG_Runway_altitude + 3.758099;
224 // Eclipse Watching w73.5 n10 (approx) 18:00 UT
225 // FG_Longitude = ( -73.5 ) * DEG_TO_RAD;
226 // FG_Latitude = ( 10.0 ) * DEG_TO_RAD;
227 // FG_Runway_altitude = 0.0;
228 // FG_Altitude = FG_Runway_altitude + 3.758099;
231 FG_Longitude = ( -113.5 ) * DEG_TO_RAD;
232 FG_Latitude = ( 33.5 ) * DEG_TO_RAD;
233 FG_Runway_altitude = 8000.0;
234 FG_Altitude = FG_Runway_altitude + 3.758099;
236 // A random test position
237 // FG_Longitude = ( 88128.00 / 3600.0 ) * DEG_TO_RAD;
238 // FG_Latitude = ( 93312.00 / 3600.0 ) * DEG_TO_RAD;
240 fgPrintf( FG_GENERAL, FG_INFO,
241 "Initial position is: (%.4f, %.4f, %.2f)\n",
242 FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG,
243 FG_Altitude * FEET_TO_METER);
245 /* Initial Velocity */
246 FG_V_north = 0.0 /* 7.287719E+00 */;
247 FG_V_east = 0.0 /* 1.521770E+03 */;
248 FG_V_down = 0.0 /* -1.265722E-05 */;
250 /* Initial Orientation */
251 FG_Phi = -2.658474E-06;
252 FG_Theta = 7.401790E-03;
253 FG_Psi = 260.0 * DEG_TO_RAD;
255 /* Initial Angular B rates */
256 FG_P_body = 7.206685E-05;
257 FG_Q_body = 0.000000E+00;
258 FG_R_body = 9.492658E-05;
260 FG_Earth_position_angle = 0.000000E+00;
262 /* Mass properties and geometry values */
263 FG_Mass = 8.547270E+01;
264 FG_I_xx = 1.048000E+03;
265 FG_I_yy = 3.000000E+03;
266 FG_I_zz = 3.530000E+03;
267 FG_I_xz = 0.000000E+00;
269 /* CG position w.r.t. ref. point */
270 FG_Dx_cg = 0.000000E+00;
271 FG_Dy_cg = 0.000000E+00;
272 FG_Dz_cg = 0.000000E+00;
274 /* Set initial position and slew parameters */
275 /* fgSlewInit(-398391.3, 120070.41, 244, 3.1415); */ /* GLOBE Airport */
276 /* fgSlewInit(-335340,162540, 15, 4.38); */
277 /* fgSlewInit(-398673.28,120625.64, 53, 4.38); */
279 /* Initialize the event manager */
282 /* Dump event stats every 60 seconds */
283 fgEventRegister( "fgEventPrintStats()", fgEventPrintStats,
284 FG_EVENT_READY, 60000 );
286 /* Initialize "time" */
290 /* fgViewUpdate() needs the sun in the right place, while
291 * fgUpdateSunPos() needs to know the view position. I'll get
292 * around this interdependency for now by calling fgUpdateSunPos()
293 * once, then moving on with normal initialization. */
296 /* Initialize view parameters */
298 fgViewUpdate(f, v, l);
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);
396 // end added section;
398 // One more try here to get the sky synced up
402 fgPrintf(FG_GENERAL, FG_INFO,"\n");
408 /* Revision 1.55 1998/04/14 02:21:03 curt
409 /* Incorporated autopilot heading hold contributed by: Jeff Goeke-Smith
410 /* <jgoeke@voyager.net>
412 * Revision 1.54 1998/04/08 23:35:36 curt
413 * Tweaks to Gnu automake/autoconf system.
415 * Revision 1.53 1998/04/03 22:09:06 curt
416 * Converting to Gnu autoconf system.
418 * Revision 1.52 1998/03/23 21:24:38 curt
419 * Source code formating tweaks.
421 * Revision 1.51 1998/03/14 00:31:22 curt
422 * Beginning initial terrain texturing experiments.
424 * Revision 1.50 1998/03/09 22:46:19 curt
427 * Revision 1.49 1998/02/23 19:07:59 curt
428 * Incorporated Durk's Astro/ tweaks. Includes unifying the sun position
429 * calculation code between sun display, and other FG sections that use this
430 * for things like lighting.
432 * Revision 1.48 1998/02/21 14:53:15 curt
433 * Added Charlie's HUD changes.
435 * Revision 1.47 1998/02/19 13:05:53 curt
436 * Incorporated some HUD tweaks from Michelle America.
437 * Tweaked the sky's sunset/rise colors.
438 * Other misc. tweaks.
440 * Revision 1.46 1998/02/18 15:07:06 curt
441 * Tweaks to build with SGI OpenGL (and therefor hopefully other accelerated
442 * drivers will work.)
444 * Revision 1.45 1998/02/16 13:39:43 curt
445 * Miscellaneous weekend tweaks. Fixed? a cache problem that caused whole
446 * tiles to occasionally be missing.
448 * Revision 1.44 1998/02/12 21:59:50 curt
449 * Incorporated code changes contributed by Charlie Hotchkiss
450 * <chotchkiss@namg.us.anritsu.com>
452 * Revision 1.43 1998/02/11 02:50:40 curt
455 * Revision 1.42 1998/02/09 22:56:58 curt
456 * Removed "depend" files from cvs control. Other minor make tweaks.
458 * Revision 1.41 1998/02/09 15:07:50 curt
461 * Revision 1.40 1998/02/07 15:29:44 curt
462 * Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
463 * <chotchkiss@namg.us.anritsu.com>
465 * Revision 1.39 1998/02/03 23:20:25 curt
466 * Lots of little tweaks to fix various consistency problems discovered by
467 * Solaris' CC. Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
468 * passed arguments along to the real printf(). Also incorporated HUD changes
469 * by Michele America.
471 * Revision 1.38 1998/02/02 20:53:58 curt
472 * Incorporated Durk's changes.
474 * Revision 1.37 1998/02/01 03:39:54 curt
477 * Revision 1.36 1998/01/31 00:43:13 curt
478 * Added MetroWorks patches from Carmen Volpe.
480 * Revision 1.35 1998/01/27 00:47:57 curt
481 * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
482 * system and commandline/config file processing code.
484 * Revision 1.34 1998/01/22 02:59:37 curt
485 * Changed #ifdef FILE_H to #ifdef _FILE_H
487 * Revision 1.33 1998/01/21 21:11:34 curt
490 * Revision 1.32 1998/01/19 19:27:08 curt
491 * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
492 * This should simplify things tremendously.
494 * Revision 1.31 1998/01/19 18:40:32 curt
495 * Tons of little changes to clean up the code and to remove fatal errors
496 * when building with the c++ compiler.
498 * Revision 1.30 1998/01/13 00:23:09 curt
499 * Initial changes to support loading and management of scenery tiles. Note,
500 * there's still a fair amount of work left to be done.
502 * Revision 1.29 1998/01/08 02:22:08 curt
503 * Beginning to integrate Tile management subsystem.
505 * Revision 1.28 1998/01/07 03:18:58 curt
506 * Moved astronomical stuff from .../Src/Scenery to .../Src/Astro/
508 * Revision 1.27 1998/01/05 18:44:35 curt
509 * Add an option to advance/decrease time from keyboard.
511 * Revision 1.26 1997/12/30 23:09:04 curt
512 * Tweaking initialization sequences.
514 * Revision 1.25 1997/12/30 22:22:33 curt
515 * Further integration of event manager.
517 * Revision 1.24 1997/12/30 20:47:44 curt
518 * Integrated new event manager with subsystem initializations.
520 * Revision 1.23 1997/12/30 16:36:50 curt
521 * Merged in Durk's changes ...
523 * Revision 1.22 1997/12/19 23:34:05 curt
524 * Lot's of tweaking with sky rendering and lighting.
526 * Revision 1.21 1997/12/19 16:45:00 curt
527 * Working on scene rendering order and options.
529 * Revision 1.20 1997/12/18 23:32:33 curt
530 * First stab at sky dome actually starting to look reasonable. :-)
532 * Revision 1.19 1997/12/17 23:13:36 curt
533 * Began working on rendering a sky.
535 * Revision 1.18 1997/12/15 23:54:49 curt
536 * Add xgl wrappers for debugging.
537 * Generate terrain normals on the fly.
539 * Revision 1.17 1997/12/15 20:59:09 curt
542 * Revision 1.16 1997/12/12 19:52:48 curt
543 * Working on lightling and material properties.
545 * Revision 1.15 1997/12/11 04:43:55 curt
546 * Fixed sun vector and lighting problems. I thing the moon is now lit
549 * Revision 1.14 1997/12/10 22:37:47 curt
550 * Prepended "fg" on the name of all global structures that didn't have it yet.
551 * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
553 * Revision 1.13 1997/11/25 19:25:32 curt
554 * Changes to integrate Durk's moon/sun code updates + clean up.
556 * Revision 1.12 1997/11/15 18:16:35 curt
559 * Revision 1.11 1997/10/30 12:38:42 curt
560 * Working on new scenery subsystem.
562 * Revision 1.10 1997/10/25 03:24:23 curt
563 * Incorporated sun, moon, and star positioning code contributed by Durk Talsma.
565 * Revision 1.9 1997/09/23 00:29:39 curt
566 * Tweaks to get things to compile with gcc-win32.
568 * Revision 1.8 1997/09/22 14:44:20 curt
569 * Continuing to try to align stars correctly.
571 * Revision 1.7 1997/09/16 15:50:30 curt
572 * Working on star alignment and time issues.
574 * Revision 1.6 1997/09/05 14:17:30 curt
575 * More tweaking with stars.
577 * Revision 1.5 1997/09/04 02:17:36 curt
580 * Revision 1.4 1997/08/27 21:32:26 curt
581 * Restructured view calculation code. Added stars.
583 * Revision 1.3 1997/08/27 03:30:19 curt
584 * Changed naming scheme of basic shared structures.
586 * Revision 1.2 1997/08/25 20:27:23 curt
587 * Merged in initial HUD and Joystick code.
589 * Revision 1.1 1997/08/23 01:46:20 curt