]> git.mxchange.org Git - flightgear.git/blob - Main/fg_init.c
c6ca50e40a2e203e6fef2f03438e9ea243eee722
[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 "fg_init.h"
31 #include "views.h"
32
33 #include "../Include/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/mesh.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
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     printf("General Initialization\n");
65     printf("======= ==============\n");
66
67     /* seed the random number generater */
68     fg_srandom();
69
70     /* determine the fg root path */
71     if ( (g->root_dir = getenv("FG_ROOT")) == NULL ) {
72         /* environment variable not defined */
73         printf("FG_ROOT needs to be defined!  See the documentation.\n");
74         exit(0);
75     } 
76     printf("FG_ROOT = %s\n", g->root_dir);
77
78     printf("\n");
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     struct fgFLIGHT *f;
90     struct fgLIGHT *l;
91     struct fgTIME *t;
92     struct fgVIEW *v;
93
94     f = &current_aircraft.flight;
95     l = &cur_light_params;
96     t = &cur_time_params;
97     v = &current_view;
98
99     printf("Initialize Subsystems\n");
100     printf("========== ==========\n");
101
102     /****************************************************************
103      * The following section sets up the flight model EOM parameters and 
104      * should really be read in from one or more files.
105      ****************************************************************/
106
107     /* Globe Aiport, AZ */
108     FG_Runway_longitude = -398391.28;
109     FG_Runway_latitude = 120070.41;
110     FG_Runway_altitude = 3234.5;
111     FG_Runway_heading = 102.0 * DEG_TO_RAD;
112
113     /* Initial Position at GLOBE airport */
114     FG_Longitude = ( -398391.28 / 3600.0 ) * DEG_TO_RAD;
115     FG_Latitude  = (  120070.41 / 3600.0 ) * DEG_TO_RAD;
116     FG_Altitude = FG_Runway_altitude + 3.758099;
117     
118     /* Initial Position north of the city of Globe */
119     /* FG_Longitude = ( -398673.28 / 3600.0 ) * DEG_TO_RAD; */
120     /* FG_Latitude  = (  120625.64 / 3600.0 ) * DEG_TO_RAD; */
121     /* FG_Longitude = ( -397867.44 / 3600.0 ) * DEG_TO_RAD; */
122     /* FG_Latitude  = (  119548.21 / 3600.0 ) * DEG_TO_RAD; */
123     /* FG_Altitude = 0.0 + 3.758099; */
124
125     /* Initial Posisition near where I used to live in Globe, AZ */
126     /* FG_Longitude = ( -398757.6 / 3600.0 ) * DEG_TO_RAD; */
127     /* FG_Latitude  = (  120160.0 / 3600.0 ) * DEG_TO_RAD; */
128     /* FG_Runway_altitude = 5000.0; */
129     /* FG_Altitude = FG_Runway_altitude + 3.758099; */
130
131     /* Initial Position: 10125 Jewell St. NE */
132     /* FG_Longitude = ( -93.15 ) * DEG_TO_RAD; */
133     /* FG_Latitude  = (  45.15 ) * DEG_TO_RAD; */
134     /* FG_Altitude = FG_Runway_altitude + 3.758099; */
135
136     /* A random test position */
137     /* FG_Longitude = ( 88128.00 / 3600.0 ) * DEG_TO_RAD; */
138     /* FG_Latitude  = ( 93312.00 / 3600.0 ) * DEG_TO_RAD; */
139
140     FG_Runway_altitude = 4000.0;
141     FG_Altitude = FG_Runway_altitude + 3.758099;
142
143     printf("Initial position is: (%.4f, %.4f, %.2f)\n", 
144            FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, 
145            FG_Altitude * FEET_TO_METER);
146
147       /* Initial Velocity */
148     FG_V_north = 0.0 /*  7.287719E+00 */;
149     FG_V_east  = 0.0 /*  1.521770E+03 */;
150     FG_V_down  = 0.0 /* -1.265722E-05 */;
151
152     /* Initial Orientation */
153     FG_Phi   = -2.658474E-06;
154     FG_Theta =  7.401790E-03;
155     FG_Psi   =  270.0 * DEG_TO_RAD;
156
157     /* Initial Angular B rates */
158     FG_P_body = 7.206685E-05;
159     FG_Q_body = 0.000000E+00;
160     FG_R_body = 9.492658E-05;
161
162     FG_Earth_position_angle = 0.000000E+00;
163
164     /* Mass properties and geometry values */
165     FG_Mass = 8.547270E+01;
166     FG_I_xx = 1.048000E+03;
167     FG_I_yy = 3.000000E+03;
168     FG_I_zz = 3.530000E+03;
169     FG_I_xz = 0.000000E+00;
170
171     /* CG position w.r.t. ref. point */
172     FG_Dx_cg = 0.000000E+00;
173     FG_Dy_cg = 0.000000E+00;
174     FG_Dz_cg = 0.000000E+00;
175
176     /* Set initial position and slew parameters */
177     /* fgSlewInit(-398391.3, 120070.41, 244, 3.1415); */ /* GLOBE Airport */
178     /* fgSlewInit(-335340,162540, 15, 4.38); */
179     /* fgSlewInit(-398673.28,120625.64, 53, 4.38); */
180
181     /* Initialize the event manager */
182     fgEventInit();
183
184     /* Dump event stats every 60 seconds */
185     fgEventRegister( "fgEventPrintStats()", fgEventPrintStats, 
186                      FG_EVENT_READY, 60000 );
187
188     /* Initialize "time" */
189     fgTimeInit(t);
190     fgTimeUpdate(f, t);
191
192     /* fgViewUpdate() needs the sun in the right place, while
193      * fgUpdateSunPos() needs to know the view position.  I'll get
194      * around this interdependency for now by calling fgUpdateSunPos()
195      * once, then moving on with normal initialization. */
196     fgUpdateSunPos();
197
198     /* Initialize view parameters */
199     fgViewInit(v);
200     fgViewUpdate(f, v, l);
201
202     /* Initialize shared sun position and sun_vec */
203     fgEventRegister( "fgUpdateSunPos()", fgUpdateSunPos, FG_EVENT_READY, 1000 );
204
205     /* Initialize the weather modeling subsystem */
206     fgWeatherInit();
207
208     /* update the weather for our current position */
209     fgEventRegister( "fgWeatherUpdate()", fgWeatherUpdate, 
210                      FG_EVENT_READY, 120000 );
211
212     /* Initialize the Cockpit subsystem */
213     if( fgCockpitInit( current_aircraft ) == NULL ) {
214         printf( "Error in Cockpit initialization!\n" );
215         exit( 1 );
216     }
217
218     /* Initialize the orbital elements of sun, moon and mayor planets */
219     fgSolarSystemInit(*t);
220
221     /* Initialize the Stars subsystem */
222     fgStarsInit();
223
224     /* Initialize the sun's position */
225     fgEventRegister( "fgSunInit()", fgSunInit, FG_EVENT_READY, 600000 );
226
227     /* Intialize the moon's position */
228     fgEventRegister( "fgMoonInit()", fgMoonInit, FG_EVENT_READY, 600000 );
229
230     /* Initialize the "sky" */
231     fgSkyInit();
232
233     /* Initialize the Scenery Management subsystem */
234     fgTileMgrInit();
235     /* fgSceneryInit(); */
236
237     /* Tell the Scenery Management system where we are so it can load
238      * the correct scenery data */
239     fgTileMgrUpdate();
240     /* fgSceneryUpdate(FG_Latitude, FG_Longitude, FG_Altitude); */
241
242     /* I'm just sticking this here for now, it should probably move 
243      * eventually */
244     cur_elev = mesh_altitude(FG_Longitude * RAD_TO_DEG * 3600.0, 
245                              FG_Latitude  * RAD_TO_DEG * 3600.0);
246     printf("True ground elevation is %.2f meters here.\n", cur_elev);
247     if ( cur_elev > -9990.0 ) {
248         FG_Runway_altitude = cur_elev * METER_TO_FEET;
249     }
250
251     if ( FG_Altitude < FG_Runway_altitude ) {
252         FG_Altitude = FG_Runway_altitude + 3.758099;
253     }
254     printf("Updated position (after elevation adj): (%.4f, %.4f, %.2f)\n", 
255            FG_Latitude * RAD_TO_DEG, FG_Longitude * RAD_TO_DEG, 
256            FG_Altitude * FEET_TO_METER);
257     /* end of thing that I just stuck in that I should probably move */
258
259     /* Initialize the flight model subsystem data structures base on
260      * above values */
261     fgFlightModelInit( FG_LARCSIM, f, 1.0 / DEFAULT_MODEL_HZ );
262
263     /* To HUD or not to HUD */
264     show_hud = 0;
265
266     /* Let's show the instrument panel */
267     displayInstruments = 0;
268
269     /* Joystick support */
270     fgJoystickInit( 0 );
271
272     /* One more try here to get the sky synced up */
273     fgSkyColorsInit();
274
275     printf("\n");
276 }
277
278
279 /* $Log$
280 /* Revision 1.30  1998/01/13 00:23:09  curt
281 /* Initial changes to support loading and management of scenery tiles.  Note,
282 /* there's still a fair amount of work left to be done.
283 /*
284  * Revision 1.29  1998/01/08 02:22:08  curt
285  * Beginning to integrate Tile management subsystem.
286  *
287  * Revision 1.28  1998/01/07 03:18:58  curt
288  * Moved astronomical stuff from .../Src/Scenery to .../Src/Astro/
289  *
290  * Revision 1.27  1998/01/05 18:44:35  curt
291  * Add an option to advance/decrease time from keyboard.
292  *
293  * Revision 1.26  1997/12/30 23:09:04  curt
294  * Tweaking initialization sequences.
295  *
296  * Revision 1.25  1997/12/30 22:22:33  curt
297  * Further integration of event manager.
298  *
299  * Revision 1.24  1997/12/30 20:47:44  curt
300  * Integrated new event manager with subsystem initializations.
301  *
302  * Revision 1.23  1997/12/30 16:36:50  curt
303  * Merged in Durk's changes ...
304  *
305  * Revision 1.22  1997/12/19 23:34:05  curt
306  * Lot's of tweaking with sky rendering and lighting.
307  *
308  * Revision 1.21  1997/12/19 16:45:00  curt
309  * Working on scene rendering order and options.
310  *
311  * Revision 1.20  1997/12/18 23:32:33  curt
312  * First stab at sky dome actually starting to look reasonable. :-)
313  *
314  * Revision 1.19  1997/12/17 23:13:36  curt
315  * Began working on rendering a sky.
316  *
317  * Revision 1.18  1997/12/15 23:54:49  curt
318  * Add xgl wrappers for debugging.
319  * Generate terrain normals on the fly.
320  *
321  * Revision 1.17  1997/12/15 20:59:09  curt
322  * Misc. tweaks.
323  *
324  * Revision 1.16  1997/12/12 19:52:48  curt
325  * Working on lightling and material properties.
326  *
327  * Revision 1.15  1997/12/11 04:43:55  curt
328  * Fixed sun vector and lighting problems.  I thing the moon is now lit
329  * correctly.
330  *
331  * Revision 1.14  1997/12/10 22:37:47  curt
332  * Prepended "fg" on the name of all global structures that didn't have it yet.
333  * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
334  *
335  * Revision 1.13  1997/11/25 19:25:32  curt
336  * Changes to integrate Durk's moon/sun code updates + clean up.
337  *
338  * Revision 1.12  1997/11/15 18:16:35  curt
339  * minor tweaks.
340  *
341  * Revision 1.11  1997/10/30 12:38:42  curt
342  * Working on new scenery subsystem.
343  *
344  * Revision 1.10  1997/10/25 03:24:23  curt
345  * Incorporated sun, moon, and star positioning code contributed by Durk Talsma.
346  *
347  * Revision 1.9  1997/09/23 00:29:39  curt
348  * Tweaks to get things to compile with gcc-win32.
349  *
350  * Revision 1.8  1997/09/22 14:44:20  curt
351  * Continuing to try to align stars correctly.
352  *
353  * Revision 1.7  1997/09/16 15:50:30  curt
354  * Working on star alignment and time issues.
355  *
356  * Revision 1.6  1997/09/05 14:17:30  curt
357  * More tweaking with stars.
358  *
359  * Revision 1.5  1997/09/04 02:17:36  curt
360  * Shufflin' stuff.
361  *
362  * Revision 1.4  1997/08/27 21:32:26  curt
363  * Restructured view calculation code.  Added stars.
364  *
365  * Revision 1.3  1997/08/27 03:30:19  curt
366  * Changed naming scheme of basic shared structures.
367  *
368  * Revision 1.2  1997/08/25 20:27:23  curt
369  * Merged in initial HUD and Joystick code.
370  *
371  * Revision 1.1  1997/08/23 01:46:20  curt
372  * Initial revision.
373  *
374  */