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