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