]> git.mxchange.org Git - flightgear.git/blob - src/Main/main.cxx
Updates from Jon (addendum to previous changes).
[flightgear.git] / src / Main / main.cxx
1 // main.cxx -- top level sim routines
2 //
3 // Written by Curtis Olson for OpenGL, started May 1997.
4 //
5 // Copyright (C) 1997 - 1999  Curtis L. Olson  - curt@flightgear.org
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
23
24 #define MICHAEL_JOHNSON_EXPERIMENTAL_ENGINE_AUDIO
25
26
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #ifdef FG_MATH_EXCEPTION_CLASH
32 #  include <math.h>
33 #endif
34
35 #ifdef HAVE_WINDOWS_H
36 #  include <windows.h>                     
37 #  include <float.h>                    
38 #endif
39
40 #include <GL/glut.h>
41 #include <simgear/xgl/xgl.h>
42
43 #include <stdio.h>
44 #include <string.h>
45 #include <string>
46
47 #ifdef HAVE_STDLIB_H
48 #   include <stdlib.h>
49 #endif
50
51 #ifdef HAVE_SYS_STAT_H
52 #  include <sys/stat.h> /* for stat() */
53 #endif
54
55 #ifdef HAVE_UNISTD_H
56 #  include <unistd.h>    /* for stat() */
57 #endif
58
59 #include <plib/pu.h>                    // plib include
60 #include <plib/ssg.h>           // plib include
61
62 #ifdef ENABLE_AUDIO_SUPPORT
63 #  include <plib/sl.h>          // plib include
64 #  include <plib/sm.h>          // plib include
65 #endif
66
67 #include <simgear/constants.h>  // for VERSION
68 #include <simgear/debug/logstream.hxx>
69 #include <simgear/math/fg_geodesy.hxx>
70 #include <simgear/math/polar3d.hxx>
71 #include <simgear/math/fg_random.h>
72 #include <simgear/misc/fgpath.hxx>
73
74 #include <Include/general.hxx>
75
76 #include <Aircraft/aircraft.hxx>
77 #include <Ephemeris/ephemeris.hxx>
78
79 #include <Autopilot/newauto.hxx>
80 #include <Cockpit/cockpit.hxx>
81 #include <Cockpit/radiostack.hxx>
82 #include <Cockpit/steam.hxx>
83
84 // bfi.hxx has to be included before uiuc_aircraft.h because of nasty
85 // #defines in uiuc_aircraft.h
86 #include "bfi.hxx"
87
88 #include <FDM/UIUCModel/uiuc_aircraft.h>
89 #include <FDM/UIUCModel/uiuc_aircraftdir.h>
90 #include <GUI/gui.h>
91 #include <Joystick/joystick.hxx>
92 #ifdef FG_NETWORK_OLK
93 #include <NetworkOLK/network.h>
94 #endif
95 #include <Objects/materialmgr.hxx>
96 #include <Scenery/scenery.hxx>
97 #include <Scenery/tilemgr.hxx>
98 #include <Sky/sky.hxx>
99 #include <Time/event.hxx>
100 #include <Time/fg_time.hxx>
101 #include <Time/fg_timer.hxx>
102 #include <Time/sunpos.hxx>
103
104 #ifndef FG_OLD_WEATHER
105 #  include <WeatherCM/FGLocalWeatherDatabase.h>
106 #else
107 #  include <Weather/weather.hxx>
108 #endif
109
110 #include "fg_init.hxx"
111 #include "fg_io.hxx"
112 #include "keyboard.hxx"
113 #include "options.hxx"
114 #include "splash.hxx"
115 #include "views.hxx"
116
117
118 // -dw- use custom sioux settings so I can see output window
119 #ifdef MACOS
120 #  ifndef FG_NDEBUG
121 #    include <sioux.h> // settings for output window
122 #  endif
123 #  include <console.h>
124 #endif
125
126
127 // This is a record containing a bit of global housekeeping information
128 FGGeneral general;
129
130 // Specify our current idle function state.  This is used to run all
131 // our initializations out of the glutIdleLoop() so that we can get a
132 // splash screen up and running right away.
133 static int idle_state = 0;
134 static int global_multi_loop;
135
136 // Another hack
137 int use_signals = 0;
138
139 // Global structures for the Audio library
140 #ifdef ENABLE_AUDIO_SUPPORT
141 slEnvelope pitch_envelope ( 1, SL_SAMPLE_ONE_SHOT ) ;
142 slEnvelope volume_envelope ( 1, SL_SAMPLE_ONE_SHOT ) ;
143 slScheduler *audio_sched;
144 smMixer *audio_mixer;
145 slSample *s1;
146 slSample *s2;
147 #endif
148
149
150 // ssg variables
151 ssgRoot *scene = NULL;
152 ssgBranch *terrain = NULL;
153 ssgSelector *penguin_sel = NULL;
154 ssgTransform *penguin_pos = NULL;
155
156 #ifdef FG_NETWORK_OLK
157 ssgSelector *fgd_sel = NULL;
158 ssgTransform *fgd_pos = NULL;
159 //sgMat4 sgTUX;
160 #endif
161
162 // current fdm/position used for view
163 FGInterface cur_view_fdm;
164
165 // Sky structures
166 FGEphemeris *ephem;
167 SGSky *thesky;
168
169 // hack
170 sgMat4 copy_of_ssgOpenGLAxisSwapMatrix =
171 {
172   {  1.0f,  0.0f,  0.0f,  0.0f },
173   {  0.0f,  0.0f, -1.0f,  0.0f },
174   {  0.0f,  1.0f,  0.0f,  0.0f },
175   {  0.0f,  0.0f,  0.0f,  1.0f }
176 } ;
177
178 // The following defines flight gear options. Because glutlib will also
179 // want to parse its own options, those options must not be included here
180 // or they will get parsed by the main program option parser. Hence case
181 // is significant for any option added that might be in conflict with
182 // glutlib's parser.
183 //
184 // glutlib parses for:
185 //    -display
186 //    -direct   (invalid in Win32)
187 //    -geometry
188 //    -gldebug
189 //    -iconized
190 //    -indirect (invalid in Win32)
191 //    -synce
192 //
193 // Note that glutlib depends upon strings while this program's
194 // option parser wants only initial characters followed by numbers
195 // or pathnames.
196 //
197
198
199 ssgSimpleState *default_state;
200 ssgSimpleState *hud_and_panel;
201 ssgSimpleState *menus;
202
203 void fgBuildRenderStates( void ) {
204     default_state = new ssgSimpleState;
205     default_state->disable( GL_TEXTURE_2D );
206     default_state->enable( GL_CULL_FACE );
207     default_state->disable( GL_COLOR_MATERIAL );
208     default_state->disable( GL_BLEND );
209     default_state->disable( GL_ALPHA_TEST );
210     default_state->disable( GL_LIGHTING );
211     default_state->setMaterial( GL_AMBIENT_AND_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
212
213     hud_and_panel = new ssgSimpleState;
214     hud_and_panel->disable( GL_CULL_FACE );
215     hud_and_panel->disable( GL_TEXTURE_2D );
216     hud_and_panel->disable( GL_LIGHTING );
217
218     menus = new ssgSimpleState;
219     menus->disable( GL_CULL_FACE );
220     menus->disable( GL_TEXTURE_2D );
221     menus->enable( GL_BLEND );
222 }
223
224
225 // fgInitVisuals() -- Initialize various GL/view parameters
226 void fgInitVisuals( void ) {
227     fgLIGHT *l;
228
229     l = &cur_light_params;
230
231 #ifndef GLUT_WRONG_VERSION
232     // Go full screen if requested ...
233     if ( current_options.get_fullscreen() ) {
234         glutFullScreen();
235     }
236 #endif
237
238     // If enabled, normal vectors specified with glNormal are scaled
239     // to unit length after transformation.  See glNormal.
240     // glEnable( GL_NORMALIZE );
241
242     glEnable( GL_LIGHTING );
243     glEnable( GL_LIGHT0 );
244     glLightfv( GL_LIGHT0, GL_POSITION, l->sun_vec );
245
246     sgVec3 sunpos;
247     sgSetVec3( sunpos, l->sun_vec[0], l->sun_vec[1], l->sun_vec[2] );
248     ssgGetLight( 0 ) -> setPosition( sunpos );
249
250     // glFogi (GL_FOG_MODE, GL_LINEAR);
251     glFogi (GL_FOG_MODE, GL_EXP2);
252     if ( (current_options.get_fog() == 1) || 
253          (current_options.get_shading() == 0) ) {
254         // if fastest fog requested, or if flat shading force fastest
255         glHint ( GL_FOG_HINT, GL_FASTEST );
256     } else if ( current_options.get_fog() == 2 ) {
257         glHint ( GL_FOG_HINT, GL_NICEST );
258     }
259     if ( current_options.get_wireframe() ) {
260         // draw wire frame
261         glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
262     }
263
264     // This is the default anyways, but it can't hurt
265     glFrontFace ( GL_CCW );
266
267     // Just testing ...
268     // glEnable(GL_POINT_SMOOTH);
269     // glEnable(GL_LINE_SMOOTH);
270     // glEnable(GL_POLYGON_SMOOTH);      
271 }
272
273
274 // Update all Visuals (redraws anything graphics related)
275 void fgRenderFrame( void ) {
276     // Update the BFI.
277     FGBFI::update();
278
279     fgLIGHT *l = &cur_light_params;
280     FGTime *t = FGTime::cur_time_params;
281     // FGView *v = &current_view;
282     static double last_visibility = -9999;
283     static bool in_puff = false;
284     static double puff_length = 0;
285     static double puff_progression = 0;
286     const double ramp_up = 0.15;
287     const double ramp_down = 0.15;
288
289     double angle;
290     // GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
291     // GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
292     // GLfloat terrain_color[4] = { 0.54, 0.44, 0.29, 1.0 };
293     // GLfloat mat_shininess[] = { 10.0 };
294     GLbitfield clear_mask;
295     
296     if ( idle_state != 1000 ) {
297         // still initializing, draw the splash screen
298         if ( current_options.get_splash_screen() == 1 ) {
299             fgSplashUpdate(0.0);
300         }
301     } else {
302         // idle_state is now 1000 meaning we've finished all our
303         // initializations and are running the main loop, so this will
304         // now work without seg faulting the system.
305
306         // printf("Ground = %.2f  Altitude = %.2f\n", scenery.cur_elev, 
307         //        FG_Altitude * FEET_TO_METER);
308     
309         // this is just a temporary hack, to make me understand Pui
310         // timerText -> setLabel (ctime (&t->cur_time));
311         // end of hack
312
313         // update view volume parameters
314         // cout << "before pilot_view update" << endl;
315         pilot_view.UpdateViewParams(*cur_fdm_state);
316         // cout << "after pilot_view update" << endl;
317         current_view.UpdateViewParams(cur_view_fdm);
318
319         // set the sun position
320         glLightfv( GL_LIGHT0, GL_POSITION, l->sun_vec );
321
322         clear_mask = GL_DEPTH_BUFFER_BIT;
323         if ( current_options.get_wireframe() ) {
324             clear_mask |= GL_COLOR_BUFFER_BIT;
325         }
326         if ( current_options.get_panel_status() ) {
327             // we can't clear the screen when the panel is active
328         } else if ( current_options.get_skyblend() ) {
329             if ( current_options.get_textures() ) {
330                 // glClearColor(black[0], black[1], black[2], black[3]);
331                 glClearColor(l->adj_fog_color[0], l->adj_fog_color[1], 
332                              l->adj_fog_color[2], l->adj_fog_color[3]);
333                 clear_mask |= GL_COLOR_BUFFER_BIT;
334             }
335         } else {
336             glClearColor(l->sky_color[0], l->sky_color[1], 
337                          l->sky_color[2], l->sky_color[3]);
338             clear_mask |= GL_COLOR_BUFFER_BIT;
339         }
340         glClear( clear_mask );
341
342         // Tell GL we are switching to model view parameters
343
344         // I really should create a derived ssg node or use a call
345         // back or something so that I can draw the sky within the
346         // ssgCullandDraw() function, but for now I just mimic what
347         // ssg does to set up the model view matrix
348         glMatrixMode(GL_MODELVIEW);
349         glLoadIdentity();
350         ssgSetCamera( current_view.VIEW );
351
352         /*
353         sgMat4 vm_tmp, view_mat;
354         sgTransposeNegateMat4 ( vm_tmp, current_view.VIEW ) ;
355         sgCopyMat4( view_mat, copy_of_ssgOpenGLAxisSwapMatrix ) ;
356         sgPreMultMat4( view_mat, vm_tmp ) ;
357         glLoadMatrixf( (float *)view_mat );
358         */
359
360         // set the opengl state to known default values
361         default_state->force();
362
363         // draw sky dome
364         if ( current_options.get_skyblend() ) {
365             sgVec3 view_pos;
366             sgSetVec3( view_pos,
367                        current_view.get_view_pos().x(),
368                        current_view.get_view_pos().y(),
369                        current_view.get_view_pos().z() );
370
371             sgVec3 zero_elev;
372             sgSetVec3( zero_elev,
373                        current_view.get_cur_zero_elev().x(),
374                        current_view.get_cur_zero_elev().y(),
375                        current_view.get_cur_zero_elev().z() );
376
377             /* cout << "thesky->repaint() sky_color = "
378                  << cur_light_params.sky_color[0] << " "
379                  << cur_light_params.sky_color[1] << " "
380                  << cur_light_params.sky_color[2] << " "
381                  << cur_light_params.sky_color[3] << endl;
382             cout << "    fog = "
383                  << cur_light_params.fog_color[0] << " "
384                  << cur_light_params.fog_color[1] << " "
385                  << cur_light_params.fog_color[2] << " "
386                  << cur_light_params.fog_color[3] << endl;
387             cout << "    sun_angle = " << cur_light_params.sun_angle
388                  << "    moon_angle = " << cur_light_params.moon_angle
389                  << endl; */
390             thesky->repaint( cur_light_params.sky_color,
391                              cur_light_params.adj_fog_color,
392                              cur_light_params.sun_angle,
393                              cur_light_params.moon_angle,
394                              ephem->getNumPlanets(), ephem->getPlanets(),
395                              ephem->getNumStars(), ephem->getStars() );
396  
397             /* cout << "thesky->reposition( view_pos = " << view_pos[0] << " "
398                  << view_pos[1] << " " << view_pos[2] << endl;
399             cout << "    zero_elev = " << zero_elev[0] << " "
400                  << zero_elev[1] << " " << zero_elev[2]
401                  << " lon = " << cur_fdm_state->get_Longitude()
402                  << " lat = " << cur_fdm_state->get_Latitude() << endl;
403             cout << "    sun_rot = " << cur_light_params.sun_rotation
404                  << " gst = " << FGTime::cur_time_params->getGst() << endl;
405             cout << "    sun ra = " << ephem->getSunRightAscension()
406                  << " sun dec = " << ephem->getSunDeclination() 
407                  << " moon ra = " << ephem->getMoonRightAscension()
408                  << " moon dec = " << ephem->getMoonDeclination() << endl; */
409
410             thesky->reposition( view_pos, zero_elev, 
411                                 cur_fdm_state->get_Longitude(),
412                                 cur_fdm_state->get_Latitude(),
413                                 cur_light_params.sun_rotation,
414                                 FGTime::cur_time_params->getGst(),
415                                 ephem->getSunRightAscension(),
416                                 ephem->getSunDeclination(), 50000.0,
417                                 ephem->getMoonRightAscension(),
418                                 ephem->getMoonDeclination(), 50000.0 );
419         }
420
421         glEnable( GL_DEPTH_TEST );
422         if ( current_options.get_fog() > 0 ) {
423             glEnable( GL_FOG );
424             glFogi( GL_FOG_MODE, GL_EXP2 );
425             glFogfv( GL_FOG_COLOR, l->adj_fog_color );
426         }
427
428         // update fog params if visibility has changed
429 #ifndef FG_OLD_WEATHER
430         double cur_visibility = WeatherDatabase->getWeatherVisibility();
431 #else
432         double cur_visibility = current_weather.get_visibility();
433 #endif
434         double actual_visibility = cur_visibility;
435
436         if ( current_options.get_clouds() ) {
437             double diff = fabs( cur_fdm_state->get_Altitude() * FEET_TO_METER -
438                                 current_options.get_clouds_asl() );
439             // cout << "altitude diff = " << diff << endl;
440             if ( diff < 75 ) {
441                 if ( ! in_puff ) {
442                     // calc chance of entering cloud puff
443                     double rnd = fg_random();
444                     double chance = rnd * rnd * rnd;
445                     if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
446                         in_puff = true;
447                         do {
448                             puff_length = fg_random() * 2.0; // up to 2 seconds
449                         } while ( puff_length <= 0.0 );
450                         puff_progression = 0.0;
451                     }
452                 }
453
454                 actual_visibility = cur_visibility * (diff - 25) / 50.0;
455
456                 if ( in_puff ) {
457                     // modify actual_visibility based on puff envelope
458
459                     if ( puff_progression <= ramp_up ) {
460                         double x = FG_PI_2 * puff_progression / ramp_up;
461                         double factor = 1.0 - sin( x );
462                         actual_visibility = actual_visibility * factor;
463                     } else if ( puff_progression >= ramp_up + puff_length ) {
464                         double x = FG_PI_2 * 
465                             (puff_progression - (ramp_up + puff_length)) /
466                             ramp_down;
467                         double factor = sin( x );
468                         actual_visibility = actual_visibility * factor;
469                     } else {
470                         actual_visibility = 0.0;
471                     }
472
473                     /* cout << "len = " << puff_length
474                          << "  x = " << x 
475                          << "  factor = " << factor
476                          << "  actual_visibility = " << actual_visibility 
477                          << endl; */
478
479                     puff_progression += ( global_multi_loop * 
480                                           current_options.get_speed_up() ) /
481                         (double)current_options.get_model_hz();
482
483                     /* cout << "gml = " << global_multi_loop 
484                          << "  speed up = " << current_options.get_speed_up()
485                          << "  hz = " << current_options.get_model_hz() << endl;
486                          */ 
487
488                     if ( puff_progression > puff_length + ramp_up + ramp_down) {
489                         in_puff = false; 
490                     }
491                 }
492
493                 // never let visibility drop below zero
494                 if ( actual_visibility < 0 ) {
495                     actual_visibility = 0;
496                 }
497             }
498         }
499
500         // cout << "actual visibility = " << actual_visibility << endl;
501
502         if ( actual_visibility != last_visibility ) {
503             last_visibility = actual_visibility;
504
505             // cout << "----> updating fog params" << endl;
506                 
507             GLfloat fog_exp_density;
508             GLfloat fog_exp2_density;
509     
510             // for GL_FOG_EXP
511             fog_exp_density = -log(0.01 / actual_visibility);
512     
513             // for GL_FOG_EXP2
514             fog_exp2_density = sqrt( -log(0.01) ) / actual_visibility;
515     
516             // Set correct opengl fog density
517             glFogf (GL_FOG_DENSITY, fog_exp2_density);
518         }
519  
520         // set lighting parameters
521         glLightfv( GL_LIGHT0, GL_AMBIENT, l->scene_ambient );
522         glLightfv( GL_LIGHT0, GL_DIFFUSE, l->scene_diffuse );
523         // glLightfv(GL_LIGHT0, GL_SPECULAR, white );
524
525         // texture parameters
526         // glEnable( GL_TEXTURE_2D );
527         glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
528         glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ) ;
529         // set base color (I don't think this is doing anything here)
530         // glMaterialfv (GL_FRONT, GL_AMBIENT, white);
531         //  (GL_FRONT, GL_DIFFUSE, white);
532         // glMaterialfv (GL_FRONT, GL_SPECULAR, white);
533         // glMaterialfv (GL_FRONT, GL_SHININESS, mat_shininess);
534
535         sgVec3 sunpos;
536         sgSetVec3( sunpos, l->sun_vec[0], l->sun_vec[1], l->sun_vec[2] );
537         ssgGetLight( 0 ) -> setPosition( sunpos );
538
539         // glMatrixMode( GL_PROJECTION );
540         // glLoadIdentity();
541         float fov = current_options.get_fov();
542         ssgSetFOV(fov * current_view.get_win_ratio(), fov);
543
544         double agl = current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER
545             - scenery.cur_elev;
546
547         // FG_LOG( FG_ALL, FG_INFO, "visibility is " 
548         //         << current_weather.get_visibility() );
549             
550         if ( agl > 10.0 ) {
551             ssgSetNearFar( 10.0f, 120000.0f );
552         } else {
553             ssgSetNearFar( 0.5f, 120000.0f );
554         }
555
556         if ( current_options.get_view_mode() == 
557              fgOPTIONS::FG_VIEW_PILOT )
558         {
559             // disable TuX
560             penguin_sel->select(0);
561         } else if ( current_options.get_view_mode() == 
562                     fgOPTIONS::FG_VIEW_FOLLOW )
563         {
564             // select view matrix from front of view matrix queue
565             // FGMat4Wrapper tmp = current_view.follow.front();
566             // sgCopyMat4( sgVIEW, tmp.m );
567
568             // enable TuX and set up his position and orientation
569             penguin_sel->select(1);
570
571             sgMat4 sgTRANS;
572             sgMakeTransMat4( sgTRANS, 
573                              pilot_view.view_pos.x(),
574                              pilot_view.view_pos.y(),
575                              pilot_view.view_pos.z() );
576
577             sgVec3 ownship_up;
578             sgSetVec3( ownship_up, 0.0, 0.0, 1.0);
579
580             sgMat4 sgROT;
581             sgMakeRotMat4( sgROT, -90.0, ownship_up );
582
583             // sgMat4 sgTMP;
584             // sgMat4 sgTUX;
585             // sgMultMat4( sgTMP, sgROT, pilot_view.VIEW_ROT );
586             // sgMultMat4( sgTUX, sgTMP, sgTRANS );
587
588             // sgTUX = ( sgROT * pilot_view.VIEW_ROT ) * sgTRANS
589             sgMat4 sgTUX;
590             sgCopyMat4( sgTUX, sgROT );
591             sgPostMultMat4( sgTUX, pilot_view.VIEW_ROT );
592             sgPostMultMat4( sgTUX, sgTRANS );
593         
594             sgCoord tuxpos;
595             sgSetCoord( &tuxpos, sgTUX );
596             penguin_pos->setTransform( &tuxpos );
597         }
598
599 # ifdef FG_NETWORK_OLK
600         if ( current_options.get_network_olk() ) {
601             sgCoord fgdpos;
602             other = head->next;             /* put listpointer to start  */
603             while ( other != tail) {        /* display all except myself */
604                 if ( strcmp( other->ipadr, fgd_mcp_ip) != 0) {
605                     other->fgd_sel->select(1);
606                     sgSetCoord( &fgdpos, other->sgFGD_COORD );
607                     other->fgd_pos->setTransform( &fgdpos );
608                 }
609                 other = other->next;
610             }
611
612             // fgd_sel->select(1);
613             // sgCopyMat4( sgTUX, current_view.sgVIEW);
614             // sgCoord fgdpos;
615             // sgSetCoord( &fgdpos, sgFGD_VIEW );
616             // fgd_pos->setTransform( &fgdpos);
617         }
618 # endif
619
620         // ssgSetCamera( current_view.VIEW );
621
622         // position tile nodes and update range selectors
623         global_tile_mgr.prep_ssg_nodes();
624
625         // force the default state so ssg can get back on track if
626         // we've changed things elsewhere
627         FGMaterialSlot m_slot;
628         FGMaterialSlot *m_ptr = &m_slot;
629         if ( material_mgr.find( "Default", m_ptr ) ) {
630             m_ptr->get_state()->force();
631         }
632
633         // draw the ssg scene
634         ssgCullAndDraw( scene );
635
636
637         // display HUD && Panel
638         glDisable( GL_FOG );
639         glDisable( GL_DEPTH_TEST );
640         // glDisable( GL_CULL_FACE );
641         // glDisable( GL_TEXTURE_2D );
642         hud_and_panel->apply();
643         fgCockpitUpdate();
644
645         // We can do translucent menus, so why not. :-)
646         // glEnable ( GL_BLEND ) ;
647         menus->apply();
648         glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
649         puDisplay();
650         // glDisable ( GL_BLEND ) ;
651
652         // glEnable( GL_FOG );
653     }
654
655     glutSwapBuffers();
656 }
657
658
659 // Update internal time dependent calculations (i.e. flight model)
660 void fgUpdateTimeDepCalcs(int multi_loop, int remainder) {
661     static fdm_state_list fdm_list;
662     // FGInterface fdm_state;
663     fgLIGHT *l = &cur_light_params;
664     FGTime *t = FGTime::cur_time_params;
665     // FGView *v = &current_view;
666     int i;
667
668     // update the flight model
669     if ( multi_loop < 0 ) {
670         multi_loop = 1;
671     }
672
673     if ( !t->getPause() ) {
674         // run Autopilot system
675         current_autopilot->run();
676
677         // printf("updating flight model x %d\n", multi_loop);
678         /* fgFDMUpdate( current_options.get_flight_model(), 
679                      fdm_state, 
680                      multi_loop * current_options.get_speed_up(),
681                      remainder ); */
682         cur_fdm_state->update( multi_loop * current_options.get_speed_up() );
683         FGSteam::update( multi_loop * current_options.get_speed_up() );
684     } else {
685         // fgFDMUpdate( current_options.get_flight_model(), 
686         //              fdm_state, 0, remainder );
687         cur_fdm_state->update( 0 );
688         FGSteam::update( 0 );
689     }
690
691     fdm_list.push_back( *cur_fdm_state );
692     while ( fdm_list.size() > 15 ) {
693         fdm_list.pop_front();
694     }
695
696     if ( current_options.get_view_mode() == fgOPTIONS::FG_VIEW_PILOT ) {
697         cur_view_fdm = *cur_fdm_state;
698         // do nothing
699     } else if ( current_options.get_view_mode() == fgOPTIONS::FG_VIEW_FOLLOW ) {
700         cur_view_fdm = fdm_list.front();
701     }
702
703     // update the view angle
704     for ( i = 0; i < multi_loop; i++ ) {
705         if ( fabs(current_view.get_goal_view_offset() - 
706                   current_view.get_view_offset()) < 0.05 )
707         {
708             current_view.set_view_offset( current_view.get_goal_view_offset() );
709             break;
710         } else {
711             // move current_view.view_offset towards current_view.goal_view_offset
712             if ( current_view.get_goal_view_offset() > 
713                  current_view.get_view_offset() )
714             {
715                 if ( current_view.get_goal_view_offset() - 
716                      current_view.get_view_offset() < FG_PI )
717                 {
718                     current_view.inc_view_offset( 0.01 );
719                 } else {
720                     current_view.inc_view_offset( -0.01 );
721                 }
722             } else {
723                 if ( current_view.get_view_offset() - 
724                      current_view.get_goal_view_offset() < FG_PI )
725                 {
726                     current_view.inc_view_offset( -0.01 );
727                 } else {
728                     current_view.inc_view_offset( 0.01 );
729                 }
730             }
731             if ( current_view.get_view_offset() > FG_2PI ) {
732                 current_view.inc_view_offset( -FG_2PI );
733             } else if ( current_view.get_view_offset() < 0 ) {
734                 current_view.inc_view_offset( FG_2PI );
735             }
736         }
737     }
738
739     double tmp = -(l->sun_rotation + FG_PI) 
740         - (cur_fdm_state->get_Psi() - current_view.get_view_offset() );
741     while ( tmp < 0.0 ) {
742         tmp += FG_2PI;
743     }
744     while ( tmp > FG_2PI ) {
745         tmp -= FG_2PI;
746     }
747     /* printf("Psi = %.2f, viewoffset = %.2f sunrot = %.2f rottosun = %.2f\n",
748            FG_Psi * RAD_TO_DEG, current_view.view_offset * RAD_TO_DEG, 
749            -(l->sun_rotation+FG_PI) * RAD_TO_DEG, tmp * RAD_TO_DEG); */
750     l->UpdateAdjFog();
751
752     // Update solar system
753     ephem->update( t, cur_fdm_state->get_Latitude() );
754
755     // Update radio stack model
756     current_radiostack->update( cur_fdm_state->get_Longitude(),
757                                 cur_fdm_state->get_Latitude(),
758                                 cur_fdm_state->get_Altitude() * FEET_TO_METER );
759 }
760
761
762 void fgInitTimeDepCalcs( void ) {
763     // initialize timer
764
765     // #ifdef HAVE_SETITIMER
766     //   fgTimerInit( 1.0 / current_options.get_model_hz(), 
767     //                fgUpdateTimeDepCalcs );
768     // #endif HAVE_SETITIMER
769 }
770
771
772 static const double alt_adjust_ft = 3.758099;
773 static const double alt_adjust_m = alt_adjust_ft * FEET_TO_METER;
774
775
776 // What should we do when we have nothing else to do?  Let's get ready
777 // for the next move and update the display?
778 static void fgMainLoop( void ) {
779     FGTime *t;
780     static long remainder = 0;
781     long elapsed;
782 #ifdef FANCY_FRAME_COUNTER
783     int i;
784     double accum;
785 #else
786     static time_t last_time = 0;
787     static int frames = 0;
788 #endif // FANCY_FRAME_COUNTER
789
790     t = FGTime::cur_time_params;
791
792     FG_LOG( FG_ALL, FG_DEBUG, "Running Main Loop");
793     FG_LOG( FG_ALL, FG_DEBUG, "======= ==== ====");
794
795 #ifdef FG_NETWORK_OLK
796     if ( current_options.get_network_olk() ) {
797         if ( net_is_registered == 0 ) {      // We first have to reg. to fgd
798             // printf("FGD: Netupdate\n");
799             fgd_send_com( "A", FGFS_host);   // Send Mat4 data
800             fgd_send_com( "B", FGFS_host);   // Recv Mat4 data
801         }
802     }
803 #endif
804
805 #if defined( ENABLE_PLIB_JOYSTICK )
806     // Read joystick and update control settings
807     if ( current_options.get_control_mode() == fgOPTIONS::FG_JOYSTICK ) {
808         fgJoystickRead();
809     }
810 #elif defined( ENABLE_GLUT_JOYSTICK )
811     // Glut joystick support works by feeding a joystick handler
812     // function to glut.  This is taken care of once in the joystick
813     // init routine and we don't have to worry about it again.
814 #endif
815
816 #ifdef FG_OLD_WEATHER
817     current_weather.Update();
818 #endif
819
820     // Fix elevation.  I'm just sticking this here for now, it should
821     // probably move eventually
822
823     /* printf("Before - ground = %.2f  runway = %.2f  alt = %.2f\n",
824            scenery.cur_elev,
825            cur_fdm_state->get_Runway_altitude() * FEET_TO_METER,
826            cur_fdm_state->get_Altitude() * FEET_TO_METER); */
827
828     if ( scenery.cur_elev > -9990 ) {
829         if ( cur_fdm_state->get_Altitude() * FEET_TO_METER < 
830              (scenery.cur_elev + alt_adjust_m - 3.0) ) {
831             // now set aircraft altitude above ground
832             printf("(*) Current Altitude = %.2f < %.2f forcing to %.2f\n", 
833                    cur_fdm_state->get_Altitude() * FEET_TO_METER,
834                    scenery.cur_elev + alt_adjust_m - 3.0,
835                    scenery.cur_elev + alt_adjust_m );
836             fgFDMForceAltitude( current_options.get_flight_model(), 
837                                 scenery.cur_elev + alt_adjust_m );
838
839             FG_LOG( FG_ALL, FG_DEBUG, 
840                     "<*> resetting altitude to " 
841                     << cur_fdm_state->get_Altitude() * FEET_TO_METER << " meters" );
842         }
843         fgFDMSetGroundElevation( current_options.get_flight_model(),
844                                  scenery.cur_elev );  // meters
845     }
846
847     /* printf("Adjustment - ground = %.2f  runway = %.2f  alt = %.2f\n",
848            scenery.cur_elev,
849            cur_fdm_state->get_Runway_altitude() * FEET_TO_METER,
850            cur_fdm_state->get_Altitude() * FEET_TO_METER); */
851
852     // update "time"
853     t->update( cur_fdm_state->get_Longitude(),
854                cur_fdm_state->get_Latitude(),
855                cur_fdm_state->get_Altitude()* FEET_TO_METER );
856
857     // Get elapsed time (in usec) for this past frame
858     elapsed = fgGetTimeInterval();
859     FG_LOG( FG_ALL, FG_DEBUG, 
860             "Elapsed time interval is = " << elapsed 
861             << ", previous remainder is = " << remainder );
862
863     // Calculate frame rate average
864 #ifdef FANCY_FRAME_COUNTER
865     /* old fps calculation */
866     if ( elapsed > 0 ) {
867         double tmp;
868         accum = 0.0;
869         for ( i = FG_FRAME_RATE_HISTORY - 2; i >= 0; i-- ) {
870             tmp = general.get_frame(i);
871             accum += tmp;
872             // printf("frame[%d] = %.2f\n", i, g->frames[i]);
873             general.set_frame(i+1,tmp);
874         }
875         tmp = 1000000.0 / (float)elapsed;
876         general.set_frame(0,tmp);
877         // printf("frame[0] = %.2f\n", general.frames[0]);
878         accum += tmp;
879         general.set_frame_rate(accum / (float)FG_FRAME_RATE_HISTORY);
880         // printf("ave = %.2f\n", general.frame_rate);
881     }
882 #else
883     if ( (t->get_cur_time() != last_time) && (last_time > 0) ) {
884         general.set_frame_rate( frames );
885         FG_LOG( FG_ALL, FG_DEBUG, 
886                 "--> Frame rate is = " << general.get_frame_rate() );
887         frames = 0;
888     }
889     last_time = t->get_cur_time();
890     ++frames;
891 #endif
892
893     // Run flight model
894     if ( ! use_signals ) {
895         // Calculate model iterations needed for next frame
896         elapsed += remainder;
897
898         global_multi_loop = (int)(((double)elapsed * 0.000001) * 
899                            current_options.get_model_hz());
900         remainder = elapsed - ( (global_multi_loop*1000000) / 
901                                 current_options.get_model_hz() );
902         FG_LOG( FG_ALL, FG_DEBUG, 
903                 "Model iterations needed = " << global_multi_loop
904                 << ", new remainder = " << remainder );
905         
906         // flight model
907         if ( global_multi_loop > 0 ) {
908             fgUpdateTimeDepCalcs(global_multi_loop, remainder);
909         } else {
910             FG_LOG( FG_ALL, FG_DEBUG, 
911                     "Elapsed time is zero ... we're zinging" );
912         }
913     }
914
915 #if ! defined( MACOS )
916     // Do any I/O channel work that might need to be done
917     fgIOProcess();
918 #endif
919
920     // see if we need to load any new scenery tiles
921     global_tile_mgr.update();
922
923     // Process/manage pending events
924     global_events.Process();
925
926     // Run audio scheduler
927 #ifdef ENABLE_AUDIO_SUPPORT
928     if ( current_options.get_sound() && !audio_sched->not_working() ) {
929
930 #   ifdef MICHAEL_JOHNSON_EXPERIMENTAL_ENGINE_AUDIO
931
932         // note: all these factors are relative to the sample.  our
933         // sample format should really contain a conversion factor so
934         // that we can get prop speed right for arbitrary samples.
935         // Note that for normal-size props, there is a point at which
936         // the prop tips approach the speed of sound; that is a pretty
937         // strong limit to how fast the prop can go.
938
939         // multiplication factor is prime pitch control; add some log
940         // component for verisimilitude
941
942         double pitch = log((controls.get_throttle(0) * 14.0) + 1.0);
943         //fprintf(stderr, "pitch1: %f ", pitch);
944         if (controls.get_throttle(0) > 0.0 || cur_fdm_state->v_rel_wind > 40.0) {
945             //fprintf(stderr, "rel_wind: %f ", cur_fdm_state->v_rel_wind);
946             // only add relative wind and AoA if prop is moving
947             // or we're really flying at idle throttle
948             if (pitch < 5.4) {  // this needs tuning
949                 // prop tips not breaking sound barrier
950                 pitch += log(cur_fdm_state->v_rel_wind + 0.8)/2;
951             } else {
952                 // prop tips breaking sound barrier
953                 pitch += log(cur_fdm_state->v_rel_wind + 0.8)/10;
954             }
955             //fprintf(stderr, "pitch2: %f ", pitch);
956             //fprintf(stderr, "AoA: %f ", FG_Gamma_vert_rad);
957
958             // Angle of Attack next... -x^3(e^x) is my best guess Just
959             // need to calculate some reasonable scaling factor and
960             // then clamp it on the positive aoa (neg adj) side
961             double aoa = cur_fdm_state->get_Gamma_vert_rad() * 2.2;
962             double tmp = 3.0;
963             double aoa_adj = pow(-aoa, tmp) * pow(M_E, aoa);
964             if (aoa_adj < -0.8) aoa_adj = -0.8;
965             pitch += aoa_adj;
966             //fprintf(stderr, "pitch3: %f ", pitch);
967
968             // don't run at absurdly slow rates -- not realistic
969             // and sounds bad to boot.  :-)
970             if (pitch < 0.8) pitch = 0.8;
971         }
972         //fprintf(stderr, "pitch4: %f\n", pitch);
973
974         double volume = controls.get_throttle(0) * 1.15 + 0.3 +
975             log(cur_fdm_state->v_rel_wind + 1.0)/14.0;
976         // fprintf(stderr, "volume: %f\n", volume);
977
978         pitch_envelope.setStep  ( 0, 0.01, pitch );
979         volume_envelope.setStep ( 0, 0.01, volume );
980
981 #   else
982
983        double param = controls.get_throttle( 0 ) * 2.0 + 1.0;
984        pitch_envelope.setStep  ( 0, 0.01, param );
985        volume_envelope.setStep ( 0, 0.01, param );
986
987 #   endif // experimental throttle patch
988
989         audio_sched -> update();
990     }
991 #endif
992
993     // redraw display
994     fgRenderFrame();
995
996     FG_LOG( FG_ALL, FG_DEBUG, "" );
997 }
998
999
1000 // This is the top level master main function that is registered as
1001 // our idle funciton
1002 //
1003
1004 // The first few passes take care of initialization things (a couple
1005 // per pass) and once everything has been initialized fgMainLoop from
1006 // then on.
1007
1008 static void fgIdleFunction ( void ) {
1009     // printf("idle state == %d\n", idle_state);
1010
1011     if ( idle_state == 0 ) {
1012         // Initialize the splash screen right away
1013         if ( current_options.get_splash_screen() ) {
1014             fgSplashInit();
1015         }
1016         
1017         idle_state++;
1018     } else if ( idle_state == 1 ) {
1019         // Start the intro music
1020 #if !defined(WIN32)
1021         if ( current_options.get_intro_music() ) {
1022             string lockfile = "/tmp/mpg123.running";
1023             FGPath mp3file( current_options.get_fg_root() );
1024             mp3file.append( "Sounds/intro.mp3" );
1025
1026             string command = "(touch " + lockfile + "; mpg123 "
1027                 + mp3file.str() + "> /dev/null 2>&1; /bin/rm "
1028                 + lockfile + ") &";
1029             FG_LOG( FG_GENERAL, FG_INFO, 
1030                     "Starting intro music: " << mp3file.str() );
1031             system ( command.c_str() );
1032         }
1033 #endif
1034
1035         idle_state++;
1036     } else if ( idle_state == 2 ) {
1037         // These are a few miscellaneous things that aren't really
1038         // "subsystems" but still need to be initialized.
1039
1040 #ifdef USE_GLIDE
1041         if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
1042             grTexLodBiasValue ( GR_TMU0, 1.0 ) ;
1043         }
1044 #endif
1045
1046         idle_state++;
1047     } else if ( idle_state == 3 ) {
1048         // This is the top level init routine which calls all the
1049         // other subsystem initialization routines.  If you are adding
1050         // a subsystem to flight gear, its initialization call should
1051         // located in this routine.
1052         if( !fgInitSubsystems()) {
1053             FG_LOG( FG_GENERAL, FG_ALERT,
1054                     "Subsystem initializations failed ..." );
1055             exit(-1);
1056         }
1057
1058         idle_state++;
1059     } else if ( idle_state == 4 ) {
1060         // setup OpenGL view parameters
1061         fgInitVisuals();
1062
1063         if ( use_signals ) {
1064             // init timer routines, signals, etc.  Arrange for an alarm
1065             // signal to be generated, etc.
1066             fgInitTimeDepCalcs();
1067         }
1068
1069         idle_state++;
1070     } else if ( idle_state == 5 ) {
1071
1072         idle_state++;
1073     } else if ( idle_state == 6 ) {
1074         // Initialize audio support
1075 #ifdef ENABLE_AUDIO_SUPPORT
1076
1077 #if !defined(WIN32)
1078         if ( current_options.get_intro_music() ) {
1079             // Let's wait for mpg123 to finish
1080             string lockfile = "/tmp/mpg123.running";
1081             struct stat stat_buf;
1082
1083             FG_LOG( FG_GENERAL, FG_INFO, 
1084                     "Waiting for mpg123 player to finish ..." );
1085             while ( stat(lockfile.c_str(), &stat_buf) == 0 ) {
1086                 // file exist, wait ...
1087                 sleep(1);
1088                 FG_LOG( FG_GENERAL, FG_INFO, ".");
1089             }
1090             FG_LOG( FG_GENERAL, FG_INFO, "");
1091         }
1092 #endif // WIN32
1093
1094         if ( current_options.get_sound() ) {
1095             audio_sched = new slScheduler ( 8000 );
1096             audio_mixer = new smMixer;
1097             audio_mixer -> setMasterVolume ( 80 ) ;  /* 80% of max volume. */
1098             audio_sched -> setSafetyMargin ( 1.0 ) ;
1099
1100             FGPath slfile( current_options.get_fg_root() );
1101             slfile.append( "Sounds/wasp.wav" );
1102
1103             s1 = new slSample ( (char *)slfile.c_str() );
1104             FG_LOG( FG_GENERAL, FG_INFO,
1105                     "Rate = " << s1 -> getRate()
1106                     << "  Bps = " << s1 -> getBps()
1107                     << "  Stereo = " << s1 -> getStereo() );
1108             audio_sched -> loopSample ( s1 );
1109
1110             if ( audio_sched->not_working() ) {
1111                 // skip
1112             } else {
1113                 pitch_envelope.setStep  ( 0, 0.01, 0.6 );
1114                 volume_envelope.setStep ( 0, 0.01, 0.6 );
1115
1116                 audio_sched -> addSampleEnvelope( s1, 0, 0, 
1117                                                   &pitch_envelope,
1118                                                   SL_PITCH_ENVELOPE );
1119                 audio_sched -> addSampleEnvelope( s1, 0, 1, 
1120                                                   &volume_envelope,
1121                                                   SL_VOLUME_ENVELOPE );
1122             }
1123
1124             // strcpy(slfile, path);
1125             // strcat(slfile, "thunder.wav");
1126             // s2 -> loadFile ( slfile );
1127             // s2 -> adjustVolume(0.5);
1128             // audio_sched -> playSample ( s2 );
1129         }
1130 #endif
1131
1132         // sleep(1);
1133         idle_state = 1000;
1134     } 
1135
1136     if ( idle_state == 1000 ) {
1137         // We've finished all our initialization steps, from now on we
1138         // run the main loop.
1139
1140         fgMainLoop();
1141     } else {
1142         if ( current_options.get_splash_screen() == 1 ) {
1143             fgSplashUpdate(0.0);
1144         }
1145     }
1146 }
1147
1148 // options.cxx needs to see this for toggle_panel()
1149 // Handle new window size or exposure
1150 void fgReshape( int width, int height ) {
1151     if ( ! current_options.get_panel_status() ) {
1152         current_view.set_win_ratio( (GLfloat) width / (GLfloat) height );
1153         glViewport(0, 0 , (GLint)(width), (GLint)(height) );
1154     } else {
1155         current_view.set_win_ratio( (GLfloat) width / 
1156                                     ((GLfloat) (height)*0.4232) );
1157         glViewport(0, (GLint)((height)*0.5768),
1158                    (GLint)(width), (GLint)((height)*0.4232) );
1159     }
1160
1161     current_view.set_winWidth( width );
1162     current_view.set_winHeight( height );
1163     current_view.force_update_fov_math();
1164
1165     // set these fov to be the same as in fgRenderFrame()
1166     // float x_fov = current_options.get_fov();
1167     // float y_fov = x_fov * 1.0 / current_view.get_win_ratio();
1168     // ssgSetFOV( x_fov, y_fov );
1169
1170     // glViewport ( 0, 0, width, height );
1171     float fov = current_options.get_fov();
1172     ssgSetFOV(fov * current_view.get_win_ratio(), fov);
1173
1174     fgHUDReshape();
1175
1176     if ( idle_state == 1000 ) {
1177         // yes we've finished all our initializations and are running
1178         // the main loop, so this will now work without seg faulting
1179         // the system.
1180         current_view.UpdateViewParams(cur_view_fdm);
1181     }
1182 }
1183
1184
1185 // Initialize GLUT and define a main window
1186 int fgGlutInit( int *argc, char **argv ) {
1187
1188 #if !defined( MACOS )
1189     // GLUT will extract all glut specific options so later on we only
1190     // need wory about our own.
1191     glutInit(argc, argv);
1192 #endif
1193
1194     // Define Display Parameters
1195     glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
1196
1197     FG_LOG( FG_GENERAL, FG_INFO, "Opening a window: " <<
1198             current_options.get_xsize() << "x" << current_options.get_ysize() );
1199
1200     // Define initial window size
1201     glutInitWindowSize( current_options.get_xsize(),
1202                          current_options.get_ysize() );
1203
1204     // Initialize windows
1205     if ( current_options.get_game_mode() == 0 ) {
1206         // Open the regular window
1207         glutCreateWindow("Flight Gear");
1208 #ifndef GLUT_WRONG_VERSION
1209     } else {
1210         // Open the cool new 'game mode' window
1211         char game_mode_str[256];
1212         sprintf( game_mode_str, "width=%d height=%d bpp=%d",
1213                  current_options.get_xsize(),
1214                  current_options.get_ysize(),
1215                  current_options.get_bpp());
1216
1217         FG_LOG( FG_GENERAL, FG_INFO, 
1218                 "game mode params = " << game_mode_str );
1219         glutGameModeString( game_mode_str );
1220         glutEnterGameMode();
1221 #endif
1222     }
1223
1224     // This seems to be the absolute earliest in the init sequence
1225     // that these calls will return valid info.  Too bad it's after
1226     // we've already created and sized out window. :-(
1227     general.set_glVendor( (char *)glGetString ( GL_VENDOR ) );
1228     general.set_glRenderer( (char *)glGetString ( GL_RENDERER ) );
1229     general.set_glVersion( (char *)glGetString ( GL_VERSION ) );
1230
1231     FG_LOG ( FG_GENERAL, FG_INFO, general.get_glRenderer() );
1232
1233 #if 0
1234     // try to determine if we should adjust the initial default
1235     // display resolution.  The options class defaults (is
1236     // initialized) to 640x480.
1237     string renderer = general.glRenderer;
1238
1239     // currently we only know how to deal with Mesa/Glide/Voodoo cards
1240     if ( renderer.find( "Glide" ) != string::npos ) {
1241         FG_LOG( FG_GENERAL, FG_INFO, "Detected a Glide driver" );
1242         if ( renderer.find( "FB/8" ) != string::npos ) {
1243             // probably a voodoo-2
1244             if ( renderer.find( "TMU/SLI" ) != string::npos ) {
1245                 // probably two SLI'd Voodoo-2's
1246                 current_options.set_xsize( 1024 );
1247                 current_options.set_ysize( 768 );
1248                 FG_LOG( FG_GENERAL, FG_INFO,
1249                         "It looks like you have two sli'd voodoo-2's." << endl
1250                         << "upgrading your win resolution to 1024 x 768" );
1251                 glutReshapeWindow(1024, 768);
1252             } else {
1253                 // probably a single non-SLI'd Voodoo-2
1254                 current_options.set_xsize( 800 );
1255                 current_options.set_ysize( 600 );
1256                 FG_LOG( FG_GENERAL, FG_INFO,
1257                         "It looks like you have a voodoo-2." << endl
1258                         << "upgrading your win resolution to 800 x 600" );
1259                 glutReshapeWindow(800, 600);
1260             }
1261         } else if ( renderer.find( "FB/2" ) != string::npos ) {
1262             // probably a voodoo-1, stick with the default
1263         }
1264     } else {
1265         // we have no special knowledge of this card, stick with the default
1266     }
1267 #endif
1268
1269     return(1);
1270 }
1271
1272
1273 // Initialize GLUT event handlers
1274 int fgGlutInitEvents( void ) {
1275     // call fgReshape() on window resizes
1276     glutReshapeFunc( fgReshape );
1277
1278     // call GLUTkey() on keyboard event
1279     glutKeyboardFunc( GLUTkey );
1280     glutSpecialFunc( GLUTspecialkey );
1281
1282     // call guiMouseFunc() whenever our little rodent is used
1283     glutMouseFunc ( guiMouseFunc );
1284     glutMotionFunc (guiMotionFunc );
1285     glutPassiveMotionFunc (guiMotionFunc );
1286
1287     // call fgMainLoop() whenever there is
1288     // nothing else to do
1289     glutIdleFunc( fgIdleFunction );
1290
1291     // draw the scene
1292     glutDisplayFunc( fgRenderFrame );
1293
1294     return(1);
1295 }
1296
1297
1298 // Main ...
1299 int main( int argc, char **argv ) {
1300
1301 #if defined( MACOS )
1302     freopen ("stdout.txt", "w", stdout );
1303     freopen ("stderr.txt", "w", stderr );
1304     argc = ccommand( &argv );
1305 #endif
1306
1307 #ifdef HAVE_BC5PLUS
1308     _control87(MCW_EM, MCW_EM);  /* defined in float.h */
1309 #endif
1310
1311     // set default log levels
1312     fglog().setLogLevels( FG_ALL, FG_INFO );
1313
1314     FG_LOG( FG_GENERAL, FG_INFO, "Flight Gear:  Version " << VERSION << endl );
1315
1316     // seed the random number generater
1317     fg_srandom();
1318
1319     // AIRCRAFT defined in uiuc_aircraft.h
1320     // AIRCRAFTDIR defined in uiuc_aircraftdir.h
1321     aircraft_ = new AIRCRAFT;
1322     aircraftdir_ = new AIRCRAFTDIR;
1323     aircraft_dir = ""; // Initialize the Aircraft directory to "" (UIUC)
1324
1325     // Load the configuration parameters
1326     if ( !fgInitConfig(argc, argv) ) {
1327         FG_LOG( FG_GENERAL, FG_ALERT, "Config option parsing failed ..." );
1328         exit(-1);
1329     }
1330
1331     // Initialize the Window/Graphics environment.
1332     if( !fgGlutInit(&argc, argv) ) {
1333         FG_LOG( FG_GENERAL, FG_ALERT, "GLUT initialization failed ..." );
1334         exit(-1);
1335     }
1336
1337     // Initialize the various GLUT Event Handlers.
1338     if( !fgGlutInitEvents() ) {
1339         FG_LOG( FG_GENERAL, FG_ALERT, 
1340                 "GLUT event handler initialization failed ..." );
1341         exit(-1);
1342     }
1343  
1344     // Initialize ssg (from plib).  Needs to come before we do any
1345     // other ssg stuff, but after opengl/glut has been initialized.
1346     ssgInit();
1347
1348     // Initialize the user interface (we need to do this before
1349     // passing off control to glut and before fgInitGeneral to get our
1350     // fonts !!!
1351     guiInit();
1352
1353     // Initialize time
1354     FGTime::cur_time_params = new FGTime();
1355     // FGTime::cur_time_params->init( cur_fdm_state->get_Longitude(), 
1356     //                                cur_fdm_state->get_Latitude() );
1357     // FGTime::cur_time_params->update( cur_fdm_state->get_Longitude() );
1358     FGTime::cur_time_params->init( 0.0, 0.0 );
1359     FGTime::cur_time_params->update( 0.0, 0.0, 0.0 );
1360
1361     // Do some quick general initializations
1362     if( !fgInitGeneral()) {
1363         FG_LOG( FG_GENERAL, FG_ALERT, 
1364                 "General initializations failed ..." );
1365         exit(-1);
1366     }
1367
1368     //
1369     // some ssg test stuff (requires data from the plib source
1370     // distribution) specifically from the ssg tux example
1371     //
1372
1373     FGPath modelpath( current_options.get_fg_root() );
1374     modelpath.append( "Models" );
1375     modelpath.append( "Geometry" );
1376   
1377     FGPath texturepath( current_options.get_fg_root() );
1378     texturepath.append( "Models" );
1379     texturepath.append( "Textures" );
1380   
1381     ssgModelPath( (char *)modelpath.c_str() );
1382     ssgTexturePath( (char *)texturepath.c_str() );
1383
1384     // Scene graph root
1385     scene = new ssgRoot;
1386     scene->setName( "Scene" );
1387
1388     // Initialize the sky
1389     FGPath ephem_data_path( current_options.get_fg_root() );
1390     ephem_data_path.append( "Astro" );
1391     ephem = new FGEphemeris( ephem_data_path.c_str() );
1392     ephem->update( FGTime::cur_time_params, 0.0 );
1393
1394     FGPath sky_tex_path( current_options.get_fg_root() );
1395     sky_tex_path.append( "Textures" );
1396     sky_tex_path.append( "Sky" );
1397     thesky = new SGSky;
1398     thesky->texture_path( sky_tex_path.str() );
1399
1400     ssgBranch *sky = thesky->build( 550.0, 550.0,
1401                                     ephem->getNumPlanets(), 
1402                                     ephem->getPlanets(), 60000.0,
1403                                     ephem->getNumStars(),
1404                                     ephem->getStars(), 60000.0 );
1405     scene->addKid( sky );
1406
1407     // Terrain branch
1408     terrain = new ssgBranch;
1409     terrain->setName( "Terrain" );
1410     scene->addKid( terrain );
1411
1412     // temporary visible aircraft "own ship"
1413     penguin_sel = new ssgSelector;
1414     penguin_pos = new ssgTransform;
1415     // ssgBranch *tux_obj = ssgMakeSphere( 10.0, 10, 10 );
1416     ssgEntity *tux_obj = ssgLoadAC( "glider.ac" );
1417     // ssgEntity *tux_obj = ssgLoadAC( "Tower1x.ac" );
1418
1419     penguin_pos->addKid( tux_obj );
1420     penguin_sel->addKid( penguin_pos );
1421     ssgFlatten( tux_obj );
1422     ssgStripify( penguin_sel );
1423     penguin_sel->clrTraversalMaskBits( SSGTRAV_HOT );
1424     scene->addKid( penguin_sel );
1425
1426 #ifdef FG_NETWORK_OLK
1427     // Do the network intialization
1428     if ( current_options.get_network_olk() ) {
1429         printf("Multipilot mode %s\n", fg_net_init( scene ) );
1430     }
1431 #endif
1432
1433     // build our custom render states
1434     fgBuildRenderStates();
1435
1436     // pass control off to the master GLUT event handler
1437     glutMainLoop();
1438
1439     // we never actually get here ... but to avoid compiler warnings,
1440     // etc.
1441     return 0;
1442 }