1 /**************************************************************************
2 * fg_init.c -- 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.
23 * (Log is kept at end of this file)
24 **************************************************************************/
30 #include <Main/fg_init.h>
31 #include <Main/views.h>
33 #include <Include/cmdargs.h>
34 #include <Include/fg_constants.h>
35 #include <Include/general.h>
37 #include <Aircraft/aircraft.h>
38 #include <Astro/moon.h>
39 #include <Astro/planets.h>
40 #include <Astro/sky.h>
41 #include <Astro/stars.h>
42 #include <Astro/sun.h>
43 #include <Cockpit/cockpit.h>
44 #include <Joystick/joystick.h>
45 #include <Math/fg_random.h>
46 #include <Scenery/scenery.h>
47 #include <Scenery/tilemgr.h>
48 #include <Time/event.h>
49 #include <Time/fg_time.h>
50 #include <Time/sunpos.h>
51 #include <Weather/weather.h>
52 #include <Main/fg_debug.h>
54 extern int show_hud; /* HUD state */
55 extern int displayInstruments;
56 extern const char *default_root;
58 /* General house keeping initializations */
60 int fgInitGeneral( void ) {
67 fgPrintf( FG_GENERAL, FG_INFO, "General Initialization\n" );
68 fgPrintf( FG_GENERAL, FG_INFO, "======= ==============\n" );
70 /* seed the random number generater */
73 // determine the fg root path. The command line parser getargs() will
74 // fill in a root directory if the option was used.
76 if( !(g->root_dir) ) {
77 // If not set by command line test for environmental var..
78 g->root_dir = getenv("FG_ROOT");
80 // No root path set? Then assume, we will exit if this is
81 // wrong when looking for support files.
82 g->root_dir = (char *)DefaultRootDir;
85 fgPrintf( FG_GENERAL, FG_INFO, "FG_ROOT = %s\n\n", g->root_dir);
87 // Dummy value can be changed if future initializations
88 // fail a critical task.
89 return ( 0 /* FALSE */ );
93 // This is the top level init routine which calls all the other
94 // initialization routines. If you are adding a subsystem to flight
95 // gear, its initialization call should located in this routine.
96 // Returns non-zero if a problem encountered.
98 int fgInitSubsystems( void ) {
101 // Ok will be flagged only if we get EVERYTHING done.
102 int ret_val = 1 /* TRUE */;
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 /* Globe Aiport, AZ */
128 FG_Runway_longitude = -398391.28;
129 FG_Runway_latitude = 120070.41;
130 FG_Runway_altitude = 3234.5;
131 FG_Runway_heading = 102.0 * DEG_TO_RAD;
133 /* Initial Position at (P13) Globe, AZ */
134 FG_Longitude = ( -398391.28 / 3600.0 ) * DEG_TO_RAD;
135 FG_Latitude = ( 120070.41 / 3600.0 ) * DEG_TO_RAD;
136 FG_Runway_altitude = (3234.5 + 300);
137 FG_Altitude = FG_Runway_altitude + 3.758099;
139 /* Initial Position at (E81) Superior, AZ */
140 /* FG_Longitude = ( -111.1270650 ) * DEG_TO_RAD; */
141 /* FG_Latitude = ( 33.2778339 ) * DEG_TO_RAD; */
142 /* FG_Runway_altitude = (2646 + 1000); */
143 /* FG_Altitude = FG_Runway_altitude + 3.758099; */
145 /* Initial Position at (TUS) Tucson, AZ */
146 /* FG_Longitude = ( -110.9412597 ) * DEG_TO_RAD; */
147 /* FG_Latitude = ( 32.1162439 ) * DEG_TO_RAD; */
148 /* FG_Runway_altitude = (2641 + 0); */
149 /* FG_Altitude = FG_Runway_altitude + 3.758099; */
151 /* Initial Position at near Anchoraze, AK */
152 /* FG_Longitude = ( -150.00 ) * DEG_TO_RAD; */
153 /* FG_Latitude = ( 61.17 ) * DEG_TO_RAD; */
154 /* FG_Runway_altitude = (2641 + 07777); */
155 /* FG_Altitude = FG_Runway_altitude + 3.758099; */
157 /* Initial Position at (SEZ) SEDONA airport */
158 /* FG_Longitude = (-111.7884614 + 0.02) * DEG_TO_RAD; */
159 /* FG_Latitude = ( 34.8486289 - 0.04) * DEG_TO_RAD; */
160 /* FG_Runway_altitude = (4827 + 800); */
161 /* FG_Altitude = FG_Runway_altitude + 3.758099; */
163 /* Initial Position at (HSP) Hot Springs, VA */
164 /* FG_Longitude = (-79.8338964 + 0.02) * DEG_TO_RAD; */
165 /* FG_Latitude = ( 37.9514564 + 0.05) * DEG_TO_RAD; */
166 /* FG_Runway_altitude = (792 + 1500); */
167 /* FG_Altitude = FG_Runway_altitude + 3.758099; */
169 /* Initial Position at (ANE) Anoka County airport */
170 /* FG_Longitude = -93.2113889 * DEG_TO_RAD; */
171 /* FG_Latitude = 45.145 * DEG_TO_RAD; */
172 /* FG_Runway_altitude = 912; */
173 /* FG_Altitude = FG_Runway_altitude + 3.758099; */
175 /* Initial Position north of the city of Globe */
176 /* FG_Longitude = ( -398673.28 / 3600.0 ) * DEG_TO_RAD; */
177 /* FG_Latitude = ( 120625.64 / 3600.0 ) * DEG_TO_RAD; */
178 /* FG_Longitude = ( -397867.44 / 3600.0 ) * DEG_TO_RAD; */
179 /* FG_Latitude = ( 119548.21 / 3600.0 ) * DEG_TO_RAD; */
180 /* FG_Altitude = 0.0 + 3.758099; */
182 /* Initial Posisition near where I used to live in Globe, AZ */
183 /* FG_Longitude = ( -398757.6 / 3600.0 ) * DEG_TO_RAD; */
184 /* FG_Latitude = ( 120160.0 / 3600.0 ) * DEG_TO_RAD; */
185 /* FG_Runway_altitude = 5000.0; */
186 /* FG_Altitude = FG_Runway_altitude + 3.758099; */
188 /* Initial Position: 10125 Jewell St. NE */
189 /* FG_Longitude = ( -93.15 ) * DEG_TO_RAD; */
190 /* FG_Latitude = ( 45.15 ) * DEG_TO_RAD; */
191 /* FG_Altitude = FG_Runway_altitude + 3.758099; */
193 /* Initial Position: Somewhere near the Grand Canyon */
194 /* FG_Longitude = ( -112.5 ) * DEG_TO_RAD; */
195 /* FG_Latitude = ( 36.5 ) * DEG_TO_RAD; */
196 /* FG_Runway_altitude = 5000.0; */
197 /* FG_Altitude = FG_Runway_altitude + 3.758099; */
199 // Initial Position: (GCN) Grand Canyon Airport, AZ
200 // FG_Longitude = ( -112.1469647 ) * DEG_TO_RAD;
201 // FG_Latitude = ( 35.9523539 ) * DEG_TO_RAD;
202 // FG_Runway_altitude = 6606.0;
203 // FG_Altitude = FG_Runway_altitude + 3.758099;
205 // Initial Position: Jim Brennon's Kingmont Observatory
206 // FG_Longitude = ( -121.1131666 ) * DEG_TO_RAD;
207 // FG_Latitude = ( 38.8293916 ) * DEG_TO_RAD;
208 // FG_Runway_altitude = 920.0;
209 // FG_Altitude = FG_Runway_altitude + 3.758099;
211 /* A random test position */
212 /* FG_Longitude = ( 88128.00 / 3600.0 ) * DEG_TO_RAD; */
213 /* FG_Latitude = ( 93312.00 / 3600.0 ) * DEG_TO_RAD; */
215 fgPrintf( FG_GENERAL, FG_INFO,
216 "Initial position is: (%.4f, %.4f, %.2f)\n",
217 FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG,
218 FG_Altitude * FEET_TO_METER);
220 /* Initial Velocity */
221 FG_V_north = 0.0 /* 7.287719E+00 */;
222 FG_V_east = 0.0 /* 1.521770E+03 */;
223 FG_V_down = 0.0 /* -1.265722E-05 */;
225 /* Initial Orientation */
226 FG_Phi = -2.658474E-06;
227 FG_Theta = 7.401790E-03;
228 FG_Psi = 270.0 * DEG_TO_RAD;
230 /* Initial Angular B rates */
231 FG_P_body = 7.206685E-05;
232 FG_Q_body = 0.000000E+00;
233 FG_R_body = 9.492658E-05;
235 FG_Earth_position_angle = 0.000000E+00;
237 /* Mass properties and geometry values */
238 FG_Mass = 8.547270E+01;
239 FG_I_xx = 1.048000E+03;
240 FG_I_yy = 3.000000E+03;
241 FG_I_zz = 3.530000E+03;
242 FG_I_xz = 0.000000E+00;
244 /* CG position w.r.t. ref. point */
245 FG_Dx_cg = 0.000000E+00;
246 FG_Dy_cg = 0.000000E+00;
247 FG_Dz_cg = 0.000000E+00;
249 /* Set initial position and slew parameters */
250 /* fgSlewInit(-398391.3, 120070.41, 244, 3.1415); */ /* GLOBE Airport */
251 /* fgSlewInit(-335340,162540, 15, 4.38); */
252 /* fgSlewInit(-398673.28,120625.64, 53, 4.38); */
254 /* Initialize the event manager */
257 /* Dump event stats every 60 seconds */
258 fgEventRegister( "fgEventPrintStats()", fgEventPrintStats,
259 FG_EVENT_READY, 60000 );
261 /* Initialize "time" */
265 /* fgViewUpdate() needs the sun in the right place, while
266 * fgUpdateSunPos() needs to know the view position. I'll get
267 * around this interdependency for now by calling fgUpdateSunPos()
268 * once, then moving on with normal initialization. */
271 /* Initialize view parameters */
273 fgViewUpdate(f, v, l);
275 /* Initialize shared sun position and sun_vec */
276 fgEventRegister( "fgUpdateSunPos()", fgUpdateSunPos, FG_EVENT_READY, 1000 );
278 /* Initialize the weather modeling subsystem */
281 /* update the weather for our current position */
282 fgEventRegister( "fgWeatherUpdate()", fgWeatherUpdate,
283 FG_EVENT_READY, 120000 );
285 /* Initialize the Cockpit subsystem */
286 if( fgCockpitInit( ¤t_aircraft )) {
287 // Cockpit initialized ok.
289 fgPrintf( FG_GENERAL, FG_EXIT, "Error in Cockpit initialization!\n" );
292 // Initialize the orbital elements of sun, moon and mayor planets
293 fgSolarSystemInit(*t);
295 // Initialize the Stars subsystem
296 if( fgStarsInit() ) {
297 // Stars initialized ok.
299 fgPrintf( FG_GENERAL, FG_EXIT, "Error in Stars initialization!\n" );
302 // Initialize the planetary subsystem
303 fgEventRegister("fgPlanetsInit()", fgPlanetsInit, FG_EVENT_READY, 600000);
305 // Initialize the sun's position
306 fgEventRegister("fgSunInit()", fgSunInit, FG_EVENT_READY, 60000 );
308 // Intialize the moon's position
309 fgEventRegister( "fgMoonInit()", fgMoonInit, FG_EVENT_READY, 600000 );
311 // Initialize the "sky"
314 // Initialize the Scenery Management subsystem
315 if( fgTileMgrInit() ) {
316 // Load the local scenery data
319 fgPrintf( FG_GENERAL, FG_EXIT,
320 "Error in Tile Manager initialization!\n" );
323 // I'm just sticking this here for now, it should probably move
325 // cur_elev = mesh_altitude(FG_Longitude * RAD_TO_DEG * 3600.0,
326 // FG_Latitude * RAD_TO_DEG * 3600.0); */
327 // fgPrintf( FG_GENERAL, FG_INFO,
328 // "True ground elevation is %.2f meters here.\n",
331 cur_elev = FG_Runway_altitude * FEET_TO_METER;
332 if ( cur_elev > -9990.0 ) {
333 FG_Runway_altitude = cur_elev * METER_TO_FEET;
336 if ( FG_Altitude < FG_Runway_altitude ) {
337 FG_Altitude = FG_Runway_altitude + 3.758099;
340 fgPrintf(FG_GENERAL, FG_INFO,
341 "Updated position (after elevation adj): (%.4f, %.4f, %.2f)\n",
342 FG_Latitude * RAD_TO_DEG, FG_Longitude * RAD_TO_DEG,
343 FG_Altitude * FEET_TO_METER);
345 /* end of thing that I just stuck in that I should probably move */
347 /* Initialize the flight model subsystem data structures base on
350 fgFlightModelInit( FG_LARCSIM, f, 1.0 / DEFAULT_MODEL_HZ );
352 // To HUD or not to HUD - Now a command line issue
355 // Let's not show the instrument panel
356 displayInstruments = 0;
359 if (fgJoystickInit(0) ) {
360 // Joystick initialized ok.
362 fgPrintf( FG_GENERAL, FG_EXIT, "Error in Joystick initialization!\n" );
365 // One more try here to get the sky synced up
369 fgPrintf(FG_GENERAL, FG_INFO,"\n");
375 /* Revision 1.44 1998/02/12 21:59:50 curt
376 /* Incorporated code changes contributed by Charlie Hotchkiss
377 /* <chotchkiss@namg.us.anritsu.com>
379 * Revision 1.43 1998/02/11 02:50:40 curt
382 * Revision 1.42 1998/02/09 22:56:58 curt
383 * Removed "depend" files from cvs control. Other minor make tweaks.
385 * Revision 1.41 1998/02/09 15:07:50 curt
388 * Revision 1.40 1998/02/07 15:29:44 curt
389 * Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
390 * <chotchkiss@namg.us.anritsu.com>
392 * Revision 1.39 1998/02/03 23:20:25 curt
393 * Lots of little tweaks to fix various consistency problems discovered by
394 * Solaris' CC. Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
395 * passed arguments along to the real printf(). Also incorporated HUD changes
396 * by Michele America.
398 * Revision 1.38 1998/02/02 20:53:58 curt
399 * Incorporated Durk's changes.
401 * Revision 1.37 1998/02/01 03:39:54 curt
404 * Revision 1.36 1998/01/31 00:43:13 curt
405 * Added MetroWorks patches from Carmen Volpe.
407 * Revision 1.35 1998/01/27 00:47:57 curt
408 * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
409 * system and commandline/config file processing code.
411 * Revision 1.34 1998/01/22 02:59:37 curt
412 * Changed #ifdef FILE_H to #ifdef _FILE_H
414 * Revision 1.33 1998/01/21 21:11:34 curt
417 * Revision 1.32 1998/01/19 19:27:08 curt
418 * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
419 * This should simplify things tremendously.
421 * Revision 1.31 1998/01/19 18:40:32 curt
422 * Tons of little changes to clean up the code and to remove fatal errors
423 * when building with the c++ compiler.
425 * Revision 1.30 1998/01/13 00:23:09 curt
426 * Initial changes to support loading and management of scenery tiles. Note,
427 * there's still a fair amount of work left to be done.
429 * Revision 1.29 1998/01/08 02:22:08 curt
430 * Beginning to integrate Tile management subsystem.
432 * Revision 1.28 1998/01/07 03:18:58 curt
433 * Moved astronomical stuff from .../Src/Scenery to .../Src/Astro/
435 * Revision 1.27 1998/01/05 18:44:35 curt
436 * Add an option to advance/decrease time from keyboard.
438 * Revision 1.26 1997/12/30 23:09:04 curt
439 * Tweaking initialization sequences.
441 * Revision 1.25 1997/12/30 22:22:33 curt
442 * Further integration of event manager.
444 * Revision 1.24 1997/12/30 20:47:44 curt
445 * Integrated new event manager with subsystem initializations.
447 * Revision 1.23 1997/12/30 16:36:50 curt
448 * Merged in Durk's changes ...
450 * Revision 1.22 1997/12/19 23:34:05 curt
451 * Lot's of tweaking with sky rendering and lighting.
453 * Revision 1.21 1997/12/19 16:45:00 curt
454 * Working on scene rendering order and options.
456 * Revision 1.20 1997/12/18 23:32:33 curt
457 * First stab at sky dome actually starting to look reasonable. :-)
459 * Revision 1.19 1997/12/17 23:13:36 curt
460 * Began working on rendering a sky.
462 * Revision 1.18 1997/12/15 23:54:49 curt
463 * Add xgl wrappers for debugging.
464 * Generate terrain normals on the fly.
466 * Revision 1.17 1997/12/15 20:59:09 curt
469 * Revision 1.16 1997/12/12 19:52:48 curt
470 * Working on lightling and material properties.
472 * Revision 1.15 1997/12/11 04:43:55 curt
473 * Fixed sun vector and lighting problems. I thing the moon is now lit
476 * Revision 1.14 1997/12/10 22:37:47 curt
477 * Prepended "fg" on the name of all global structures that didn't have it yet.
478 * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
480 * Revision 1.13 1997/11/25 19:25:32 curt
481 * Changes to integrate Durk's moon/sun code updates + clean up.
483 * Revision 1.12 1997/11/15 18:16:35 curt
486 * Revision 1.11 1997/10/30 12:38:42 curt
487 * Working on new scenery subsystem.
489 * Revision 1.10 1997/10/25 03:24:23 curt
490 * Incorporated sun, moon, and star positioning code contributed by Durk Talsma.
492 * Revision 1.9 1997/09/23 00:29:39 curt
493 * Tweaks to get things to compile with gcc-win32.
495 * Revision 1.8 1997/09/22 14:44:20 curt
496 * Continuing to try to align stars correctly.
498 * Revision 1.7 1997/09/16 15:50:30 curt
499 * Working on star alignment and time issues.
501 * Revision 1.6 1997/09/05 14:17:30 curt
502 * More tweaking with stars.
504 * Revision 1.5 1997/09/04 02:17:36 curt
507 * Revision 1.4 1997/08/27 21:32:26 curt
508 * Restructured view calculation code. Added stars.
510 * Revision 1.3 1997/08/27 03:30:19 curt
511 * Changed naming scheme of basic shared structures.
513 * Revision 1.2 1997/08/25 20:27:23 curt
514 * Merged in initial HUD and Joystick code.
516 * Revision 1.1 1997/08/23 01:46:20 curt