]> git.mxchange.org Git - flightgear.git/blob - Main/GLUTmain.cxx
Type-ified fgTIME and fgVIEW.
[flightgear.git] / Main / GLUTmain.cxx
1 //
2 // GLUTmain.cxx -- top level sim routines
3 //
4 // Written by Curtis Olson for OpenGL, started May 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 #ifdef HAVE_CONFIG_H
27 #  include <config.h>
28 #endif
29
30 #ifdef HAVE_WINDOWS_H
31 #  include <windows.h>                     
32 #endif
33
34 #include <GL/glut.h>
35 #include <XGL/xgl.h>
36 #include <stdio.h>
37
38 #ifdef HAVE_STDLIB_H
39 #   include <stdlib.h>
40 #endif
41
42 #include <Include/fg_constants.h>  // for VERSION
43 #include <Include/general.h>
44
45 #include <Aircraft/aircraft.h>
46 #include <Astro/moon.hxx>
47 #include <Astro/planets.hxx>
48 #include <Astro/sky.hxx>
49 #include <Astro/stars.hxx>
50 #include <Astro/sun.hxx>
51 #include <Cockpit/cockpit.hxx>
52 #include <Debug/fg_debug.h>
53 #include <Joystick/joystick.h>
54 #include <Math/fg_geodesy.h>
55 #include <Math/mat3.h>
56 #include <Math/polar.h>
57 #include <Scenery/scenery.h>
58 #include <Scenery/tilemgr.hxx>
59 #include <Time/event.hxx>
60 #include <Time/fg_time.hxx>
61 #include <Time/fg_timer.hxx>
62 #include <Time/sunpos.hxx>
63 #include <Weather/weather.h>
64
65 #include "GLUTkey.hxx"
66 #include "fg_init.hxx"
67 #include "options.hxx"
68 #include "views.hxx"
69
70
71 // This is a record containing global housekeeping information
72 fgGENERAL general;
73
74 // view parameters
75 static GLfloat win_ratio = 1.0;
76 static GLint winWidth, winHeight;
77
78 // Another hack
79 int use_signals = 0;
80
81 // Yet another other hack. Used for my prototype instrument code. (Durk)
82 int displayInstruments; 
83
84 /*
85 // The following defines flight gear options. Because glutlib will also
86 // want to parse its own options, those options must not be included here
87 // or they will get parsed by the main program option parser. Hence case
88 // is significant for any option added that might be in conflict with
89 // glutlib's parser.
90 //
91 // glutlib parses for:
92 //    -display
93 //    -direct   (invalid in Win32)
94 //    -geometry
95 //    -gldebug
96 //    -iconized
97 //    -indirect (invalid in Win32)
98 //    -synce
99 //
100 // Note that glutlib depends upon strings while this program's
101 // option parser wants only initial characters followed by numbers
102 // or pathnames.
103 //
104 const char *fg_cmdargopts = "a:c:Hhp:r:v:x:?";
105 //
106 // Where
107 //   -a aircraftfilename    aircraft start over ride
108 //   -c0x0000 - 0xffffffff  debug class setting
109 //    H,h.? help on command line use (does not need Option struct)
110 //   -p  priority
111 //   -r flightgear          root path to program support files
112 //   -v0 -v1                initial view mode (hud/no_hud currently)
113 //   -xlogpathname          debug logfile name
114 //
115 // Defaults in arguments to indicate not set on command line.
116 // Program defaults set variables from constants if neither command
117 // options or environmental variables effect values.
118 //
119
120 char  acArgbuf          [ MAXPATH + 1] = "\0";
121 int   debugArgValue                    = -2;
122 int   priorityArgValue                 = -1;
123 char  rootArgbuf        [ MAXPATH + 1] = "\0";
124 int   viewArg                          = -1;
125 char  logArgbuf         [ MAXPATH + 1] = "\0";
126
127 // There is a reason for defining the option structs by name and then
128 // creating an array of pointers to options. C++ is unfriendly to
129 // initializing arrays of objects that are not built in types. Always
130 // look forward. (Besides, you can follow what is going on better and
131 // add or modify with greater security. -ch
132 //
133 Option aircraftOption = { 'a',
134                           OPT_STRING,
135                           acArgbuf,
136                           "Startup aircraft pathname override"
137                         };
138 Option debugOption    = { 'c',
139                           OPT_LHEX,       // Long int (32 bits)
140                           &debugArgValue,
141                           "Debug trace level"
142                         };
143 Option priorityOption = { 'p',
144                           OPT_INTEGER,
145                           &priorityArgValue,
146                           "Debug priority Threshold"
147                         };
148 Option rootOption     = { 'r',
149                           OPT_STRING,
150                           rootArgbuf,
151                           "Root directory for execution"
152                         };
153 Option hudOption      = { 'v',
154                           OPT_INTEGER,
155                           &viewArg,
156                           "View mode start" // Naked,HUD,Panel,Chase,Tower...
157                         };
158
159 // Only naked view and HUD are implemented at this time
160 Option logfileOption  = { 'x',
161                           OPT_STRING,
162                           logArgbuf,
163                           "Debug log file name"
164                         };
165
166 #define OptsDefined 6
167 Option *CmdLineOptions[ OptsDefined ] = {
168   &aircraftOption,
169   &debugOption,
170   &hudOption,
171   &priorityOption,
172   &rootOption,
173   &logfileOption
174   };
175
176 const char *DefaultRootDir  = "\\Flightgear";
177 const char *DefaultAircraft = "Navion.acf";
178 const char *DefaultDebuglog = "fgdebug.log";
179 const int   DefaultViewMode = HUD_VIEW;
180 */
181
182 // Debug defaults handled in fg_debug.c
183
184 // fgInitVisuals() -- Initialize various GL/view parameters
185 static void fgInitVisuals( void ) {
186     fgLIGHT *l;
187     struct fgWEATHER *w;
188
189     l = &cur_light_params;
190     w = &current_weather;
191
192     // xglDisable( GL_DITHER );
193
194     // If enabled, normal vectors specified with glNormal are scaled
195     // to unit length after transformation.  See glNormal.
196     xglEnable( GL_NORMALIZE );
197
198     xglEnable( GL_LIGHTING );
199     xglEnable( GL_LIGHT0 );
200     xglLightfv( GL_LIGHT0, GL_POSITION, l->sun_vec );
201
202     xglFogi (GL_FOG_MODE, GL_LINEAR);
203     xglFogf (GL_FOG_START, w->visibility / 1000000.0 );
204     xglFogf (GL_FOG_END, w->visibility);
205     // xglFogf (GL_FOG_DENSITY, w->visibility);
206     xglHint (GL_FOG_HINT, GL_NICEST /* GL_FASTEST */ );
207
208     // draw wire frame
209     // xglPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
210 }
211
212
213 // Update the view volume, position, and orientation
214 static void fgUpdateViewParams( void ) {
215     fgFLIGHT *f;
216     fgLIGHT *l;
217     fgVIEW *v;
218
219     f = current_aircraft.flight;
220     l = &cur_light_params;
221     v = &current_view;
222
223     fgViewUpdate(f, v, l);
224
225     if (displayInstruments) {
226         xglViewport( 0, (GLint)(winHeight / 2 ) , 
227                      (GLint)winWidth, (GLint)winHeight / 2 );
228         // Tell GL we are about to modify the projection parameters
229         xglMatrixMode(GL_PROJECTION);
230         xglLoadIdentity();
231         gluPerspective(65.0, 2.0/win_ratio, 1.0, 100000.0);
232     } else {
233         xglViewport(0, 0 , (GLint)winWidth, (GLint) winHeight);
234         // Tell GL we are about to modify the projection parameters
235         xglMatrixMode(GL_PROJECTION);
236         xglLoadIdentity();
237         gluPerspective(65.0, 1.0/win_ratio, 10.0, 100000.0);
238     }
239
240     xglMatrixMode(GL_MODELVIEW);
241     xglLoadIdentity();
242     
243     // set up our view volume (default)
244     gluLookAt(v->view_pos.x, v->view_pos.y, v->view_pos.z,
245                v->view_pos.x + v->view_forward[0], 
246                v->view_pos.y + v->view_forward[1], 
247                v->view_pos.z + v->view_forward[2],
248                v->view_up[0], v->view_up[1], v->view_up[2]);
249
250     // look almost straight up (testing and eclipse watching)
251     /* gluLookAt(v->view_pos.x, v->view_pos.y, v->view_pos.z,
252                v->view_pos.x + v->view_up[0] + .001, 
253                v->view_pos.y + v->view_up[1] + .001, 
254                v->view_pos.z + v->view_up[2] + .001,
255                v->view_up[0], v->view_up[1], v->view_up[2]); */
256
257     // lock view horizontally towards sun (testing)
258     /* gluLookAt(v->view_pos.x, v->view_pos.y, v->view_pos.z,
259                v->view_pos.x + v->surface_to_sun[0], 
260                v->view_pos.y + v->surface_to_sun[1], 
261                v->view_pos.z + v->surface_to_sun[2],
262                v->view_up[0], v->view_up[1], v->view_up[2]); */
263
264     // lock view horizontally towards south (testing)
265     /* gluLookAt(v->view_pos.x, v->view_pos.y, v->view_pos.z,
266                v->view_pos.x + v->surface_south[0], 
267                v->view_pos.y + v->surface_south[1], 
268                v->view_pos.z + v->surface_south[2],
269                v->view_up[0], v->view_up[1], v->view_up[2]); */
270
271     // set the sun position
272     xglLightfv( GL_LIGHT0, GL_POSITION, l->sun_vec );
273 }
274
275
276 // Draw a basic instrument panel
277 static void fgUpdateInstrViewParams( void ) {
278     xglViewport(0, 0 , (GLint)winWidth, (GLint)winHeight / 2);
279   
280     xglMatrixMode(GL_PROJECTION);
281     xglPushMatrix();
282   
283     xglLoadIdentity();
284     gluOrtho2D(0, 640, 0, 480);
285     xglMatrixMode(GL_MODELVIEW);
286     xglPushMatrix();
287     xglLoadIdentity();
288
289     xglColor3f(1.0, 1.0, 1.0);
290     xglIndexi(7);
291   
292     xglDisable(GL_DEPTH_TEST);
293     xglDisable(GL_LIGHTING);
294   
295     xglLineWidth(1);
296     xglColor3f (0.5, 0.5, 0.5);
297
298     xglBegin(GL_QUADS);
299     xglVertex2f(0.0, 0.00);
300     xglVertex2f(0.0, 480.0);
301     xglVertex2f(640.0,480.0);
302     xglVertex2f(640.0, 0.0);
303     xglEnd();
304
305     xglRectf(0.0,0.0, 640, 480);
306     xglEnable(GL_DEPTH_TEST);
307     xglEnable(GL_LIGHTING);
308     xglMatrixMode(GL_PROJECTION);
309     xglPopMatrix();
310     xglMatrixMode(GL_MODELVIEW);
311     xglPopMatrix();
312 }
313
314
315 // Update all Visuals (redraws anything graphics related)
316 static void fgRenderFrame( void ) {
317     fgLIGHT *l;
318     fgOPTIONS *o;
319     fgTIME *t;
320     fgVIEW *v;
321     double angle;
322     GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
323     GLfloat color[4] = { 0.54, 0.44, 0.29, 1.0 };
324
325     l = &cur_light_params;
326     o = &current_options;
327     t = &cur_time_params;
328     v = &current_view;
329
330     // update view volume parameters
331     fgUpdateViewParams();
332
333     xglClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
334
335     // Tell GL we are switching to model view parameters
336     xglMatrixMode(GL_MODELVIEW);
337     // xglLoadIdentity();
338
339     // draw sky
340     xglDisable( GL_DEPTH_TEST );
341     xglDisable( GL_LIGHTING );
342     xglDisable( GL_CULL_FACE );
343     xglDisable( GL_FOG );
344     xglShadeModel( GL_SMOOTH );
345     fgSkyRender();
346
347     // setup transformation for drawing astronomical objects
348     xglPushMatrix();
349     // Translate to view position
350     xglTranslatef( v->view_pos.x, v->view_pos.y, v->view_pos.z );
351     // Rotate based on gst (sidereal time)
352     angle = t->gst * 15.041085; /* should be 15.041085, Curt thought it was 15*/
353     // printf("Rotating astro objects by %.2f degrees\n",angle);
354     xglRotatef( angle, 0.0, 0.0, -1.0 );
355
356     // draw stars and planets
357     fgStarsRender();
358     fgPlanetsRender();
359
360     // draw the sun
361     fgSunRender();
362
363     // render the moon
364     xglEnable( GL_LIGHTING );
365     // set lighting parameters
366     xglLightfv(GL_LIGHT0, GL_AMBIENT, white );
367     xglLightfv(GL_LIGHT0, GL_DIFFUSE, white );
368     xglEnable( GL_CULL_FACE );
369     
370     // Let's try some blending technique's (Durk)
371     glEnable(GL_BLEND);
372     glBlendFunc(GL_ONE, GL_ONE);
373     fgMoonRender();
374     glDisable(GL_BLEND);
375
376     xglPopMatrix();
377
378     // draw scenery
379     xglShadeModel( /* GL_FLAT */ GL_SMOOTH ); 
380     xglEnable( GL_DEPTH_TEST );
381     xglEnable( GL_FOG );
382     xglFogfv (GL_FOG_COLOR, l->fog_color);
383     // set lighting parameters
384     xglLightfv(GL_LIGHT0, GL_AMBIENT, l->scene_ambient );
385     xglLightfv(GL_LIGHT0, GL_DIFFUSE, l->scene_diffuse );
386
387     if ( o->use_textures ) {
388         // texture parameters
389         xglEnable( GL_TEXTURE_2D );
390         xglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ) ;
391         xglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ) ;
392         xglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ) ;
393         xglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
394                           GL_LINEAR /* GL_LINEAR_MIPMAP_LINEAR */ ) ;
395         xglTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
396         xglHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ) ;
397         // set base color (I don't think this is doing anything here)
398         xglMaterialfv (GL_FRONT, GL_AMBIENT, white);
399         xglMaterialfv (GL_FRONT, GL_DIFFUSE, white);
400     } else {
401         xglDisable( GL_TEXTURE_2D );
402         xglMaterialfv (GL_FRONT, GL_AMBIENT, color);
403         xglMaterialfv (GL_FRONT, GL_DIFFUSE, color);
404     }
405
406     fgTileMgrRender();
407
408     xglDisable( GL_TEXTURE_2D );
409
410     // display HUD
411     if( o->hud_status ) {
412         fgCockpitUpdate();
413     }
414
415     // display instruments
416     if (displayInstruments) {
417         fgUpdateInstrViewParams();
418     }
419
420     xglutSwapBuffers();
421 }
422
423
424 // Update internal time dependent calculations (i.e. flight model)
425 void fgUpdateTimeDepCalcs(int multi_loop) {
426     fgFLIGHT *f;
427     fgTIME *t;
428     fgVIEW *v;
429     int i;
430
431     f = current_aircraft.flight;
432     t = &cur_time_params;
433     v = &current_view;
434
435     // update the flight model
436     if ( multi_loop < 0 ) {
437         multi_loop = DEFAULT_MULTILOOP;
438     }
439
440     // printf("updating flight model x %d\n", multi_loop);
441     fgFlightModelUpdate(FG_LARCSIM, f, multi_loop);
442
443     // update the view angle
444     for ( i = 0; i < multi_loop; i++ ) {
445         if ( fabs(v->goal_view_offset - v->view_offset) < 0.05 ) {
446             v->view_offset = v->goal_view_offset;
447             break;
448         } else {
449             // move v->view_offset towards v->goal_view_offset
450             if ( v->goal_view_offset > v->view_offset ) {
451                 if ( v->goal_view_offset - v->view_offset < FG_PI ) {
452                     v->view_offset += 0.01;
453                 } else {
454                     v->view_offset -= 0.01;
455                 }
456             } else {
457                 if ( v->view_offset - v->goal_view_offset < FG_PI ) {
458                     v->view_offset -= 0.01;
459                 } else {
460                     v->view_offset += 0.01;
461                 }
462             }
463             if ( v->view_offset > FG_2PI ) {
464                 v->view_offset -= FG_2PI;
465             } else if ( v->view_offset < 0 ) {
466                 v->view_offset += FG_2PI;
467             }
468         }
469     }
470 }
471
472
473 void fgInitTimeDepCalcs( void ) {
474     // initialize timer
475
476 #ifdef HAVE_SETITIMER
477     fgTimerInit( 1.0 / DEFAULT_TIMER_HZ, fgUpdateTimeDepCalcs );
478 #endif HAVE_SETITIMER
479
480 }
481
482
483 // What should we do when we have nothing else to do?  Let's get ready
484 // for the next move and update the display?
485 static void fgMainLoop( void ) {
486     static int remainder = 0;
487     int elapsed, multi_loop;
488     double cur_elev;
489     // double joy_x, joy_y;
490     // int joy_b1, joy_b2;
491     fgAIRCRAFT *a;
492     fgFLIGHT *f;
493     fgTIME *t;
494
495     fgPrintf( FG_ALL, FG_DEBUG, "Running Main Loop\n");
496     fgPrintf( FG_ALL, FG_DEBUG, "======= ==== ====\n");
497
498     a = &current_aircraft;
499     f = a->flight;
500     t = &cur_time_params;
501
502     // update "time"
503     fgTimeUpdate(f, t);
504
505     // Read joystick
506     /* fgJoystickRead( &joy_x, &joy_y, &joy_b1, &joy_b2 );
507     printf( "Joystick X %f  Y %f  B1 %d  B2 %d\n",  
508             joy_x, joy_y, joy_b1, joy_b2 );
509     fgElevSet( -joy_y );
510     fgAileronSet( joy_x ); */
511
512     // Calculate model iterations needed
513     elapsed = fgGetTimeInterval();
514     fgPrintf( FG_ALL, FG_BULK, 
515               "Time interval is = %d, previous remainder is = %d\n", 
516               elapsed, remainder);
517     fgPrintf( FG_ALL, FG_BULK, 
518               "--> Frame rate is = %.2f\n", 1000.0 / (float)elapsed);
519     elapsed += remainder;
520
521     multi_loop = (int)(((float)elapsed * 0.001) * DEFAULT_MODEL_HZ);
522     remainder = elapsed - ((multi_loop*1000) / DEFAULT_MODEL_HZ);
523     fgPrintf( FG_ALL, FG_BULK, 
524               "Model iterations needed = %d, new remainder = %d\n", 
525               multi_loop, remainder);
526         
527     // Run flight model
528     if ( ! use_signals ) {
529         // flight model
530         fgUpdateTimeDepCalcs(multi_loop);
531     }
532
533     // I'm just sticking this here for now, it should probably move
534     // eventually
535     /* cur_elev = mesh_altitude(FG_Longitude * RAD_TO_ARCSEC, 
536                                FG_Latitude  * RAD_TO_ARCSEC); */
537     // there is no ground collision detection really, so for now I
538     // just hard code the ground elevation to be 0 */
539     cur_elev = 0;
540
541     // printf("Ground elevation is %.2f meters here.\n", cur_elev);
542     // FG_Runway_altitude = cur_elev * METER_TO_FEET;
543
544     if ( FG_Altitude * FEET_TO_METER < cur_elev + 3.758099) {
545         // set this here, otherwise if we set runway height above our
546         // current height we get a really nasty bounce.
547         FG_Runway_altitude = FG_Altitude - 3.758099;
548
549         // now set aircraft altitude above ground
550         FG_Altitude = cur_elev * METER_TO_FEET + 3.758099;
551         fgPrintf( FG_ALL, FG_BULK, "<*> resetting altitude to %.0f meters\n", 
552                FG_Altitude * FEET_TO_METER);
553     }
554
555     fgAircraftOutputCurrent(a);
556
557     // see if we need to load any new scenery tiles
558     fgTileMgrUpdate();
559
560     // Process/manage pending events
561     fgEventProcess();
562
563     // redraw display
564     fgRenderFrame();
565
566     fgPrintf( FG_ALL, FG_DEBUG, "\n");
567 }
568
569
570 // Handle new window size or exposure
571 static void fgReshape( int width, int height ) {
572     // Do this so we can call fgReshape(0,0) ourselves without having
573     // to know what the values of width & height are.
574     if ( (height > 0) && (width > 0) ) {
575         win_ratio = (GLfloat) height / (GLfloat) width;
576     }
577
578     winWidth = width;
579     winHeight = height;
580
581     // Inform gl of our view window size (now handled elsewhere)
582     // xglViewport(0, 0, (GLint)width, (GLint)height);
583     fgUpdateViewParams();
584     
585     // xglClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
586 }
587
588
589 // Initialize GLUT and define a main window
590 int fgGlutInit( int argc, char **argv ) {
591     // GLUT will extract all glut specific options so later on we only
592     // need wory about our own.
593     xglutInit(&argc, argv);
594
595     // Define Display Parameters
596     xglutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
597
598     // Define initial window size
599     xglutInitWindowSize(640, 480);
600
601     // Initialize windows
602     xglutCreateWindow("Flight Gear");
603
604     return(1);
605 }
606
607
608 // Initialize GLUT event handlers
609 int fgGlutInitEvents( void ) {
610     // call fgReshape() on window resizes
611     xglutReshapeFunc( fgReshape );
612
613     // call GLUTkey() on keyboard event
614     xglutKeyboardFunc( GLUTkey );
615     glutSpecialFunc( GLUTspecialkey );
616
617     // call fgMainLoop() whenever there is
618     // nothing else to do
619     xglutIdleFunc( fgMainLoop );
620
621     // draw the scene
622     xglutDisplayFunc( fgRenderFrame );
623
624     return(1);
625 }
626
627
628 // Main ...
629 int main( int argc, char **argv ) {
630     fgFLIGHT *f;
631     int result;  // Used in command line argument.
632
633     f = current_aircraft.flight;
634
635     // Initialize the debugging output system
636     fgInitDebug();
637
638     fgPrintf(FG_GENERAL, FG_INFO, "Flight Gear:  Version %s\n\n", VERSION);
639
640     // Initialize the Window/Graphics environment.
641     if( !fgGlutInit(argc, argv) ) {
642         fgPrintf( FG_GENERAL, FG_EXIT, "GLUT initialization failed ...\n" );
643     }
644
645     // Parse remaining command line options
646     result = current_options.parse(argc, argv);
647
648     if ( result != FG_OPTIONS_OK ) {
649         // Something must have gone horribly wrong with the command
650         // line parsing or maybe the user just requested help ... :-)
651         current_options.usage();
652         fgPrintf( FG_GENERAL, FG_EXIT, "\nShutting down ...\n");
653     }
654
655     // These are a few miscellaneous things that aren't really
656     // "subsystems" but still need to be initialized.
657     if( !fgInitGeneral()) {
658         fgPrintf( FG_GENERAL, FG_EXIT, "General initializations failed ...\n" );
659     }
660
661     // This is the top level init routine which calls all the other
662     // subsystem initialization routines.  If you are adding a
663     // subsystem to flight gear, its initialization call should
664     // located in this routine.
665     if( !fgInitSubsystems()) {
666         fgPrintf( FG_GENERAL, FG_EXIT,
667                   "Subsystem initializations failed ...\n" );
668     }
669
670     // setup OpenGL view parameters
671     fgInitVisuals();
672
673     if ( use_signals ) {
674         // init timer routines, signals, etc.  Arrange for an alarm
675         // signal to be generated, etc.
676         fgInitTimeDepCalcs();
677     }
678
679     // Initialize the various GLUT Event Handlers.
680     if( !fgGlutInitEvents() ) {
681         fgPrintf( FG_GENERAL, FG_EXIT, 
682                   "GLUT event handler initialization failed ...\n" );
683     }
684
685     // pass control off to the master GLUT event handler
686     glutMainLoop();
687
688     // we never actually get here ... but just in case ... :-)
689     return(0);
690 }
691
692
693 #ifdef __SUNPRO_CC
694 extern "C" {
695     void __eprintf( void ) {
696     }
697 }
698 #endif
699
700
701 // $Log$
702 // Revision 1.8  1998/04/28 01:20:21  curt
703 // Type-ified fgTIME and fgVIEW.
704 // Added a command line option to disable textures.
705 //
706 // Revision 1.7  1998/04/26 05:10:02  curt
707 // "struct fgLIGHT" -> "fgLIGHT" because fgLIGHT is typedef'd.
708 //
709 // Revision 1.6  1998/04/25 22:06:30  curt
710 // Edited cvs log messages in source files ... bad bad bad!
711 //
712 // Revision 1.5  1998/04/25 20:24:01  curt
713 // Cleaned up initialization sequence to eliminate interdependencies
714 // between sun position, lighting, and view position.  This creates a
715 // valid single pass initialization path.
716 //
717 // Revision 1.4  1998/04/24 14:19:30  curt
718 // Fog tweaks.
719 //
720 // Revision 1.3  1998/04/24 00:49:18  curt
721 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
722 // Trying out some different option parsing code.
723 // Some code reorganization.
724 //
725 // Revision 1.2  1998/04/22 13:25:41  curt
726 // C++ - ifing the code.
727 // Starting a bit of reorganization of lighting code.
728 //
729 // Revision 1.1  1998/04/21 17:02:39  curt
730 // Prepairing for C++ integration.
731 //
732 // Revision 1.71  1998/04/18 04:11:26  curt
733 // Moved fg_debug to it's own library, added zlib support.
734 //
735 // Revision 1.70  1998/04/14 02:21:02  curt
736 // Incorporated autopilot heading hold contributed by:  Jeff Goeke-Smith
737 // <jgoeke@voyager.net>
738 //
739 // Revision 1.69  1998/04/08 23:35:34  curt
740 // Tweaks to Gnu automake/autoconf system.
741 //
742 // Revision 1.68  1998/04/03 22:09:03  curt
743 // Converting to Gnu autoconf system.
744 //
745 // Revision 1.67  1998/03/23 21:24:37  curt
746 // Source code formating tweaks.
747 //
748 // Revision 1.66  1998/03/14 00:31:20  curt
749 // Beginning initial terrain texturing experiments.
750 //
751 // Revision 1.65  1998/03/09 22:45:57  curt
752 // Minor tweaks for building on sparc platform.
753 //
754 // Revision 1.64  1998/02/20 00:16:23  curt
755 // Thursday's tweaks.
756 //
757 // Revision 1.63  1998/02/16 16:17:39  curt
758 // Minor tweaks.
759 //
760 // Revision 1.62  1998/02/16 13:39:42  curt
761 // Miscellaneous weekend tweaks.  Fixed? a cache problem that caused whole
762 // tiles to occasionally be missing.
763 //
764 // Revision 1.61  1998/02/12 21:59:46  curt
765 // Incorporated code changes contributed by Charlie Hotchkiss
766 // <chotchkiss@namg.us.anritsu.com>
767 //
768 // Revision 1.60  1998/02/11 02:50:40  curt
769 // Minor changes.
770 //
771 // Revision 1.59  1998/02/09 22:56:54  curt
772 // Removed "depend" files from cvs control.  Other minor make tweaks.
773 //
774 // Revision 1.58  1998/02/09 15:07:49  curt
775 // Minor tweaks.
776 //
777 // Revision 1.57  1998/02/07 15:29:40  curt
778 // Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
779 // <chotchkiss@namg.us.anritsu.com>
780 //
781 // Revision 1.56  1998/02/03 23:20:23  curt
782 // Lots of little tweaks to fix various consistency problems discovered by
783 // Solaris' CC.  Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
784 // passed arguments along to the real printf().  Also incorporated HUD changes
785 // by Michele America.
786 //
787 // Revision 1.55  1998/02/02 20:53:58  curt
788 // Incorporated Durk's changes.
789 //
790 // Revision 1.54  1998/01/31 00:43:10  curt
791 // Added MetroWorks patches from Carmen Volpe.
792 //
793 // Revision 1.53  1998/01/27 18:35:54  curt
794 // Minor tweaks.
795 //
796 // Revision 1.52  1998/01/27 00:47:56  curt
797 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
798 // system and commandline/config file processing code.
799 //
800 // Revision 1.51  1998/01/26 15:57:05  curt
801 // Tweaks for dynamic scenery development.
802 //
803 // Revision 1.50  1998/01/19 19:27:07  curt
804 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
805 // This should simplify things tremendously.
806 //
807 // Revision 1.49  1998/01/19 18:40:31  curt
808 // Tons of little changes to clean up the code and to remove fatal errors
809 // when building with the c++ compiler.
810 //
811 // Revision 1.48  1998/01/19 18:35:46  curt
812 // Minor tweaks and fixes for cygwin32.
813 //
814 // Revision 1.47  1998/01/13 00:23:08  curt
815 // Initial changes to support loading and management of scenery tiles.  Note,
816 // there's still a fair amount of work left to be done.
817 //
818 // Revision 1.46  1998/01/08 02:22:06  curt
819 // Beginning to integrate Tile management subsystem.
820 //
821 // Revision 1.45  1998/01/07 03:18:55  curt
822 // Moved astronomical stuff from .../Src/Scenery to .../Src/Astro/
823 //
824 // Revision 1.44  1997/12/30 22:22:31  curt
825 // Further integration of event manager.
826 //
827 // Revision 1.43  1997/12/30 20:47:43  curt
828 // Integrated new event manager with subsystem initializations.
829 //
830 // Revision 1.42  1997/12/30 16:36:47  curt
831 // Merged in Durk's changes ...
832 //
833 // Revision 1.41  1997/12/30 13:06:56  curt
834 // A couple lighting tweaks ...
835 //
836 // Revision 1.40  1997/12/30 01:38:37  curt
837 // Switched back to per vertex normals and smooth shading for terrain.
838 //
839 // Revision 1.39  1997/12/22 23:45:45  curt
840 // First stab at sunset/sunrise sky glow effects.
841 //
842 // Revision 1.38  1997/12/22 04:14:28  curt
843 // Aligned sky with sun so dusk/dawn effects can be correct relative to the sun.
844 //
845 // Revision 1.37  1997/12/19 23:34:03  curt
846 // Lot's of tweaking with sky rendering and lighting.
847 //
848 // Revision 1.36  1997/12/19 16:44:57  curt
849 // Working on scene rendering order and options.
850 //
851 // Revision 1.35  1997/12/18 23:32:32  curt
852 // First stab at sky dome actually starting to look reasonable. :-)
853 //
854 // Revision 1.34  1997/12/17 23:13:34  curt
855 // Began working on rendering a sky.
856 //
857 // Revision 1.33  1997/12/15 23:54:45  curt
858 // Add xgl wrappers for debugging.
859 // Generate terrain normals on the fly.
860 //
861 // Revision 1.32  1997/12/15 20:59:08  curt
862 // Misc. tweaks.
863 //
864 // Revision 1.31  1997/12/12 21:41:25  curt
865 // More light/material property tweaking ... still a ways off.
866 //
867 // Revision 1.30  1997/12/12 19:52:47  curt
868 // Working on lightling and material properties.
869 //
870 // Revision 1.29  1997/12/11 04:43:54  curt
871 // Fixed sun vector and lighting problems.  I thing the moon is now lit
872 // correctly.
873 //
874 // Revision 1.28  1997/12/10 22:37:45  curt
875 // Prepended "fg" on the name of all global structures that didn't have it yet.
876 // i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
877 //
878 // Revision 1.27  1997/12/09 05:11:54  curt
879 // Working on tweaking lighting.
880 //
881 // Revision 1.26  1997/12/09 04:25:29  curt
882 // Working on adding a global lighting params structure.
883 //
884 // Revision 1.25  1997/12/08 22:54:09  curt
885 // Enabled GL_CULL_FACE.
886 //
887 // Revision 1.24  1997/11/25 19:25:32  curt
888 // Changes to integrate Durk's moon/sun code updates + clean up.
889 //
890 // Revision 1.23  1997/11/15 18:16:34  curt
891 // minor tweaks.
892 //
893 // Revision 1.22  1997/10/30 12:38:41  curt
894 // Working on new scenery subsystem.
895 //
896 // Revision 1.21  1997/09/23 00:29:38  curt
897 // Tweaks to get things to compile with gcc-win32.
898 //
899 // Revision 1.20  1997/09/22 14:44:19  curt
900 // Continuing to try to align stars correctly.
901 //
902 // Revision 1.19  1997/09/18 16:20:08  curt
903 // At dusk/dawn add/remove stars in stages.
904 //
905 // Revision 1.18  1997/09/16 22:14:51  curt
906 // Tweaked time of day lighting equations.  Don't draw stars during the day.
907 //
908 // Revision 1.17  1997/09/16 15:50:29  curt
909 // Working on star alignment and time issues.
910 //
911 // Revision 1.16  1997/09/13 02:00:06  curt
912 // Mostly working on stars and generating sidereal time for accurate star
913 // placement.
914 //
915 // Revision 1.15  1997/09/05 14:17:27  curt
916 // More tweaking with stars.
917 //
918 // Revision 1.14  1997/09/05 01:35:53  curt
919 // Working on getting stars right.
920 //
921 // Revision 1.13  1997/09/04 02:17:34  curt
922 // Shufflin' stuff.
923 //
924 // Revision 1.12  1997/08/27 21:32:24  curt
925 // Restructured view calculation code.  Added stars.
926 //
927 // Revision 1.11  1997/08/27 03:30:16  curt
928 // Changed naming scheme of basic shared structures.
929 //
930 // Revision 1.10  1997/08/25 20:27:22  curt
931 // Merged in initial HUD and Joystick code.
932 //
933 // Revision 1.9  1997/08/22 21:34:39  curt
934 // Doing a bit of reorganizing and house cleaning.
935 //
936 // Revision 1.8  1997/08/19 23:55:03  curt
937 // Worked on better simulating real lighting.
938 //
939 // Revision 1.7  1997/08/16 12:22:38  curt
940 // Working on improving the lighting/shading.
941 //
942 // Revision 1.6  1997/08/13 20:24:56  curt
943 // Changes due to changing sunpos interface.
944 //
945 // Revision 1.5  1997/08/06 21:08:32  curt
946 // Sun position now really* works (I think) ... I still have sun time warping
947 // code in place, probably should remove it soon.
948 //
949 // Revision 1.4  1997/08/06 15:41:26  curt
950 // Working on correct sun position.
951 //
952 // Revision 1.3  1997/08/06 00:24:22  curt
953 // Working on correct real time sun lighting.
954 //
955 // Revision 1.2  1997/08/04 20:25:15  curt
956 // Organizational tweaking.
957 //
958 // Revision 1.1  1997/08/02 18:45:00  curt
959 // Renamed GLmain.c GLUTmain.c
960 //
961 // Revision 1.43  1997/08/02 16:23:47  curt
962 // Misc. tweaks.
963 //
964 // Revision 1.42  1997/08/01 19:43:33  curt
965 // Making progress with coordinate system overhaul.
966 //
967 // Revision 1.41  1997/07/31 22:52:37  curt
968 // Working on redoing internal coordinate systems & scenery transformations.
969 //
970 // Revision 1.40  1997/07/30 16:12:42  curt
971 // Moved fg_random routines from Util/ to Math/
972 //
973 // Revision 1.39  1997/07/21 14:45:01  curt
974 // Minor tweaks.
975 //
976 // Revision 1.38  1997/07/19 23:04:47  curt
977 // Added an initial weather section.
978 //
979 // Revision 1.37  1997/07/19 22:34:02  curt
980 // Moved PI definitions to ../constants.h
981 // Moved random() stuff to ../Utils/ and renamed fg_random()
982 //
983 // Revision 1.36  1997/07/18 23:41:25  curt
984 // Tweaks for building with Cygnus Win32 compiler.
985 //
986 // Revision 1.35  1997/07/18 14:28:34  curt
987 // Hacked in some support for wind/turbulence.
988 //
989 // Revision 1.34  1997/07/16 20:04:48  curt
990 // Minor tweaks to aid Win32 port.
991 //
992 // Revision 1.33  1997/07/12 03:50:20  curt
993 // Added an #include <Windows32/Base.h> to help compiling for Win32
994 //
995 // Revision 1.32  1997/07/11 03:23:18  curt
996 // Solved some scenery display/orientation problems.  Still have a positioning
997 // (or transformation?) problem.
998 //
999 // Revision 1.31  1997/07/11 01:29:58  curt
1000 // More tweaking of terrian floor.
1001 //
1002 // Revision 1.30  1997/07/10 04:26:37  curt
1003 // We now can interpolated ground elevation for any position in the grid.  We
1004 // can use this to enforce a "hard" ground.  We still need to enforce some
1005 // bounds checking so that we don't try to lookup data points outside the
1006 // grid data set.
1007 //
1008 // Revision 1.29  1997/07/09 21:31:12  curt
1009 // Working on making the ground "hard."
1010 //
1011 // Revision 1.28  1997/07/08 18:20:12  curt
1012 // Working on establishing a hard ground.
1013 //
1014 // Revision 1.27  1997/07/07 20:59:49  curt
1015 // Working on scenery transformations to enable us to fly fluidly over the
1016 // poles with no discontinuity/distortion in scenery.
1017 //
1018 // Revision 1.26  1997/07/05 20:43:34  curt
1019 // renamed mat3 directory to Math so we could add other math related routines.
1020 //
1021 // Revision 1.25  1997/06/29 21:19:17  curt
1022 // Working on scenery management system.
1023 //
1024 // Revision 1.24  1997/06/26 22:14:53  curt
1025 // Beginning work on a scenery management system.
1026 //
1027 // Revision 1.23  1997/06/26 19:08:33  curt
1028 // Restructuring make, adding automatic "make dep" support.
1029 //
1030 // Revision 1.22  1997/06/25 15:39:47  curt
1031 // Minor changes to compile with rsxnt/win32.
1032 //
1033 // Revision 1.21  1997/06/22 21:44:41  curt
1034 // Working on intergrating the VRML (subset) parser.
1035 //
1036 // Revision 1.20  1997/06/21 17:12:53  curt
1037 // Capitalized subdirectory names.
1038 //
1039 // Revision 1.19  1997/06/18 04:10:31  curt
1040 // A couple more runway tweaks ...
1041 //
1042 // Revision 1.18  1997/06/18 02:21:24  curt
1043 // Hacked in a runway
1044 //
1045 // Revision 1.17  1997/06/17 16:51:58  curt
1046 // Timer interval stuff now uses gettimeofday() instead of ftime()
1047 //
1048 // Revision 1.16  1997/06/17 04:19:16  curt
1049 // More timer related tweaks with respect to view direction changes.
1050 //
1051 // Revision 1.15  1997/06/17 03:41:10  curt
1052 // Nonsignal based interval timing is now working.
1053 // This would be a good time to look at cleaning up the code structure a bit.
1054 //
1055 // Revision 1.14  1997/06/16 19:32:51  curt
1056 // Starting to add general timer support.
1057 //
1058 // Revision 1.13  1997/06/02 03:40:06  curt
1059 // A tiny bit more view tweaking.
1060 //
1061 // Revision 1.12  1997/06/02 03:01:38  curt
1062 // Working on views (side, front, back, transitions, etc.)
1063 //
1064 // Revision 1.11  1997/05/31 19:16:25  curt
1065 // Elevator trim added.
1066 //
1067 // Revision 1.10  1997/05/31 04:13:52  curt
1068 // WE CAN NOW FLY!!!
1069 //
1070 // Continuing work on the LaRCsim flight model integration.
1071 // Added some MSFS-like keyboard input handling.
1072 //
1073 // Revision 1.9  1997/05/30 19:27:01  curt
1074 // The LaRCsim flight model is starting to look like it is working.
1075 //
1076 // Revision 1.8  1997/05/30 03:54:10  curt
1077 // Made a bit more progress towards integrating the LaRCsim flight model.
1078 //
1079 // Revision 1.7  1997/05/29 22:39:49  curt
1080 // Working on incorporating the LaRCsim flight model.
1081 //
1082 // Revision 1.6  1997/05/29 12:31:39  curt
1083 // Minor tweaks, moving towards general flight model integration.
1084 //
1085 // Revision 1.5  1997/05/29 02:33:23  curt
1086 // Updated to reflect changing interfaces in other "modules."
1087 //
1088 // Revision 1.4  1997/05/27 17:44:31  curt
1089 // Renamed & rearranged variables and routines.   Added some initial simple
1090 // timer/alarm routines so the flight model can be updated on a regular 
1091 // interval.
1092 //
1093 // Revision 1.3  1997/05/23 15:40:25  curt
1094 // Added GNU copyright headers.
1095 // Fog now works!
1096 //
1097 // Revision 1.2  1997/05/23 00:35:12  curt
1098 // Trying to get fog to work ...
1099 //
1100 // Revision 1.1  1997/05/21 15:57:51  curt
1101 // Renamed due to added GLUT support.
1102 //
1103 // Revision 1.3  1997/05/19 18:22:42  curt
1104 // Parameter tweaking ... starting to stub in fog support.
1105 //
1106 // Revision 1.2  1997/05/17 00:17:34  curt
1107 // Trying to stub in support for standard OpenGL.
1108 //
1109 // Revision 1.1  1997/05/16 16:05:52  curt
1110 // Initial revision.
1111 //