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