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