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