]> git.mxchange.org Git - flightgear.git/blob - Main/GLUTmain.cxx
Tweaked FDM interface.
[flightgear.git] / Main / GLUTmain.cxx
1 // GLUTmain.cxx -- top level sim routines
2 //
3 // Written by Curtis Olson for OpenGL, started May 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@me.umn.edu
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22 // (Log is kept at end of this file)
23
24 #define MICHAEL_JOHNSON_EXPERIMENTAL_ENGINE_AUDIO
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 #include <string>
40
41 #ifdef HAVE_STDLIB_H
42 #   include <stdlib.h>
43 #endif
44
45 #ifdef HAVE_SYS_STAT_H
46 #  include <sys/stat.h> /* for stat() */
47 #endif
48
49 #ifdef HAVE_UNISTD_H
50 #  include <unistd.h>    /* for stat() */
51 #endif
52
53 #include <Include/fg_constants.h>  // for VERSION
54 #include <Include/general.hxx>
55
56 #include <Aircraft/aircraft.hxx>
57 #include <Astro/sky.hxx>
58 #include <Astro/stars.hxx>
59 #include <Astro/solarsystem.hxx>
60
61 #ifdef ENABLE_AUDIO_SUPPORT
62 #  include <Audio/src/sl.h>
63 #  include <Audio/src/sm.h>
64 #endif
65
66 #include <Autopilot/autopilot.hxx>
67 #include <Cockpit/cockpit.hxx>
68 #include <Debug/logstream.hxx>
69 #include <GUI/gui.h>
70 #include <Joystick/joystick.hxx>
71 #include <Math/fg_geodesy.hxx>
72 #include <Math/mat3.h>
73 #include <Math/polar3d.hxx>
74 #include <PUI/pu.h>
75 #include <Scenery/scenery.hxx>
76 #include <Scenery/tilemgr.hxx>
77 #include <Time/event.hxx>
78 #include <Time/fg_time.hxx>
79 #include <Time/fg_timer.hxx>
80 #include <Time/sunpos.hxx>
81 #include <Weather/weather.hxx>
82
83 #include "GLUTkey.hxx"
84 #include "fg_init.hxx"
85 #include "options.hxx"
86 #include "splash.hxx"
87 #include "views.hxx"
88
89
90 // This is a record containing a bit of global housekeeping information
91 FGGeneral general;
92
93 // Specify our current idle function state.  This is used to run all
94 // our initializations out of the glutIdleLoop() so that we can get a
95 // splash screen up and running right away.
96 static int idle_state = 0;
97
98 // Another hack
99 int use_signals = 0;
100
101 // Yet another hack, this time for the panel
102 int panel_hist = 0;
103
104 // Global structures for the Audio library
105 #ifdef ENABLE_AUDIO_SUPPORT
106 slEnvelope pitch_envelope ( 1, SL_SAMPLE_ONE_SHOT ) ;
107 slEnvelope volume_envelope ( 1, SL_SAMPLE_ONE_SHOT ) ;
108 slScheduler *audio_sched;
109 smMixer *audio_mixer;
110 slSample *s1;
111 slSample *s2;
112 #endif
113
114
115 // The following defines flight gear options. Because glutlib will also
116 // want to parse its own options, those options must not be included here
117 // or they will get parsed by the main program option parser. Hence case
118 // is significant for any option added that might be in conflict with
119 // glutlib's parser.
120 //
121 // glutlib parses for:
122 //    -display
123 //    -direct   (invalid in Win32)
124 //    -geometry
125 //    -gldebug
126 //    -iconized
127 //    -indirect (invalid in Win32)
128 //    -synce
129 //
130 // Note that glutlib depends upon strings while this program's
131 // option parser wants only initial characters followed by numbers
132 // or pathnames.
133 //
134
135
136 // fgInitVisuals() -- Initialize various GL/view parameters
137 static void fgInitVisuals( void ) {
138     fgLIGHT *l;
139
140     l = &cur_light_params;
141
142     // Go full screen if requested ...
143     if ( current_options.get_fullscreen() ) {
144         glutFullScreen();
145     }
146
147     // If enabled, normal vectors specified with glNormal are scaled
148     // to unit length after transformation.  See glNormal.
149     // xglEnable( GL_NORMALIZE );
150
151     xglEnable( GL_LIGHTING );
152     xglEnable( GL_LIGHT0 );
153     xglLightfv( GL_LIGHT0, GL_POSITION, l->sun_vec );
154
155     // xglFogi (GL_FOG_MODE, GL_LINEAR);
156     xglFogi (GL_FOG_MODE, GL_EXP2);
157     // Fog density is now set when the weather system is initialized
158     // xglFogf (GL_FOG_DENSITY, w->fog_density);
159     if ( (current_options.get_fog() == 1) || 
160          (current_options.get_shading() == 0) ) {
161         // if fastest fog requested, or if flat shading force fastest
162         xglHint ( GL_FOG_HINT, GL_FASTEST );
163     } else if ( current_options.get_fog() == 2 ) {
164         xglHint ( GL_FOG_HINT, GL_NICEST );
165     }
166     if ( current_options.get_wireframe() ) {
167         // draw wire frame
168         xglPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
169     }
170
171     // This is the default anyways, but it can't hurt
172     xglFrontFace ( GL_CCW );
173
174     // Just testing ...
175     // xglEnable(GL_POINT_SMOOTH);
176     // xglEnable(GL_LINE_SMOOTH);
177     // xglEnable(GL_POLYGON_SMOOTH);      
178 }
179
180
181 #if 0
182 // Draw a basic instrument panel
183 static void fgUpdateInstrViewParams( void ) {
184
185     exit(0);
186
187     fgVIEW *v = &current_view;
188
189     xglViewport(0, 0 , (GLint)(v->winWidth), (GLint)(v->winHeight) / 2);
190   
191     xglMatrixMode(GL_PROJECTION);
192     xglPushMatrix();
193   
194     xglLoadIdentity();
195     gluOrtho2D(0, 640, 0, 480);
196     xglMatrixMode(GL_MODELVIEW);
197     xglPushMatrix();
198     xglLoadIdentity();
199
200     xglColor3f(1.0, 1.0, 1.0);
201     xglIndexi(7);
202   
203     xglDisable(GL_DEPTH_TEST);
204     xglDisable(GL_LIGHTING);
205   
206     xglLineWidth(1);
207     xglColor3f (0.5, 0.5, 0.5);
208
209     xglBegin(GL_QUADS);
210     xglVertex2f(0.0, 0.00);
211     xglVertex2f(0.0, 480.0);
212     xglVertex2f(640.0,480.0);
213     xglVertex2f(640.0, 0.0);
214     xglEnd();
215
216     xglRectf(0.0,0.0, 640, 480);
217     xglEnable(GL_DEPTH_TEST);
218     xglEnable(GL_LIGHTING);
219     xglMatrixMode(GL_PROJECTION);
220     xglPopMatrix();
221     xglMatrixMode(GL_MODELVIEW);
222     xglPopMatrix();
223 }
224 #endif
225
226
227 // Update all Visuals (redraws anything graphics related)
228 static void fgRenderFrame( void ) {
229     fgLIGHT *l = &cur_light_params;
230     fgTIME *t = &cur_time_params;
231     FGView *v = &current_view;
232
233     double angle;
234     static int iteration = 0;
235     // GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
236     GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
237     GLfloat terrain_color[4] = { 0.54, 0.44, 0.29, 1.0 };
238     GLbitfield clear_mask;
239
240     if ( idle_state != 1000 ) {
241         // still initializing, draw the splash screen
242         if ( current_options.get_splash_screen() == 1 ) {
243             fgSplashUpdate(0.0);
244         }
245     } else {
246         // idle_state is now 1000 meaning we've finished all our
247         // initializations and are running the main loop, so this will
248         // now work without seg faulting the system.
249
250         // printf("Ground = %.2f  Altitude = %.2f\n", scenery.cur_elev, 
251         //        FG_Altitude * FEET_TO_METER);
252     
253         // this is just a temporary hack, to make me understand Pui
254         // timerText -> setLabel (ctime (&t->cur_time));
255         // end of hack
256
257         // update view volume parameters
258         v->UpdateViewParams();
259
260         // set the sun position
261         xglLightfv( GL_LIGHT0, GL_POSITION, l->sun_vec );
262
263         clear_mask = GL_DEPTH_BUFFER_BIT;
264         if ( current_options.get_wireframe() ) {
265             clear_mask |= GL_COLOR_BUFFER_BIT;
266         }
267         if ( current_options.get_panel_status() ) {
268             // we can't clear the screen when the panel is active
269         } else if ( current_options.get_skyblend() ) {
270             if ( current_options.get_textures() ) {
271                 // glClearColor(black[0], black[1], black[2], black[3]);
272                 glClearColor(l->adj_fog_color[0], l->adj_fog_color[1], 
273                              l->adj_fog_color[2], l->adj_fog_color[3]);
274                 clear_mask |= GL_COLOR_BUFFER_BIT;              
275             }
276         } else {
277             glClearColor(l->sky_color[0], l->sky_color[1], 
278                          l->sky_color[2], l->sky_color[3]);
279             clear_mask |= GL_COLOR_BUFFER_BIT;
280         }
281         xglClear( clear_mask );
282
283         // Tell GL we are switching to model view parameters
284         xglMatrixMode(GL_MODELVIEW);
285         // xglLoadIdentity();
286
287         // draw sky
288         xglDisable( GL_DEPTH_TEST );
289         xglDisable( GL_LIGHTING );
290         xglDisable( GL_CULL_FACE );
291         xglDisable( GL_FOG );
292         xglShadeModel( GL_SMOOTH );
293         if ( current_options.get_skyblend() ) {
294             fgSkyRender();
295         }
296
297         // setup transformation for drawing astronomical objects
298         xglPushMatrix();
299         // Translate to view position
300         Point3D view_pos = v->get_view_pos();
301         xglTranslatef( view_pos.x(), view_pos.y(), view_pos.z() );
302         // Rotate based on gst (sidereal time)
303         // note: constant should be 15.041085, Curt thought it was 15
304         angle = t->gst * 15.041085;
305         // printf("Rotating astro objects by %.2f degrees\n",angle);
306         xglRotatef( angle, 0.0, 0.0, -1.0 );
307
308         // draw stars and planets
309         fgStarsRender();
310         SolarSystem::theSolarSystem->draw();
311
312         xglPopMatrix();
313
314         // draw scenery
315         if ( current_options.get_shading() ) {
316             xglShadeModel( GL_SMOOTH ); 
317         } else {
318             xglShadeModel( GL_FLAT ); 
319         }
320         xglEnable( GL_DEPTH_TEST );
321         if ( current_options.get_fog() > 0 ) {
322             xglEnable( GL_FOG );
323             xglFogfv (GL_FOG_COLOR, l->adj_fog_color);
324         }
325         // set lighting parameters
326         xglLightfv(GL_LIGHT0, GL_AMBIENT, l->scene_ambient );
327         xglLightfv(GL_LIGHT0, GL_DIFFUSE, l->scene_diffuse );
328         
329         if ( current_options.get_textures() ) {
330             // texture parameters
331             xglEnable( GL_TEXTURE_2D );
332             xglTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
333             xglHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ) ;
334             // set base color (I don't think this is doing anything here)
335             xglMaterialfv (GL_FRONT, GL_AMBIENT, white);
336             xglMaterialfv (GL_FRONT, GL_DIFFUSE, white);
337         } else {
338             xglDisable( GL_TEXTURE_2D );
339             xglMaterialfv (GL_FRONT, GL_AMBIENT, terrain_color);
340             xglMaterialfv (GL_FRONT, GL_DIFFUSE, terrain_color);
341             // xglMaterialfv (GL_FRONT, GL_AMBIENT, white);
342             // xglMaterialfv (GL_FRONT, GL_DIFFUSE, white);
343         }
344
345         fgTileMgrRender();
346
347         xglDisable( GL_TEXTURE_2D );
348         xglDisable( GL_FOG );
349
350         if ( (iteration == 0) && (current_options.get_panel_status()) ) {   
351             // Did we run this loop before ?? ...and do we need the panel ??
352             fgPanelReInit(0, 0, 1024, 768);
353         }
354
355         // display HUD && Panel
356         fgCockpitUpdate();
357         iteration = 1; // don't ReInit the panel in the future
358
359         // We can do translucent menus, so why not. :-)
360         xglEnable    ( GL_BLEND ) ;
361         xglBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
362         puDisplay();
363         xglDisable   ( GL_BLEND ) ;
364         xglEnable( GL_FOG );
365     }
366
367     xglutSwapBuffers();
368 }
369
370
371 // Update internal time dependent calculations (i.e. flight model)
372 void fgUpdateTimeDepCalcs(int multi_loop, int remainder) {
373     FGState *f = current_aircraft.fdm_state;
374     fgLIGHT *l = &cur_light_params;
375     fgTIME *t = &cur_time_params;
376     FGView *v = &current_view;
377     int i;
378
379     // update the flight model
380     if ( multi_loop < 0 ) {
381         multi_loop = DEFAULT_MULTILOOP;
382     }
383
384     if ( !t->pause ) {
385         // run Autopilot system
386         fgAPRun();
387
388         // printf("updating flight model x %d\n", multi_loop);
389         fgFDMUpdate( current_options.get_flight_model(), 
390                              cur_fdm_state, multi_loop, remainder );
391     } else {
392         fgFDMUpdate( current_options.get_flight_model(), 
393                              cur_fdm_state, 0, remainder );
394     }
395
396     // update the view angle
397     for ( i = 0; i < multi_loop; i++ ) {
398         if ( fabs(v->get_goal_view_offset() - v->get_view_offset()) < 0.05 ) {
399             v->set_view_offset( v->get_goal_view_offset() );
400             break;
401         } else {
402             // move v->view_offset towards v->goal_view_offset
403             if ( v->get_goal_view_offset() > v->get_view_offset() ) {
404                 if ( v->get_goal_view_offset() - v->get_view_offset() < FG_PI ){
405                     v->inc_view_offset( 0.01 );
406                 } else {
407                     v->inc_view_offset( -0.01 );
408                 }
409             } else {
410                 if ( v->get_view_offset() - v->get_goal_view_offset() < FG_PI ){
411                     v->inc_view_offset( -0.01 );
412                 } else {
413                     v->inc_view_offset( 0.01 );
414                 }
415             }
416             if ( v->get_view_offset() > FG_2PI ) {
417                 v->inc_view_offset( -FG_2PI );
418             } else if ( v->get_view_offset() < 0 ) {
419                 v->inc_view_offset( FG_2PI );
420             }
421         }
422     }
423
424     double tmp = -(l->sun_rotation + FG_PI) 
425         - (f->get_Psi() - v->get_view_offset() );
426     while ( tmp < 0.0 ) {
427         tmp += FG_2PI;
428     }
429     while ( tmp > FG_2PI ) {
430         tmp -= FG_2PI;
431     }
432     /* printf("Psi = %.2f, viewoffset = %.2f sunrot = %.2f rottosun = %.2f\n",
433            FG_Psi * RAD_TO_DEG, v->view_offset * RAD_TO_DEG, 
434            -(l->sun_rotation+FG_PI) * RAD_TO_DEG, tmp * RAD_TO_DEG); */
435     l->UpdateAdjFog();
436 }
437
438
439 void fgInitTimeDepCalcs( void ) {
440     // initialize timer
441
442     // #ifdef HAVE_SETITIMER
443     //   fgTimerInit( 1.0 / DEFAULT_TIMER_HZ, fgUpdateTimeDepCalcs );
444     // #endif HAVE_SETITIMER
445 }
446
447 static const double alt_adjust_ft = 3.758099;
448 static const double alt_adjust_m = alt_adjust_ft * FEET_TO_METER;
449
450 // What should we do when we have nothing else to do?  Let's get ready
451 // for the next move and update the display?
452 static void fgMainLoop( void ) {
453     FGState *f;
454     fgTIME *t;
455     static long remainder = 0;
456     long elapsed, multi_loop;
457     // int i;
458     // double accum;
459     static time_t last_time = 0;
460     static int frames = 0;
461
462     f = current_aircraft.fdm_state;
463     t = &cur_time_params;
464
465     FG_LOG( FG_ALL, FG_DEBUG, "Running Main Loop");
466     FG_LOG( FG_ALL, FG_DEBUG, "======= ==== ====");
467
468 #if defined( ENABLE_LINUX_JOYSTICK )
469     // Read joystick and update control settings
470     fgJoystickRead();
471 #elif defined( ENABLE_GLUT_JOYSTICK )
472     // Glut joystick support works by feeding a joystick handler
473     // function to glut.  This is taken care of once in the joystick
474     // init routine and we don't have to worry about it again.
475 #endif
476
477     current_weather.Update();
478
479     // Fix elevation.  I'm just sticking this here for now, it should
480     // probably move eventually
481
482     /* printf("Before - ground = %.2f  runway = %.2f  alt = %.2f\n",
483            scenery.cur_elev,
484            f->get_Runway_altitude() * FEET_TO_METER,
485            f->get_Altitude() * FEET_TO_METER); */
486
487     if ( scenery.cur_elev > -9990 ) {
488         if ( f->get_Altitude() * FEET_TO_METER < 
489              (scenery.cur_elev + alt_adjust_m - 3.0) ) {
490             // now set aircraft altitude above ground
491             printf("Current Altitude = %.2f < %.2f forcing to %.2f\n", 
492                    f->get_Altitude() * FEET_TO_METER,
493                    scenery.cur_elev + alt_adjust_m - 3.0,
494                    scenery.cur_elev + alt_adjust_m );
495             fgFDMForceAltitude( current_options.get_flight_model(), 
496                                 scenery.cur_elev + alt_adjust_m );
497
498             FG_LOG( FG_ALL, FG_DEBUG, 
499                     "<*> resetting altitude to " 
500                     << f->get_Altitude() * FEET_TO_METER << " meters" );
501         }
502         fgFDMSetGroundElevation( current_options.get_flight_model(),
503                                  scenery.cur_elev );  // meters
504     }
505
506     /* printf("Adjustment - ground = %.2f  runway = %.2f  alt = %.2f\n",
507            scenery.cur_elev,
508            f->get_Runway_altitude() * FEET_TO_METER,
509            f->get_Altitude() * FEET_TO_METER); */
510
511     // update "time"
512     fgTimeUpdate(f, t);
513
514     // Get elapsed time (in usec) for this past frame
515     elapsed = fgGetTimeInterval();
516     FG_LOG( FG_ALL, FG_DEBUG, 
517             "Elapsed time interval is = " << elapsed 
518             << ", previous remainder is = " << remainder );
519
520     // Calculate frame rate average
521     if ( (t->cur_time != last_time) && (last_time > 0) ) {
522         general.set_frame_rate( frames );
523         FG_LOG( FG_ALL, FG_DEBUG, 
524                 "--> Frame rate is = " << general.get_frame_rate() );
525         frames = 0;
526     }
527     last_time = t->cur_time;
528     ++frames;
529
530     /* old fps calculation
531     if ( elapsed > 0 ) {
532         accum = 0.0;
533         for ( i = FG_FRAME_RATE_HISTORY - 2; i >= 0; i-- ) {
534             accum += g->frames[i];
535             // printf("frame[%d] = %.2f\n", i, g->frames[i]);
536             g->frames[i+1] = g->frames[i];
537         }
538         g->frames[0] = 1000.0 / (float)elapsed;
539         // printf("frame[0] = %.2f\n", g->frames[0]);
540         accum += g->frames[0];
541         g->frame_rate = accum / (float)FG_FRAME_RATE_HISTORY;
542         // printf("ave = %.2f\n", g->frame_rate);
543     }
544     */
545
546     // Run flight model
547     if ( ! use_signals ) {
548         // Calculate model iterations needed for next frame
549         elapsed += remainder;
550
551         multi_loop = (int)(((double)elapsed * 0.000001) * DEFAULT_MODEL_HZ);
552         remainder = elapsed - ((multi_loop*1000000) / DEFAULT_MODEL_HZ);
553         FG_LOG( FG_ALL, FG_DEBUG, 
554                 "Model iterations needed = " << multi_loop
555                 << ", new remainder = " << remainder );
556         
557         // flight model
558         if ( multi_loop > 0 ) {
559             fgUpdateTimeDepCalcs(multi_loop, remainder);
560         } else {
561             FG_LOG( FG_ALL, FG_INFO, "Elapsed time is zero ... we're zinging" );
562         }
563     }
564
565     // Do any serial port work that might need to be done
566     fgSerialProcess();
567
568     // see if we need to load any new scenery tiles
569     fgTileMgrUpdate();
570
571     // Process/manage pending events
572     global_events.Process();
573
574     // Run audio scheduler
575 #ifdef ENABLE_AUDIO_SUPPORT
576     if ( current_options.get_sound() && audio_sched->working() ) {
577
578 #   ifdef MICHAEL_JOHNSON_EXPERIMENTAL_ENGINE_AUDIO
579
580         // note: all these factors are relative to the sample.  our
581         // sample format should really contain a conversion factor so
582         // that we can get prop speed right for arbitrary samples.
583         // Note that for normal-size props, there is a point at which
584         // the prop tips approach the speed of sound; that is a pretty
585         // strong limit to how fast the prop can go.
586
587         // multiplication factor is prime pitch control; add some log
588         // component for verisimilitude
589
590         double pitch = log((controls.get_throttle(0) * 14.0) + 1.0);
591         //fprintf(stderr, "pitch1: %f ", pitch);
592         if (controls.get_throttle(0) > 0.0 || f->v_rel_wind > 40.0) {
593             //fprintf(stderr, "rel_wind: %f ", f->v_rel_wind);
594             // only add relative wind and AoA if prop is moving
595             // or we're really flying at idle throttle
596             if (pitch < 5.4) {  // this needs tuning
597                 // prop tips not breaking sound barrier
598                 pitch += log(f->v_rel_wind + 0.8)/2;
599             } else {
600                 // prop tips breaking sound barrier
601                 pitch += log(f->v_rel_wind + 0.8)/10;
602             }
603             //fprintf(stderr, "pitch2: %f ", pitch);
604             //fprintf(stderr, "AoA: %f ", FG_Gamma_vert_rad);
605
606             // Angle of Attack next... -x^3(e^x) is my best guess Just
607             // need to calculate some reasonable scaling factor and
608             // then clamp it on the positive aoa (neg adj) side
609             double aoa = f->get_Gamma_vert_rad() * 2.2;
610             double tmp = 3.0;
611             double aoa_adj = pow(-aoa, tmp) * pow(M_E, aoa);
612             if (aoa_adj < -0.8) aoa_adj = -0.8;
613             pitch += aoa_adj;
614             //fprintf(stderr, "pitch3: %f ", pitch);
615
616             // don't run at absurdly slow rates -- not realistic
617             // and sounds bad to boot.  :-)
618             if (pitch < 0.8) pitch = 0.8;
619         }
620         //fprintf(stderr, "pitch4: %f\n", pitch);
621
622         double volume = controls.get_throttle(0) * 1.15 + 0.3 +
623             log(f->v_rel_wind + 1.0)/14.0;
624         // fprintf(stderr, "volume: %f\n", volume);
625
626         pitch_envelope.setStep  ( 0, 0.01, pitch );
627         volume_envelope.setStep ( 0, 0.01, volume );
628
629 #   else
630
631        double param = controls.get_throttle( 0 ) * 2.0 + 1.0;
632        pitch_envelope.setStep  ( 0, 0.01, param );
633        volume_envelope.setStep ( 0, 0.01, param );
634
635 #   endif // experimental throttle patch
636
637         audio_sched -> update();
638     }
639 #endif
640
641     // redraw display
642     fgRenderFrame();
643
644     FG_LOG( FG_ALL, FG_DEBUG, "" );
645 }
646
647
648 // This is the top level master main function that is registered as
649 // our idle funciton
650 //
651
652 // The first few passes take care of initialization things (a couple
653 // per pass) and once everything has been initialized fgMainLoop from
654 // then on.
655
656 static void fgIdleFunction ( void ) {
657     // printf("idle state == %d\n", idle_state);
658
659     if ( idle_state == 0 ) {
660         // Initialize the splash screen right away
661         if ( current_options.get_splash_screen() ) {
662             fgSplashInit();
663         }
664         
665         idle_state++;
666     } else if ( idle_state == 1 ) {
667         // Start the intro music
668 #if !defined(WIN32)
669         if ( current_options.get_intro_music() ) {
670             string lockfile = "/tmp/mpg123.running";
671             string mp3file = current_options.get_fg_root() +
672                 "/Sounds/intro.mp3";
673             string command = "(touch " + lockfile + "; mpg123 " + mp3file +
674                  "> /dev/null 2>&1; /bin/rm " + lockfile + ") &";
675             FG_LOG( FG_GENERAL, FG_INFO, 
676                     "Starting intro music: " << mp3file );
677             system ( command.c_str() );
678         }
679 #endif
680
681         idle_state++;
682     } else if ( idle_state == 2 ) {
683         // These are a few miscellaneous things that aren't really
684         // "subsystems" but still need to be initialized.
685
686 #ifdef USE_GLIDE
687         if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
688             grTexLodBiasValue ( GR_TMU0, 1.0 ) ;
689         }
690 #endif
691
692         idle_state++;
693     } else if ( idle_state == 3 ) {
694         // This is the top level init routine which calls all the
695         // other subsystem initialization routines.  If you are adding
696         // a subsystem to flight gear, its initialization call should
697         // located in this routine.
698         if( !fgInitSubsystems()) {
699             FG_LOG( FG_GENERAL, FG_ALERT,
700                     "Subsystem initializations failed ..." );
701             exit(-1);
702         }
703
704         idle_state++;
705     } else if ( idle_state == 4 ) {
706         // setup OpenGL view parameters
707         fgInitVisuals();
708
709         if ( use_signals ) {
710             // init timer routines, signals, etc.  Arrange for an alarm
711             // signal to be generated, etc.
712             fgInitTimeDepCalcs();
713         }
714
715         idle_state++;
716     } else if ( idle_state == 5 ) {
717
718         idle_state++;
719     } else if ( idle_state == 6 ) {
720         // Initialize audio support
721 #ifdef ENABLE_AUDIO_SUPPORT
722
723 #if !defined(WIN32)
724         if ( current_options.get_intro_music() ) {
725             // Let's wait for mpg123 to finish
726             string lockfile = "/tmp/mpg123.running";
727             struct stat stat_buf;
728
729             FG_LOG( FG_GENERAL, FG_INFO, 
730                     "Waiting for mpg123 player to finish ..." );
731             while ( stat(lockfile.c_str(), &stat_buf) == 0 ) {
732                 // file exist, wait ...
733                 sleep(1);
734                 FG_LOG( FG_GENERAL, FG_INFO, ".");
735             }
736             FG_LOG( FG_GENERAL, FG_INFO, "");
737         }
738 #endif // WIN32
739
740         audio_sched = new slScheduler ( 8000 );
741         audio_mixer = new smMixer;
742         audio_mixer -> setMasterVolume ( 50 ) ;  /* 80% of max volume. */
743         audio_sched -> setSafetyMargin ( 1.0 ) ;
744         string slfile = current_options.get_fg_root() + "/Sounds/wasp.wav";
745
746         s1 = new slSample ( (char *)slfile.c_str() );
747         FG_LOG( FG_GENERAL, FG_INFO,
748                 "Rate = " << s1 -> getRate()
749                 << "  Bps = " << s1 -> getBps()
750                 << "  Stereo = " << s1 -> getStereo() );
751         audio_sched -> loopSample ( s1 );
752
753         if ( audio_sched->working() ) {
754             pitch_envelope.setStep  ( 0, 0.01, 0.6 );
755             volume_envelope.setStep ( 0, 0.01, 0.6 );
756
757             audio_sched -> addSampleEnvelope( s1, 0, 0, &
758                                               pitch_envelope,
759                                               SL_PITCH_ENVELOPE );
760             audio_sched -> addSampleEnvelope( s1, 0, 1, 
761                                               &volume_envelope,
762                                               SL_VOLUME_ENVELOPE );
763         }
764
765         // strcpy(slfile, path);
766         // strcat(slfile, "thunder.wav");
767         // s2 -> loadFile ( slfile );
768         // s2 -> adjustVolume(0.5);
769         // audio_sched -> playSample ( s2 );
770 #endif
771
772         // sleep(1);
773         idle_state = 1000;
774     } 
775
776     if ( idle_state == 1000 ) {
777         // We've finished all our initialization steps, from now on we
778         // run the main loop.
779
780         fgMainLoop();
781     } else {
782         if ( current_options.get_splash_screen() == 1 ) {
783             fgSplashUpdate(0.0);
784         }
785     }
786 }
787
788
789 // Handle new window size or exposure
790 static void fgReshape( int width, int height ) {
791     // Do this so we can call fgReshape(0,0) ourselves without having
792     // to know what the values of width & height are.
793     if ( (height > 0) && (width > 0) ) {
794         if ( ! current_options.get_panel_status() ) {
795             current_view.set_win_ratio( (GLfloat) width / (GLfloat) height );
796         } else {
797             current_view.set_win_ratio( (GLfloat) width / 
798                                         ((GLfloat) (height)*0.4232) );
799         }
800     }
801
802     current_view.set_winWidth( width );
803     current_view.set_winHeight( height );
804     current_view.force_update_fov_math();
805
806     // Inform gl of our view window size (now handled elsewhere)
807     // xglViewport(0, 0, (GLint)width, (GLint)height);
808     if ( idle_state == 1000 ) {
809         // yes we've finished all our initializations and are running
810         // the main loop, so this will now work without seg faulting
811         // the system.
812         current_view.UpdateViewParams();
813         if ( current_options.get_panel_status() ) {
814             fgPanelReInit(0, 0, 1024, 768);
815         }
816     }
817 }
818
819
820 // Initialize GLUT and define a main window
821 int fgGlutInit( int *argc, char **argv ) {
822     // GLUT will extract all glut specific options so later on we only
823     // need wory about our own.
824     xglutInit(argc, argv);
825
826     // Define Display Parameters
827     xglutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
828
829     FG_LOG( FG_GENERAL, FG_INFO, "Opening a window: " <<
830             current_options.get_xsize() << "x" << current_options.get_ysize() );
831
832     // Define initial window size
833     xglutInitWindowSize( current_options.get_xsize(),
834                          current_options.get_ysize() );
835
836     // Initialize windows
837     if ( current_options.get_game_mode() == 0 ) {
838         // Open the regular window
839         xglutCreateWindow("Flight Gear");
840     } else {
841         // Open the cool new 'game mode' window
842         string game_mode_params = "width=" + current_options.get_xsize();
843         game_mode_params += "height=" + current_options.get_ysize();
844         game_mode_params += " bpp=16";
845         cout << "game mode params = " << game_mode_params;
846         glutGameModeString( game_mode_params.c_str() );
847         glutEnterGameMode();
848     }
849
850     // This seems to be the absolute earliest in the init sequence
851     // that these calls will return valid info.  Too bad it's after
852     // we've already created and sized out window. :-(
853     general.set_glVendor( (char *)glGetString ( GL_VENDOR ) );
854     general.set_glRenderer( (char *)glGetString ( GL_RENDERER ) );
855     general.set_glVersion( (char *)glGetString ( GL_VERSION ) );
856
857     FG_LOG ( FG_GENERAL, FG_INFO, general.get_glRenderer() );
858
859 #if 0
860     // try to determine if we should adjust the initial default
861     // display resolution.  The options class defaults (is
862     // initialized) to 640x480.
863     string renderer = general.glRenderer;
864
865     // currently we only know how to deal with Mesa/Glide/Voodoo cards
866     if ( renderer.find( "Glide" ) != string::npos ) {
867         FG_LOG( FG_GENERAL, FG_INFO, "Detected a Glide driver" );
868         if ( renderer.find( "FB/8" ) != string::npos ) {
869             // probably a voodoo-2
870             if ( renderer.find( "TMU/SLI" ) != string::npos ) {
871                 // probably two SLI'd Voodoo-2's
872                 current_options.set_xsize( 1024 );
873                 current_options.set_ysize( 768 );
874                 FG_LOG( FG_GENERAL, FG_INFO,
875                         "It looks like you have two sli'd voodoo-2's." << endl
876                         << "upgrading your win resolution to 1024 x 768" );
877                 glutReshapeWindow(1024, 768);
878             } else {
879                 // probably a single non-SLI'd Voodoo-2
880                 current_options.set_xsize( 800 );
881                 current_options.set_ysize( 600 );
882                 FG_LOG( FG_GENERAL, FG_INFO,
883                         "It looks like you have a voodoo-2." << endl
884                         << "upgrading your win resolution to 800 x 600" );
885                 glutReshapeWindow(800, 600);
886             }
887         } else if ( renderer.find( "FB/2" ) != string::npos ) {
888             // probably a voodoo-1, stick with the default
889         }
890     } else {
891         // we have no special knowledge of this card, stick with the default
892     }
893 #endif
894
895     return(1);
896 }
897
898
899 // Initialize GLUT event handlers
900 int fgGlutInitEvents( void ) {
901     // call fgReshape() on window resizes
902     xglutReshapeFunc( fgReshape );
903
904     // call GLUTkey() on keyboard event
905     xglutKeyboardFunc( GLUTkey );
906     glutSpecialFunc( GLUTspecialkey );
907
908     // call guiMouseFunc() whenever our little rodent is used
909     glutMouseFunc ( guiMouseFunc );
910     glutMotionFunc (guiMotionFunc );
911     glutPassiveMotionFunc (guiMotionFunc );
912
913     // call fgMainLoop() whenever there is
914     // nothing else to do
915     xglutIdleFunc( fgIdleFunction );
916
917     // draw the scene
918     xglutDisplayFunc( fgRenderFrame );
919
920     return(1);
921 }
922
923
924 // Main ...
925 int main( int argc, char **argv ) {
926     FGState *f;
927
928     f = current_aircraft.fdm_state;
929
930 #ifdef HAVE_BC5PLUS
931     _control87(MCW_EM, MCW_EM);  /* defined in float.h */
932 #endif
933
934     // Initialize the [old] debugging output system
935     // fgInitDebug();
936
937     // set default log levels
938     fglog().setLogLevels( FG_ALL, FG_INFO );
939
940     FG_LOG( FG_GENERAL, FG_INFO, "Flight Gear:  Version " << VERSION << endl );
941
942     string root;
943
944     FG_LOG( FG_GENERAL, FG_INFO, "General Initialization" );
945     FG_LOG( FG_GENERAL, FG_INFO, "======= ==============" );
946
947     // Attempt to locate and parse a config file
948     // First check fg_root
949     string config = current_options.get_fg_root() + "/system.fgfsrc";
950     current_options.parse_config_file( config );
951
952     // Next check home directory
953     char* envp = ::getenv( "HOME" );
954     if ( envp != NULL ) {
955         config = envp;
956         config += "/.fgfsrc";
957         current_options.parse_config_file( config );
958     }
959
960     // Parse remaining command line options
961     // These will override anything specified in a config file
962     if ( current_options.parse_command_line(argc, argv) !=
963                                       fgOPTIONS::FG_OPTIONS_OK )
964     {
965         // Something must have gone horribly wrong with the command
966         // line parsing or maybe the user just requested help ... :-)
967         current_options.usage();
968         // FG_LOG( FG_GENERAL, FG_ALERT, "\nExiting ...");
969         cout << endl << "Exiting ..." << endl;
970         exit(-1);
971     }
972     
973     // Initialize the Window/Graphics environment.
974     if( !fgGlutInit(&argc, argv) ) {
975         FG_LOG( FG_GENERAL, FG_ALERT, "GLUT initialization failed ..." );
976         exit(-1);
977     }
978
979     // Initialize the various GLUT Event Handlers.
980     if( !fgGlutInitEvents() ) {
981         FG_LOG( FG_GENERAL, FG_ALERT, 
982                 "GLUT event handler initialization failed ..." );
983         exit(-1);
984     }
985
986     // First do some quick general initializations
987     if( !fgInitGeneral()) {
988         FG_LOG( FG_GENERAL, FG_ALERT, 
989                 "General initializations failed ..." );
990         exit(-1);
991     }
992
993     // Init the user interface (we need to do this before passing off
994     // control to glut
995     guiInit();
996
997     // pass control off to the master GLUT event handler
998     glutMainLoop();
999
1000     // we never actually get here ... but just in case ... :-)
1001     return(0);
1002 }
1003
1004
1005 // $Log$
1006 // Revision 1.82  1999/01/20 13:42:24  curt
1007 // Tweaked FDM interface.
1008 // Testing check sum support for NMEA serial output.
1009 //
1010 // Revision 1.81  1999/01/19 20:57:03  curt
1011 // MacOS portability changes contributed by "Robert Puyol" <puyol@abvent.fr>
1012 //
1013 // Revision 1.80  1999/01/09 13:37:40  curt
1014 // Convert fgTIMESTAMP to FGTimeStamp which holds usec instead of ms.
1015 //
1016 // Revision 1.79  1999/01/08 03:23:56  curt
1017 // Beginning work on compensating for sim time vs. real world time "jitter".
1018 //
1019 // Revision 1.78  1999/01/07 20:25:08  curt
1020 // Updated struct fgGENERAL to class FGGeneral.
1021 //
1022 // Revision 1.77  1998/12/18 23:40:55  curt
1023 // New frame rate counting mechanism.
1024 //
1025 // Revision 1.76  1998/12/11 20:26:26  curt
1026 // Fixed view frustum culling accuracy bug so we can look out the sides and
1027 // back without tri-stripes dropping out.
1028 //
1029 // Revision 1.75  1998/12/09 18:50:23  curt
1030 // Converted "class fgVIEW" to "class FGView" and updated to make data
1031 // members private and make required accessor functions.
1032 //
1033 // Revision 1.74  1998/12/06 14:52:54  curt
1034 // Fixed a problem with the initial starting altitude.  "v->abs_view_pos" wasn't
1035 // being calculated correctly at the beginning causing the first terrain
1036 // intersection to fail, returning a ground altitude of zero, causing the plane
1037 // to free fall for one frame, until the ground altitude was corrected, but now
1038 // being under the ground we got a big bounce and the plane always ended up
1039 // upside down.
1040 //
1041 // Revision 1.73  1998/12/06 13:51:22  curt
1042 // Turned "struct fgWEATHER" into "class FGWeather".
1043 //
1044 // Revision 1.72  1998/12/05 15:54:18  curt
1045 // Renamed class fgFLIGHT to class FGState as per request by JSB.
1046 //
1047 // Revision 1.71  1998/12/05 14:19:51  curt
1048 // Looking into a problem with cur_view_params.abs_view_pos initialization.
1049 //
1050 // Revision 1.70  1998/12/03 01:17:14  curt
1051 // Converted fgFLIGHT to a class.
1052 //
1053 // Revision 1.69  1998/11/23 20:51:26  curt
1054 // Fiddling with when I can get info from the opengl driver.
1055 //
1056 // Revision 1.68  1998/11/20 01:02:35  curt
1057 // Try to detect Mesa/Glide/Voodoo and chose the appropriate resolution.
1058 //
1059 // Revision 1.67  1998/11/16 13:59:58  curt
1060 // Added pow() macro bug work around.
1061 // Added support for starting FGFS at various resolutions.
1062 // Added some initial serial port support.
1063 // Specify default log levels in main().
1064 //
1065 // Revision 1.66  1998/11/11 00:24:00  curt
1066 // Added Michael Johnson's audio patches for testing.
1067 // Also did a few tweaks to avoid numerical problems when starting at a place
1068 // with no (or bogus) scenery.
1069 //
1070 // Revision 1.65  1998/11/09 23:39:22  curt
1071 // Tweaks for the instrument panel.
1072 //
1073 // Revision 1.64  1998/11/07 19:07:09  curt
1074 // Enable release builds using the --without-logging option to the configure
1075 // script.  Also a couple log message cleanups, plus some C to C++ comment
1076 // conversion.
1077 //
1078 // Revision 1.63  1998/11/06 21:18:08  curt
1079 // Converted to new logstream debugging facility.  This allows release
1080 // builds with no messages at all (and no performance impact) by using
1081 // the -DFG_NDEBUG flag.
1082 //
1083 // Revision 1.62  1998/10/27 02:14:35  curt
1084 // Changes to support GLUT joystick routines as fall back.
1085 //
1086 // Revision 1.61  1998/10/25 14:08:47  curt
1087 // Turned "struct fgCONTROLS" into a class, with inlined accessor functions.
1088 //
1089 // Revision 1.60  1998/10/25 10:57:18  curt
1090 // Changes to use the new joystick library if it is available.
1091 //
1092 // Revision 1.59  1998/10/17 01:34:21  curt
1093 // C++ ifying ...
1094 //
1095 // Revision 1.58  1998/10/16 23:27:52  curt
1096 // C++-ifying.
1097 //
1098 // Revision 1.57  1998/10/16 00:54:00  curt
1099 // Converted to Point3D class.
1100 //
1101 // Revision 1.56  1998/10/02 12:46:47  curt
1102 // Added an "auto throttle"
1103 //
1104 // Revision 1.55  1998/09/29 14:58:18  curt
1105 // Use working() instead of !not_working() for audio.
1106 //
1107 // Revision 1.54  1998/09/29 02:03:38  curt
1108 // Autopilot mods.
1109 //
1110 // Revision 1.53  1998/09/26 13:18:35  curt
1111 // Check if audio "working()" before doing audio manipulations.
1112 //
1113 // Revision 1.52  1998/09/25 16:02:07  curt
1114 // Added support for pitch and volume envelopes and tied them to the
1115 // throttle setting.
1116 //
1117 // Revision 1.51  1998/09/15 04:27:28  curt
1118 // Changes for new Astro code.
1119 //
1120 // Revision 1.50  1998/09/15 02:09:24  curt
1121 // Include/fg_callback.hxx
1122 //   Moved code inline to stop g++ 2.7 from complaining.
1123 //
1124 // Simulator/Time/event.[ch]xx
1125 //   Changed return type of fgEVENT::printStat().  void caused g++ 2.7 to
1126 //   complain bitterly.
1127 //
1128 // Minor bugfix and changes.
1129 //
1130 // Simulator/Main/GLUTmain.cxx
1131 //   Added missing type to idle_state definition - eliminates a warning.
1132 //
1133 // Simulator/Main/fg_init.cxx
1134 //   Changes to airport lookup.
1135 //
1136 // Simulator/Main/options.cxx
1137 //   Uses fg_gzifstream when loading config file.
1138 //
1139 // Revision 1.49  1998/09/09 16:25:39  curt
1140 // Only use GLUT_STENCIL if the instument panel has been requested.
1141 //
1142 // Revision 1.48  1998/08/28 18:15:03  curt
1143 // Added new cockpit code from Friedemann Reinhard
1144 // <mpt218@faupt212.physik.uni-erlangen.de>
1145 //
1146 // Revision 1.47  1998/08/27 17:02:04  curt
1147 // Contributions from Bernie Bright <bbright@c031.aone.net.au>
1148 // - use strings for fg_root and airport_id and added methods to return
1149 //   them as strings,
1150 // - inlined all access methods,
1151 // - made the parsing functions private methods,
1152 // - deleted some unused functions.
1153 // - propogated some of these changes out a bit further.
1154 //
1155 // Revision 1.46  1998/08/22  14:49:56  curt
1156 // Attempting to iron out seg faults and crashes.
1157 // Did some shuffling to fix a initialization order problem between view
1158 // position, scenery elevation.
1159 //
1160 // Revision 1.45  1998/08/20 20:32:31  curt
1161 // Reshuffled some of the code in and around views.[ch]xx
1162 //
1163 // Revision 1.44  1998/08/20 15:10:33  curt
1164 // Added GameGLUT support.
1165 //
1166 // Revision 1.43  1998/08/12 21:01:47  curt
1167 // Master volume from 30% -> 80%
1168 //
1169 // Revision 1.42  1998/07/30 23:48:25  curt
1170 // Output position & orientation when pausing.
1171 // Eliminated libtool use.
1172 // Added options to specify initial position and orientation.
1173 // Changed default fov to 55 degrees.
1174 // Added command line option to start in paused or unpaused state.
1175 //
1176 // Revision 1.41  1998/07/27 18:41:24  curt
1177 // Added a pause command "p"
1178 // Fixed some initialization order problems between pui and glut.
1179 // Added an --enable/disable-sound option.
1180 //
1181 // Revision 1.40  1998/07/24 21:56:59  curt
1182 // Set near clip plane to 0.5 meters when close to the ground.  Also, let the view get a bit closer to the ground before hitting the hard limit.
1183 //
1184 // Revision 1.39  1998/07/24 21:39:08  curt
1185 // Debugging output tweaks.
1186 // Cast glGetString to (char *) to avoid compiler errors.
1187 // Optimizations to fgGluLookAt() by Norman Vine.
1188 //
1189 // Revision 1.38  1998/07/22 21:40:43  curt
1190 // Clear to adjusted fog color (for sunrise/sunset effects)
1191 // Make call to fog sunrise/sunset adjustment method.
1192 // Add a stdc++ library bug work around to fg_init.cxx
1193 //
1194 // Revision 1.37  1998/07/20 12:49:44  curt
1195 // Tweaked color buffer clearing defaults.  We clear the color buffer if we
1196 // are doing textures.  Assumptions:  If we are doing textures we have hardware
1197 // support that can clear the color buffer for "free."  If we are doing software
1198 // rendering with textures, then the extra clear time gets lost in the noise.
1199 //
1200 // Revision 1.36  1998/07/16 17:33:35  curt
1201 // "H" / "h" now control hud brightness as well with off being one of the
1202 //   states.
1203 // Better checking for xmesa/fx 3dfx fullscreen/window support for deciding
1204 //   whether or not to build in the feature.
1205 // Translucent menu support.
1206 // HAVE_AUDIO_SUPPORT -> ENABLE_AUDIO_SUPPORT
1207 // Use fork() / wait() for playing mp3 init music in background under unix.
1208 // Changed default tile diameter to 5.
1209 //
1210 // Revision 1.35  1998/07/13 21:01:36  curt
1211 // Wrote access functions for current fgOPTIONS.
1212 //
1213 // Revision 1.34  1998/07/13 15:32:37  curt
1214 // Clear color buffer if drawing wireframe.
1215 // When specifying and airport, start elevation at -1000 and let the system
1216 // position you at ground level.
1217 //
1218 // Revision 1.33  1998/07/12 03:14:42  curt
1219 // Added ground collision detection.
1220 // Did some serious horsing around to be able to "hug" the ground properly
1221 //   and still be able to take off.
1222 // Set the near clip plane to 1.0 meters when less than 10 meters above the
1223 //   ground.
1224 // Did some serious horsing around getting the initial airplane position to be
1225 //   correct based on rendered terrain elevation.
1226 // Added a little cheat/hack that will prevent the view position from ever
1227 //   dropping below the terrain, even when the flight model doesn't quite
1228 //   put you as high as you'd like.
1229 //
1230 // Revision 1.32  1998/07/08 14:45:07  curt
1231 // polar3d.h renamed to polar3d.hxx
1232 // vector.h renamed to vector.hxx
1233 // updated audio support so it waits to create audio classes (and tie up
1234 //   /dev/dsp) until the mpg123 player is finished.
1235 //
1236 // Revision 1.31  1998/07/06 21:34:17  curt
1237 // Added an enable/disable splash screen option.
1238 // Added an enable/disable intro music option.
1239 // Added an enable/disable instrument panel option.
1240 // Added an enable/disable mouse pointer option.
1241 // Added using namespace std for compilers that support this.
1242 //
1243 // Revision 1.30  1998/07/06 02:42:03  curt
1244 // Added support for switching between fullscreen and window mode for
1245 // Mesa/3dfx/glide.
1246 //
1247 // Added a basic splash screen.  Restructured the main loop and top level
1248 // initialization routines to do this.
1249 //
1250 // Hacked in some support for playing a startup mp3 sound file while rest
1251 // of sim initializes.  Currently only works in Unix using the mpg123 player.
1252 // Waits for the mpg123 player to finish before initializing internal
1253 // sound drivers.
1254 //
1255 // Revision 1.29  1998/07/04 00:52:22  curt
1256 // Add my own version of gluLookAt() (which is nearly identical to the
1257 // Mesa/glu version.)  But, by calculating the Model View matrix our selves
1258 // we can save this matrix without having to read it back in from the video
1259 // card.  This hopefully allows us to save a few cpu cycles when rendering
1260 // out the fragments because we can just use glLoadMatrixd() with the
1261 // precalculated matrix for each tile rather than doing a push(), translate(),
1262 // pop() for every fragment.
1263 //
1264 // Panel status defaults to off for now until it gets a bit more developed.
1265 //
1266 // Extract OpenGL driver info on initialization.
1267 //
1268 // Revision 1.28  1998/06/27 16:54:32  curt
1269 // Replaced "extern displayInstruments" with a entry in fgOPTIONS.
1270 // Don't change the view port when displaying the panel.
1271 //
1272 // Revision 1.27  1998/06/17 21:35:10  curt
1273 // Refined conditional audio support compilation.
1274 // Moved texture parameter setup calls to ../Scenery/materials.cxx
1275 // #include <string.h> before various STL includes.
1276 // Make HUD default state be enabled.
1277 //
1278 // Revision 1.26  1998/06/13 00:40:32  curt
1279 // Tweaked fog command line options.
1280 //
1281 // Revision 1.25  1998/06/12 14:27:26  curt
1282 // Pui -> PUI, Gui -> GUI.
1283 //
1284 // Revision 1.24  1998/06/12 00:57:39  curt
1285 // Added support for Pui/Gui.
1286 // Converted fog to GL_FOG_EXP2.
1287 // Link to static simulator parts.
1288 // Update runfg.bat to try to be a little smarter.
1289 //
1290 // Revision 1.23  1998/06/08 17:57:04  curt
1291 // Minor sound/startup position tweaks.
1292 //
1293 // Revision 1.22  1998/06/05 18:18:40  curt
1294 // A bit of fiddling with audio ...
1295 //
1296 // Revision 1.21  1998/06/03 22:01:06  curt
1297 // Tweaking sound library usage.
1298 //
1299 // Revision 1.20  1998/06/03 00:47:11  curt
1300 // Updated to compile in audio support if OSS available.
1301 // Updated for new version of Steve's audio library.
1302 // STL includes don't use .h
1303 // Small view optimizations.
1304 //
1305 // Revision 1.19  1998/06/01 17:54:40  curt
1306 // Added Linux audio support.
1307 // avoid glClear( COLOR_BUFFER_BIT ) when not using it to set the sky color.
1308 // map stl tweaks.
1309 //
1310 // Revision 1.18  1998/05/29 20:37:19  curt
1311 // Tweaked material properties & lighting a bit in GLUTmain.cxx.
1312 // Read airport list into a "map" STL for dynamic list sizing and fast tree
1313 // based lookups.
1314 //
1315 // Revision 1.17  1998/05/22 21:28:52  curt
1316 // Modifications to use the new fgEVENT_MGR class.
1317 //
1318 // Revision 1.16  1998/05/20 20:51:33  curt
1319 // Tweaked smooth shaded texture lighting properties.
1320 // Converted fgLIGHT to a C++ class.
1321 //
1322 // Revision 1.15  1998/05/16 13:08:34  curt
1323 // C++ - ified views.[ch]xx
1324 // Shuffled some additional view parameters into the fgVIEW class.
1325 // Changed tile-radius to tile-diameter because it is a much better
1326 //   name.
1327 // Added a WORLD_TO_EYE transformation to views.cxx.  This allows us
1328 //  to transform world space to eye space for view frustum culling.
1329 //
1330 // Revision 1.14  1998/05/13 18:29:57  curt
1331 // Added a keyboard binding to dynamically adjust field of view.
1332 // Added a command line option to specify fov.
1333 // Adjusted terrain color.
1334 // Root path info moved to fgOPTIONS.
1335 // Added ability to parse options out of a config file.
1336 //
1337 // Revision 1.13  1998/05/11 18:18:15  curt
1338 // For flat shading use "glHint (GL_FOG_HINT, GL_FASTEST )"
1339 //
1340 // Revision 1.12  1998/05/07 23:14:15  curt
1341 // Added "D" key binding to set autopilot heading.
1342 // Made frame rate calculation average out over last 10 frames.
1343 // Borland C++ floating point exception workaround.
1344 // Added a --tile-radius=n option.
1345 //
1346 // Revision 1.11  1998/05/06 03:16:23  curt
1347 // Added an averaged global frame rate counter.
1348 // Added an option to control tile radius.
1349 //
1350 // Revision 1.10  1998/05/03 00:47:31  curt
1351 // Added an option to enable/disable full-screen mode.
1352 //
1353 // Revision 1.9  1998/04/30 12:34:17  curt
1354 // Added command line rendering options:
1355 //   enable/disable fog/haze
1356 //   specify smooth/flat shading
1357 //   disable sky blending and just use a solid color
1358 //   enable wireframe drawing mode
1359 //
1360 // Revision 1.8  1998/04/28 01:20:21  curt
1361 // Type-ified fgTIME and fgVIEW.
1362 // Added a command line option to disable textures.
1363 //
1364 // Revision 1.7  1998/04/26 05:10:02  curt
1365 // "struct fgLIGHT" -> "fgLIGHT" because fgLIGHT is typedef'd.
1366 //
1367 // Revision 1.6  1998/04/25 22:06:30  curt
1368 // Edited cvs log messages in source files ... bad bad bad!
1369 //
1370 // Revision 1.5  1998/04/25 20:24:01  curt
1371 // Cleaned up initialization sequence to eliminate interdependencies
1372 // between sun position, lighting, and view position.  This creates a
1373 // valid single pass initialization path.
1374 //
1375 // Revision 1.4  1998/04/24 14:19:30  curt
1376 // Fog tweaks.
1377 //
1378 // Revision 1.3  1998/04/24 00:49:18  curt
1379 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
1380 // Trying out some different option parsing code.
1381 // Some code reorganization.
1382 //
1383 // Revision 1.2  1998/04/22 13:25:41  curt
1384 // C++ - ifing the code.
1385 // Starting a bit of reorganization of lighting code.
1386 //
1387 // Revision 1.1  1998/04/21 17:02:39  curt
1388 // Prepairing for C++ integration.
1389 //
1390 // Revision 1.71  1998/04/18 04:11:26  curt
1391 // Moved fg_debug to it's own library, added zlib support.
1392 //
1393 // Revision 1.70  1998/04/14 02:21:02  curt
1394 // Incorporated autopilot heading hold contributed by:  Jeff Goeke-Smith
1395 // <jgoeke@voyager.net>
1396 //
1397 // Revision 1.69  1998/04/08 23:35:34  curt
1398 // Tweaks to Gnu automake/autoconf system.
1399 //
1400 // Revision 1.68  1998/04/03 22:09:03  curt
1401 // Converting to Gnu autoconf system.
1402 //
1403 // Revision 1.67  1998/03/23 21:24:37  curt
1404 // Source code formating tweaks.
1405 //
1406 // Revision 1.66  1998/03/14 00:31:20  curt
1407 // Beginning initial terrain texturing experiments.
1408 //
1409 // Revision 1.65  1998/03/09 22:45:57  curt
1410 // Minor tweaks for building on sparc platform.
1411 //
1412 // Revision 1.64  1998/02/20 00:16:23  curt
1413 // Thursday's tweaks.
1414 //
1415 // Revision 1.63  1998/02/16 16:17:39  curt
1416 // Minor tweaks.
1417 //
1418 // Revision 1.62  1998/02/16 13:39:42  curt
1419 // Miscellaneous weekend tweaks.  Fixed? a cache problem that caused whole
1420 // tiles to occasionally be missing.
1421 //
1422 // Revision 1.61  1998/02/12 21:59:46  curt
1423 // Incorporated code changes contributed by Charlie Hotchkiss
1424 // <chotchkiss@namg.us.anritsu.com>
1425 //
1426 // Revision 1.60  1998/02/11 02:50:40  curt
1427 // Minor changes.
1428 //
1429 // Revision 1.59  1998/02/09 22:56:54  curt
1430 // Removed "depend" files from cvs control.  Other minor make tweaks.
1431 //
1432 // Revision 1.58  1998/02/09 15:07:49  curt
1433 // Minor tweaks.
1434 //
1435 // Revision 1.57  1998/02/07 15:29:40  curt
1436 // Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
1437 // <chotchkiss@namg.us.anritsu.com>
1438 //
1439 // Revision 1.56  1998/02/03 23:20:23  curt
1440 // Lots of little tweaks to fix various consistency problems discovered by
1441 // Solaris' CC.  Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
1442 // passed arguments along to the real printf().  Also incorporated HUD changes
1443 // by Michele America.
1444 //
1445 // Revision 1.55  1998/02/02 20:53:58  curt
1446 // Incorporated Durk's changes.
1447 //
1448 // Revision 1.54  1998/01/31 00:43:10  curt
1449 // Added MetroWorks patches from Carmen Volpe.
1450 //
1451 // Revision 1.53  1998/01/27 18:35:54  curt
1452 // Minor tweaks.
1453 //
1454 // Revision 1.52  1998/01/27 00:47:56  curt
1455 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
1456 // system and commandline/config file processing code.
1457 //
1458 // Revision 1.51  1998/01/26 15:57:05  curt
1459 // Tweaks for dynamic scenery development.
1460 //
1461 // Revision 1.50  1998/01/19 19:27:07  curt
1462 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
1463 // This should simplify things tremendously.
1464 //
1465 // Revision 1.49  1998/01/19 18:40:31  curt
1466 // Tons of little changes to clean up the code and to remove fatal errors
1467 // when building with the c++ compiler.
1468 //
1469 // Revision 1.48  1998/01/19 18:35:46  curt
1470 // Minor tweaks and fixes for cygwin32.
1471 //
1472 // Revision 1.47  1998/01/13 00:23:08  curt
1473 // Initial changes to support loading and management of scenery tiles.  Note,
1474 // there's still a fair amount of work left to be done.
1475 //
1476 // Revision 1.46  1998/01/08 02:22:06  curt
1477 // Beginning to integrate Tile management subsystem.
1478 //
1479 // Revision 1.45  1998/01/07 03:18:55  curt
1480 // Moved astronomical stuff from .../Src/Scenery to .../Src/Astro/
1481 //
1482 // Revision 1.44  1997/12/30 22:22:31  curt
1483 // Further integration of event manager.
1484 //
1485 // Revision 1.43  1997/12/30 20:47:43  curt
1486 // Integrated new event manager with subsystem initializations.
1487 //
1488 // Revision 1.42  1997/12/30 16:36:47  curt
1489 // Merged in Durk's changes ...
1490 //
1491 // Revision 1.41  1997/12/30 13:06:56  curt
1492 // A couple lighting tweaks ...
1493 //
1494 // Revision 1.40  1997/12/30 01:38:37  curt
1495 // Switched back to per vertex normals and smooth shading for terrain.
1496 //
1497 // Revision 1.39  1997/12/22 23:45:45  curt
1498 // First stab at sunset/sunrise sky glow effects.
1499 //
1500 // Revision 1.38  1997/12/22 04:14:28  curt
1501 // Aligned sky with sun so dusk/dawn effects can be correct relative to the sun.
1502 //
1503 // Revision 1.37  1997/12/19 23:34:03  curt
1504 // Lot's of tweaking with sky rendering and lighting.
1505 //
1506 // Revision 1.36  1997/12/19 16:44:57  curt
1507 // Working on scene rendering order and options.
1508 //
1509 // Revision 1.35  1997/12/18 23:32:32  curt
1510 // First stab at sky dome actually starting to look reasonable. :-)
1511 //
1512 // Revision 1.34  1997/12/17 23:13:34  curt
1513 // Began working on rendering a sky.
1514 //
1515 // Revision 1.33  1997/12/15 23:54:45  curt
1516 // Add xgl wrappers for debugging.
1517 // Generate terrain normals on the fly.
1518 //
1519 // Revision 1.32  1997/12/15 20:59:08  curt
1520 // Misc. tweaks.
1521 //
1522 // Revision 1.31  1997/12/12 21:41:25  curt
1523 // More light/material property tweaking ... still a ways off.
1524 //
1525 // Revision 1.30  1997/12/12 19:52:47  curt
1526 // Working on lightling and material properties.
1527 //
1528 // Revision 1.29  1997/12/11 04:43:54  curt
1529 // Fixed sun vector and lighting problems.  I thing the moon is now lit
1530 // correctly.
1531 //
1532 // Revision 1.28  1997/12/10 22:37:45  curt
1533 // Prepended "fg" on the name of all global structures that didn't have it yet.
1534 // i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
1535 //
1536 // Revision 1.27  1997/12/09 05:11:54  curt
1537 // Working on tweaking lighting.
1538 //
1539 // Revision 1.26  1997/12/09 04:25:29  curt
1540 // Working on adding a global lighting params structure.
1541 //
1542 // Revision 1.25  1997/12/08 22:54:09  curt
1543 // Enabled GL_CULL_FACE.
1544 //
1545 // Revision 1.24  1997/11/25 19:25:32  curt
1546 // Changes to integrate Durk's moon/sun code updates + clean up.
1547 //
1548 // Revision 1.23  1997/11/15 18:16:34  curt
1549 // minor tweaks.
1550 //
1551 // Revision 1.22  1997/10/30 12:38:41  curt
1552 // Working on new scenery subsystem.
1553 //
1554 // Revision 1.21  1997/09/23 00:29:38  curt
1555 // Tweaks to get things to compile with gcc-win32.
1556 //
1557 // Revision 1.20  1997/09/22 14:44:19  curt
1558 // Continuing to try to align stars correctly.
1559 //
1560 // Revision 1.19  1997/09/18 16:20:08  curt
1561 // At dusk/dawn add/remove stars in stages.
1562 //
1563 // Revision 1.18  1997/09/16 22:14:51  curt
1564 // Tweaked time of day lighting equations.  Don't draw stars during the day.
1565 //
1566 // Revision 1.17  1997/09/16 15:50:29  curt
1567 // Working on star alignment and time issues.
1568 //
1569 // Revision 1.16  1997/09/13 02:00:06  curt
1570 // Mostly working on stars and generating sidereal time for accurate star
1571 // placement.
1572 //
1573 // Revision 1.15  1997/09/05 14:17:27  curt
1574 // More tweaking with stars.
1575 //
1576 // Revision 1.14  1997/09/05 01:35:53  curt
1577 // Working on getting stars right.
1578 //
1579 // Revision 1.13  1997/09/04 02:17:34  curt
1580 // Shufflin' stuff.
1581 //
1582 // Revision 1.12  1997/08/27 21:32:24  curt
1583 // Restructured view calculation code.  Added stars.
1584 //
1585 // Revision 1.11  1997/08/27 03:30:16  curt
1586 // Changed naming scheme of basic shared structures.
1587 //
1588 // Revision 1.10  1997/08/25 20:27:22  curt
1589 // Merged in initial HUD and Joystick code.
1590 //
1591 // Revision 1.9  1997/08/22 21:34:39  curt
1592 // Doing a bit of reorganizing and house cleaning.
1593 //
1594 // Revision 1.8  1997/08/19 23:55:03  curt
1595 // Worked on better simulating real lighting.
1596 //
1597 // Revision 1.7  1997/08/16 12:22:38  curt
1598 // Working on improving the lighting/shading.
1599 //
1600 // Revision 1.6  1997/08/13 20:24:56  curt
1601 // Changes due to changing sunpos interface.
1602 //
1603 // Revision 1.5  1997/08/06 21:08:32  curt
1604 // Sun position now really* works (I think) ... I still have sun time warping
1605 // code in place, probably should remove it soon.
1606 //
1607 // Revision 1.4  1997/08/06 15:41:26  curt
1608 // Working on correct sun position.
1609 //
1610 // Revision 1.3  1997/08/06 00:24:22  curt
1611 // Working on correct real time sun lighting.
1612 //
1613 // Revision 1.2  1997/08/04 20:25:15  curt
1614 // Organizational tweaking.
1615 //
1616 // Revision 1.1  1997/08/02 18:45:00  curt
1617 // Renamed GLmain.c GLUTmain.c
1618 //
1619 // Revision 1.43  1997/08/02 16:23:47  curt
1620 // Misc. tweaks.
1621 //
1622 // Revision 1.42  1997/08/01 19:43:33  curt
1623 // Making progress with coordinate system overhaul.
1624 //
1625 // Revision 1.41  1997/07/31 22:52:37  curt
1626 // Working on redoing internal coordinate systems & scenery transformations.
1627 //
1628 // Revision 1.40  1997/07/30 16:12:42  curt
1629 // Moved fg_random routines from Util/ to Math/
1630 //
1631 // Revision 1.39  1997/07/21 14:45:01  curt
1632 // Minor tweaks.
1633 //
1634 // Revision 1.38  1997/07/19 23:04:47  curt
1635 // Added an initial weather section.
1636 //
1637 // Revision 1.37  1997/07/19 22:34:02  curt
1638 // Moved PI definitions to ../constants.h
1639 // Moved random() stuff to ../Utils/ and renamed fg_random()
1640 //
1641 // Revision 1.36  1997/07/18 23:41:25  curt
1642 // Tweaks for building with Cygnus Win32 compiler.
1643 //
1644 // Revision 1.35  1997/07/18 14:28:34  curt
1645 // Hacked in some support for wind/turbulence.
1646 //
1647 // Revision 1.34  1997/07/16 20:04:48  curt
1648 // Minor tweaks to aid Win32 port.
1649 //
1650 // Revision 1.33  1997/07/12 03:50:20  curt
1651 // Added an #include <Windows32/Base.h> to help compiling for Win32
1652 //
1653 // Revision 1.32  1997/07/11 03:23:18  curt
1654 // Solved some scenery display/orientation problems.  Still have a positioning
1655 // (or transformation?) problem.
1656 //
1657 // Revision 1.31  1997/07/11 01:29:58  curt
1658 // More tweaking of terrian floor.
1659 //
1660 // Revision 1.30  1997/07/10 04:26:37  curt
1661 // We now can interpolated ground elevation for any position in the grid.  We
1662 // can use this to enforce a "hard" ground.  We still need to enforce some
1663 // bounds checking so that we don't try to lookup data points outside the
1664 // grid data set.
1665 //
1666 // Revision 1.29  1997/07/09 21:31:12  curt
1667 // Working on making the ground "hard."
1668 //
1669 // Revision 1.28  1997/07/08 18:20:12  curt
1670 // Working on establishing a hard ground.
1671 //
1672 // Revision 1.27  1997/07/07 20:59:49  curt
1673 // Working on scenery transformations to enable us to fly fluidly over the
1674 // poles with no discontinuity/distortion in scenery.
1675 //
1676 // Revision 1.26  1997/07/05 20:43:34  curt
1677 // renamed mat3 directory to Math so we could add other math related routines.
1678 //
1679 // Revision 1.25  1997/06/29 21:19:17  curt
1680 // Working on scenery management system.
1681 //
1682 // Revision 1.24  1997/06/26 22:14:53  curt
1683 // Beginning work on a scenery management system.
1684 //
1685 // Revision 1.23  1997/06/26 19:08:33  curt
1686 // Restructuring make, adding automatic "make dep" support.
1687 //
1688 // Revision 1.22  1997/06/25 15:39:47  curt
1689 // Minor changes to compile with rsxnt/win32.
1690 //
1691 // Revision 1.21  1997/06/22 21:44:41  curt
1692 // Working on intergrating the VRML (subset) parser.
1693 //
1694 // Revision 1.20  1997/06/21 17:12:53  curt
1695 // Capitalized subdirectory names.
1696 //
1697 // Revision 1.19  1997/06/18 04:10:31  curt
1698 // A couple more runway tweaks ...
1699 //
1700 // Revision 1.18  1997/06/18 02:21:24  curt
1701 // Hacked in a runway
1702 //
1703 // Revision 1.17  1997/06/17 16:51:58  curt
1704 // Timer interval stuff now uses gettimeofday() instead of ftime()
1705 //
1706 // Revision 1.16  1997/06/17 04:19:16  curt
1707 // More timer related tweaks with respect to view direction changes.
1708 //
1709 // Revision 1.15  1997/06/17 03:41:10  curt
1710 // Nonsignal based interval timing is now working.
1711 // This would be a good time to look at cleaning up the code structure a bit.
1712 //
1713 // Revision 1.14  1997/06/16 19:32:51  curt
1714 // Starting to add general timer support.
1715 //
1716 // Revision 1.13  1997/06/02 03:40:06  curt
1717 // A tiny bit more view tweaking.
1718 //
1719 // Revision 1.12  1997/06/02 03:01:38  curt
1720 // Working on views (side, front, back, transitions, etc.)
1721 //
1722 // Revision 1.11  1997/05/31 19:16:25  curt
1723 // Elevator trim added.
1724 //
1725 // Revision 1.10  1997/05/31 04:13:52  curt
1726 // WE CAN NOW FLY!!!
1727 //
1728 // Continuing work on the LaRCsim flight model integration.
1729 // Added some MSFS-like keyboard input handling.
1730 //
1731 // Revision 1.9  1997/05/30 19:27:01  curt
1732 // The LaRCsim flight model is starting to look like it is working.
1733 //
1734 // Revision 1.8  1997/05/30 03:54:10  curt
1735 // Made a bit more progress towards integrating the LaRCsim flight model.
1736 //
1737 // Revision 1.7  1997/05/29 22:39:49  curt
1738 // Working on incorporating the LaRCsim flight model.
1739 //
1740 // Revision 1.6  1997/05/29 12:31:39  curt
1741 // Minor tweaks, moving towards general flight model integration.
1742 //
1743 // Revision 1.5  1997/05/29 02:33:23  curt
1744 // Updated to reflect changing interfaces in other "modules."
1745 //
1746 // Revision 1.4  1997/05/27 17:44:31  curt
1747 // Renamed & rearranged variables and routines.   Added some initial simple
1748 // timer/alarm routines so the flight model can be updated on a regular 
1749 // interval.
1750 //
1751 // Revision 1.3  1997/05/23 15:40:25  curt
1752 // Added GNU copyright headers.
1753 // Fog now works!
1754 //
1755 // Revision 1.2  1997/05/23 00:35:12  curt
1756 // Trying to get fog to work ...
1757 //
1758 // Revision 1.1  1997/05/21 15:57:51  curt
1759 // Renamed due to added GLUT support.
1760 //
1761 // Revision 1.3  1997/05/19 18:22:42  curt
1762 // Parameter tweaking ... starting to stub in fog support.
1763 //
1764 // Revision 1.2  1997/05/17 00:17:34  curt
1765 // Trying to stub in support for standard OpenGL.
1766 //
1767 // Revision 1.1  1997/05/16 16:05:52  curt
1768 // Initial revision.
1769 //