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