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