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