]> git.mxchange.org Git - flightgear.git/blob - Main/fg_init.c
282e7e88f9edef938eb07e7c8b6863025162a620
[flightgear.git] / Main / fg_init.c
1 /**************************************************************************
2  * fg_init.c -- Flight Gear top level initialization routines
3  *
4  * Written by Curtis Olson, started August 1997.
5  *
6  * Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
7  *
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.
12  *
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.
17  *
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.
21  *
22  * $Id$
23  * (Log is kept at end of this file)
24  **************************************************************************/
25
26
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include <Main/fg_init.h>
31 #include <Main/views.h>
32
33 #include <Include/fg_constants.h>
34 #include <Include/general.h>
35
36 #include <Aircraft/aircraft.h>
37 #include <Astro/moon.h>
38 #include <Astro/planets.h>
39 #include <Astro/sky.h>
40 #include <Astro/stars.h>
41 #include <Astro/sun.h>
42 #include <Cockpit/cockpit.h>
43 #include <Joystick/joystick.h>
44 #include <Math/fg_random.h>
45 #include <Scenery/scenery.h>
46 #include <Scenery/tilemgr.h>
47 #include <Time/event.h>
48 #include <Time/fg_time.h>
49 #include <Time/sunpos.h>
50 #include <Weather/weather.h>
51 #include <Main/fg_debug.h>
52
53 extern int show_hud;             /* HUD state */
54 extern int displayInstruments;
55
56
57 /* General house keeping initializations */
58
59 void fgInitGeneral( void ) {
60     struct fgGENERAL *g;
61
62     g = &general;
63
64     fgInitDebug();
65
66     fgPrintf( FG_GENERAL, FG_INFO, "General Initialization\n" );
67     fgPrintf( FG_GENERAL, FG_INFO, "======= ==============\n" );
68
69     /* seed the random number generater */
70     fg_srandom();
71
72     /* determine the fg root path */
73     if ( (g->root_dir = getenv("FG_ROOT")) == NULL ) {
74         /* environment variable not defined */
75         fgPrintf(FG_GENERAL, FG_EXIT, "FG_ROOT needs to be defined!  "
76                                   "See the documentation.\n");
77     } 
78     fgPrintf( FG_GENERAL, FG_INFO, "FG_ROOT = %s\n\n", g->root_dir);
79 }
80
81
82 /* This is the top level init routine which calls all the other
83  * initialization routines.  If you are adding a subsystem to flight
84  * gear, its initialization call should located in this routine.*/
85
86 void fgInitSubsystems( void ) {
87     double cur_elev;
88
89     fgFLIGHT *f;
90     struct fgLIGHT *l;
91     struct fgTIME *t;
92     struct fgVIEW *v;
93
94     l = &cur_light_params;
95     t = &cur_time_params;
96     v = &current_view;
97
98     fgPrintf( FG_GENERAL, FG_INFO, "Initialize Subsystems\n");
99     fgPrintf( FG_GENERAL, FG_INFO, "========== ==========\n");
100
101     /****************************************************************
102      * The following section sets up the flight model EOM parameters and 
103      * should really be read in from one or more files.
104      ****************************************************************/
105
106     /* Must happen before any of the flight model or control
107      * parameters are set */
108     fgAircraftInit();
109     f = current_aircraft.flight;
110
111     /* Globe Aiport, AZ */
112     FG_Runway_longitude = -398391.28;
113     FG_Runway_latitude = 120070.41;
114     FG_Runway_altitude = 3234.5;
115     FG_Runway_heading = 102.0 * DEG_TO_RAD;
116
117     /* Initial Position at (P13) Globe, AZ */
118     FG_Longitude = ( -398391.28 / 3600.0 ) * DEG_TO_RAD;
119     FG_Latitude  = (  120070.41 / 3600.0 ) * DEG_TO_RAD;
120     FG_Runway_altitude = (3234.5 + 300);
121     FG_Altitude = FG_Runway_altitude + 3.758099;
122
123     /* Initial Position at (E81) Superior, AZ */
124     /* FG_Longitude = ( -111.1270650 ) * DEG_TO_RAD; */
125     /* FG_Latitude  = (  33.2778339 ) * DEG_TO_RAD; */
126     /* FG_Runway_altitude = (2646 + 500); */
127     /* FG_Altitude = FG_Runway_altitude + 3.758099; */
128
129     /* Initial Position at (TUS) Tucson, AZ */
130     /* FG_Longitude = ( -110.9412597 ) * DEG_TO_RAD; */
131     /* FG_Latitude  = (  32.1162439 ) * DEG_TO_RAD; */
132     /* FG_Runway_altitude = (2641 + 0); */
133     /* FG_Altitude = FG_Runway_altitude + 3.758099; */
134
135     /* Initial Position at near Anchoraze, AK */
136     /* FG_Longitude = ( -150.00 ) * DEG_TO_RAD; */
137     /* FG_Latitude  = (  61.17 ) * DEG_TO_RAD; */
138     /* FG_Runway_altitude = (2641 + 07777); */
139     /* FG_Altitude = FG_Runway_altitude + 3.758099; */
140
141     /* Initial Position at (SEZ) SEDONA airport */
142     /* FG_Longitude = (-111.7884614 + 0.02) * DEG_TO_RAD; */
143     /* FG_Latitude  = (  34.8486289 - 0.04) * DEG_TO_RAD; */
144     /* FG_Runway_altitude = (4827 + 800); */
145     /* FG_Altitude = FG_Runway_altitude + 3.758099; */
146         
147     /* Initial Position at (HSP) Hot Springs, VA */
148     /* FG_Longitude = (-79.8338964 + 0.02) * DEG_TO_RAD; */
149     /* FG_Latitude  = ( 37.9514564 + 0.05) * DEG_TO_RAD; */
150     /* FG_Runway_altitude = (792 + 1500); */
151     /* FG_Altitude = FG_Runway_altitude + 3.758099; */
152     
153     /* Initial Position at (ANE) Anoka County airport */
154     /* FG_Longitude = -93.2113889 * DEG_TO_RAD; */
155     /* FG_Latitude  = 45.145 * DEG_TO_RAD; */
156     /* FG_Runway_altitude = 912; */
157     /* FG_Altitude = FG_Runway_altitude + 3.758099; */
158     
159     /* Initial Position north of the city of Globe */
160     /* FG_Longitude = ( -398673.28 / 3600.0 ) * DEG_TO_RAD; */
161     /* FG_Latitude  = (  120625.64 / 3600.0 ) * DEG_TO_RAD; */
162     /* FG_Longitude = ( -397867.44 / 3600.0 ) * DEG_TO_RAD; */
163     /* FG_Latitude  = (  119548.21 / 3600.0 ) * DEG_TO_RAD; */
164     /* FG_Altitude = 0.0 + 3.758099; */
165
166     /* Initial Posisition near where I used to live in Globe, AZ */
167     /* FG_Longitude = ( -398757.6 / 3600.0 ) * DEG_TO_RAD; */
168     /* FG_Latitude  = (  120160.0 / 3600.0 ) * DEG_TO_RAD;  */
169     /* FG_Runway_altitude = 5000.0; */
170     /* FG_Altitude = FG_Runway_altitude + 3.758099; */
171
172     /* Initial Position: 10125 Jewell St. NE */
173     /* FG_Longitude = ( -93.15 ) * DEG_TO_RAD; */
174     /* FG_Latitude  = (  45.15 ) * DEG_TO_RAD; */
175     /* FG_Altitude = FG_Runway_altitude + 3.758099; */
176
177     /* Initial Position: Somewhere near the Grand Canyon */
178     /* FG_Longitude = ( -112.5 ) * DEG_TO_RAD; */
179     /* FG_Latitude  = (  36.5 ) * DEG_TO_RAD; */
180     /* FG_Runway_altitude = 5000.0; */
181     /* FG_Altitude = FG_Runway_altitude + 3.758099; */
182
183     /* A random test position */
184     /* FG_Longitude = ( 88128.00 / 3600.0 ) * DEG_TO_RAD; */
185     /* FG_Latitude  = ( 93312.00 / 3600.0 ) * DEG_TO_RAD; */
186
187     fgPrintf( FG_GENERAL, FG_INFO, 
188               "Initial position is: (%.4f, %.4f, %.2f)\n", 
189               FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, 
190               FG_Altitude * FEET_TO_METER);
191
192     /* Initial Velocity */
193     FG_V_north = 0.0 /*  7.287719E+00 */;
194     FG_V_east  = 0.0 /*  1.521770E+03 */;
195     FG_V_down  = 0.0 /* -1.265722E-05 */;
196
197     /* Initial Orientation */
198     FG_Phi   = -2.658474E-06;
199     FG_Theta =  7.401790E-03;
200     FG_Psi   =  270.0 * DEG_TO_RAD;
201
202     /* Initial Angular B rates */
203     FG_P_body = 7.206685E-05;
204     FG_Q_body = 0.000000E+00;
205     FG_R_body = 9.492658E-05;
206
207     FG_Earth_position_angle = 0.000000E+00;
208
209     /* Mass properties and geometry values */
210     FG_Mass = 8.547270E+01;
211     FG_I_xx = 1.048000E+03;
212     FG_I_yy = 3.000000E+03;
213     FG_I_zz = 3.530000E+03;
214     FG_I_xz = 0.000000E+00;
215
216     /* CG position w.r.t. ref. point */
217     FG_Dx_cg = 0.000000E+00;
218     FG_Dy_cg = 0.000000E+00;
219     FG_Dz_cg = 0.000000E+00;
220
221     /* Set initial position and slew parameters */
222     /* fgSlewInit(-398391.3, 120070.41, 244, 3.1415); */ /* GLOBE Airport */
223     /* fgSlewInit(-335340,162540, 15, 4.38); */
224     /* fgSlewInit(-398673.28,120625.64, 53, 4.38); */
225
226     /* Initialize the event manager */
227     fgEventInit();
228
229     /* Dump event stats every 60 seconds */
230     fgEventRegister( "fgEventPrintStats()", fgEventPrintStats, 
231                      FG_EVENT_READY, 60000 );
232
233     /* Initialize "time" */
234     fgTimeInit(t);
235     fgTimeUpdate(f, t);
236
237     /* fgViewUpdate() needs the sun in the right place, while
238      * fgUpdateSunPos() needs to know the view position.  I'll get
239      * around this interdependency for now by calling fgUpdateSunPos()
240      * once, then moving on with normal initialization. */
241     fgUpdateSunPos();
242
243     /* Initialize view parameters */
244     fgViewInit(v);
245     fgViewUpdate(f, v, l);
246
247     /* Initialize shared sun position and sun_vec */
248     fgEventRegister( "fgUpdateSunPos()", fgUpdateSunPos, FG_EVENT_READY, 1000 );
249
250     /* Initialize the weather modeling subsystem */
251     fgWeatherInit();
252
253     /* update the weather for our current position */
254     fgEventRegister( "fgWeatherUpdate()", fgWeatherUpdate, 
255                      FG_EVENT_READY, 120000 );
256
257     /* Initialize the Cockpit subsystem */
258     if( fgCockpitInit( &current_aircraft ) == NULL ) {
259         fgPrintf( FG_GENERAL, FG_EXIT, "Error in Cockpit initialization!\n" );
260     }
261
262     /* Initialize the orbital elements of sun, moon and mayor planets */
263     fgSolarSystemInit(*t);
264
265     /* Initialize the Stars subsystem */
266     fgStarsInit();
267
268     /* Initialize the planetary subsystem */
269     fgEventRegister("fgPlanetsInit()", fgPlanetsInit, FG_EVENT_READY, 600000);
270
271     /* Initialize the sun's position */
272     fgEventRegister( "fgSunInit()", fgSunInit, FG_EVENT_READY, 60000 );
273
274     /* Intialize the moon's position */
275     fgEventRegister( "fgMoonInit()", fgMoonInit, FG_EVENT_READY, 600000 );
276
277     /* Initialize the "sky" */
278     fgSkyInit();
279
280     /* Initialize the Scenery Management subsystem */
281     fgTileMgrInit();
282     /* fgSceneryInit(); */
283
284     /* Tell the Scenery Management system where we are so it can load
285      * the correct scenery data */
286     fgTileMgrUpdate();
287     /* fgSceneryUpdate(FG_Latitude, FG_Longitude, FG_Altitude); */
288
289     /* I'm just sticking this here for now, it should probably move 
290      * eventually */
291     /* cur_elev = mesh_altitude(FG_Longitude * RAD_TO_DEG * 3600.0, 
292                              FG_Latitude  * RAD_TO_DEG * 3600.0); */
293     /* fgPrintf( FG_GENERAL, FG_INFO, 
294        "True ground elevation is %.2f meters here.\n",
295        cur_elev); */
296     cur_elev = FG_Runway_altitude * FEET_TO_METER;
297     if ( cur_elev > -9990.0 ) {
298         FG_Runway_altitude = cur_elev * METER_TO_FEET;
299     }
300
301     if ( FG_Altitude < FG_Runway_altitude ) {
302         FG_Altitude = FG_Runway_altitude + 3.758099;
303     }
304     fgPrintf(FG_GENERAL, FG_INFO,
305            "Updated position (after elevation adj): (%.4f, %.4f, %.2f)\n", 
306            FG_Latitude * RAD_TO_DEG, FG_Longitude * RAD_TO_DEG, 
307            FG_Altitude * FEET_TO_METER);
308     /* end of thing that I just stuck in that I should probably move */
309
310     /* Initialize the flight model subsystem data structures base on
311      * above values */
312     fgFlightModelInit( FG_LARCSIM, f, 1.0 / DEFAULT_MODEL_HZ );
313
314     /* To HUD or not to HUD */
315     show_hud = 0;
316
317     /* Let's show the instrument panel */
318     displayInstruments = 0;
319
320     /* Joystick support */
321     fgJoystickInit( 0 );
322
323     /* One more try here to get the sky synced up */
324     fgSkyColorsInit();
325
326     fgPrintf(FG_GENERAL, FG_INFO,"\n");
327 }
328
329
330 /* $Log$
331 /* Revision 1.41  1998/02/09 15:07:50  curt
332 /* Minor tweaks.
333 /*
334  * Revision 1.40  1998/02/07 15:29:44  curt
335  * Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
336  * <chotchkiss@namg.us.anritsu.com>
337  *
338  * Revision 1.39  1998/02/03 23:20:25  curt
339  * Lots of little tweaks to fix various consistency problems discovered by
340  * Solaris' CC.  Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
341  * passed arguments along to the real printf().  Also incorporated HUD changes
342  * by Michele America.
343  *
344  * Revision 1.38  1998/02/02 20:53:58  curt
345  * Incorporated Durk's changes.
346  *
347  * Revision 1.37  1998/02/01 03:39:54  curt
348  * Minor tweaks.
349  *
350  * Revision 1.36  1998/01/31 00:43:13  curt
351  * Added MetroWorks patches from Carmen Volpe.
352  *
353  * Revision 1.35  1998/01/27 00:47:57  curt
354  * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
355  * system and commandline/config file processing code.
356  *
357  * Revision 1.34  1998/01/22 02:59:37  curt
358  * Changed #ifdef FILE_H to #ifdef _FILE_H
359  *
360  * Revision 1.33  1998/01/21 21:11:34  curt
361  * Misc. tweaks.
362  *
363  * Revision 1.32  1998/01/19 19:27:08  curt
364  * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
365  * This should simplify things tremendously.
366  *
367  * Revision 1.31  1998/01/19 18:40:32  curt
368  * Tons of little changes to clean up the code and to remove fatal errors
369  * when building with the c++ compiler.
370  *
371  * Revision 1.30  1998/01/13 00:23:09  curt
372  * Initial changes to support loading and management of scenery tiles.  Note,
373  * there's still a fair amount of work left to be done.
374  *
375  * Revision 1.29  1998/01/08 02:22:08  curt
376  * Beginning to integrate Tile management subsystem.
377  *
378  * Revision 1.28  1998/01/07 03:18:58  curt
379  * Moved astronomical stuff from .../Src/Scenery to .../Src/Astro/
380  *
381  * Revision 1.27  1998/01/05 18:44:35  curt
382  * Add an option to advance/decrease time from keyboard.
383  *
384  * Revision 1.26  1997/12/30 23:09:04  curt
385  * Tweaking initialization sequences.
386  *
387  * Revision 1.25  1997/12/30 22:22:33  curt
388  * Further integration of event manager.
389  *
390  * Revision 1.24  1997/12/30 20:47:44  curt
391  * Integrated new event manager with subsystem initializations.
392  *
393  * Revision 1.23  1997/12/30 16:36:50  curt
394  * Merged in Durk's changes ...
395  *
396  * Revision 1.22  1997/12/19 23:34:05  curt
397  * Lot's of tweaking with sky rendering and lighting.
398  *
399  * Revision 1.21  1997/12/19 16:45:00  curt
400  * Working on scene rendering order and options.
401  *
402  * Revision 1.20  1997/12/18 23:32:33  curt
403  * First stab at sky dome actually starting to look reasonable. :-)
404  *
405  * Revision 1.19  1997/12/17 23:13:36  curt
406  * Began working on rendering a sky.
407  *
408  * Revision 1.18  1997/12/15 23:54:49  curt
409  * Add xgl wrappers for debugging.
410  * Generate terrain normals on the fly.
411  *
412  * Revision 1.17  1997/12/15 20:59:09  curt
413  * Misc. tweaks.
414  *
415  * Revision 1.16  1997/12/12 19:52:48  curt
416  * Working on lightling and material properties.
417  *
418  * Revision 1.15  1997/12/11 04:43:55  curt
419  * Fixed sun vector and lighting problems.  I thing the moon is now lit
420  * correctly.
421  *
422  * Revision 1.14  1997/12/10 22:37:47  curt
423  * Prepended "fg" on the name of all global structures that didn't have it yet.
424  * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
425  *
426  * Revision 1.13  1997/11/25 19:25:32  curt
427  * Changes to integrate Durk's moon/sun code updates + clean up.
428  *
429  * Revision 1.12  1997/11/15 18:16:35  curt
430  * minor tweaks.
431  *
432  * Revision 1.11  1997/10/30 12:38:42  curt
433  * Working on new scenery subsystem.
434  *
435  * Revision 1.10  1997/10/25 03:24:23  curt
436  * Incorporated sun, moon, and star positioning code contributed by Durk Talsma.
437  *
438  * Revision 1.9  1997/09/23 00:29:39  curt
439  * Tweaks to get things to compile with gcc-win32.
440  *
441  * Revision 1.8  1997/09/22 14:44:20  curt
442  * Continuing to try to align stars correctly.
443  *
444  * Revision 1.7  1997/09/16 15:50:30  curt
445  * Working on star alignment and time issues.
446  *
447  * Revision 1.6  1997/09/05 14:17:30  curt
448  * More tweaking with stars.
449  *
450  * Revision 1.5  1997/09/04 02:17:36  curt
451  * Shufflin' stuff.
452  *
453  * Revision 1.4  1997/08/27 21:32:26  curt
454  * Restructured view calculation code.  Added stars.
455  *
456  * Revision 1.3  1997/08/27 03:30:19  curt
457  * Changed naming scheme of basic shared structures.
458  *
459  * Revision 1.2  1997/08/25 20:27:23  curt
460  * Merged in initial HUD and Joystick code.
461  *
462  * Revision 1.1  1997/08/23 01:46:20  curt
463  * Initial revision.
464  *
465  */