1 // main.cxx -- top level sim routines
3 // Written by Curtis Olson for OpenGL, started May 1997.
5 // Copyright (C) 1997 - 1999 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
28 #ifdef FG_MATH_EXCEPTION_CLASH
38 #include <simgear/xgl/xgl.h>
48 #ifdef HAVE_SYS_STAT_H
49 # include <sys/stat.h> // for stat()
53 # include <unistd.h> // for stat()
59 #ifdef ENABLE_AUDIO_SUPPORT
64 #include <simgear/constants.h> // for VERSION
65 #include <simgear/debug/logstream.hxx>
66 #include <simgear/math/polar3d.hxx>
67 #include <simgear/math/fg_random.h>
68 #include <simgear/misc/fgpath.hxx>
69 #include <simgear/sky/sky.hxx>
70 #include <simgear/timing/sg_time.hxx>
71 #include <simgear/timing/lowleveltime.h>
73 #include <Include/general.hxx>
75 #include <Aircraft/aircraft.hxx>
77 #include <Autopilot/newauto.hxx>
78 #include <Cockpit/cockpit.hxx>
79 #include <Cockpit/radiostack.hxx>
80 #include <Cockpit/steam.hxx>
82 #include <FDM/UIUCModel/uiuc_aircraftdir.h>
84 #include <Joystick/joystick.hxx>
86 #include <NetworkOLK/network.h>
88 #include <Scenery/scenery.hxx>
89 #include <Scenery/tilemgr.hxx>
90 #include <Time/event.hxx>
91 #include <Time/fg_timer.hxx>
92 #include <Time/light.hxx>
93 #include <Time/sunpos.hxx>
94 #include <Time/tmp.hxx>
96 // $$$ begin - added VS Renganathan
97 #include <simgear/misc/fgstream.hxx>
98 #include <FDM/flight.hxx>
99 void fgLoadDCS (void);
100 void fgUpdateDCS (void);
101 ssgSelector *ship_sel = NULL;
102 ssgTransform *ship_pos = NULL;
103 //int totalDCSobj = 0;
104 // $$$ end - added VS Renganathan
106 #ifndef FG_OLD_WEATHER
107 # include <WeatherCM/FGLocalWeatherDatabase.h>
109 # include <Weather/weather.hxx>
115 #include "fg_init.hxx"
117 #include "globals.hxx"
118 #include "keyboard.hxx"
119 #include "splash.hxx"
121 // -dw- use custom sioux settings so I can see output window
124 # include <sioux.h> // settings for output window
126 # include <console.h>
130 // This is a record containing a bit of global housekeeping information
133 // Specify our current idle function state. This is used to run all
134 // our initializations out of the glutIdleLoop() so that we can get a
135 // splash screen up and running right away.
136 static int idle_state = 0;
137 static int global_multi_loop;
139 // attempt to avoid a large bounce at startup
140 static bool initial_freeze = true;
145 // forward declaration
146 void fgReshape( int width, int height );
148 // Global structures for the Audio library
149 #ifdef ENABLE_AUDIO_SUPPORT
150 slEnvelope pitch_envelope ( 1, SL_SAMPLE_ONE_SHOT ) ;
151 slEnvelope volume_envelope ( 1, SL_SAMPLE_ONE_SHOT ) ;
152 slScheduler *audio_sched;
153 smMixer *audio_mixer;
160 ssgRoot *scene = NULL;
161 ssgBranch *terrain = NULL;
162 ssgSelector *penguin_sel = NULL;
163 ssgTransform *penguin_pos = NULL;
165 #ifdef FG_NETWORK_OLK
166 ssgSelector *fgd_sel = NULL;
167 ssgTransform *fgd_pos = NULL;
171 // current fdm/position used for view
172 FGInterface cur_view_fdm;
178 sgMat4 copy_of_ssgOpenGLAxisSwapMatrix =
180 { 1.0f, 0.0f, 0.0f, 0.0f },
181 { 0.0f, 0.0f, -1.0f, 0.0f },
182 { 0.0f, 1.0f, 0.0f, 0.0f },
183 { 0.0f, 0.0f, 0.0f, 1.0f }
186 // The following defines flightgear options. Because glutlib will also
187 // want to parse its own options, those options must not be included here
188 // or they will get parsed by the main program option parser. Hence case
189 // is significant for any option added that might be in conflict with
192 // glutlib parses for:
194 // -direct (invalid in Win32)
198 // -indirect (invalid in Win32)
201 // Note that glutlib depends upon strings while this program's
202 // option parser wants only initial characters followed by numbers
207 ssgSimpleState *default_state;
208 ssgSimpleState *hud_and_panel;
209 ssgSimpleState *menus;
211 void fgBuildRenderStates( void ) {
212 default_state = new ssgSimpleState;
213 default_state->ref();
214 default_state->disable( GL_TEXTURE_2D );
215 default_state->enable( GL_CULL_FACE );
216 default_state->enable( GL_COLOR_MATERIAL );
217 default_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
218 default_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
219 default_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
220 default_state->disable( GL_BLEND );
221 default_state->disable( GL_ALPHA_TEST );
222 default_state->disable( GL_LIGHTING );
224 hud_and_panel = new ssgSimpleState;
225 hud_and_panel->ref();
226 hud_and_panel->disable( GL_CULL_FACE );
227 hud_and_panel->disable( GL_TEXTURE_2D );
228 hud_and_panel->disable( GL_LIGHTING );
229 hud_and_panel->enable( GL_BLEND );
231 menus = new ssgSimpleState;
233 menus->disable( GL_CULL_FACE );
234 menus->disable( GL_TEXTURE_2D );
235 menus->enable( GL_BLEND );
239 // fgInitVisuals() -- Initialize various GL/view parameters
240 void fgInitVisuals( void ) {
243 l = &cur_light_params;
245 #ifndef GLUT_WRONG_VERSION
246 // Go full screen if requested ...
247 if ( globals->get_options()->get_fullscreen() ) {
252 // If enabled, normal vectors specified with glNormal are scaled
253 // to unit length after transformation. See glNormal.
254 // glEnable( GL_NORMALIZE );
256 glEnable( GL_LIGHTING );
257 glEnable( GL_LIGHT0 );
258 glLightfv( GL_LIGHT0, GL_POSITION, l->sun_vec );
261 sgSetVec3( sunpos, l->sun_vec[0], l->sun_vec[1], l->sun_vec[2] );
262 ssgGetLight( 0 ) -> setPosition( sunpos );
264 // glFogi (GL_FOG_MODE, GL_LINEAR);
265 glFogi (GL_FOG_MODE, GL_EXP2);
266 if ( (globals->get_options()->get_fog() == 1) ||
267 (globals->get_options()->get_shading() == 0) ) {
268 // if fastest fog requested, or if flat shading force fastest
269 glHint ( GL_FOG_HINT, GL_FASTEST );
270 } else if ( globals->get_options()->get_fog() == 2 ) {
271 glHint ( GL_FOG_HINT, GL_NICEST );
273 if ( globals->get_options()->get_wireframe() ) {
275 glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
278 // This is the default anyways, but it can't hurt
279 glFrontFace ( GL_CCW );
282 // glEnable(GL_POINT_SMOOTH);
283 // glEnable(GL_LINE_SMOOTH);
284 // glEnable(GL_POLYGON_SMOOTH);
288 // Update all Visuals (redraws anything graphics related)
289 void fgRenderFrame( void ) {
293 fgLIGHT *l = &cur_light_params;
294 static double last_visibility = -9999;
297 // GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
298 // GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
299 // GLfloat terrain_color[4] = { 0.54, 0.44, 0.29, 1.0 };
300 // GLfloat mat_shininess[] = { 10.0 };
301 GLbitfield clear_mask;
303 if ( idle_state != 1000 ) {
304 // still initializing, draw the splash screen
305 if ( globals->get_options()->get_splash_screen() == 1 ) {
309 // idle_state is now 1000 meaning we've finished all our
310 // initializations and are running the main loop, so this will
311 // now work without seg faulting the system.
313 // printf("Ground = %.2f Altitude = %.2f\n", scenery.cur_elev,
314 // FG_Altitude * FEET_TO_METER);
316 // this is just a temporary hack, to make me understand Pui
317 // timerText -> setLabel (ctime (&t->cur_time));
320 // calculate our current position in cartesian space
321 scenery.center = scenery.next_center;
322 // printf("scenery center = %.2f %.2f %.2f\n", scenery.center.x(),
323 // scenery.center.y(), scenery.center.z());
325 FGViewerRPH *pilot_view =
326 (FGViewerRPH *)globals->get_viewmgr()->get_view( 0 );
328 pilot_view->set_geod_view_pos( cur_fdm_state->get_Longitude(),
329 cur_fdm_state->get_Lat_geocentric(),
330 cur_fdm_state->get_Altitude() *
332 pilot_view->set_sea_level_radius( cur_fdm_state->
333 get_Sea_level_radius() *
335 pilot_view->set_rph( cur_fdm_state->get_Phi(),
336 cur_fdm_state->get_Theta(),
337 cur_fdm_state->get_Psi() );
339 FGViewerLookAt *chase_view =
340 (FGViewerLookAt *)globals->get_viewmgr()->get_view( 1 );
342 sgVec3 po; // chase view pilot_offset
343 sgVec3 wup; // chase view world up
344 sgSetVec3( po, 0.0, 0.0, 50.0 );
345 sgCopyVec3( wup, pilot_view->get_world_up() );
346 sgMat4 CXFM; // chase view + pilot offset xform
348 chase_view->get_view_offset() * RAD_TO_DEG -
349 cur_fdm_state->get_Psi() * RAD_TO_DEG,
351 sgVec3 npo; // new pilot offset after rotation
352 sgXformVec3( po, po, pilot_view->get_UP() );
353 sgXformVec3( npo, po, CXFM );
355 chase_view->set_geod_view_pos( cur_fdm_state->get_Longitude(),
356 cur_fdm_state->get_Lat_geocentric(),
357 cur_fdm_state->get_Altitude() *
359 chase_view->set_sea_level_radius( cur_fdm_state->
360 get_Sea_level_radius() *
362 chase_view->set_pilot_offset( npo[0], npo[1], npo[2] );
363 chase_view->set_view_forward( pilot_view->get_view_pos() );
364 chase_view->set_view_up( wup );
368 sgCopyMat4( rph, pilot_view->get_VIEW() );
369 cout << "RPH Matrix = " << endl;
371 for ( i = 0; i < 4; i++ ) {
372 for ( j = 0; j < 4; j++ ) {
373 printf("%10.4f ", rph[i][j]);
379 sgCopyMat4( la, chase_view->get_VIEW() );
380 cout << "LookAt Matrix = " << endl;
381 for ( i = 0; i < 4; i++ ) {
382 for ( j = 0; j < 4; j++ ) {
383 printf("%10.4f ", la[i][j]);
390 if ( ! fgPanelVisible() ) {
392 (GLint)(globals->get_options()->get_xsize()),
393 (GLint)(globals->get_options()->get_ysize()) );
396 int( (current_panel->getViewHeight() -
397 current_panel->getYOffset())
398 * (globals->get_options()->get_ysize() / 768.0) );
400 (GLint)(globals->get_options()->get_ysize() - view_h),
401 (GLint)(globals->get_options()->get_xsize()),
405 // set the sun position
406 glLightfv( GL_LIGHT0, GL_POSITION, l->sun_vec );
408 clear_mask = GL_DEPTH_BUFFER_BIT;
409 if ( globals->get_options()->get_wireframe() ) {
410 clear_mask |= GL_COLOR_BUFFER_BIT;
413 if ( globals->get_options()->get_skyblend() ) {
414 if ( globals->get_options()->get_textures() ) {
415 // glClearColor(black[0], black[1], black[2], black[3]);
416 glClearColor(l->adj_fog_color[0], l->adj_fog_color[1],
417 l->adj_fog_color[2], l->adj_fog_color[3]);
418 clear_mask |= GL_COLOR_BUFFER_BIT;
421 glClearColor(l->sky_color[0], l->sky_color[1],
422 l->sky_color[2], l->sky_color[3]);
423 clear_mask |= GL_COLOR_BUFFER_BIT;
425 glClear( clear_mask );
427 // Tell GL we are switching to model view parameters
429 // I really should create a derived ssg node or use a call
430 // back or something so that I can draw the sky within the
431 // ssgCullAndDraw() function, but for now I just mimic what
432 // ssg does to set up the model view matrix
433 glMatrixMode(GL_MODELVIEW);
435 ssgSetCamera( (sgMat4)globals->get_current_view()->get_VIEW() );
437 // set the opengl state to known default values
438 default_state->force();
440 // update fog params if visibility has changed
441 #ifndef FG_OLD_WEATHER
442 thesky->set_visibility( WeatherDatabase->getWeatherVisibility() );
444 thesky->set_visibility( current_weather.get_visibility() );
447 thesky->modify_vis( cur_fdm_state->get_Altitude() * FEET_TO_METER,
449 ( global_multi_loop *
450 globals->get_options()->get_speed_up() ) /
451 (double)globals->get_options()->get_model_hz() );
453 double actual_visibility = thesky->get_visibility();
454 // cout << "actual visibility = " << actual_visibility << endl;
456 if ( actual_visibility != last_visibility ) {
457 last_visibility = actual_visibility;
459 // cout << "----> updating fog params" << endl;
461 GLfloat fog_exp_density;
462 GLfloat fog_exp2_density;
465 fog_exp_density = -log(0.01 / actual_visibility);
468 fog_exp2_density = sqrt( -log(0.01) ) / actual_visibility;
470 // Set correct opengl fog density
471 glFogf (GL_FOG_DENSITY, fog_exp2_density);
474 // update the sky dome
475 if ( globals->get_options()->get_skyblend() ) {
476 /* cout << "thesky->repaint() sky_color = "
477 << cur_light_params.sky_color[0] << " "
478 << cur_light_params.sky_color[1] << " "
479 << cur_light_params.sky_color[2] << " "
480 << cur_light_params.sky_color[3] << endl;
482 << cur_light_params.fog_color[0] << " "
483 << cur_light_params.fog_color[1] << " "
484 << cur_light_params.fog_color[2] << " "
485 << cur_light_params.fog_color[3] << endl;
486 cout << " sun_angle = " << cur_light_params.sun_angle
487 << " moon_angle = " << cur_light_params.moon_angle
489 thesky->repaint( cur_light_params.sky_color,
490 cur_light_params.adj_fog_color,
491 cur_light_params.sun_angle,
492 cur_light_params.moon_angle,
493 globals->get_ephem()->getNumPlanets(),
494 globals->get_ephem()->getPlanets(),
495 globals->get_ephem()->getNumStars(),
496 globals->get_ephem()->getStars() );
498 /* cout << "thesky->reposition( view_pos = " << view_pos[0] << " "
499 << view_pos[1] << " " << view_pos[2] << endl;
500 cout << " zero_elev = " << zero_elev[0] << " "
501 << zero_elev[1] << " " << zero_elev[2]
502 << " lon = " << cur_fdm_state->get_Longitude()
503 << " lat = " << cur_fdm_state->get_Latitude() << endl;
504 cout << " sun_rot = " << cur_light_params.sun_rotation
505 << " gst = " << SGTime::cur_time_params->getGst() << endl;
506 cout << " sun ra = " << globals->get_ephem()->getSunRightAscension()
507 << " sun dec = " << globals->get_ephem()->getSunDeclination()
508 << " moon ra = " << globals->get_ephem()->getMoonRightAscension()
509 << " moon dec = " << globals->get_ephem()->getMoonDeclination() << endl; */
511 thesky->reposition( globals->get_current_view()->get_view_pos(),
512 globals->get_current_view()->get_zero_elev(),
513 globals->get_current_view()->get_world_up(),
514 cur_fdm_state->get_Longitude(),
515 cur_fdm_state->get_Latitude(),
516 cur_fdm_state->get_Altitude() * FEET_TO_METER,
517 cur_light_params.sun_rotation,
518 globals->get_time_params()->getGst(),
519 globals->get_ephem()->getSunRightAscension(),
520 globals->get_ephem()->getSunDeclination(),
522 globals->get_ephem()->getMoonRightAscension(),
523 globals->get_ephem()->getMoonDeclination(),
527 glEnable( GL_DEPTH_TEST );
528 if ( globals->get_options()->get_fog() > 0 ) {
530 glFogi( GL_FOG_MODE, GL_EXP2 );
531 glFogfv( GL_FOG_COLOR, l->adj_fog_color );
534 // set lighting parameters
535 GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
536 glLightModelfv( GL_LIGHT_MODEL_AMBIENT, l->scene_ambient );
537 glLightfv( GL_LIGHT0, GL_AMBIENT, black );
538 glLightfv( GL_LIGHT0, GL_DIFFUSE, l->scene_diffuse );
539 // glLightfv(GL_LIGHT0, GL_SPECULAR, white );
541 // texture parameters
542 // glEnable( GL_TEXTURE_2D );
543 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
544 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ) ;
545 // set base color (I don't think this is doing anything here)
546 // glMaterialfv (GL_FRONT, GL_AMBIENT, white);
547 // (GL_FRONT, GL_DIFFUSE, white);
548 // glMaterialfv (GL_FRONT, GL_SPECULAR, white);
549 // glMaterialfv (GL_FRONT, GL_SHININESS, mat_shininess);
552 sgSetVec3( sunpos, l->sun_vec[0], l->sun_vec[1], l->sun_vec[2] );
553 ssgGetLight( 0 ) -> setPosition( sunpos );
555 // glMatrixMode( GL_PROJECTION );
557 float fov = globals->get_options()->get_fov();
558 ssgSetFOV(fov, fov * globals->get_options()->get_win_ratio());
560 double agl = current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER
563 // FG_LOG( FG_ALL, FG_INFO, "visibility is "
564 // << current_weather.get_visibility() );
567 ssgSetNearFar( 10.0f, 120000.0f );
569 ssgSetNearFar( 0.5f, 120000.0f );
572 if ( globals->get_viewmgr()->get_current() == 0 ) {
574 penguin_sel->select(0);
576 // enable TuX and set up his position and orientation
577 penguin_sel->select(1);
579 FGViewerRPH *pilot_view =
580 (FGViewerRPH *)globals->get_viewmgr()->get_view( 0 );
583 sgMakeTransMat4( sgTRANS, pilot_view->get_view_pos() );
586 sgSetVec3( ownship_up, 0.0, 0.0, 1.0);
589 sgMakeRotMat4( sgROT, -90.0, ownship_up );
593 // sgMultMat4( sgTMP, sgROT, pilot_view.VIEW_ROT );
594 // sgMultMat4( sgTUX, sgTMP, sgTRANS );
596 // sgTUX = ( sgROT * pilot_view.VIEW_ROT ) * sgTRANS
598 sgCopyMat4( sgTUX, sgROT );
599 sgPostMultMat4( sgTUX, pilot_view->get_VIEW_ROT() );
600 sgPostMultMat4( sgTUX, sgTRANS );
603 sgSetCoord( &tuxpos, sgTUX );
604 penguin_pos->setTransform( &tuxpos );
607 // $$$ begin - added VS Renganthan 17 Oct 2K
609 // $$$ end - added VS Renganthan 17 Oct 2K
611 # ifdef FG_NETWORK_OLK
612 if ( globals->get_options()->get_network_olk() ) {
614 other = head->next; /* put listpointer to start */
615 while ( other != tail) { /* display all except myself */
616 if ( strcmp( other->ipadr, fgd_mcp_ip) != 0) {
617 other->fgd_sel->select(1);
618 sgSetCoord( &fgdpos, other->sgFGD_COORD );
619 other->fgd_pos->setTransform( &fgdpos );
624 // fgd_sel->select(1);
625 // sgCopyMat4( sgTUX, current_view.sgVIEW);
627 // sgSetCoord( &fgdpos, sgFGD_VIEW );
628 // fgd_pos->setTransform( &fgdpos);
632 // position tile nodes and update range selectors
633 global_tile_mgr.prep_ssg_nodes();
635 // draw the sky backdrop
638 // draw the ssg scene
639 glEnable( GL_DEPTH_TEST );
640 ssgCullAndDraw( scene );
642 // draw the sky cloud layers
643 thesky->postDraw( cur_fdm_state->get_Altitude() * FEET_TO_METER );
645 // display HUD && Panel
647 glDisable( GL_DEPTH_TEST );
648 // glDisable( GL_CULL_FACE );
649 // glDisable( GL_TEXTURE_2D );
650 hud_and_panel->apply();
653 // We can do translucent menus, so why not. :-)
654 // glEnable ( GL_BLEND ) ;
656 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
658 // glDisable ( GL_BLEND ) ;
660 // glEnable( GL_FOG );
667 // Update internal time dependent calculations (i.e. flight model)
668 void fgUpdateTimeDepCalcs(int multi_loop, int remainder) {
669 static fdm_state_list fdm_list;
670 fgLIGHT *l = &cur_light_params;
673 // update the flight model
674 if ( multi_loop < 0 ) {
678 if ( !globals->get_freeze() && !initial_freeze ) {
679 // run Autopilot system
680 current_autopilot->run();
682 // printf("updating flight model x %d\n", multi_loop);
683 /* fgFDMUpdate( globals->get_options()->get_flight_model(),
685 multi_loop * globals->get_options()->get_speed_up(),
687 cur_fdm_state->update( multi_loop *
688 globals->get_options()->get_speed_up() );
689 FGSteam::update( multi_loop * globals->get_options()->get_speed_up() );
691 // fgFDMUpdate( globals->get_options()->get_flight_model(),
692 // fdm_state, 0, remainder );
693 cur_fdm_state->update( 0 );
694 FGSteam::update( 0 );
696 if ( global_tile_mgr.queue_size() == 0 ) {
697 initial_freeze = false;
701 fdm_list.push_back( *cur_fdm_state );
702 while ( fdm_list.size() > 15 ) {
703 fdm_list.pop_front();
706 if ( globals->get_options()->get_view_mode() == FGOptions::FG_VIEW_PILOT ) {
707 cur_view_fdm = *cur_fdm_state;
709 } else if ( globals->get_options()->get_view_mode()
710 == FGOptions::FG_VIEW_FOLLOW )
712 cur_view_fdm = fdm_list.front();
715 // update the view angle
716 FGViewer *v = globals->get_current_view();
717 for ( i = 0; i < multi_loop; i++ ) {
718 if ( fabs(v->get_goal_view_offset() - v->get_view_offset()) < 0.05 ) {
719 v->set_view_offset( v->get_goal_view_offset() );
722 // move current_view.view_offset towards
723 // current_view.goal_view_offset
724 if ( v->get_goal_view_offset() > v->get_view_offset() )
726 if ( v->get_goal_view_offset() - v->get_view_offset() < FG_PI ){
727 v->inc_view_offset( 0.01 );
729 v->inc_view_offset( -0.01 );
732 if ( v->get_view_offset() - v->get_goal_view_offset() < FG_PI ){
733 v->inc_view_offset( -0.01 );
735 v->inc_view_offset( 0.01 );
738 if ( v->get_view_offset() > FG_2PI ) {
739 v->inc_view_offset( -FG_2PI );
740 } else if ( v->get_view_offset() < 0 ) {
741 v->inc_view_offset( FG_2PI );
746 double tmp = -(l->sun_rotation + FG_PI)
747 - (cur_fdm_state->get_Psi() -
748 globals->get_current_view()->get_view_offset() );
749 while ( tmp < 0.0 ) {
752 while ( tmp > FG_2PI ) {
755 /* printf("Psi = %.2f, viewoffset = %.2f sunrot = %.2f rottosun = %.2f\n",
756 FG_Psi * RAD_TO_DEG, current_view.view_offset * RAD_TO_DEG,
757 -(l->sun_rotation+FG_PI) * RAD_TO_DEG, tmp * RAD_TO_DEG); */
760 // Update solar system
761 globals->get_ephem()->update( globals->get_time_params()->getMjd(),
762 globals->get_time_params()->getLst(),
763 cur_fdm_state->get_Latitude() );
765 // Update radio stack model
766 current_radiostack->update( cur_fdm_state->get_Longitude(),
767 cur_fdm_state->get_Latitude(),
768 cur_fdm_state->get_Altitude() * FEET_TO_METER );
772 void fgInitTimeDepCalcs( void ) {
775 // #ifdef HAVE_SETITIMER
776 // fgTimerInit( 1.0 / globals->get_options()->get_model_hz(),
777 // fgUpdateTimeDepCalcs );
778 // #endif HAVE_SETITIMER
782 static const double alt_adjust_ft = 3.758099;
783 static const double alt_adjust_m = alt_adjust_ft * FEET_TO_METER;
786 // What should we do when we have nothing else to do? Let's get ready
787 // for the next move and update the display?
788 static void fgMainLoop( void ) {
789 static long remainder = 0;
791 #ifdef FANCY_FRAME_COUNTER
795 static time_t last_time = 0;
796 static int frames = 0;
797 #endif // FANCY_FRAME_COUNTER
799 SGTime *t = globals->get_time_params();
801 FG_LOG( FG_ALL, FG_DEBUG, "Running Main Loop");
802 FG_LOG( FG_ALL, FG_DEBUG, "======= ==== ====");
804 #ifdef FG_NETWORK_OLK
805 if ( globals->get_options()->get_network_olk() ) {
806 if ( net_is_registered == 0 ) { // We first have to reg. to fgd
807 // printf("FGD: Netupdate\n");
808 fgd_send_com( "A", FGFS_host); // Send Mat4 data
809 fgd_send_com( "B", FGFS_host); // Recv Mat4 data
814 #if defined( ENABLE_PLIB_JOYSTICK )
815 // Read joystick and update control settings
816 if ( globals->get_options()->get_control_mode() == FGOptions::FG_JOYSTICK )
820 #elif defined( ENABLE_GLUT_JOYSTICK )
821 // Glut joystick support works by feeding a joystick handler
822 // function to glut. This is taken care of once in the joystick
823 // init routine and we don't have to worry about it again.
826 #ifdef FG_OLD_WEATHER
827 current_weather.Update();
830 // Fix elevation. I'm just sticking this here for now, it should
831 // probably move eventually
833 /* printf("Before - ground = %.2f runway = %.2f alt = %.2f\n",
835 cur_fdm_state->get_Runway_altitude() * FEET_TO_METER,
836 cur_fdm_state->get_Altitude() * FEET_TO_METER); */
838 if ( scenery.cur_elev > -9990 ) {
839 if ( cur_fdm_state->get_Altitude() * FEET_TO_METER <
840 (scenery.cur_elev + alt_adjust_m - 3.0) ) {
841 // now set aircraft altitude above ground
842 printf("(*) Current Altitude = %.2f < %.2f forcing to %.2f\n",
843 cur_fdm_state->get_Altitude() * FEET_TO_METER,
844 scenery.cur_elev + alt_adjust_m - 3.0,
845 scenery.cur_elev + alt_adjust_m );
846 fgFDMForceAltitude( globals->get_options()->get_flight_model(),
847 scenery.cur_elev + alt_adjust_m );
849 FG_LOG( FG_ALL, FG_DEBUG,
850 "<*> resetting altitude to "
851 << cur_fdm_state->get_Altitude() * FEET_TO_METER
854 //fgFDMSetGroundElevation( globals->get_options()->get_flight_model(),
855 // scenery.cur_elev ); // meters
858 /* printf("Adjustment - ground = %.2f runway = %.2f alt = %.2f\n",
860 cur_fdm_state->get_Runway_altitude() * FEET_TO_METER,
861 cur_fdm_state->get_Altitude() * FEET_TO_METER); */
864 if ( globals->get_warp_delta() != 0 ) {
865 globals->inc_warp( globals->get_warp_delta() );
868 t->update( cur_fdm_state->get_Longitude(),
869 cur_fdm_state->get_Latitude(),
870 globals->get_warp() );
872 if ( globals->get_warp_delta() != 0 ) {
873 fgUpdateSkyAndLightingParams();
876 // update magvar model
877 globals->get_mag()->update( cur_fdm_state->get_Longitude(),
878 cur_fdm_state->get_Latitude(),
879 cur_fdm_state->get_Altitude()* FEET_TO_METER,
880 globals->get_time_params()->getJD() );
882 // Get elapsed time (in usec) for this past frame
883 elapsed = fgGetTimeInterval();
884 FG_LOG( FG_ALL, FG_DEBUG,
885 "Elapsed time interval is = " << elapsed
886 << ", previous remainder is = " << remainder );
888 // Calculate frame rate average
889 #ifdef FANCY_FRAME_COUNTER
890 /* old fps calculation */
894 for ( i = FG_FRAME_RATE_HISTORY - 2; i >= 0; i-- ) {
895 tmp = general.get_frame(i);
897 // printf("frame[%d] = %.2f\n", i, g->frames[i]);
898 general.set_frame(i+1,tmp);
900 tmp = 1000000.0 / (float)elapsed;
901 general.set_frame(0,tmp);
902 // printf("frame[0] = %.2f\n", general.frames[0]);
904 general.set_frame_rate(accum / (float)FG_FRAME_RATE_HISTORY);
905 // printf("ave = %.2f\n", general.frame_rate);
908 if ( (t->get_cur_time() != last_time) && (last_time > 0) ) {
909 general.set_frame_rate( frames );
910 FG_LOG( FG_ALL, FG_DEBUG,
911 "--> Frame rate is = " << general.get_frame_rate() );
914 last_time = t->get_cur_time();
919 if ( ! use_signals ) {
920 // Calculate model iterations needed for next frame
921 elapsed += remainder;
923 global_multi_loop = (int)(((double)elapsed * 0.000001) *
924 globals->get_options()->get_model_hz());
925 remainder = elapsed - ( (global_multi_loop*1000000) /
926 globals->get_options()->get_model_hz() );
927 FG_LOG( FG_ALL, FG_DEBUG,
928 "Model iterations needed = " << global_multi_loop
929 << ", new remainder = " << remainder );
932 if ( global_multi_loop > 0 ) {
933 fgUpdateTimeDepCalcs(global_multi_loop, remainder);
935 FG_LOG( FG_ALL, FG_DEBUG,
936 "Elapsed time is zero ... we're zinging" );
940 #if ! defined( macintosh )
941 // Do any I/O channel work that might need to be done
945 // see if we need to load any new scenery tiles
946 global_tile_mgr.update( cur_fdm_state->get_Longitude() * RAD_TO_DEG,
947 cur_fdm_state->get_Latitude() * RAD_TO_DEG );
949 // Process/manage pending events
950 global_events.Process();
952 // Run audio scheduler
953 #ifdef ENABLE_AUDIO_SUPPORT
954 if ( globals->get_options()->get_sound() && !audio_sched->not_working() ) {
956 if ( globals->get_options()->get_aircraft() == "c172" ) {
957 // pitch corresponds to rpm
958 // volume corresponds to manifold pressure
961 if ( cur_fdm_state->get_engine(0) != NULL ) {
962 rpm_factor = cur_fdm_state->get_engine(0)->get_RPM() / 2500.0;
966 // cout << "rpm = " << cur_fdm_state->get_engine(0)->get_RPM()
969 double pitch = 0.3 + rpm_factor * 3.0;
971 // don't run at absurdly slow rates -- not realistic
972 // and sounds bad to boot. :-)
973 if (pitch < 0.7) { pitch = 0.7; }
974 if (pitch > 5.0) { pitch = 5.0; }
975 // cout << "pitch = " << pitch << endl;
978 if ( cur_fdm_state->get_engine(0) != NULL ) {
980 cur_fdm_state->get_engine(0)->get_Manifold_Pressure() / 28;
985 // << cur_fdm_state->get_engine(0)->get_Manifold_Pressure()
988 double volume = mp_factor;
990 if ( volume < 0.3 ) { volume = 0.3; }
991 if ( volume > 2.0 ) { volume = 2.0; }
992 // cout << "volume = " << volume << endl;
994 pitch_envelope.setStep ( 0, 0.01, pitch );
995 volume_envelope.setStep ( 0, 0.01, volume );
997 double param = controls.get_throttle( 0 ) * 2.0 + 1.0;
998 pitch_envelope.setStep ( 0, 0.01, param );
999 volume_envelope.setStep ( 0, 0.01, param );
1002 audio_sched -> update();
1009 FG_LOG( FG_ALL, FG_DEBUG, "" );
1013 // This is the top level master main function that is registered as
1014 // our idle funciton
1017 // The first few passes take care of initialization things (a couple
1018 // per pass) and once everything has been initialized fgMainLoop from
1021 static void fgIdleFunction ( void ) {
1022 // printf("idle state == %d\n", idle_state);
1024 if ( idle_state == 0 ) {
1025 // Initialize the splash screen right away
1026 if ( globals->get_options()->get_splash_screen() ) {
1031 } else if ( idle_state == 1 ) {
1032 // Start the intro music
1034 if ( globals->get_options()->get_intro_music() ) {
1035 string lockfile = "/tmp/mpg123.running";
1036 FGPath mp3file( globals->get_options()->get_fg_root() );
1037 mp3file.append( "Sounds/intro.mp3" );
1039 string command = "(touch " + lockfile + "; mpg123 "
1040 + mp3file.str() + "> /dev/null 2>&1; /bin/rm "
1042 FG_LOG( FG_GENERAL, FG_INFO,
1043 "Starting intro music: " << mp3file.str() );
1044 system ( command.c_str() );
1049 } else if ( idle_state == 2 ) {
1050 // These are a few miscellaneous things that aren't really
1051 // "subsystems" but still need to be initialized.
1054 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
1055 grTexLodBiasValue ( GR_TMU0, 1.0 ) ;
1060 } else if ( idle_state == 3 ) {
1061 // This is the top level init routine which calls all the
1062 // other subsystem initialization routines. If you are adding
1063 // a subsystem to flightgear, its initialization call should
1064 // located in this routine.
1065 if( !fgInitSubsystems()) {
1066 FG_LOG( FG_GENERAL, FG_ALERT,
1067 "Subsystem initializations failed ..." );
1072 } else if ( idle_state == 4 ) {
1073 // setup OpenGL view parameters
1076 if ( use_signals ) {
1077 // init timer routines, signals, etc. Arrange for an alarm
1078 // signal to be generated, etc.
1079 fgInitTimeDepCalcs();
1083 } else if ( idle_state == 5 ) {
1086 } else if ( idle_state == 6 ) {
1087 // Initialize audio support
1088 #ifdef ENABLE_AUDIO_SUPPORT
1091 if ( globals->get_options()->get_intro_music() ) {
1092 // Let's wait for mpg123 to finish
1093 string lockfile = "/tmp/mpg123.running";
1094 struct stat stat_buf;
1096 FG_LOG( FG_GENERAL, FG_INFO,
1097 "Waiting for mpg123 player to finish ..." );
1098 while ( stat(lockfile.c_str(), &stat_buf) == 0 ) {
1099 // file exist, wait ...
1101 FG_LOG( FG_GENERAL, FG_INFO, ".");
1103 FG_LOG( FG_GENERAL, FG_INFO, "");
1107 if ( globals->get_options()->get_sound() ) {
1108 audio_sched = new slScheduler ( 8000 );
1109 audio_mixer = new smMixer;
1110 audio_mixer -> setMasterVolume ( 80 ) ; /* 80% of max volume. */
1111 audio_sched -> setSafetyMargin ( 1.0 ) ;
1113 FGPath slfile( globals->get_options()->get_fg_root() );
1114 slfile.append( "Sounds/wasp.wav" );
1116 s1 = new slSample ( (char *)slfile.c_str() );
1117 FG_LOG( FG_GENERAL, FG_INFO,
1118 "Rate = " << s1 -> getRate()
1119 << " Bps = " << s1 -> getBps()
1120 << " Stereo = " << s1 -> getStereo() );
1121 audio_sched -> loopSample ( s1 );
1123 if ( audio_sched->not_working() ) {
1126 pitch_envelope.setStep ( 0, 0.01, 0.6 );
1127 volume_envelope.setStep ( 0, 0.01, 0.6 );
1129 audio_sched -> addSampleEnvelope( s1, 0, 0,
1131 SL_PITCH_ENVELOPE );
1132 audio_sched -> addSampleEnvelope( s1, 0, 1,
1134 SL_VOLUME_ENVELOPE );
1137 // strcpy(slfile, path);
1138 // strcat(slfile, "thunder.wav");
1139 // s2 -> loadFile ( slfile );
1140 // s2 -> adjustVolume(0.5);
1141 // audio_sched -> playSample ( s2 );
1148 cout << "Panel visible = " << fgPanelVisible() << endl;
1149 fgReshape( globals->get_options()->get_xsize(),
1150 globals->get_options()->get_ysize() );
1153 if ( idle_state == 1000 ) {
1154 // We've finished all our initialization steps, from now on we
1155 // run the main loop.
1159 if ( globals->get_options()->get_splash_screen() == 1 ) {
1160 fgSplashUpdate(0.0);
1165 // options.cxx needs to see this for toggle_panel()
1166 // Handle new window size or exposure
1167 void fgReshape( int width, int height ) {
1168 if ( ! fgPanelVisible() || idle_state != 1000 ) {
1169 globals->get_options()->set_win_ratio( (float)height /
1171 glViewport(0, 0 , (GLint)(width), (GLint)(height) );
1174 int((current_panel->getViewHeight() - current_panel->getYOffset())
1175 * (height / 768.0)) + 1;
1176 globals->get_options()->set_win_ratio( (float)view_h /
1178 glViewport(0, (GLint)(height - view_h),
1179 (GLint)(width), (GLint)(view_h) );
1182 globals->get_options()->set_xsize( width );
1183 globals->get_options()->set_ysize( height );
1185 float fov = globals->get_options()->get_fov();
1186 ssgSetFOV(fov, fov * globals->get_options()->get_win_ratio());
1192 // Initialize GLUT and define a main window
1193 int fgGlutInit( int *argc, char **argv ) {
1195 #if !defined( macintosh )
1196 // GLUT will extract all glut specific options so later on we only
1197 // need wory about our own.
1198 glutInit(argc, argv);
1201 // Define Display Parameters
1202 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
1204 FG_LOG( FG_GENERAL, FG_INFO, "Opening a window: " <<
1205 globals->get_options()->get_xsize() << "x"
1206 << globals->get_options()->get_ysize() );
1208 // Define initial window size
1209 glutInitWindowSize( globals->get_options()->get_xsize(),
1210 globals->get_options()->get_ysize() );
1212 // Initialize windows
1213 if ( globals->get_options()->get_game_mode() == 0 ) {
1214 // Open the regular window
1215 glutCreateWindow("FlightGear");
1216 #ifndef GLUT_WRONG_VERSION
1218 // Open the cool new 'game mode' window
1219 char game_mode_str[256];
1220 sprintf( game_mode_str, "width=%d height=%d bpp=%d",
1221 globals->get_options()->get_xsize(),
1222 globals->get_options()->get_ysize(),
1223 globals->get_options()->get_bpp());
1225 FG_LOG( FG_GENERAL, FG_INFO,
1226 "game mode params = " << game_mode_str );
1227 glutGameModeString( game_mode_str );
1228 glutEnterGameMode();
1232 // This seems to be the absolute earliest in the init sequence
1233 // that these calls will return valid info. Too bad it's after
1234 // we've already created and sized out window. :-(
1235 general.set_glVendor( (char *)glGetString ( GL_VENDOR ) );
1236 general.set_glRenderer( (char *)glGetString ( GL_RENDERER ) );
1237 general.set_glVersion( (char *)glGetString ( GL_VERSION ) );
1239 glGetIntegerv( GL_MAX_TEXTURE_SIZE, &tmp );
1240 general.set_glMaxTexSize( tmp );
1241 FG_LOG ( FG_GENERAL, FG_INFO, general.get_glRenderer() );
1242 FG_LOG ( FG_GENERAL, FG_INFO, "Max texture size = " << tmp );
1245 // try to determine if we should adjust the initial default
1246 // display resolution. The options class defaults (is
1247 // initialized) to 640x480.
1248 string renderer = general.glRenderer;
1250 // currently we only know how to deal with Mesa/Glide/Voodoo cards
1251 if ( renderer.find( "Glide" ) != string::npos ) {
1252 FG_LOG( FG_GENERAL, FG_INFO, "Detected a Glide driver" );
1253 if ( renderer.find( "FB/8" ) != string::npos ) {
1254 // probably a voodoo-2
1255 if ( renderer.find( "TMU/SLI" ) != string::npos ) {
1256 // probably two SLI'd Voodoo-2's
1257 globals->get_options()->set_xsize( 1024 );
1258 globals->get_options()->set_ysize( 768 );
1259 FG_LOG( FG_GENERAL, FG_INFO,
1260 "It looks like you have two sli'd voodoo-2's." << endl
1261 << "upgrading your win resolution to 1024 x 768" );
1262 glutReshapeWindow(1024, 768);
1264 // probably a single non-SLI'd Voodoo-2
1265 globals->get_options()->set_xsize( 800 );
1266 globals->get_options()->set_ysize( 600 );
1267 FG_LOG( FG_GENERAL, FG_INFO,
1268 "It looks like you have a voodoo-2." << endl
1269 << "upgrading your win resolution to 800 x 600" );
1270 glutReshapeWindow(800, 600);
1272 } else if ( renderer.find( "FB/2" ) != string::npos ) {
1273 // probably a voodoo-1, stick with the default
1276 // we have no special knowledge of this card, stick with the default
1284 // Initialize GLUT event handlers
1285 int fgGlutInitEvents( void ) {
1286 // call fgReshape() on window resizes
1287 glutReshapeFunc( fgReshape );
1289 // call GLUTkey() on keyboard event
1290 glutKeyboardFunc( GLUTkey );
1291 glutSpecialFunc( GLUTspecialkey );
1293 // call guiMouseFunc() whenever our little rodent is used
1294 glutMouseFunc ( guiMouseFunc );
1295 glutMotionFunc (guiMotionFunc );
1296 glutPassiveMotionFunc (guiMotionFunc );
1298 // call fgMainLoop() whenever there is
1299 // nothing else to do
1300 glutIdleFunc( fgIdleFunction );
1303 glutDisplayFunc( fgRenderFrame );
1310 int main( int argc, char **argv ) {
1312 #if defined( macintosh )
1313 freopen ("stdout.txt", "w", stdout );
1314 freopen ("stderr.txt", "w", stderr );
1315 argc = ccommand( &argv );
1319 _control87(MCW_EM, MCW_EM); /* defined in float.h */
1322 // set default log levels
1323 fglog().setLogLevels( FG_ALL, FG_INFO );
1325 FG_LOG( FG_GENERAL, FG_INFO, "FlightGear: Version "
1326 << FLIGHTGEAR_VERSION << endl );
1328 // seed the random number generater
1331 // Allocate global data structures. This needs to happen before
1332 // we parse command line options
1333 globals = new FGGlobals;
1335 SGRoute *route = new SGRoute;
1336 globals->set_route( route );
1338 FGOptions *options = new FGOptions;
1339 globals->set_options( options );
1341 FGViewMgr *viewmgr = new FGViewMgr;
1342 globals->set_viewmgr( viewmgr );
1344 FGViewerRPH *pv = new FGViewerRPH;
1345 globals->get_viewmgr()->add_view( pv );
1347 FGViewerLookAt *chase = new FGViewerLookAt;
1348 globals->get_viewmgr()->add_view( chase );
1350 // set current view to 0 (first) which is our main pilot view
1351 globals->set_current_view( globals->get_viewmgr()->get_view( 0 ) );
1353 // Scan the config file(s) and command line options to see if
1354 // fg_root was specified (ignore all other options for now)
1355 fgInitFGRoot(argc, argv);
1357 // cout << "1. airport_id = " << globals->get_options()->get_airport_id() << endl;
1359 // Read global preferences from $FG_ROOT/preferences.xml
1360 FGPath props_path(globals->get_options()->get_fg_root());
1361 props_path.append("preferences.xml");
1362 FG_LOG(FG_INPUT, FG_INFO, "Reading global preferences");
1363 if (!readPropertyList(props_path.str(), ¤t_properties)) {
1364 FG_LOG(FG_INPUT, FG_ALERT, "Failed to read global preferences from "
1365 << props_path.str());
1367 FG_LOG(FG_INPUT, FG_INFO, "Finished Reading global preferences");
1370 // cout << "2. airport_id = " << globals->get_options()->get_airport_id() << endl;
1372 // Initialize the Aircraft directory to "" (UIUC)
1375 // Load the configuration parameters
1376 if ( !fgInitConfig(argc, argv) ) {
1377 FG_LOG( FG_GENERAL, FG_ALERT, "Config option parsing failed ..." );
1381 // Initialize the Window/Graphics environment.
1382 if( !fgGlutInit(&argc, argv) ) {
1383 FG_LOG( FG_GENERAL, FG_ALERT, "GLUT initialization failed ..." );
1387 // Initialize the various GLUT Event Handlers.
1388 if( !fgGlutInitEvents() ) {
1389 FG_LOG( FG_GENERAL, FG_ALERT,
1390 "GLUT event handler initialization failed ..." );
1394 // Initialize ssg (from plib). Needs to come before we do any
1395 // other ssg stuff, but after opengl/glut has been initialized.
1398 // Initialize the user interface (we need to do this before
1399 // passing off control to glut and before fgInitGeneral to get our
1403 // set current_options lon/lat if an airport id is specified
1404 // cout << "3. airport_id = " << globals->get_options()->get_airport_id() << endl;
1405 if ( globals->get_options()->get_airport_id().length() ) {
1406 // fgSetPosFromAirportID( globals->get_options()->get_airport_id() );
1407 fgSetPosFromAirportIDandHdg( globals->get_options()->get_airport_id(),
1408 globals->get_options()->get_heading() );
1412 FGPath zone( globals->get_options()->get_fg_root() );
1413 zone.append( "Timezone" );
1414 SGTime *t = new SGTime( globals->get_options()->get_lon() * DEG_TO_RAD,
1415 globals->get_options()->get_lat() * DEG_TO_RAD,
1418 // Handle potential user specified time offsets
1419 time_t cur_time = t->get_cur_time();
1420 time_t currGMT = sgTimeGetGMT( gmtime(&cur_time) );
1421 time_t systemLocalTime = sgTimeGetGMT( localtime(&cur_time) );
1422 time_t aircraftLocalTime =
1423 sgTimeGetGMT( fgLocaltime(&cur_time, t->get_zonename() ) );
1425 // Okay, we now have six possible scenarios
1426 switch ( globals->get_options()->get_time_offset_type() ) {
1427 case FGOptions::FG_TIME_SYS_OFFSET:
1428 globals->set_warp( globals->get_options()->get_time_offset() );
1430 case FGOptions::FG_TIME_GMT_OFFSET:
1431 globals->set_warp( globals->get_options()->get_time_offset() -
1432 (currGMT - systemLocalTime) );
1434 case FGOptions::FG_TIME_LAT_OFFSET:
1435 globals->set_warp( globals->get_options()->get_time_offset() -
1436 (aircraftLocalTime - systemLocalTime) );
1438 case FGOptions::FG_TIME_SYS_ABSOLUTE:
1439 globals->set_warp( globals->get_options()->get_time_offset() -
1441 //printf("warp = %d\n", warp);
1443 case FGOptions::FG_TIME_GMT_ABSOLUTE:
1444 globals->set_warp( globals->get_options()->get_time_offset() -
1447 case FGOptions::FG_TIME_LAT_ABSOLUTE:
1448 globals->set_warp( globals->get_options()->get_time_offset() -
1449 (aircraftLocalTime - systemLocalTime) -
1453 FG_LOG( FG_GENERAL, FG_ALERT, "Unsupported type" );
1457 FG_LOG( FG_GENERAL, FG_INFO, "After time init, warp = "
1458 << globals->get_warp() );
1460 globals->set_warp_delta( 0 );
1462 t->update( 0.0, 0.0, globals->get_warp() );
1464 globals->set_time_params( t );
1466 // Do some quick general initializations
1467 if( !fgInitGeneral()) {
1468 FG_LOG( FG_GENERAL, FG_ALERT,
1469 "General initializations failed ..." );
1474 // some ssg test stuff (requires data from the plib source
1475 // distribution) specifically from the ssg tux example
1478 FGPath modelpath( globals->get_options()->get_fg_root() );
1479 // modelpath.append( "Models" );
1480 // modelpath.append( "Geometry" );
1482 FGPath texturepath( globals->get_options()->get_fg_root() );
1483 texturepath.append( "Models" );
1484 texturepath.append( "Textures" );
1486 ssgModelPath( (char *)modelpath.c_str() );
1487 ssgTexturePath( (char *)texturepath.c_str() );
1490 scene = new ssgRoot;
1491 scene->setName( "Scene" );
1493 // Initialize the sky
1494 FGPath ephem_data_path( globals->get_options()->get_fg_root() );
1495 ephem_data_path.append( "Astro" );
1496 SGEphemeris *ephem = new SGEphemeris( ephem_data_path.c_str() );
1497 ephem->update( globals->get_time_params()->getMjd(),
1498 globals->get_time_params()->getLst(),
1500 globals->set_ephem( ephem );
1502 FGPath sky_tex_path( globals->get_options()->get_fg_root() );
1503 sky_tex_path.append( "Textures" );
1504 sky_tex_path.append( "Sky" );
1506 thesky->texture_path( sky_tex_path.str() );
1508 thesky->build( 550.0, 550.0,
1509 globals->get_ephem()->getNumPlanets(),
1510 globals->get_ephem()->getPlanets(), 60000.0,
1511 globals->get_ephem()->getNumStars(),
1512 globals->get_ephem()->getStars(), 60000.0 );
1514 if ( globals->get_options()->get_clouds() == true ) {
1515 thesky->add_cloud_layer( 2600.0, 200.0, 50.0, 40000.0,
1516 SG_CLOUD_MOSTLY_SUNNY );
1517 thesky->add_cloud_layer( 6000.0, 20.0, 10.0, 40000.0,
1519 // thesky->add_cloud_layer( 1000.0, 200.0, 50.0,
1520 // SG_CLOUD_MOSTLY_SUNNY );
1521 // thesky->add_cloud_layer( 1800.0, 400.0, 100.0, SG_CLOUD_OVERCAST );
1522 // thesky->add_cloud_layer( 5000.0, 20.0, 10.0, SG_CLOUD_CIRRUS );
1525 // Initialize MagVar model
1526 SGMagVar *magvar = new SGMagVar();
1527 globals->set_mag( magvar );
1530 terrain = new ssgBranch;
1531 terrain->setName( "Terrain" );
1532 scene->addKid( terrain );
1534 // temporary visible aircraft "own ship"
1535 penguin_sel = new ssgSelector;
1536 penguin_pos = new ssgTransform;
1538 current_properties.getStringValue("/sim/model/path",
1539 "Models/Geometry/glider.ac");
1540 ssgEntity *tux_obj = ssgLoad((char *)(tux_path.c_str()));
1542 // align the model properly for FGFS
1543 ssgTransform *tux_align = new ssgTransform;
1544 tux_align->addKid(tux_obj);
1547 current_properties.getFloatValue("/sim/model/h-rotation", 0.0);
1549 current_properties.getFloatValue("/sim/model/p-rotation", 0.0);
1551 current_properties.getFloatValue("/sim/model/r-rotation", 0.0);
1552 sgMakeRotMat4(tux_matrix, h_rot, h_rot, r_rot);
1553 tux_align->setTransform(tux_matrix);
1555 penguin_pos->addKid( tux_align );
1556 penguin_sel->addKid( penguin_pos );
1557 ssgFlatten( tux_obj );
1558 ssgStripify( penguin_sel );
1559 penguin_sel->clrTraversalMaskBits( SSGTRAV_HOT );
1560 scene->addKid( penguin_sel );
1562 // $$$ begin - added VS Renganthan 17 Oct 2K
1564 // $$$ end - added VS Renganthan 17 Oct 2K
1566 #ifdef FG_NETWORK_OLK
1567 // Do the network intialization
1568 if ( globals->get_options()->get_network_olk() ) {
1569 printf("Multipilot mode %s\n", fg_net_init( scene ) );
1573 // build our custom render states
1574 fgBuildRenderStates();
1576 // pass control off to the master GLUT event handler
1579 // we never actually get here ... but to avoid compiler warnings,
1585 // $$$ end - added VS Renganathan, 15 Oct 2K
1586 void fgLoadDCS(void) {
1588 string obj_filename;
1590 FGPath modelpath( globals->get_options()->get_fg_root() );
1591 modelpath.append( "Models" );
1592 modelpath.append( "Geometry" );
1594 FGPath texturepath( globals->get_options()->get_fg_root() );
1595 texturepath.append( "Models" );
1596 texturepath.append( "Textures" );
1598 ssgModelPath( (char *)modelpath.c_str() );
1599 ssgTexturePath( (char *)texturepath.c_str() );
1601 // while ( ! in.eof() ) {
1602 // in >> obj_filename >> obj_lat >> obj_lon >> obj_alt;
1603 // totalDCSobj = totalDCSobj+1;
1605 ship_sel = new ssgSelector;
1606 ship_pos = new ssgTransform;
1607 ssgEntity *ship_obj = ssgLoadOBJ( "saratoga.obj" );
1608 if ( ship_obj != NULL ) {
1609 ship_pos->addKid( ship_obj ); // add object to transform node
1610 ship_sel->addKid( ship_pos ); // add transform node to selector
1611 // ssgFlatten( ship_obj );
1612 // ssgStripify( ship_sel );
1613 ship_sel->clrTraversalMaskBits( SSGTRAV_HOT );
1614 scene->addKid( ship_sel ); //add selector node to root node
1616 FG_LOG( FG_TERRAIN, FG_ALERT, "Cannot open file: " << "saratoga.obj" );
1619 // if (in.eof()) break;
1626 void fgUpdateDCS (void) {
1628 // double eye_lat,eye_lon,eye_alt;
1629 static double obj_lat=15.377603*DEG_TO_RAD;
1630 static double obj_lon= 73.816436*DEG_TO_RAD;
1631 static double obj_alt=0.15;
1632 // static double obj_head;
1633 double sl_radius,obj_latgc;
1634 // float nresultmat[4][4];
1637 obj_lat = obj_lat + 0.0000001;
1639 // Instantaneous Geodetic Lat/Lon/Alt of moving object
1640 // obj_lon = current_aircraft.fdm_state->get_aux5()*DEG_TO_RAD;
1641 // obj_lat = current_aircraft.fdm_state->get_aux6()*DEG_TO_RAD;
1642 // obj_alt = current_aircraft.fdm_state->get_aux7();
1644 // Geodetic to Geocentric angles for rotation
1645 sgGeodToGeoc(obj_lat,obj_alt,&sl_radius,&obj_latgc);
1647 // moving object gbs-posn in cartesian coords
1648 Point3D obj_posn = Point3D( obj_lon,obj_lat,obj_alt);
1649 Point3D obj_pos = sgGeodToCart( obj_posn );
1651 // Translate moving object w.r.t eye
1652 Point3D Objtrans = obj_pos-scenery.center;
1657 // rotate dynamic objects for lat,lon & alt and other motion about its axes
1658 if ( ship_sel != NULL ) {
1659 ship_sel->select(1);
1662 sgMakeTransMat4( sgTRANS, bz[0],bz[1],bz[2]);
1664 sgVec3 ship_fwd,ship_rt,ship_up;
1665 sgSetVec3( ship_fwd, 1.0, 0.0, 0.0);//east,roll
1666 sgSetVec3( ship_rt, 0.0, 1.0, 0.0);//up,pitch
1667 sgSetVec3( ship_up, 0.0, 0.0, 1.0); //north,yaw
1669 sgMat4 sgROT_lon, sgROT_lat, sgROT_hdg;
1670 sgMakeRotMat4( sgROT_lon, obj_lon*RAD_TO_DEG, ship_up );
1671 sgMakeRotMat4( sgROT_lat, 90-obj_latgc*RAD_TO_DEG, ship_rt );
1672 sgMakeRotMat4( sgROT_hdg, 180.0, ship_up );
1675 sgCopyMat4( sgTUX, sgROT_hdg );
1676 sgPostMultMat4( sgTUX, sgROT_lat );
1677 sgPostMultMat4( sgTUX, sgROT_lon );
1678 sgPostMultMat4( sgTUX, sgTRANS );
1681 sgSetCoord( &shippos, sgTUX );
1682 ship_pos->setTransform( &shippos );
1686 // $$$ end - added VS Renganathan, 15 Oct 2K