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