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