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