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