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