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