]> git.mxchange.org Git - flightgear.git/blob - src/Main/main.cxx
05414d4cd278dfd4b3cbc2e0556dee7229c19fba
[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 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #ifdef SG_MATH_EXCEPTION_CLASH
29 #  include <math.h>
30 #endif
31
32 #ifdef HAVE_WINDOWS_H
33 #  include <windows.h>
34 #  include <float.h>
35 #endif
36
37 #include <GL/glut.h>
38 #include <GL/gl.h>
39
40 #include <stdio.h>
41 #include <string.h>
42 #include <string>
43
44 #ifdef HAVE_STDLIB_H
45 #   include <stdlib.h>
46 #endif
47
48 #ifdef HAVE_SYS_STAT_H
49 #  include <sys/stat.h>         // for stat()
50 #endif
51
52 #ifdef HAVE_UNISTD_H
53 #  include <unistd.h>           // for stat()
54 #endif
55
56 // #ifdef HAVE_LIBX11
57 // #  include <GL/glext.h>
58 // #endif
59
60 #include <plib/pu.h>
61 #include <plib/ssg.h>
62
63 #include <simgear/constants.h>  // for VERSION
64 #include <simgear/debug/logstream.hxx>
65 #include <simgear/math/polar3d.hxx>
66 #include <simgear/math/sg_random.h>
67 #include <simgear/misc/sg_path.hxx>
68 #include <simgear/sky/sky.hxx>
69 #include <simgear/timing/sg_time.hxx>
70 #include <simgear/timing/lowleveltime.h>
71
72 #include <Include/general.hxx>
73
74 #include <Aircraft/aircraft.hxx>
75
76 #include <Autopilot/newauto.hxx>
77 #include <Cockpit/cockpit.hxx>
78 #include <Cockpit/radiostack.hxx>
79 #include <Cockpit/steam.hxx>
80
81 #include <FDM/UIUCModel/uiuc_aircraftdir.h>
82 #include <GUI/gui.h>
83 #include <GUI/sgVec3Slider.hxx>
84 #include <Joystick/joystick.hxx>
85 #ifdef FG_NETWORK_OLK
86 #include <NetworkOLK/network.h>
87 #endif
88 #include <Objects/matlib.hxx>
89 #include <Scenery/scenery.hxx>
90 #include <Scenery/tilemgr.hxx>
91 #ifdef ENABLE_AUDIO_SUPPORT
92 #  include <Sound/soundmgr.hxx>
93 #  include <Sound/morse.hxx>
94 #endif
95 #include <Time/event.hxx>
96 #include <Time/fg_timer.hxx>
97 #include <Time/light.hxx>
98 #include <Time/sunpos.hxx>
99 #include <Time/tmp.hxx>
100
101 // begin - added Venky
102 //    $$$ begin - added VS Renganathan
103 #include <simgear/misc/sgstream.hxx>
104 #include <FDM/flight.hxx>
105 #include <FDM/ADA.hxx>
106 void fgLoadDCS (void);
107 void fgUpdateDCS (void);
108 ssgSelector *ship_sel=NULL;
109 // upto 32 instances of a same object can be loaded.
110 ssgTransform *ship_pos[32];
111 double obj_lat[32],obj_lon[32],obj_alt[32];
112 int objc=0;
113 //    $$$ end - added VS Renganathan
114 // end - added Venky
115
116 #ifndef FG_OLD_WEATHER
117 #  include <WeatherCM/FGLocalWeatherDatabase.h>
118 #else
119 #  include <Weather/weather.hxx>
120 #endif
121
122 #include "version.h"
123
124 #include "bfi.hxx"
125 #include "fg_init.hxx"
126 #include "fg_io.hxx"
127 #include "globals.hxx"
128 #include "keyboard.hxx"
129 #include "splash.hxx"
130
131 #ifdef macintosh
132 #  include <console.h>          // -dw- for command line dialog
133 #endif
134
135
136 // This is a record containing a bit of global housekeeping information
137 FGGeneral general;
138
139 // Specify our current idle function state.  This is used to run all
140 // our initializations out of the glutIdleLoop() so that we can get a
141 // splash screen up and running right away.
142 static int idle_state = 0;
143 static long global_multi_loop;
144
145 // attempt to avoid a large bounce at startup
146 static bool initial_freeze = true;
147
148 // forward declaration
149 void fgReshape( int width, int height );
150
151 // Global structures for the Audio library
152 #ifdef ENABLE_AUDIO_SUPPORT
153    static FGSimpleSound *s1;
154    static FGSimpleSound *s2;
155 #endif
156
157
158 // ssg variables
159 ssgRoot *scene = NULL;
160 ssgBranch *terrain = NULL;
161
162 // aircraft model stuff
163 ssgSelector *acmodel_selector = NULL;
164 ssgTransform *acmodel_pos = NULL;
165 ssgSelector *prop_selector = NULL;
166 ssgSelector *flaps_selector = NULL;
167 int acmodel_npropsettings;
168 int acmodel_proprpms[4][2];  // different propeller settings
169
170 ssgRoot *lighting = NULL;
171 ssgBranch *ground = NULL;
172 ssgBranch *airport = NULL;
173
174 #ifdef FG_NETWORK_OLK
175 ssgSelector *fgd_sel = NULL;
176 ssgTransform *fgd_pos = NULL;
177 #endif
178
179 // current fdm/position used for view
180 FGInterface cur_view_fdm;
181
182 // Sky structures
183 SGSky *thesky;
184
185 // hack
186 sgMat4 copy_of_ssgOpenGLAxisSwapMatrix =
187 {
188   {  1.0f,  0.0f,  0.0f,  0.0f },
189   {  0.0f,  0.0f, -1.0f,  0.0f },
190   {  0.0f,  1.0f,  0.0f,  0.0f },
191   {  0.0f,  0.0f,  0.0f,  1.0f }
192 } ;
193
194 // The following defines flightgear options. Because glutlib will also
195 // want to parse its own options, those options must not be included here
196 // or they will get parsed by the main program option parser. Hence case
197 // is significant for any option added that might be in conflict with
198 // glutlib's parser.
199 //
200 // glutlib parses for:
201 //    -display
202 //    -direct   (invalid in Win32)
203 //    -geometry
204 //    -gldebug
205 //    -iconized
206 //    -indirect (invalid in Win32)
207 //    -synce
208 //
209 // Note that glutlib depends upon strings while this program's
210 // option parser wants only initial characters followed by numbers
211 // or pathnames.
212 //
213
214
215 ssgSimpleState *default_state;
216 ssgSimpleState *hud_and_panel;
217 ssgSimpleState *menus;
218
219 void fgBuildRenderStates( void ) {
220     default_state = new ssgSimpleState;
221     default_state->ref();
222     default_state->disable( GL_TEXTURE_2D );
223     default_state->enable( GL_CULL_FACE );
224     default_state->enable( GL_COLOR_MATERIAL );
225     default_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
226     default_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
227     default_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
228     default_state->disable( GL_BLEND );
229     default_state->disable( GL_ALPHA_TEST );
230     default_state->disable( GL_LIGHTING );
231
232     hud_and_panel = new ssgSimpleState;
233     hud_and_panel->ref();
234     hud_and_panel->disable( GL_CULL_FACE );
235     hud_and_panel->disable( GL_TEXTURE_2D );
236     hud_and_panel->disable( GL_LIGHTING );
237     hud_and_panel->enable( GL_BLEND );
238
239     menus = new ssgSimpleState;
240     menus->ref();
241     menus->disable( GL_CULL_FACE );
242     menus->disable( GL_TEXTURE_2D );
243     menus->enable( GL_BLEND );
244 }
245
246 // fgFindNode -- a function that finds a named node in an ssg graph
247 ssgEntity *fgFindNode( ssgEntity *node, const char *name ) {
248   if ( node->getName() != NULL && strcmp( name, node->getName() ) == 0 ) {
249     return node;
250   } else if ( node->isAKindOf( ssgTypeBranch() ) ) {
251     ssgEntity *kid = ((ssgBranch*)node)->getKid(0);
252     while (kid != NULL) {
253       ssgEntity *n = fgFindNode(kid, name);
254       if (n != NULL)
255         return n;
256
257       kid = ((ssgBranch*)node)->getNextKid();
258     }
259   }
260
261   return NULL;
262 }
263
264 // fgInitVisuals() -- Initialize various GL/view parameters
265 void fgInitVisuals( void ) {
266     fgLIGHT *l;
267
268     l = &cur_light_params;
269
270 #ifndef GLUT_WRONG_VERSION
271     // Go full screen if requested ...
272     if ( fgGetBool("/sim/startup/fullscreen") ) {
273         glutFullScreen();
274     }
275 #endif
276
277     // If enabled, normal vectors specified with glNormal are scaled
278     // to unit length after transformation.  See glNormal.
279     // glEnable( GL_NORMALIZE );
280
281     glEnable( GL_LIGHTING );
282     glEnable( GL_LIGHT0 );
283     glLightfv( GL_LIGHT0, GL_POSITION, l->sun_vec );
284
285     sgVec3 sunpos;
286     sgSetVec3( sunpos, l->sun_vec[0], l->sun_vec[1], l->sun_vec[2] );
287     ssgGetLight( 0 ) -> setPosition( sunpos );
288
289     // glFogi (GL_FOG_MODE, GL_LINEAR);
290     glFogi (GL_FOG_MODE, GL_EXP2);
291     if ( (fgGetString("/sim/rendering/fog") == "disabled") || 
292          (!fgGetBool("/sim/rendering/shading"))) {
293         // if fastest fog requested, or if flat shading force fastest
294         glHint ( GL_FOG_HINT, GL_FASTEST );
295     } else if ( fgGetString("/sim/rendering/fog") == "nicest" ) {
296         glHint ( GL_FOG_HINT, GL_NICEST );
297     }
298     if ( fgGetBool("/sim/rendering/wireframe") ) {
299         // draw wire frame
300         glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
301     }
302
303     // This is the default anyways, but it can't hurt
304     glFrontFace ( GL_CCW );
305
306     // Just testing ...
307     // glEnable(GL_POINT_SMOOTH);
308     // glEnable(GL_LINE_SMOOTH);
309     // glEnable(GL_POLYGON_SMOOTH);      
310 }
311
312
313 // Update all Visuals (redraws anything graphics related)
314 void fgRenderFrame( void ) {
315     // Update the BFI.
316     FGBFI::update();
317
318     fgLIGHT *l = &cur_light_params;
319     static double last_visibility = -9999;
320
321     static GLfloat fog_exp_density;
322     static GLfloat fog_exp2_density;
323     static GLfloat fog_exp2_punch_through;
324     
325     // double angle;
326     // GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
327     // GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
328     // GLfloat terrain_color[4] = { 0.54, 0.44, 0.29, 1.0 };
329     // GLfloat mat_shininess[] = { 10.0 };
330     GLbitfield clear_mask;
331     
332     if ( idle_state != 1000 ) {
333         // still initializing, draw the splash screen
334         if ( fgGetBool("/sim/startup/splash-screen") ) {
335             fgSplashUpdate(0.0);
336         }
337     } else {
338         // idle_state is now 1000 meaning we've finished all our
339         // initializations and are running the main loop, so this will
340         // now work without seg faulting the system.
341
342         // printf("Ground = %.2f  Altitude = %.2f\n", scenery.cur_elev, 
343         //        FG_Altitude * SG_FEET_TO_METER);
344     
345         // this is just a temporary hack, to make me understand Pui
346         // timerText -> setLabel (ctime (&t->cur_time));
347         // end of hack
348
349         // calculate our current position in cartesian space
350         scenery.center = scenery.next_center;
351         // printf("scenery center = %.2f %.2f %.2f\n", scenery.center.x(),
352         //        scenery.center.y(), scenery.center.z());
353
354         FGViewerRPH *pilot_view =
355             (FGViewerRPH *)globals->get_viewmgr()->get_view( 0 );
356
357         pilot_view->set_geod_view_pos( cur_fdm_state->get_Longitude(), 
358                                        cur_fdm_state->get_Lat_geocentric(), 
359                                        cur_fdm_state->get_Altitude() *
360                                        SG_FEET_TO_METER );
361         pilot_view->set_sea_level_radius( cur_fdm_state->
362                                           get_Sea_level_radius() *
363                                           SG_FEET_TO_METER ); 
364         pilot_view->set_rph( cur_fdm_state->get_Phi(),
365                              cur_fdm_state->get_Theta(),
366                              cur_fdm_state->get_Psi() );
367
368         FGViewerLookAt *chase_view =
369             (FGViewerLookAt *)globals->get_viewmgr()->get_view( 1 );
370
371         sgVec3 po;              // chase view pilot_offset
372         sgVec3 wup;             // chase view world up
373         sgSetVec3( po, 0.0, 0.0, 100.0 );
374         sgCopyVec3( wup, pilot_view->get_world_up() );
375         sgMat4 CXFM;            // chase view + pilot offset xform
376         sgMakeRotMat4( CXFM,
377                        chase_view->get_view_offset() * SGD_RADIANS_TO_DEGREES -
378                        cur_fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES,
379                        wup );
380         sgVec3 npo;             // new pilot offset after rotation
381         sgVec3 *pPO = PilotOffsetGet();
382         sgXformVec3( po, *pPO, pilot_view->get_UP() );
383         sgXformVec3( npo, po, CXFM );
384
385         chase_view->set_geod_view_pos( cur_fdm_state->get_Longitude(), 
386                                        cur_fdm_state->get_Lat_geocentric(), 
387                                        cur_fdm_state->get_Altitude() *
388                                        SG_FEET_TO_METER );
389         chase_view->set_sea_level_radius( cur_fdm_state->
390                                           get_Sea_level_radius() *
391                                           SG_FEET_TO_METER );
392         chase_view->set_pilot_offset( npo[0], npo[1], npo[2] );
393         chase_view->set_view_forward( pilot_view->get_view_pos() ); 
394         chase_view->set_view_up( wup );
395
396 #if 0
397         sgMat4 rph;
398         sgCopyMat4( rph, pilot_view->get_VIEW() );
399         cout << "RPH Matrix = " << endl;
400         int i, j;
401         for ( i = 0; i < 4; i++ ) {
402             for ( j = 0; j < 4; j++ ) {
403                 printf("%10.4f ", rph[i][j]);
404             }
405             cout << endl;
406         }
407
408         sgMat4 la;
409         sgCopyMat4( la, chase_view->get_VIEW() );
410         cout << "LookAt Matrix = " << endl;
411         for ( i = 0; i < 4; i++ ) {
412             for ( j = 0; j < 4; j++ ) {
413                 printf("%10.4f ", la[i][j]);
414             }
415             cout << endl;
416         }
417 #endif
418
419         // update view port
420         fgReshape( fgGetInt("/sim/startup/xsize"),
421                    fgGetInt("/sim/startup/ysize") );
422
423 #if 0
424         // swing and a miss
425
426         if ( ! fgPanelVisible() ) {
427             xglViewport( 0, 0 ,
428                          (GLint)(fgGetInt("/sim/startup/xsize")),
429                          (GLint)(fgGetInt("/sim/startup/ysize")) );
430         } else {
431             int view_h =
432                 int( (current_panel->getViewHeight() -
433                       current_panel->getYOffset())
434                      * (fgGetInt("/sim/startup/ysize") / 768.0) );
435             glViewport( 0, 
436                         (GLint)(fgGetInt("/sim/startup/ysize") - view_h),
437                         (GLint)(fgGetInt("/sim/startup/xsize")),
438                         (GLint)(view_h) );
439         }
440 #endif
441
442         // set the sun position
443         glLightfv( GL_LIGHT0, GL_POSITION, l->sun_vec );
444
445         clear_mask = GL_DEPTH_BUFFER_BIT;
446         if ( fgGetBool("/sim/rendering/wireframe") ) {
447             clear_mask |= GL_COLOR_BUFFER_BIT;
448         }
449
450         if ( fgGetBool("/sim/rendering/skyblend") ) {
451             if ( fgGetBool("/sim/rendering/textures") ) {
452                 // glClearColor(black[0], black[1], black[2], black[3]);
453                 glClearColor(l->adj_fog_color[0], l->adj_fog_color[1], 
454                              l->adj_fog_color[2], l->adj_fog_color[3]);
455                 clear_mask |= GL_COLOR_BUFFER_BIT;
456             }
457         } else {
458             glClearColor(l->sky_color[0], l->sky_color[1], 
459                          l->sky_color[2], l->sky_color[3]);
460             clear_mask |= GL_COLOR_BUFFER_BIT;
461         }
462         glClear( clear_mask );
463
464         // Tell GL we are switching to model view parameters
465
466         // I really should create a derived ssg node or use a call
467         // back or something so that I can draw the sky within the
468         // ssgCullAndDraw() function, but for now I just mimic what
469         // ssg does to set up the model view matrix
470         glMatrixMode(GL_MODELVIEW);
471         glLoadIdentity();
472         ssgSetCamera( (sgVec4 *)globals->get_current_view()->get_VIEW() );
473
474         // set the opengl state to known default values
475         default_state->force();
476
477         // update fog params if visibility has changed
478 #ifndef FG_OLD_WEATHER
479         thesky->set_visibility( WeatherDatabase->getWeatherVisibility() );
480 #else
481         thesky->set_visibility( current_weather.get_visibility() );
482 #endif
483
484         thesky->modify_vis( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER,
485                             ( global_multi_loop * 
486                               fgGetInt("/sim/speed-up") ) /
487                             (double)fgGetInt("/sim/model-hz") );
488
489         double actual_visibility = thesky->get_visibility();
490         // cout << "actual visibility = " << actual_visibility << endl;
491
492         if ( actual_visibility != last_visibility ) {
493             last_visibility = actual_visibility;
494
495             // cout << "----> updating fog params" << endl;
496                 
497             // for GL_FOG_EXP
498             fog_exp_density = -log(0.01) / actual_visibility;
499     
500             // for GL_FOG_EXP2
501             fog_exp2_density = sqrt( -log(0.01) ) / actual_visibility;
502             fog_exp2_punch_through = sqrt( -log(0.01) ) / 
503                 ( actual_visibility * 1.5 );
504         }
505
506         // Set correct opengl fog density
507         glFogf (GL_FOG_DENSITY, fog_exp2_density);
508
509         // update the sky dome
510         if ( fgGetBool("/sim/rendering/skyblend") ) {
511             /* cout << "thesky->repaint() sky_color = "
512                  << cur_light_params.sky_color[0] << " "
513                  << cur_light_params.sky_color[1] << " "
514                  << cur_light_params.sky_color[2] << " "
515                  << cur_light_params.sky_color[3] << endl;
516             cout << "    fog = "
517                  << cur_light_params.fog_color[0] << " "
518                  << cur_light_params.fog_color[1] << " "
519                  << cur_light_params.fog_color[2] << " "
520                  << cur_light_params.fog_color[3] << endl;
521             cout << "    sun_angle = " << cur_light_params.sun_angle
522                  << "    moon_angle = " << cur_light_params.moon_angle
523                  << endl; */
524             thesky->repaint( cur_light_params.sky_color,
525                              cur_light_params.adj_fog_color,
526                              cur_light_params.sun_angle,
527                              cur_light_params.moon_angle,
528                              globals->get_ephem()->getNumPlanets(),
529                              globals->get_ephem()->getPlanets(),
530                              globals->get_ephem()->getNumStars(),
531                              globals->get_ephem()->getStars() );
532  
533             /* cout << "thesky->reposition( view_pos = " << view_pos[0] << " "
534                  << view_pos[1] << " " << view_pos[2] << endl;
535             cout << "    zero_elev = " << zero_elev[0] << " "
536                  << zero_elev[1] << " " << zero_elev[2]
537                  << " lon = " << cur_fdm_state->get_Longitude()
538                  << " lat = " << cur_fdm_state->get_Latitude() << endl;
539             cout << "    sun_rot = " << cur_light_params.sun_rotation
540                  << " gst = " << SGTime::cur_time_params->getGst() << endl;
541             cout << "    sun ra = " << globals->get_ephem()->getSunRightAscension()
542                  << " sun dec = " << globals->get_ephem()->getSunDeclination() 
543                  << " moon ra = " << globals->get_ephem()->getMoonRightAscension()
544                  << " moon dec = " << globals->get_ephem()->getMoonDeclination() << endl; */
545
546             thesky->reposition( globals->get_current_view()->get_view_pos(),
547                                 globals->get_current_view()->get_zero_elev(),
548                                 globals->get_current_view()->get_world_up(),
549                                 cur_fdm_state->get_Longitude(),
550                                 cur_fdm_state->get_Latitude(),
551                                 cur_fdm_state->get_Altitude() * SG_FEET_TO_METER,
552                                 cur_light_params.sun_rotation,
553                                 globals->get_time_params()->getGst(),
554                                 globals->get_ephem()->getSunRightAscension(),
555                                 globals->get_ephem()->getSunDeclination(),
556                                 50000.0,
557                                 globals->get_ephem()->getMoonRightAscension(),
558                                 globals->get_ephem()->getMoonDeclination(),
559                                 50000.0 );
560         }
561
562         glEnable( GL_DEPTH_TEST );
563         if ( fgGetString("/sim/rendering/fog") != (string)"disabled" ) {
564             glEnable( GL_FOG );
565             glFogi( GL_FOG_MODE, GL_EXP2 );
566             glFogfv( GL_FOG_COLOR, l->adj_fog_color );
567         }
568
569         // set sun/lighting parameters
570         ssgGetLight( 0 ) -> setPosition( l->sun_vec );
571
572         // GL_LIGHT_MODEL_AMBIENT has a default non-zero value so if
573         // we only update GL_AMBIENT for our lights we will never get
574         // a completely dark scene.  So, we set GL_LIGHT_MODEL_AMBIENT
575         // explicitely to black.
576         GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
577         GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
578         glLightModelfv( GL_LIGHT_MODEL_AMBIENT, black );
579
580         ssgGetLight( 0 ) -> setColour( GL_AMBIENT, l->scene_ambient );
581         ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, l->scene_diffuse );
582         // ssgGetLight( 0 ) -> setColour( GL_SPECULAR, l->scene_white );
583
584         // texture parameters
585         // glEnable( GL_TEXTURE_2D );
586         glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
587         glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ) ;
588
589         // glMatrixMode( GL_PROJECTION );
590         // glLoadIdentity();
591         float fov = globals->get_current_view()->get_fov();
592         ssgSetFOV(fov, fov * globals->get_current_view()->get_win_ratio());
593
594         double agl = current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER
595             - scenery.cur_elev;
596
597         // SG_LOG( SG_ALL, SG_INFO, "visibility is " 
598         //         << current_weather.get_visibility() );
599             
600         if ( agl > 10.0 ) {
601             ssgSetNearFar( 10.0f, 120000.0f );
602         } else {
603             ssgSetNearFar( 0.5f, 120000.0f );
604         }
605
606         if ( globals->get_viewmgr()->get_current() == 0 ) {
607             // disable aircraft model
608             acmodel_selector->select(0);
609         } else { 
610             // enable aircraft model and set up its position and orientation
611             acmodel_selector->select(1);
612
613             FGViewerRPH *pilot_view =
614                 (FGViewerRPH *)globals->get_viewmgr()->get_view( 0 );
615
616             sgMat4 sgTRANS;
617             sgMakeTransMat4( sgTRANS, pilot_view->get_view_pos() );
618
619             sgVec3 ownship_up;
620             sgSetVec3( ownship_up, 0.0, 0.0, 1.0);
621
622             sgMat4 sgROT;
623             sgMakeRotMat4( sgROT, -90.0, ownship_up );
624
625             // sgMat4 sgTMP;
626             // sgMat4 sgTUX;
627             // sgMultMat4( sgTMP, sgROT, pilot_view.VIEW_ROT );
628             // sgMultMat4( sgTUX, sgTMP, sgTRANS );
629
630             // sgTUX = ( sgROT * pilot_view.VIEW_ROT ) * sgTRANS
631             sgMat4 sgTUX;
632             sgCopyMat4( sgTUX, sgROT );
633             sgPostMultMat4( sgTUX, pilot_view->get_VIEW_ROT() );
634             sgPostMultMat4( sgTUX, sgTRANS );
635         
636             sgCoord tuxpos;
637             sgSetCoord( &tuxpos, sgTUX );
638             acmodel_pos->setTransform( &tuxpos );
639
640             // set up moving parts
641             if (flaps_selector != NULL) {
642               flaps_selector->select( (controls.get_flaps() > 0.5f) ? 1 : 2 );
643             }
644
645             if (prop_selector != NULL) {
646               int propsel_mask = 0;
647               for (int i = 0; i < acmodel_npropsettings; i++) {
648                 if (FGBFI::getRPM() >= acmodel_proprpms[i][0] &&
649                     FGBFI::getRPM() <= acmodel_proprpms[i][1]) {
650                   propsel_mask |= 1 << i;
651                 }
652               }
653               prop_selector->select(propsel_mask);
654             }
655         }
656
657         // $$$ begin - added VS Renganthan 17 Oct 2K
658         fgUpdateDCS();
659         // $$$ end - added VS Renganthan 17 Oct 2K
660
661 # ifdef FG_NETWORK_OLK
662         if ( fgGetBool("/sim/networking/network-olk") ) {
663             sgCoord fgdpos;
664             other = head->next;             /* put listpointer to start  */
665             while ( other != tail) {        /* display all except myself */
666                 if ( strcmp( other->ipadr, fgd_mcp_ip) != 0) {
667                     other->fgd_sel->select(1);
668                     sgSetCoord( &fgdpos, other->sgFGD_COORD );
669                     other->fgd_pos->setTransform( &fgdpos );
670                 }
671                 other = other->next;
672             }
673
674             // fgd_sel->select(1);
675             // sgCopyMat4( sgTUX, current_view.sgVIEW);
676             // sgCoord fgdpos;
677             // sgSetCoord( &fgdpos, sgFGD_VIEW );
678             // fgd_pos->setTransform( &fgdpos);
679         }
680 # endif
681
682         // position tile nodes and update range selectors
683         global_tile_mgr.prep_ssg_nodes();
684
685         if ( fgGetBool("/sim/rendering/skyblend") ) {
686             // draw the sky backdrop
687
688             // we need a white diffuse light for the phase of the moon
689             ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, white );
690
691             thesky->preDraw();
692
693             // return to the desired diffuse color
694             ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, l->scene_diffuse );
695         }
696
697         // draw the ssg scene
698         glEnable( GL_DEPTH_TEST );
699         ssgCullAndDraw( scene );
700
701         // change state for lighting here
702
703         // draw lighting
704         // Set punch through fog density
705         glFogf (GL_FOG_DENSITY, fog_exp2_punch_through);
706
707         ssgCullAndDraw( lighting );
708
709         if ( fgGetBool("/sim/rendering/skyblend") ) {
710             // draw the sky cloud layers
711             thesky->postDraw( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER );
712         }
713
714         // display HUD && Panel
715         glDisable( GL_FOG );
716         glDisable( GL_DEPTH_TEST );
717         // glDisable( GL_CULL_FACE );
718         // glDisable( GL_TEXTURE_2D );
719
720         // update the controls subsystem
721         controls.update();
722
723         hud_and_panel->apply();
724         fgCockpitUpdate();
725
726         // update the panel subsystem
727         if (current_panel != 0)
728           current_panel->update();
729
730         // We can do translucent menus, so why not. :-)
731         menus->apply();
732         glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
733         puDisplay();
734         // glDisable ( GL_BLEND ) ;
735
736         // glEnable( GL_FOG );
737     }
738
739     glutSwapBuffers();
740 }
741
742
743 // Update internal time dependent calculations (i.e. flight model)
744 void fgUpdateTimeDepCalcs() {
745     static bool inited = false;
746
747     fgLIGHT *l = &cur_light_params;
748     int i;
749
750     long multi_loop = 1;
751
752     if ( !globals->get_freeze() && !initial_freeze ) {
753         // conceptually, this could be done for each fdm instance ...
754
755         if ( !inited ) {
756             cur_fdm_state->stamp();
757             inited = true;
758         }
759
760         SGTimeStamp current;
761         current.stamp();
762         long elapsed = current - cur_fdm_state->get_time_stamp();
763         cur_fdm_state->set_time_stamp( current );
764         elapsed += cur_fdm_state->get_remainder();
765         // cout << "elapsed = " << elapsed << endl;
766         // cout << "dt = " << cur_fdm_state->get_delta_t() << endl;
767         multi_loop = (long)(((double)elapsed * 0.000001) /
768                                cur_fdm_state->get_delta_t() );
769         cur_fdm_state->set_multi_loop( multi_loop );
770         long remainder = elapsed - (long)( (multi_loop*1000000) *
771                                      cur_fdm_state->get_delta_t() );
772         cur_fdm_state->set_remainder( remainder );
773         // cout << "remainder = " << remainder << endl;
774
775         // chop max interations to something reasonable if the sim was
776         // delayed for an excesive amount of time
777         if ( multi_loop > 2.0 / cur_fdm_state->get_delta_t() ) {
778             multi_loop = (int)(2.0 / cur_fdm_state->get_delta_t());
779             cur_fdm_state->set_remainder( 0 );
780         }
781
782         // cout << "multi_loop = " << multi_loop << endl;
783         for ( i = 0; i < multi_loop * fgGetInt("/sim/speed-up"); ++i ) {
784             // run Autopilot system
785             current_autopilot->run();
786
787             // update autopilot
788             cur_fdm_state->update( 1 );
789         }
790         FGSteam::update( multi_loop * fgGetInt("/sim/speed-up") );
791     } else {
792         cur_fdm_state->update( 0 );
793         FGSteam::update( 0 );
794
795         //if ( global_tile_mgr.queue_size() == 0 ) {
796             initial_freeze = false;
797             //}
798     }
799
800     if ( fgGetString("/sim/view-mode") == "pilot" ) {
801         cur_view_fdm = *cur_fdm_state;
802         // do nothing
803     }
804
805     // update the view angle
806     FGViewer *v = globals->get_current_view();
807     for ( i = 0; i < multi_loop; i++ ) {
808         if ( fabs(v->get_goal_view_offset() - v->get_view_offset()) < 0.05 ) {
809             v->set_view_offset( v->get_goal_view_offset() );
810             break;
811         } else {
812             // move current_view.view_offset towards
813             // current_view.goal_view_offset
814             if ( v->get_goal_view_offset() > v->get_view_offset() )
815             {
816                 if ( v->get_goal_view_offset() - v->get_view_offset() < SGD_PI ){
817                     v->inc_view_offset( 0.01 );
818                 } else {
819                     v->inc_view_offset( -0.01 );
820                 }
821             } else {
822                 if ( v->get_view_offset() - v->get_goal_view_offset() < SGD_PI ){
823                     v->inc_view_offset( -0.01 );
824                 } else {
825                     v->inc_view_offset( 0.01 );
826                 }
827             }
828             if ( v->get_view_offset() > SGD_2PI ) {
829                 v->inc_view_offset( -SGD_2PI );
830             } else if ( v->get_view_offset() < 0 ) {
831                 v->inc_view_offset( SGD_2PI );
832             }
833         }
834     }
835
836     double tmp = -(l->sun_rotation + SGD_PI) 
837         - (cur_fdm_state->get_Psi() -
838            globals->get_current_view()->get_view_offset() );
839     while ( tmp < 0.0 ) {
840         tmp += SGD_2PI;
841     }
842     while ( tmp > SGD_2PI ) {
843         tmp -= SGD_2PI;
844     }
845     /* printf("Psi = %.2f, viewoffset = %.2f sunrot = %.2f rottosun = %.2f\n",
846            FG_Psi * SGD_RADIANS_TO_DEGREES, current_view.view_offset * SGD_RADIANS_TO_DEGREES, 
847            -(l->sun_rotation+SGD_PI) * SGD_RADIANS_TO_DEGREES, tmp * SGD_RADIANS_TO_DEGREES); */
848     l->UpdateAdjFog();
849
850     // Update solar system
851     globals->get_ephem()->update( globals->get_time_params()->getMjd(),
852                                   globals->get_time_params()->getLst(),
853                                   cur_fdm_state->get_Latitude() );
854
855     // Update radio stack model
856     current_radiostack->update();
857 }
858
859
860 void fgInitTimeDepCalcs( void ) {
861     // initialize timer
862
863     // #ifdef HAVE_SETITIMER
864     //   fgTimerInit( 1.0 / fgGetInt("/sim/model-hz"), 
865     //                fgUpdateTimeDepCalcs );
866     // #endif HAVE_SETITIMER
867 }
868
869
870 static const double alt_adjust_ft = 3.758099;
871 static const double alt_adjust_m = alt_adjust_ft * SG_FEET_TO_METER;
872
873
874 // What should we do when we have nothing else to do?  Let's get ready
875 // for the next move and update the display?
876 static void fgMainLoop( void ) {
877     static long remainder = 0;
878     long elapsed;
879 #ifdef FANCY_FRAME_COUNTER
880     int i;
881     double accum;
882 #else
883     static time_t last_time = 0;
884     static int frames = 0;
885 #endif // FANCY_FRAME_COUNTER
886
887     SGTime *t = globals->get_time_params();
888
889     SG_LOG( SG_ALL, SG_DEBUG, "Running Main Loop");
890     SG_LOG( SG_ALL, SG_DEBUG, "======= ==== ====");
891
892 #ifdef FG_NETWORK_OLK
893     if ( fgGetBool("/sim/networking/network-olk") ) {
894         if ( net_is_registered == 0 ) {      // We first have to reg. to fgd
895             // printf("FGD: Netupdate\n");
896             fgd_send_com( "A", FGFS_host);   // Send Mat4 data
897             fgd_send_com( "B", FGFS_host);   // Recv Mat4 data
898         }
899     }
900 #endif
901
902 #if defined( ENABLE_PLIB_JOYSTICK )
903     // Read joystick and update control settings
904     if ( fgGetString("/sim/control-mode") == "joystick" )
905     {
906         fgJoystickRead();
907     }
908 #elif defined( ENABLE_GLUT_JOYSTICK )
909     // Glut joystick support works by feeding a joystick handler
910     // function to glut.  This is taken care of once in the joystick
911     // init routine and we don't have to worry about it again.
912 #endif
913
914 #ifdef FG_OLD_WEATHER
915     current_weather.Update();
916 #endif
917
918     // Fix elevation.  I'm just sticking this here for now, it should
919     // probably move eventually
920
921     /* printf("Before - ground = %.2f  runway = %.2f  alt = %.2f\n",
922            scenery.cur_elev,
923            cur_fdm_state->get_Runway_altitude() * SG_FEET_TO_METER,
924            cur_fdm_state->get_Altitude() * SG_FEET_TO_METER); */
925
926     if ( scenery.cur_elev > -9990 ) {
927         if ( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER < 
928              (scenery.cur_elev + alt_adjust_m - 3.0) ) {
929             // now set aircraft altitude above ground
930             printf("(*) Current Altitude = %.2f < %.2f forcing to %.2f\n", 
931                    cur_fdm_state->get_Altitude() * SG_FEET_TO_METER,
932                    scenery.cur_elev + alt_adjust_m - 3.0,
933                    scenery.cur_elev + alt_adjust_m );
934             fgFDMForceAltitude( fgGetString("/sim/flight-model"), 
935                                 scenery.cur_elev + alt_adjust_m );
936
937             SG_LOG( SG_ALL, SG_DEBUG, 
938                     "<*> resetting altitude to " 
939                     << cur_fdm_state->get_Altitude() * SG_FEET_TO_METER
940                     << " meters" );
941         }
942     }
943
944     /* printf("Adjustment - ground = %.2f  runway = %.2f  alt = %.2f\n",
945            scenery.cur_elev,
946            cur_fdm_state->get_Runway_altitude() * SG_FEET_TO_METER,
947            cur_fdm_state->get_Altitude() * SG_FEET_TO_METER); */
948
949     // update "time"
950     if ( globals->get_warp_delta() != 0 ) {
951         globals->inc_warp( globals->get_warp_delta() );
952     }
953
954     t->update( cur_fdm_state->get_Longitude(),
955                cur_fdm_state->get_Latitude(),
956                globals->get_warp() );
957
958     if ( globals->get_warp_delta() != 0 ) {
959         fgUpdateSkyAndLightingParams();
960     }
961
962     // update magvar model
963     globals->get_mag()->update( cur_fdm_state->get_Longitude(),
964                                 cur_fdm_state->get_Latitude(),
965                                 cur_fdm_state->get_Altitude()* SG_FEET_TO_METER,
966                                 globals->get_time_params()->getJD() );
967
968     // Get elapsed time (in usec) for this past frame
969     elapsed = fgGetTimeInterval();
970     SG_LOG( SG_ALL, SG_DEBUG, 
971             "Elapsed time interval is = " << elapsed 
972             << ", previous remainder is = " << remainder );
973
974     // Calculate frame rate average
975 #ifdef FANCY_FRAME_COUNTER
976     /* old fps calculation */
977     if ( elapsed > 0 ) {
978         double tmp;
979         accum = 0.0;
980         for ( i = FG_FRAME_RATE_HISTORY - 2; i >= 0; i-- ) {
981             tmp = general.get_frame(i);
982             accum += tmp;
983             // printf("frame[%d] = %.2f\n", i, g->frames[i]);
984             general.set_frame(i+1,tmp);
985         }
986         tmp = 1000000.0 / (float)elapsed;
987         general.set_frame(0,tmp);
988         // printf("frame[0] = %.2f\n", general.frames[0]);
989         accum += tmp;
990         general.set_frame_rate(accum / (float)FG_FRAME_RATE_HISTORY);
991         // printf("ave = %.2f\n", general.frame_rate);
992     }
993 #else
994     if ( (t->get_cur_time() != last_time) && (last_time > 0) ) {
995         general.set_frame_rate( frames );
996         SG_LOG( SG_ALL, SG_DEBUG, 
997                 "--> Frame rate is = " << general.get_frame_rate() );
998         frames = 0;
999     }
1000     last_time = t->get_cur_time();
1001     ++frames;
1002 #endif
1003
1004     // Run flight model
1005
1006     // Calculate model iterations needed for next frame
1007     elapsed += remainder;
1008
1009     global_multi_loop = (long)(((double)elapsed * 0.000001) * 
1010                               fgGetInt("/sim/model-hz"));
1011     remainder = elapsed - ( (global_multi_loop*1000000) / 
1012                             fgGetInt("/sim/model-hz") );
1013     SG_LOG( SG_ALL, SG_DEBUG, 
1014             "Model iterations needed = " << global_multi_loop
1015             << ", new remainder = " << remainder );
1016         
1017     // chop max interations to something reasonable if the sim was
1018     // delayed for an excesive amount of time
1019     if ( global_multi_loop > 2.0 * fgGetInt("/sim/model-hz") ) {
1020         global_multi_loop = (int)(2.0 * fgGetInt("/sim/model-hz") );
1021         remainder = 0;
1022     }
1023
1024     // flight model
1025     if ( global_multi_loop > 0 ) {
1026         fgUpdateTimeDepCalcs();
1027     } else {
1028         SG_LOG( SG_ALL, SG_DEBUG, 
1029                 "Elapsed time is zero ... we're zinging" );
1030     }
1031
1032 #if ! defined( macintosh )
1033     // Do any I/O channel work that might need to be done
1034     fgIOProcess();
1035 #endif
1036
1037     // see if we need to load any new scenery tiles
1038     global_tile_mgr.update( cur_fdm_state->get_Longitude()
1039                             * SGD_RADIANS_TO_DEGREES,
1040                             cur_fdm_state->get_Latitude()
1041                             * SGD_RADIANS_TO_DEGREES );
1042
1043     // see if we need to load any deferred-load textures
1044     material_lib.load_next_deferred();
1045
1046     // Process/manage pending events
1047     global_events.Process();
1048
1049     // Run audio scheduler
1050 #ifdef ENABLE_AUDIO_SUPPORT
1051     if ( fgGetBool("/sim/sound") && globals->get_soundmgr()->is_working() ) {
1052         if ( fgGetString("/sim/aircraft") == "c172" ) {
1053             // pitch corresponds to rpm
1054             // volume corresponds to manifold pressure
1055
1056             // cout << "AUDIO working = "
1057             //      << globals->get_soundmgr()->is_working() << endl;
1058
1059             double rpm_factor;
1060             if ( cur_fdm_state->get_engine(0) != NULL ) {
1061                 rpm_factor = cur_fdm_state->get_engine(0)->get_RPM() / 2500.0;
1062             } else {
1063                 rpm_factor = 1.0;
1064             }
1065             // cout << "rpm = " << cur_fdm_state->get_engine(0)->get_RPM()
1066             //      << endl;
1067
1068             double pitch = 0.3 + rpm_factor * 3.0;
1069         
1070             // don't run at absurdly slow rates -- not realistic
1071             // and sounds bad to boot.  :-)
1072             if (pitch < 0.7) { pitch = 0.7; }
1073             if (pitch > 5.0) { pitch = 5.0; }
1074
1075             double mp_factor;
1076             if ( cur_fdm_state->get_engine(0) != NULL ) {
1077                 mp_factor = 
1078                     cur_fdm_state->get_engine(0)->get_Manifold_Pressure() / 100;
1079             } else {
1080                 mp_factor = 0.3;
1081             }
1082             /* cout << "mp = " 
1083                  << cur_fdm_state->get_engine(0)->get_Manifold_Pressure()
1084                  << endl; */
1085
1086             double volume = 0.15 + mp_factor / 2.0;
1087
1088             if ( volume < 0.15 ) { volume = 0.15; }
1089             if ( volume > 0.5 ) { volume = 0.5; }
1090             // cout << "volume = " << volume << endl;
1091
1092             s1->set_pitch( pitch );
1093             s1->set_volume( volume );
1094         } else {
1095             double param = controls.get_throttle( 0 ) * 2.0 + 1.0;
1096             s1->set_pitch( param );
1097             s1->set_volume( param );
1098         }
1099
1100         globals->get_soundmgr()->update();
1101     }
1102 #endif
1103
1104     // redraw display
1105     fgRenderFrame();
1106
1107     SG_LOG( SG_ALL, SG_DEBUG, "" );
1108 }
1109
1110
1111 // This is the top level master main function that is registered as
1112 // our idle funciton
1113 //
1114
1115 // The first few passes take care of initialization things (a couple
1116 // per pass) and once everything has been initialized fgMainLoop from
1117 // then on.
1118
1119 static void fgIdleFunction ( void ) {
1120     // printf("idle state == %d\n", idle_state);
1121
1122     if ( idle_state == 0 ) {
1123         // Initialize the splash screen right away
1124         if ( fgGetBool("/sim/startup/splash-screen") ) {
1125             fgSplashInit();
1126         }
1127         
1128         idle_state++;
1129     } else if ( idle_state == 1 ) {
1130         // Start the intro music
1131 #if !defined(WIN32)
1132         if ( fgGetBool("/sim/startup/intro-music") ) {
1133             string lockfile = "/tmp/mpg123.running";
1134             SGPath mp3file( globals->get_fg_root() );
1135             mp3file.append( "Sounds/intro.mp3" );
1136
1137             string command = "(touch " + lockfile + "; mpg123 "
1138                 + mp3file.str() + "> /dev/null 2>&1; /bin/rm "
1139                 + lockfile + ") &";
1140             SG_LOG( SG_GENERAL, SG_INFO, 
1141                     "Starting intro music: " << mp3file.str() );
1142             system ( command.c_str() );
1143         }
1144 #endif
1145
1146         idle_state++;
1147     } else if ( idle_state == 2 ) {
1148         // These are a few miscellaneous things that aren't really
1149         // "subsystems" but still need to be initialized.
1150
1151 #ifdef USE_GLIDE
1152         if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
1153             grTexLodBiasValue ( GR_TMU0, 1.0 ) ;
1154         }
1155 #endif
1156
1157         idle_state++;
1158     } else if ( idle_state == 3 ) {
1159         // This is the top level init routine which calls all the
1160         // other subsystem initialization routines.  If you are adding
1161         // a subsystem to flightgear, its initialization call should
1162         // located in this routine.
1163         if( !fgInitSubsystems()) {
1164             SG_LOG( SG_GENERAL, SG_ALERT,
1165                     "Subsystem initializations failed ..." );
1166             exit(-1);
1167         }
1168
1169         idle_state++;
1170     } else if ( idle_state == 4 ) {
1171         // setup OpenGL view parameters
1172         fgInitVisuals();
1173
1174         idle_state++;
1175     } else if ( idle_state == 5 ) {
1176
1177         idle_state++;
1178     } else if ( idle_state == 6 ) {
1179         // Initialize audio support
1180 #ifdef ENABLE_AUDIO_SUPPORT
1181
1182 #if !defined(WIN32)
1183         if ( fgGetBool("/sim/startup/intro-music") ) {
1184             // Let's wait for mpg123 to finish
1185             string lockfile = "/tmp/mpg123.running";
1186             struct stat stat_buf;
1187
1188             SG_LOG( SG_GENERAL, SG_INFO, 
1189                     "Waiting for mpg123 player to finish ..." );
1190             while ( stat(lockfile.c_str(), &stat_buf) == 0 ) {
1191                 // file exist, wait ...
1192                 sleep(1);
1193                 SG_LOG( SG_GENERAL, SG_INFO, ".");
1194             }
1195             SG_LOG( SG_GENERAL, SG_INFO, "");
1196         }
1197 #endif // WIN32
1198
1199         if ( fgGetBool("/sim/sound") ) {
1200             globals->get_soundmgr()->init();
1201
1202             s1 = new FGSimpleSound( fgGetString("/sim/sounds/engine",
1203                                                 "Sounds/wasp.wav") );
1204             globals->get_soundmgr()->add( s1, "engine loop" );
1205             globals->get_soundmgr()->play_looped( "engine loop" );
1206             SG_LOG( SG_GENERAL, SG_INFO,
1207                     "Rate = " << s1->get_sample()->getRate()
1208                     << "  Bps = " << s1->get_sample()->getBps()
1209                     << "  Stereo = " << s1->get_sample()->getStereo() );
1210
1211             s2 = new FGSimpleSound( "Sounds/corflaps.wav" );
1212             // FGMorse mmm;
1213             // mmm.init();
1214             // s2 = mmm.make_ident( "JLI" );
1215             s2->set_volume( 0.3 );
1216             globals->get_soundmgr()->add( s2, "flaps" );
1217         }
1218 #endif
1219
1220         // sleep(1);
1221         idle_state = 1000;
1222
1223         cout << "Panel visible = " << fgPanelVisible() << endl;
1224         fgReshape( fgGetInt("/sim/startup/xsize"),
1225                    fgGetInt("/sim/startup/ysize") );
1226     } 
1227
1228     if ( idle_state == 1000 ) {
1229         // We've finished all our initialization steps, from now on we
1230         // run the main loop.
1231
1232         fgMainLoop();
1233     } else {
1234         if ( fgGetBool("/sim/startup/splash-screen") ) {
1235             fgSplashUpdate(0.0);
1236         }
1237     }
1238 }
1239
1240 // options.cxx needs to see this for toggle_panel()
1241 // Handle new window size or exposure
1242 void fgReshape( int width, int height ) {
1243     // for all views
1244     for ( int i = 0; i < globals->get_viewmgr()->size(); ++i ) {
1245         if ( ! fgPanelVisible() || idle_state != 1000 ) {
1246             globals->get_viewmgr()->get_view(i)->
1247                 set_win_ratio( (float)height / (float)width );
1248         } else {
1249             int view_h =
1250                 int((current_panel->getViewHeight() -
1251                      current_panel->getYOffset())
1252                     * (height / 768.0)) + 1;
1253             globals->get_viewmgr()->get_view(i)->
1254                 set_win_ratio( (float)view_h / (float)width );
1255         }
1256     }
1257
1258     if ( ! fgPanelVisible() || idle_state != 1000 ) {
1259         glViewport(0, 0 , (GLint)(width), (GLint)(height) );
1260     } else {
1261         int view_h =
1262             int((current_panel->getViewHeight() - current_panel->getYOffset())
1263                 * (height / 768.0)) + 1;
1264         glViewport(0, (GLint)(height - view_h),
1265                    (GLint)(width), (GLint)(view_h) );
1266     }
1267
1268     fgSetInt("/sim/startup/xsize", width);
1269     fgSetInt("/sim/startup/ysize", height);
1270
1271     float fov = globals->get_current_view()->get_fov();
1272     ssgSetFOV(fov, fov * globals->get_current_view()->get_win_ratio());
1273
1274     fgHUDReshape();
1275 }
1276
1277
1278 // Initialize GLUT and define a main window
1279 int fgGlutInit( int *argc, char **argv ) {
1280
1281 #if !defined( macintosh )
1282     // GLUT will extract all glut specific options so later on we only
1283     // need wory about our own.
1284     glutInit(argc, argv);
1285 #endif
1286
1287     // Define Display Parameters
1288     glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
1289
1290     SG_LOG( SG_GENERAL, SG_INFO, "Opening a window: " <<
1291             fgGetInt("/sim/startup/xsize") << "x"
1292             << fgGetInt("/sim/startup/ysize") );
1293
1294     // Define initial window size
1295     glutInitWindowSize( fgGetInt("/sim/startup/xsize"),
1296                         fgGetInt("/sim/startup/ysize") );
1297
1298     // Initialize windows
1299     if ( !fgGetBool("/sim/startup/game-mode")) {
1300         // Open the regular window
1301         glutCreateWindow("FlightGear");
1302 #ifndef GLUT_WRONG_VERSION
1303     } else {
1304         // Open the cool new 'game mode' window
1305         char game_mode_str[256];
1306         sprintf( game_mode_str, "width=%d height=%d bpp=%d",
1307                  fgGetInt("/sim/startup/xsize"),
1308                  fgGetInt("/sim/startup/ysize"),
1309                  fgGetInt("/sim/rendering/bits-per-pixel"));
1310
1311         SG_LOG( SG_GENERAL, SG_INFO, 
1312                 "game mode params = " << game_mode_str );
1313         glutGameModeString( game_mode_str );
1314         glutEnterGameMode();
1315 #endif
1316     }
1317
1318     // This seems to be the absolute earliest in the init sequence
1319     // that these calls will return valid info.  Too bad it's after
1320     // we've already created and sized out window. :-(
1321     general.set_glVendor( (char *)glGetString ( GL_VENDOR ) );
1322     general.set_glRenderer( (char *)glGetString ( GL_RENDERER ) );
1323     general.set_glVersion( (char *)glGetString ( GL_VERSION ) );
1324     SG_LOG( SG_GENERAL, SG_INFO, general.get_glRenderer() );
1325
1326     GLint tmp;
1327     glGetIntegerv( GL_MAX_TEXTURE_SIZE, &tmp );
1328     general.set_glMaxTexSize( tmp );
1329     SG_LOG ( SG_GENERAL, SG_INFO, "Max texture size = " << tmp );
1330
1331     glGetIntegerv( GL_DEPTH_BITS, &tmp );
1332     general.set_glDepthBits( tmp );
1333     SG_LOG ( SG_GENERAL, SG_INFO, "Depth buffer bits = " << tmp );
1334
1335     return 1;
1336 }
1337
1338
1339 // Initialize GLUT event handlers
1340 int fgGlutInitEvents( void ) {
1341     // call fgReshape() on window resizes
1342     glutReshapeFunc( fgReshape );
1343
1344     // call GLUTkey() on keyboard event
1345     glutKeyboardFunc( GLUTkey );
1346     glutSpecialFunc( GLUTspecialkey );
1347
1348     // call guiMouseFunc() whenever our little rodent is used
1349     glutMouseFunc ( guiMouseFunc );
1350     glutMotionFunc (guiMotionFunc );
1351     glutPassiveMotionFunc (guiMotionFunc );
1352
1353     // call fgMainLoop() whenever there is
1354     // nothing else to do
1355     glutIdleFunc( fgIdleFunction );
1356
1357     // draw the scene
1358     glutDisplayFunc( fgRenderFrame );
1359
1360     return(1);
1361 }
1362
1363
1364 // Main ...
1365 int main( int argc, char **argv ) {
1366
1367 #if defined( macintosh )
1368     freopen ("stdout.txt", "w", stdout );
1369     freopen ("stderr.txt", "w", stderr );
1370     argc = ccommand( &argv );
1371 #endif
1372
1373 #ifdef HAVE_BC5PLUS
1374     _control87(MCW_EM, MCW_EM);  /* defined in float.h */
1375 #endif
1376
1377     // set default log levels
1378     sglog().setLogLevels( SG_ALL, SG_INFO );
1379
1380     string version;
1381 #ifdef FLIGHTGEAR_VERSION
1382     version = FLIGHTGEAR_VERSION;
1383 #else
1384     version = "unknown version";
1385 #endif
1386     SG_LOG( SG_GENERAL, SG_INFO, "FlightGear:  Version "
1387             << version << endl );
1388
1389     // Allocate global data structures.  This needs to happen before
1390     // we parse command line options
1391
1392     globals = new FGGlobals;
1393
1394     // seed the random number generater
1395     sg_srandom_time();
1396
1397     SGRoute *route = new SGRoute;
1398     globals->set_route( route );
1399
1400 #ifdef ENABLE_AUDIO_SUPPORT
1401     FGSoundMgr *soundmgr = new FGSoundMgr;
1402     globals->set_soundmgr( soundmgr );
1403 #endif
1404
1405     FGViewMgr *viewmgr = new FGViewMgr;
1406     globals->set_viewmgr( viewmgr );
1407
1408     FGViewerRPH *pv = new FGViewerRPH;
1409     globals->get_viewmgr()->add_view( pv );
1410
1411     FGViewerLookAt *chase = new FGViewerLookAt;
1412     globals->get_viewmgr()->add_view( chase );
1413
1414     string_list *col = new string_list;
1415     globals->set_channel_options_list( col );
1416
1417     // set current view to 0 (first) which is our main pilot view
1418     globals->set_current_view( globals->get_viewmgr()->get_view( 0 ) );
1419
1420     // Scan the config file(s) and command line options to see if
1421     // fg_root was specified (ignore all other options for now)
1422     fgInitFGRoot(argc, argv);
1423
1424     // Initialize the Aircraft directory to "" (UIUC)
1425     aircraft_dir = "";
1426
1427     // Load the configuration parameters
1428     if ( !fgInitConfig(argc, argv) ) {
1429         SG_LOG( SG_GENERAL, SG_ALERT, "Config option parsing failed ..." );
1430         exit(-1);
1431     }
1432
1433     // Initialize the Window/Graphics environment.
1434     if( !fgGlutInit(&argc, argv) ) {
1435         SG_LOG( SG_GENERAL, SG_ALERT, "GLUT initialization failed ..." );
1436         exit(-1);
1437     }
1438
1439     // Initialize the various GLUT Event Handlers.
1440     if( !fgGlutInitEvents() ) {
1441         SG_LOG( SG_GENERAL, SG_ALERT, 
1442                 "GLUT event handler initialization failed ..." );
1443         exit(-1);
1444     }
1445  
1446     // Initialize ssg (from plib).  Needs to come before we do any
1447     // other ssg stuff, but after opengl/glut has been initialized.
1448     ssgInit();
1449
1450     // Initialize the user interface (we need to do this before
1451     // passing off control to glut and before fgInitGeneral to get our
1452     // fonts !!!
1453     guiInit();
1454
1455 #ifdef GL_EXT_texture_lod_bias
1456     glTexEnvf( GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, -0.5 ) ;
1457 #endif
1458
1459 #if 0
1460 #ifdef GL_EXT_texture_filter_anisotropic
1461     float max_anisotropy;
1462     glGetFloatv( GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_anisotropy );
1463     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT,
1464                      max_anisotropy );
1465     cout << "Max anisotropy = " << max_anisotropy << endl;
1466 #endif
1467 #endif
1468
1469     // set current_options lon/lat if an airport id is specified
1470     // cout << "3. airport_id = " << fgGetString("/sim/startup/airport-id") << endl;
1471     if ( fgGetString("/sim/startup/airport-id").length() ) {
1472         // fgSetPosFromAirportID( fgGetString("/sim/startup/airport-id") );
1473         fgSetPosFromAirportIDandHdg( fgGetString("/sim/startup/airport-id"),
1474                                      fgGetDouble("/orientation/heading") );
1475     }
1476
1477     // Initialize time
1478     SGPath zone( globals->get_fg_root() );
1479     zone.append( "Timezone" );
1480     SGTime *t = new SGTime( fgGetDouble("/position/longitude") * SGD_DEGREES_TO_RADIANS,
1481                             fgGetDouble("/position/latitude") * SGD_DEGREES_TO_RADIANS,
1482                             zone.str() );
1483
1484     // Handle potential user specified time offsets
1485     time_t cur_time = t->get_cur_time();
1486     time_t currGMT = sgTimeGetGMT( gmtime(&cur_time) );
1487     time_t systemLocalTime = sgTimeGetGMT( localtime(&cur_time) );
1488     time_t aircraftLocalTime = 
1489         sgTimeGetGMT( fgLocaltime(&cur_time, t->get_zonename() ) );
1490
1491     // Okay, we now have six possible scenarios
1492     int offset = fgGetInt("/sim/startup/time-offset");
1493     const string &offset_type = fgGetString("/sim/startup/time-offset-type");
1494     if (offset_type == "system-offset") {
1495         globals->set_warp( offset );
1496     } else if (offset_type == "gmt-offset") {
1497         globals->set_warp( offset - (currGMT - systemLocalTime) );
1498     } else if (offset_type == "latitude-offset") {
1499         globals->set_warp( offset - (aircraftLocalTime - systemLocalTime) );
1500     } else if (offset_type == "system") {
1501         globals->set_warp( offset - cur_time );
1502     } else if (offset_type == "gmt") {
1503         globals->set_warp( offset - currGMT );
1504     } else if (offset_type == "latitude") {
1505         globals->set_warp( offset - (aircraftLocalTime - systemLocalTime) - 
1506                            cur_time ); 
1507     } else {
1508         SG_LOG( SG_GENERAL, SG_ALERT,
1509                 "Unsupported offset type " << offset_type );
1510         exit( -1 );
1511     }
1512
1513     SG_LOG( SG_GENERAL, SG_INFO, "After time init, warp = " 
1514             << globals->get_warp() );
1515
1516     globals->set_warp_delta( 0 );
1517
1518     t->update( 0.0, 0.0, globals->get_warp() );
1519
1520     globals->set_time_params( t );
1521
1522     // Do some quick general initializations
1523     if( !fgInitGeneral()) {
1524         SG_LOG( SG_GENERAL, SG_ALERT, 
1525                 "General initializations failed ..." );
1526         exit(-1);
1527     }
1528
1529     SGPath modelpath( globals->get_fg_root() );
1530     ssgModelPath( (char *)modelpath.c_str() );
1531   
1532     // Scene graph root
1533     scene = new ssgRoot;
1534     scene->setName( "Scene" );
1535
1536     lighting = new ssgRoot;
1537     lighting->setName( "Lighting" );
1538
1539     // Initialize the sky
1540     SGPath ephem_data_path( globals->get_fg_root() );
1541     ephem_data_path.append( "Astro" );
1542     SGEphemeris *ephem = new SGEphemeris( ephem_data_path.c_str() );
1543     ephem->update( globals->get_time_params()->getMjd(),
1544                    globals->get_time_params()->getLst(),
1545                    0.0 );
1546     globals->set_ephem( ephem );
1547
1548     thesky = new SGSky;
1549
1550     SGPath sky_tex_path( globals->get_fg_root() );
1551     sky_tex_path.append( "Textures" );
1552     sky_tex_path.append( "Sky" );
1553     thesky->texture_path( sky_tex_path.str() );
1554
1555     thesky->build( 550.0, 550.0,
1556                    globals->get_ephem()->getNumPlanets(), 
1557                    globals->get_ephem()->getPlanets(), 60000.0,
1558                    globals->get_ephem()->getNumStars(),
1559                    globals->get_ephem()->getStars(), 60000.0 );
1560
1561     if ( fgGetBool("/environment/clouds/status") ) {
1562         // thesky->add_cloud_layer( 2000.0, 200.0, 50.0, 40000.0,
1563         //                          SG_CLOUD_OVERCAST );
1564         thesky->add_cloud_layer( 2600.0, 200.0, 50.0, 40000.0,
1565                                  SG_CLOUD_MOSTLY_CLOUDY );
1566         // thesky->add_cloud_layer( 3000.0, 200.0, 50.0, 40000.0,
1567         //                          SG_CLOUD_MOSTLY_SUNNY );
1568         thesky->add_cloud_layer( 6000.0, 20.0, 10.0, 40000.0,
1569                                  SG_CLOUD_CIRRUS );
1570     }
1571
1572     // Initialize MagVar model
1573     SGMagVar *magvar = new SGMagVar();
1574     globals->set_mag( magvar );
1575
1576     // Terrain branch
1577     terrain = new ssgBranch;
1578     terrain->setName( "Terrain" );
1579     scene->addKid( terrain );
1580
1581     // Lighting
1582     ground = new ssgBranch;
1583     ground->setName( "Ground Lighting" );
1584     lighting->addKid( ground );
1585
1586     airport = new ssgBranch;
1587     airport->setName( "Airport Lighting" );
1588     lighting->addKid( airport );
1589
1590     // temporary visible aircraft "own ship"
1591     acmodel_selector = new ssgSelector;
1592     acmodel_pos = new ssgTransform;
1593
1594     // Get the model location, and load textures from the same
1595     // directory.  Use an absolute path for the model to avoid
1596     // incompatibilities in different versions of PLIB.
1597     string acmodel_path =
1598         fgGetString("/sim/model/path", "Models/Geometry/glider.ac");
1599     SGPath full_model = globals->get_fg_root();
1600     full_model.append(acmodel_path);
1601
1602 #if !defined( PLIB_1_2_X )
1603     // this should be redundant ... but it breaks for relative paths
1604     // ssgModelPath( (char *)full_model.dir().c_str() );
1605 #endif
1606
1607     ssgTexturePath( (char *)full_model.dir().c_str() );
1608     ssgEntity *acmodel_obj = ssgLoad( (char *)full_model.c_str() );
1609     if( !acmodel_obj ) {
1610         // fall back to default
1611         acmodel_obj = ssgLoad( (char *)"Models/Geometry/glider.ac" );
1612         if( !acmodel_obj ) {
1613             SG_LOG( SG_GENERAL, SG_ALERT, "FAILED to LOAD an AC model! ..." );
1614             exit(-1);
1615         }
1616     }
1617
1618     // find moving parts (if this is an MDL model)
1619     flaps_selector = (ssgSelector*)fgFindNode( acmodel_obj, "FLAPS" );
1620     prop_selector  = (ssgSelector*)fgFindNode( acmodel_obj, "PROP"  );
1621
1622     acmodel_npropsettings = 0;
1623     if (prop_selector != NULL) {
1624       for (ssgEntity* kid = prop_selector->getKid(0); kid != NULL;
1625            kid = prop_selector->getNextKid()) {
1626         int prop_low, prop_high;
1627         if ( sscanf(kid->getName(), "PROP_%d_%d", 
1628                     &prop_low, &prop_high) == 2 ) {
1629           prop_low  = (int)((float)prop_low  * (5000.0f / 32767.0f));
1630           prop_high = (int)((float)prop_high * (5000.0f / 32767.0f));
1631           acmodel_proprpms[acmodel_npropsettings][0] = prop_low ;
1632           acmodel_proprpms[acmodel_npropsettings][1] = prop_high;
1633           acmodel_npropsettings++;
1634
1635           SG_LOG( SG_GENERAL, SG_INFO, "PROPELLER SETTING " << prop_low <<
1636                   " " << prop_high );
1637         }
1638       }
1639     }
1640
1641     // align the model properly for FGFS
1642     ssgTransform *acmodel_align = new ssgTransform;
1643     acmodel_align->addKid(acmodel_obj);
1644     sgMat4 rot_matrix;
1645     sgMat4 off_matrix;
1646     sgMat4 res_matrix;
1647     float h_rot = fgGetFloat("/sim/model/h-rotation", 0.0);
1648     float p_rot = fgGetFloat("/sim/model/p-rotation", 0.0);
1649     float r_rot = fgGetFloat("/sim/model/r-rotation", 0.0);
1650     float x_off = fgGetFloat("/sim/model/x-offset", 0.0);
1651     float y_off = fgGetFloat("/sim/model/y-offset", 0.0);
1652     float z_off = fgGetFloat("/sim/model/z-offset", 0.0);
1653     sgMakeRotMat4(rot_matrix, h_rot, p_rot, r_rot);
1654     sgMakeTransMat4(off_matrix, x_off, y_off, z_off);
1655     sgMultMat4(res_matrix, off_matrix, rot_matrix);
1656     acmodel_align->setTransform(res_matrix);
1657
1658     acmodel_pos->addKid( acmodel_align );
1659     acmodel_selector->addKid( acmodel_pos );
1660     //ssgFlatten( acmodel_obj );
1661     //ssgStripify( acmodel_selector );
1662     acmodel_selector->clrTraversalMaskBits( SSGTRAV_HOT );
1663     scene->addKid( acmodel_selector );
1664
1665     // $$$ begin - added VS Renganthan 17 Oct 2K
1666     fgLoadDCS();
1667     // $$$ end - added VS Renganthan 17 Oct 2K
1668
1669 #ifdef FG_NETWORK_OLK
1670     // Do the network intialization
1671     if ( fgGetBool("/sim/networking/network-olk") ) {
1672         printf("Multipilot mode %s\n", fg_net_init( scene ) );
1673     }
1674 #endif
1675
1676     // build our custom render states
1677     fgBuildRenderStates();
1678
1679     // pass control off to the master GLUT event handler
1680     glutMainLoop();
1681
1682     // we never actually get here ... but to avoid compiler warnings,
1683     // etc.
1684     return 0;
1685 }
1686
1687
1688 // $$$ end - added VS Renganathan, 15 Oct 2K
1689 //         - added Venky         , 12 Nov 2K
1690
1691 void fgLoadDCS(void) {
1692
1693     ssgEntity *ship_obj = NULL;
1694     // double bz[3];
1695     // int j=0;
1696     char obj_filename[25];
1697
1698     for ( int k = 0; k < 32; k++ ) {
1699         ship_pos[k]=NULL;
1700     }
1701
1702     SGPath tile_path( globals->get_fg_root());
1703     tile_path.append( "Scenery" );
1704     tile_path.append( "Objects.txt" );
1705     sg_gzifstream in( tile_path.str() );
1706     if ( ! in.is_open() ) {
1707         SG_LOG( SG_TERRAIN, SG_ALERT, "Cannot open file: " << tile_path.str() );
1708     } else {
1709
1710         SGPath modelpath( globals->get_fg_root() );
1711         modelpath.append( "Models" );
1712         modelpath.append( "Geometry" );
1713   
1714         SGPath texturepath( globals->get_fg_root() );
1715         texturepath.append( "Models" );
1716         texturepath.append( "Textures" );
1717  
1718         ssgModelPath( (char *)modelpath.c_str() );
1719         ssgTexturePath( (char *)texturepath.c_str() );
1720
1721         ship_sel = new ssgSelector;
1722
1723         char c;
1724         while ( ! in.eof() ) {
1725             in >> ::skipws;
1726             if ( in.get( c ) && c == '#' ) { 
1727                 in >> skipeol;
1728             } else { 
1729                 in.putback(c);
1730                 in >> obj_filename >> obj_lat[objc] >> obj_lon[objc] >> obj_alt[objc];
1731                 /* cout << endl << obj_filename << " " << obj_lat[objc] << " " << obj_lon[objc] <<  " " << obj_alt[objc] << endl;
1732                    int chj=getchar();*/
1733                 
1734                 obj_lon[objc] *=SGD_DEGREES_TO_RADIANS;
1735                 obj_lat[objc] *=SGD_DEGREES_TO_RADIANS;
1736                 
1737                 ship_pos[objc] = new ssgTransform;
1738        
1739                 // type "repeat" in objects.txt to load one more
1740                 // instance of the last object.
1741
1742                 if ( strcmp(obj_filename,"repeat") != 0) {
1743                     ship_obj = ssgLoadOBJ( obj_filename );
1744                 }
1745       
1746                 if ( ship_obj != NULL ) {
1747                     ship_pos[objc]->addKid( ship_obj ); // add object to transform node
1748                     ship_sel->addKid( ship_pos[objc] ); // add transform node to selector
1749                 } else {
1750                     SG_LOG( SG_TERRAIN, SG_ALERT, "Cannot open file: "
1751                             << obj_filename );
1752                 }
1753             
1754                 objc++;
1755
1756                 if (in.eof()) break;
1757             }
1758         } // while
1759
1760         SG_LOG ( SG_TERRAIN, SG_ALERT, "Finished object processing." );
1761
1762         ship_sel->clrTraversalMaskBits( SSGTRAV_HOT );
1763         scene->addKid( ship_sel ); //add selector node to root node 
1764     }
1765
1766     return;
1767  }
1768
1769
1770 void fgUpdateDCS (void) {
1771
1772     // double eye_lat,eye_lon,eye_alt;
1773     // static double obj_head;
1774     double sl_radius,obj_latgc;
1775     // float nresultmat[4][4];
1776     // sgMat4 Trans,rothead,rotlon,rot180,rotlat,resultmat1,resultmat2,resultmat3;
1777     double bz[3];
1778
1779     // Instantaneous Geodetic Lat/Lon/Alt of moving object
1780     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
1781     
1782     // Deck should be the first object in objects.txt in case of fdm=ada
1783
1784     if (fgGetString("/sim/flight-model") == "ada") {
1785         obj_lon[0] = fdm->get_aux5()*SGD_DEGREES_TO_RADIANS;
1786         obj_lat[0] = fdm->get_aux6()*SGD_DEGREES_TO_RADIANS;
1787         obj_alt[0] = fdm->get_aux7();
1788     }
1789     
1790     for ( int m = 0; m < objc; m++ ) {
1791         //cout << endl <<  obj_lat[m]*SGD_RADIANS_TO_DEGREES << " " << obj_lon[m]*SGD_RADIANS_TO_DEGREES << " " << obj_alt[m] << " " << objc << endl;
1792         //int v=getchar();
1793
1794         //Geodetic to Geocentric angles for rotation
1795         sgGeodToGeoc(obj_lat[m],obj_alt[m],&sl_radius,&obj_latgc);
1796
1797         //moving object gbs-posn in cartesian coords
1798         Point3D obj_posn = Point3D( obj_lon[m],obj_lat[m],obj_alt[m]);
1799         Point3D obj_pos = sgGeodToCart( obj_posn );
1800
1801         // Translate moving object w.r.t eye
1802         Point3D Objtrans = obj_pos-scenery.center;
1803         bz[0]=Objtrans.x();
1804         bz[1]=Objtrans.y();
1805         bz[2]=Objtrans.z();
1806
1807        // rotate dynamic objects for lat,lon & alt and other motion about its axes
1808         
1809         sgMat4 sgTRANS;
1810         sgMakeTransMat4( sgTRANS, bz[0],bz[1],bz[2]);
1811
1812         sgVec3 ship_fwd,ship_rt,ship_up;
1813         sgSetVec3( ship_fwd, 1.0, 0.0, 0.0);//east,roll
1814         sgSetVec3( ship_rt, 0.0, 1.0, 0.0);//up,pitch
1815         sgSetVec3( ship_up, 0.0, 0.0, 1.0); //north,yaw
1816
1817         sgMat4 sgROT_lon, sgROT_lat, sgROT_hdg;
1818         sgMakeRotMat4( sgROT_lon, obj_lon[m]*SGD_RADIANS_TO_DEGREES, ship_up );
1819         sgMakeRotMat4( sgROT_lat, 90-obj_latgc*SGD_RADIANS_TO_DEGREES, ship_rt );
1820         sgMakeRotMat4( sgROT_hdg, 180.0, ship_up );
1821         
1822         sgMat4 sgTUX;
1823         sgCopyMat4( sgTUX, sgROT_hdg );
1824         sgPostMultMat4( sgTUX, sgROT_lat );
1825         sgPostMultMat4( sgTUX, sgROT_lon );
1826         sgPostMultMat4( sgTUX, sgTRANS );
1827
1828         sgCoord shippos;
1829         sgSetCoord(&shippos, sgTUX );
1830         ship_pos[m]->setTransform( &shippos );
1831     }
1832     if ( ship_sel != NULL ) {
1833         ship_sel->select(0xFFFFFFFF);
1834     }
1835 }
1836
1837 // $$$ end - added VS Renganathan, 15 Oct 2K
1838 //           added Venky         , 12 Nov 2K