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() */
56 #include <plib/pu.h> // plib include
57 #include <plib/ssg.h> // plib include
59 #ifdef ENABLE_AUDIO_SUPPORT
60 # include <plib/sl.h> // plib include
61 # include <plib/sm.h> // plib include
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/sunpos.hxx>
93 #include <Time/tmp.hxx>
95 // $$$ begin - added VS Renganathan
96 #include <simgear/misc/fgstream.hxx>
97 #include <FDM/flight.hxx>
98 void fgLoadDCS (void);
99 void fgUpdateDCS (void);
100 ssgSelector *ship_sel = NULL;
101 ssgTransform *ship_pos = NULL;
102 //int totalDCSobj = 0;
103 // $$$ end - added VS Renganathan
105 #ifndef FG_OLD_WEATHER
106 # include <WeatherCM/FGLocalWeatherDatabase.h>
108 # include <Weather/weather.hxx>
114 #include "fg_init.hxx"
116 #include "globals.hxx"
117 #include "keyboard.hxx"
118 #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 globals->get_current_view()->
326 set_geod_view_pos( cur_fdm_state->get_Longitude(),
327 cur_fdm_state->get_Lat_geocentric(),
328 cur_fdm_state->get_Altitude() *
330 globals->get_current_view()->
331 set_sea_level_radius( cur_fdm_state->get_Sea_level_radius() *
333 globals->get_current_view()->
334 set_hpr( cur_fdm_state->get_Psi(),
335 cur_fdm_state->get_Theta(),
336 cur_fdm_state->get_Phi() );
338 // update view volume parameters
339 // cout << "before pilot_view update" << endl;
340 if ( globals->get_options()->get_view_mode() ==
341 FGOptions::FG_VIEW_FOLLOW )
343 float * offset = globals->get_current_view()->get_pilot_offset();
344 globals->get_current_view()->set_pilot_offset( offset[0],
348 globals->get_current_view()->set_pilot_offset(0.0, 0.0, 0.0);
351 if ( ! fgPanelVisible() ) {
353 (GLint)(globals->get_options()->get_xsize()),
354 (GLint)(globals->get_options()->get_ysize()) );
357 int( (current_panel->getViewHeight() -
358 current_panel->getYOffset())
359 * (globals->get_options()->get_ysize() / 768.0) );
361 (GLint)(globals->get_options()->get_ysize() - view_h),
362 (GLint)(globals->get_options()->get_xsize()),
366 // set the sun position
367 glLightfv( GL_LIGHT0, GL_POSITION, l->sun_vec );
369 clear_mask = GL_DEPTH_BUFFER_BIT;
370 if ( globals->get_options()->get_wireframe() ) {
371 clear_mask |= GL_COLOR_BUFFER_BIT;
374 if ( globals->get_options()->get_skyblend() ) {
375 if ( globals->get_options()->get_textures() ) {
376 // glClearColor(black[0], black[1], black[2], black[3]);
377 glClearColor(l->adj_fog_color[0], l->adj_fog_color[1],
378 l->adj_fog_color[2], l->adj_fog_color[3]);
379 clear_mask |= GL_COLOR_BUFFER_BIT;
382 glClearColor(l->sky_color[0], l->sky_color[1],
383 l->sky_color[2], l->sky_color[3]);
384 clear_mask |= GL_COLOR_BUFFER_BIT;
386 glClear( clear_mask );
388 // Tell GL we are switching to model view parameters
390 // I really should create a derived ssg node or use a call
391 // back or something so that I can draw the sky within the
392 // ssgCullAndDraw() function, but for now I just mimic what
393 // ssg does to set up the model view matrix
394 glMatrixMode(GL_MODELVIEW);
396 ssgSetCamera( (sgMat4)globals->get_current_view()->get_VIEW() );
399 sgMat4 vm_tmp, view_mat;
400 sgTransposeNegateMat4 ( vm_tmp, current_view.VIEW ) ;
401 sgCopyMat4( view_mat, copy_of_ssgOpenGLAxisSwapMatrix ) ;
402 sgPreMultMat4( view_mat, vm_tmp ) ;
403 glLoadMatrixf( (float *)view_mat );
406 // set the opengl state to known default values
407 default_state->force();
409 // update fog params if visibility has changed
410 #ifndef FG_OLD_WEATHER
411 thesky->set_visibility( WeatherDatabase->getWeatherVisibility() );
413 thesky->set_visibility( current_weather.get_visibility() );
416 thesky->modify_vis( cur_fdm_state->get_Altitude() * FEET_TO_METER,
418 ( global_multi_loop *
419 globals->get_options()->get_speed_up() ) /
420 (double)globals->get_options()->get_model_hz() );
422 double actual_visibility = thesky->get_visibility();
423 // cout << "actual visibility = " << actual_visibility << endl;
425 if ( actual_visibility != last_visibility ) {
426 last_visibility = actual_visibility;
428 // cout << "----> updating fog params" << endl;
430 GLfloat fog_exp_density;
431 GLfloat fog_exp2_density;
434 fog_exp_density = -log(0.01 / actual_visibility);
437 fog_exp2_density = sqrt( -log(0.01) ) / actual_visibility;
439 // Set correct opengl fog density
440 glFogf (GL_FOG_DENSITY, fog_exp2_density);
443 // update the sky dome
444 if ( globals->get_options()->get_skyblend() ) {
447 globals->get_current_view()->get_view_pos().x(),
448 globals->get_current_view()->get_view_pos().y(),
449 globals->get_current_view()->get_view_pos().z() );
452 sgSetVec3( zero_elev,
453 globals->get_current_view()->get_cur_zero_elev().x(),
454 globals->get_current_view()->get_cur_zero_elev().y(),
455 globals->get_current_view()->get_cur_zero_elev().z() );
457 /* cout << "thesky->repaint() sky_color = "
458 << cur_light_params.sky_color[0] << " "
459 << cur_light_params.sky_color[1] << " "
460 << cur_light_params.sky_color[2] << " "
461 << cur_light_params.sky_color[3] << endl;
463 << cur_light_params.fog_color[0] << " "
464 << cur_light_params.fog_color[1] << " "
465 << cur_light_params.fog_color[2] << " "
466 << cur_light_params.fog_color[3] << endl;
467 cout << " sun_angle = " << cur_light_params.sun_angle
468 << " moon_angle = " << cur_light_params.moon_angle
470 thesky->repaint( cur_light_params.sky_color,
471 cur_light_params.adj_fog_color,
472 cur_light_params.sun_angle,
473 cur_light_params.moon_angle,
474 globals->get_ephem()->getNumPlanets(),
475 globals->get_ephem()->getPlanets(),
476 globals->get_ephem()->getNumStars(),
477 globals->get_ephem()->getStars() );
479 /* cout << "thesky->reposition( view_pos = " << view_pos[0] << " "
480 << view_pos[1] << " " << view_pos[2] << endl;
481 cout << " zero_elev = " << zero_elev[0] << " "
482 << zero_elev[1] << " " << zero_elev[2]
483 << " lon = " << cur_fdm_state->get_Longitude()
484 << " lat = " << cur_fdm_state->get_Latitude() << endl;
485 cout << " sun_rot = " << cur_light_params.sun_rotation
486 << " gst = " << SGTime::cur_time_params->getGst() << endl;
487 cout << " sun ra = " << globals->get_ephem()->getSunRightAscension()
488 << " sun dec = " << globals->get_ephem()->getSunDeclination()
489 << " moon ra = " << globals->get_ephem()->getMoonRightAscension()
490 << " moon dec = " << globals->get_ephem()->getMoonDeclination() << endl; */
492 thesky->reposition( view_pos, zero_elev,
493 globals->get_current_view()->get_local_up(),
494 cur_fdm_state->get_Longitude(),
495 cur_fdm_state->get_Latitude(),
496 cur_fdm_state->get_Altitude() * FEET_TO_METER,
497 cur_light_params.sun_rotation,
498 globals->get_time_params()->getGst(),
499 globals->get_ephem()->getSunRightAscension(),
500 globals->get_ephem()->getSunDeclination(),
502 globals->get_ephem()->getMoonRightAscension(),
503 globals->get_ephem()->getMoonDeclination(),
507 glEnable( GL_DEPTH_TEST );
508 if ( globals->get_options()->get_fog() > 0 ) {
510 glFogi( GL_FOG_MODE, GL_EXP2 );
511 glFogfv( GL_FOG_COLOR, l->adj_fog_color );
514 // set lighting parameters
515 GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
516 glLightModelfv( GL_LIGHT_MODEL_AMBIENT, l->scene_ambient );
517 glLightfv( GL_LIGHT0, GL_AMBIENT, black );
518 glLightfv( GL_LIGHT0, GL_DIFFUSE, l->scene_diffuse );
519 // glLightfv(GL_LIGHT0, GL_SPECULAR, white );
521 // texture parameters
522 // glEnable( GL_TEXTURE_2D );
523 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
524 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ) ;
525 // set base color (I don't think this is doing anything here)
526 // glMaterialfv (GL_FRONT, GL_AMBIENT, white);
527 // (GL_FRONT, GL_DIFFUSE, white);
528 // glMaterialfv (GL_FRONT, GL_SPECULAR, white);
529 // glMaterialfv (GL_FRONT, GL_SHININESS, mat_shininess);
532 sgSetVec3( sunpos, l->sun_vec[0], l->sun_vec[1], l->sun_vec[2] );
533 ssgGetLight( 0 ) -> setPosition( sunpos );
535 // glMatrixMode( GL_PROJECTION );
537 float fov = globals->get_options()->get_fov();
538 ssgSetFOV(fov, fov * globals->get_options()->get_win_ratio());
540 double agl = current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER
543 // FG_LOG( FG_ALL, FG_INFO, "visibility is "
544 // << current_weather.get_visibility() );
547 ssgSetNearFar( 10.0f, 120000.0f );
549 ssgSetNearFar( 0.5f, 120000.0f );
552 if ( globals->get_options()->get_view_mode() ==
553 FGOptions::FG_VIEW_PILOT )
556 penguin_sel->select(0);
557 } else if ( globals->get_options()->get_view_mode() ==
558 FGOptions::FG_VIEW_FOLLOW )
560 // select view matrix from front of view matrix queue
561 // FGMat4Wrapper tmp = current_view.follow.front();
562 // sgCopyMat4( sgVIEW, tmp.m );
564 // enable TuX and set up his position and orientation
565 penguin_sel->select(1);
568 sgMakeTransMat4( sgTRANS,
569 globals->get_current_view()->get_view_pos().x(),
570 globals->get_current_view()->get_view_pos().y(),
571 globals->get_current_view()->get_view_pos().z() );
574 sgSetVec3( ownship_up, 0.0, 0.0, 1.0);
577 sgMakeRotMat4( sgROT, -90.0, ownship_up );
581 // sgMultMat4( sgTMP, sgROT, pilot_view.VIEW_ROT );
582 // sgMultMat4( sgTUX, sgTMP, sgTRANS );
584 // sgTUX = ( sgROT * pilot_view.VIEW_ROT ) * sgTRANS
586 sgCopyMat4( sgTUX, sgROT );
587 sgPostMultMat4( sgTUX, globals->get_current_view()->get_VIEW_ROT() );
588 sgPostMultMat4( sgTUX, sgTRANS );
591 sgSetCoord( &tuxpos, sgTUX );
592 penguin_pos->setTransform( &tuxpos );
595 // $$$ begin - added VS Renganthan 17 Oct 2K
597 // $$$ end - added VS Renganthan 17 Oct 2K
599 # ifdef FG_NETWORK_OLK
600 if ( globals->get_options()->get_network_olk() ) {
602 other = head->next; /* put listpointer to start */
603 while ( other != tail) { /* display all except myself */
604 if ( strcmp( other->ipadr, fgd_mcp_ip) != 0) {
605 other->fgd_sel->select(1);
606 sgSetCoord( &fgdpos, other->sgFGD_COORD );
607 other->fgd_pos->setTransform( &fgdpos );
612 // fgd_sel->select(1);
613 // sgCopyMat4( sgTUX, current_view.sgVIEW);
615 // sgSetCoord( &fgdpos, sgFGD_VIEW );
616 // fgd_pos->setTransform( &fgdpos);
620 // ssgSetCamera( current_view.VIEW );
622 // position tile nodes and update range selectors
623 global_tile_mgr.prep_ssg_nodes();
625 // force the default state so ssg can get back on track if
626 // we've changed things elsewhere (this is now handled
629 // FGMaterialSlot m_slot;
630 // FGMaterialSlot *m_ptr = &m_slot;
631 // if ( material_mgr.find( "Default", m_ptr ) ) {
632 // m_ptr->get_state()->force();
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 for ( i = 0; i < multi_loop; i++ ) {
717 if ( fabs(globals->get_current_view()->get_goal_view_offset() -
718 globals->get_current_view()->get_view_offset()) < 0.05 )
720 globals->get_current_view()->set_view_offset( globals->get_current_view()->get_goal_view_offset() );
723 // move current_view.view_offset towards
724 // current_view.goal_view_offset
725 if ( globals->get_current_view()->get_goal_view_offset() >
726 globals->get_current_view()->get_view_offset() )
728 if ( globals->get_current_view()->get_goal_view_offset() -
729 globals->get_current_view()->get_view_offset() < FG_PI )
731 globals->get_current_view()->inc_view_offset( 0.01 );
733 globals->get_current_view()->inc_view_offset( -0.01 );
736 if ( globals->get_current_view()->get_view_offset() -
737 globals->get_current_view()->get_goal_view_offset() <
740 globals->get_current_view()->inc_view_offset( -0.01 );
742 globals->get_current_view()->inc_view_offset( 0.01 );
745 if ( globals->get_current_view()->get_view_offset() > FG_2PI ) {
746 globals->get_current_view()->inc_view_offset( -FG_2PI );
747 } else if ( globals->get_current_view()->get_view_offset() < 0 ) {
748 globals->get_current_view()->inc_view_offset( FG_2PI );
753 double tmp = -(l->sun_rotation + FG_PI)
754 - (cur_fdm_state->get_Psi() -
755 globals->get_current_view()->get_view_offset() );
756 while ( tmp < 0.0 ) {
759 while ( tmp > FG_2PI ) {
762 /* printf("Psi = %.2f, viewoffset = %.2f sunrot = %.2f rottosun = %.2f\n",
763 FG_Psi * RAD_TO_DEG, current_view.view_offset * RAD_TO_DEG,
764 -(l->sun_rotation+FG_PI) * RAD_TO_DEG, tmp * RAD_TO_DEG); */
767 // Update solar system
768 globals->get_ephem()->update( globals->get_time_params()->getMjd(),
769 globals->get_time_params()->getLst(),
770 cur_fdm_state->get_Latitude() );
772 // Update radio stack model
773 current_radiostack->update( cur_fdm_state->get_Longitude(),
774 cur_fdm_state->get_Latitude(),
775 cur_fdm_state->get_Altitude() * FEET_TO_METER );
779 void fgInitTimeDepCalcs( void ) {
782 // #ifdef HAVE_SETITIMER
783 // fgTimerInit( 1.0 / globals->get_options()->get_model_hz(),
784 // fgUpdateTimeDepCalcs );
785 // #endif HAVE_SETITIMER
789 static const double alt_adjust_ft = 3.758099;
790 static const double alt_adjust_m = alt_adjust_ft * FEET_TO_METER;
793 // What should we do when we have nothing else to do? Let's get ready
794 // for the next move and update the display?
795 static void fgMainLoop( void ) {
796 static long remainder = 0;
798 #ifdef FANCY_FRAME_COUNTER
802 static time_t last_time = 0;
803 static int frames = 0;
804 #endif // FANCY_FRAME_COUNTER
806 SGTime *t = globals->get_time_params();
808 FG_LOG( FG_ALL, FG_DEBUG, "Running Main Loop");
809 FG_LOG( FG_ALL, FG_DEBUG, "======= ==== ====");
811 #ifdef FG_NETWORK_OLK
812 if ( globals->get_options()->get_network_olk() ) {
813 if ( net_is_registered == 0 ) { // We first have to reg. to fgd
814 // printf("FGD: Netupdate\n");
815 fgd_send_com( "A", FGFS_host); // Send Mat4 data
816 fgd_send_com( "B", FGFS_host); // Recv Mat4 data
821 #if defined( ENABLE_PLIB_JOYSTICK )
822 // Read joystick and update control settings
823 if ( globals->get_options()->get_control_mode() == FGOptions::FG_JOYSTICK )
827 #elif defined( ENABLE_GLUT_JOYSTICK )
828 // Glut joystick support works by feeding a joystick handler
829 // function to glut. This is taken care of once in the joystick
830 // init routine and we don't have to worry about it again.
833 #ifdef FG_OLD_WEATHER
834 current_weather.Update();
837 // Fix elevation. I'm just sticking this here for now, it should
838 // probably move eventually
840 /* printf("Before - ground = %.2f runway = %.2f alt = %.2f\n",
842 cur_fdm_state->get_Runway_altitude() * FEET_TO_METER,
843 cur_fdm_state->get_Altitude() * FEET_TO_METER); */
845 if ( scenery.cur_elev > -9990 ) {
846 if ( cur_fdm_state->get_Altitude() * FEET_TO_METER <
847 (scenery.cur_elev + alt_adjust_m - 3.0) ) {
848 // now set aircraft altitude above ground
849 printf("(*) Current Altitude = %.2f < %.2f forcing to %.2f\n",
850 cur_fdm_state->get_Altitude() * FEET_TO_METER,
851 scenery.cur_elev + alt_adjust_m - 3.0,
852 scenery.cur_elev + alt_adjust_m );
853 fgFDMForceAltitude( globals->get_options()->get_flight_model(),
854 scenery.cur_elev + alt_adjust_m );
856 FG_LOG( FG_ALL, FG_DEBUG,
857 "<*> resetting altitude to "
858 << cur_fdm_state->get_Altitude() * FEET_TO_METER
861 //fgFDMSetGroundElevation( globals->get_options()->get_flight_model(),
862 // scenery.cur_elev ); // meters
865 /* printf("Adjustment - ground = %.2f runway = %.2f alt = %.2f\n",
867 cur_fdm_state->get_Runway_altitude() * FEET_TO_METER,
868 cur_fdm_state->get_Altitude() * FEET_TO_METER); */
871 if ( globals->get_warp_delta() != 0 ) {
872 globals->inc_warp( globals->get_warp_delta() );
875 t->update( cur_fdm_state->get_Longitude(),
876 cur_fdm_state->get_Latitude(),
877 globals->get_warp() );
879 if ( globals->get_warp_delta() != 0 ) {
880 fgUpdateSkyAndLightingParams();
883 // update magvar model
884 globals->get_mag()->update( cur_fdm_state->get_Longitude(),
885 cur_fdm_state->get_Latitude(),
886 cur_fdm_state->get_Altitude()* FEET_TO_METER,
887 globals->get_time_params()->getJD() );
889 // Get elapsed time (in usec) for this past frame
890 elapsed = fgGetTimeInterval();
891 FG_LOG( FG_ALL, FG_DEBUG,
892 "Elapsed time interval is = " << elapsed
893 << ", previous remainder is = " << remainder );
895 // Calculate frame rate average
896 #ifdef FANCY_FRAME_COUNTER
897 /* old fps calculation */
901 for ( i = FG_FRAME_RATE_HISTORY - 2; i >= 0; i-- ) {
902 tmp = general.get_frame(i);
904 // printf("frame[%d] = %.2f\n", i, g->frames[i]);
905 general.set_frame(i+1,tmp);
907 tmp = 1000000.0 / (float)elapsed;
908 general.set_frame(0,tmp);
909 // printf("frame[0] = %.2f\n", general.frames[0]);
911 general.set_frame_rate(accum / (float)FG_FRAME_RATE_HISTORY);
912 // printf("ave = %.2f\n", general.frame_rate);
915 if ( (t->get_cur_time() != last_time) && (last_time > 0) ) {
916 general.set_frame_rate( frames );
917 FG_LOG( FG_ALL, FG_DEBUG,
918 "--> Frame rate is = " << general.get_frame_rate() );
921 last_time = t->get_cur_time();
926 if ( ! use_signals ) {
927 // Calculate model iterations needed for next frame
928 elapsed += remainder;
930 global_multi_loop = (int)(((double)elapsed * 0.000001) *
931 globals->get_options()->get_model_hz());
932 remainder = elapsed - ( (global_multi_loop*1000000) /
933 globals->get_options()->get_model_hz() );
934 FG_LOG( FG_ALL, FG_DEBUG,
935 "Model iterations needed = " << global_multi_loop
936 << ", new remainder = " << remainder );
939 if ( global_multi_loop > 0 ) {
940 fgUpdateTimeDepCalcs(global_multi_loop, remainder);
942 FG_LOG( FG_ALL, FG_DEBUG,
943 "Elapsed time is zero ... we're zinging" );
947 #if ! defined( macintosh )
948 // Do any I/O channel work that might need to be done
952 // see if we need to load any new scenery tiles
953 global_tile_mgr.update( cur_fdm_state->get_Longitude() * RAD_TO_DEG,
954 cur_fdm_state->get_Latitude() * RAD_TO_DEG );
956 // Process/manage pending events
957 global_events.Process();
959 // Run audio scheduler
960 #ifdef ENABLE_AUDIO_SUPPORT
961 if ( globals->get_options()->get_sound() && !audio_sched->not_working() ) {
963 if ( globals->get_options()->get_aircraft() == "c172" ) {
964 // pitch corresponds to rpm
965 // volume corresponds to manifold pressure
968 if ( cur_fdm_state->get_engine(0) != NULL ) {
969 rpm_factor = cur_fdm_state->get_engine(0)->get_RPM() / 2500.0;
973 // cout << "rpm = " << cur_fdm_state->get_engine(0)->get_RPM()
976 double pitch = 0.3 + rpm_factor * 3.0;
978 // don't run at absurdly slow rates -- not realistic
979 // and sounds bad to boot. :-)
980 if (pitch < 0.7) { pitch = 0.7; }
981 if (pitch > 5.0) { pitch = 5.0; }
982 // cout << "pitch = " << pitch << endl;
985 if ( cur_fdm_state->get_engine(0) != NULL ) {
987 cur_fdm_state->get_engine(0)->get_Manifold_Pressure() / 28;
992 // << cur_fdm_state->get_engine(0)->get_Manifold_Pressure()
995 double volume = mp_factor;
997 if ( volume < 0.3 ) { volume = 0.3; }
998 if ( volume > 2.0 ) { volume = 2.0; }
999 // cout << "volume = " << volume << endl;
1001 pitch_envelope.setStep ( 0, 0.01, pitch );
1002 volume_envelope.setStep ( 0, 0.01, volume );
1004 double param = controls.get_throttle( 0 ) * 2.0 + 1.0;
1005 pitch_envelope.setStep ( 0, 0.01, param );
1006 volume_envelope.setStep ( 0, 0.01, param );
1009 audio_sched -> update();
1016 FG_LOG( FG_ALL, FG_DEBUG, "" );
1020 // This is the top level master main function that is registered as
1021 // our idle funciton
1024 // The first few passes take care of initialization things (a couple
1025 // per pass) and once everything has been initialized fgMainLoop from
1028 static void fgIdleFunction ( void ) {
1029 // printf("idle state == %d\n", idle_state);
1031 if ( idle_state == 0 ) {
1032 // Initialize the splash screen right away
1033 if ( globals->get_options()->get_splash_screen() ) {
1038 } else if ( idle_state == 1 ) {
1039 // Start the intro music
1041 if ( globals->get_options()->get_intro_music() ) {
1042 string lockfile = "/tmp/mpg123.running";
1043 FGPath mp3file( globals->get_options()->get_fg_root() );
1044 mp3file.append( "Sounds/intro.mp3" );
1046 string command = "(touch " + lockfile + "; mpg123 "
1047 + mp3file.str() + "> /dev/null 2>&1; /bin/rm "
1049 FG_LOG( FG_GENERAL, FG_INFO,
1050 "Starting intro music: " << mp3file.str() );
1051 system ( command.c_str() );
1056 } else if ( idle_state == 2 ) {
1057 // These are a few miscellaneous things that aren't really
1058 // "subsystems" but still need to be initialized.
1061 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
1062 grTexLodBiasValue ( GR_TMU0, 1.0 ) ;
1067 } else if ( idle_state == 3 ) {
1068 // This is the top level init routine which calls all the
1069 // other subsystem initialization routines. If you are adding
1070 // a subsystem to flightgear, its initialization call should
1071 // located in this routine.
1072 if( !fgInitSubsystems()) {
1073 FG_LOG( FG_GENERAL, FG_ALERT,
1074 "Subsystem initializations failed ..." );
1079 } else if ( idle_state == 4 ) {
1080 // setup OpenGL view parameters
1083 if ( use_signals ) {
1084 // init timer routines, signals, etc. Arrange for an alarm
1085 // signal to be generated, etc.
1086 fgInitTimeDepCalcs();
1090 } else if ( idle_state == 5 ) {
1093 } else if ( idle_state == 6 ) {
1094 // Initialize audio support
1095 #ifdef ENABLE_AUDIO_SUPPORT
1098 if ( globals->get_options()->get_intro_music() ) {
1099 // Let's wait for mpg123 to finish
1100 string lockfile = "/tmp/mpg123.running";
1101 struct stat stat_buf;
1103 FG_LOG( FG_GENERAL, FG_INFO,
1104 "Waiting for mpg123 player to finish ..." );
1105 while ( stat(lockfile.c_str(), &stat_buf) == 0 ) {
1106 // file exist, wait ...
1108 FG_LOG( FG_GENERAL, FG_INFO, ".");
1110 FG_LOG( FG_GENERAL, FG_INFO, "");
1114 if ( globals->get_options()->get_sound() ) {
1115 audio_sched = new slScheduler ( 8000 );
1116 audio_mixer = new smMixer;
1117 audio_mixer -> setMasterVolume ( 80 ) ; /* 80% of max volume. */
1118 audio_sched -> setSafetyMargin ( 1.0 ) ;
1120 FGPath slfile( globals->get_options()->get_fg_root() );
1121 slfile.append( "Sounds/wasp.wav" );
1123 s1 = new slSample ( (char *)slfile.c_str() );
1124 FG_LOG( FG_GENERAL, FG_INFO,
1125 "Rate = " << s1 -> getRate()
1126 << " Bps = " << s1 -> getBps()
1127 << " Stereo = " << s1 -> getStereo() );
1128 audio_sched -> loopSample ( s1 );
1130 if ( audio_sched->not_working() ) {
1133 pitch_envelope.setStep ( 0, 0.01, 0.6 );
1134 volume_envelope.setStep ( 0, 0.01, 0.6 );
1136 audio_sched -> addSampleEnvelope( s1, 0, 0,
1138 SL_PITCH_ENVELOPE );
1139 audio_sched -> addSampleEnvelope( s1, 0, 1,
1141 SL_VOLUME_ENVELOPE );
1144 // strcpy(slfile, path);
1145 // strcat(slfile, "thunder.wav");
1146 // s2 -> loadFile ( slfile );
1147 // s2 -> adjustVolume(0.5);
1148 // audio_sched -> playSample ( s2 );
1155 cout << "Panel visible = " << fgPanelVisible() << endl;
1156 fgReshape( globals->get_options()->get_xsize(),
1157 globals->get_options()->get_ysize() );
1160 if ( idle_state == 1000 ) {
1161 // We've finished all our initialization steps, from now on we
1162 // run the main loop.
1166 if ( globals->get_options()->get_splash_screen() == 1 ) {
1167 fgSplashUpdate(0.0);
1172 // options.cxx needs to see this for toggle_panel()
1173 // Handle new window size or exposure
1174 void fgReshape( int width, int height ) {
1175 if ( ! fgPanelVisible() || idle_state != 1000 ) {
1176 globals->get_options()->set_win_ratio( (float)height /
1178 glViewport(0, 0 , (GLint)(width), (GLint)(height) );
1181 int((current_panel->getViewHeight() - current_panel->getYOffset())
1182 * (height / 768.0)) + 1;
1183 globals->get_options()->set_win_ratio( (float)view_h /
1185 glViewport(0, (GLint)(height - view_h),
1186 (GLint)(width), (GLint)(view_h) );
1189 globals->get_options()->set_xsize( width );
1190 globals->get_options()->set_ysize( height );
1192 float fov = globals->get_options()->get_fov();
1193 ssgSetFOV(fov, fov * globals->get_options()->get_win_ratio());
1197 if ( idle_state == 1000 ) {
1198 // yes we've finished all our initializations and are running
1199 // the main loop, so this will now work without seg faulting
1201 // globals->get_current_view()->UpdateViewParams();
1206 // Initialize GLUT and define a main window
1207 int fgGlutInit( int *argc, char **argv ) {
1209 #if !defined( macintosh )
1210 // GLUT will extract all glut specific options so later on we only
1211 // need wory about our own.
1212 glutInit(argc, argv);
1215 // Define Display Parameters
1216 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
1218 FG_LOG( FG_GENERAL, FG_INFO, "Opening a window: " <<
1219 globals->get_options()->get_xsize() << "x"
1220 << globals->get_options()->get_ysize() );
1222 // Define initial window size
1223 glutInitWindowSize( globals->get_options()->get_xsize(),
1224 globals->get_options()->get_ysize() );
1226 // Initialize windows
1227 if ( globals->get_options()->get_game_mode() == 0 ) {
1228 // Open the regular window
1229 glutCreateWindow("FlightGear");
1230 #ifndef GLUT_WRONG_VERSION
1232 // Open the cool new 'game mode' window
1233 char game_mode_str[256];
1234 sprintf( game_mode_str, "width=%d height=%d bpp=%d",
1235 globals->get_options()->get_xsize(),
1236 globals->get_options()->get_ysize(),
1237 globals->get_options()->get_bpp());
1239 FG_LOG( FG_GENERAL, FG_INFO,
1240 "game mode params = " << game_mode_str );
1241 glutGameModeString( game_mode_str );
1242 glutEnterGameMode();
1246 // This seems to be the absolute earliest in the init sequence
1247 // that these calls will return valid info. Too bad it's after
1248 // we've already created and sized out window. :-(
1249 general.set_glVendor( (char *)glGetString ( GL_VENDOR ) );
1250 general.set_glRenderer( (char *)glGetString ( GL_RENDERER ) );
1251 general.set_glVersion( (char *)glGetString ( GL_VERSION ) );
1253 glGetIntegerv( GL_MAX_TEXTURE_SIZE, &tmp );
1254 general.set_glMaxTexSize( tmp );
1255 FG_LOG ( FG_GENERAL, FG_INFO, general.get_glRenderer() );
1256 FG_LOG ( FG_GENERAL, FG_INFO, "Max texture size = " << tmp );
1259 // try to determine if we should adjust the initial default
1260 // display resolution. The options class defaults (is
1261 // initialized) to 640x480.
1262 string renderer = general.glRenderer;
1264 // currently we only know how to deal with Mesa/Glide/Voodoo cards
1265 if ( renderer.find( "Glide" ) != string::npos ) {
1266 FG_LOG( FG_GENERAL, FG_INFO, "Detected a Glide driver" );
1267 if ( renderer.find( "FB/8" ) != string::npos ) {
1268 // probably a voodoo-2
1269 if ( renderer.find( "TMU/SLI" ) != string::npos ) {
1270 // probably two SLI'd Voodoo-2's
1271 globals->get_options()->set_xsize( 1024 );
1272 globals->get_options()->set_ysize( 768 );
1273 FG_LOG( FG_GENERAL, FG_INFO,
1274 "It looks like you have two sli'd voodoo-2's." << endl
1275 << "upgrading your win resolution to 1024 x 768" );
1276 glutReshapeWindow(1024, 768);
1278 // probably a single non-SLI'd Voodoo-2
1279 globals->get_options()->set_xsize( 800 );
1280 globals->get_options()->set_ysize( 600 );
1281 FG_LOG( FG_GENERAL, FG_INFO,
1282 "It looks like you have a voodoo-2." << endl
1283 << "upgrading your win resolution to 800 x 600" );
1284 glutReshapeWindow(800, 600);
1286 } else if ( renderer.find( "FB/2" ) != string::npos ) {
1287 // probably a voodoo-1, stick with the default
1290 // we have no special knowledge of this card, stick with the default
1298 // Initialize GLUT event handlers
1299 int fgGlutInitEvents( void ) {
1300 // call fgReshape() on window resizes
1301 glutReshapeFunc( fgReshape );
1303 // call GLUTkey() on keyboard event
1304 glutKeyboardFunc( GLUTkey );
1305 glutSpecialFunc( GLUTspecialkey );
1307 // call guiMouseFunc() whenever our little rodent is used
1308 glutMouseFunc ( guiMouseFunc );
1309 glutMotionFunc (guiMotionFunc );
1310 glutPassiveMotionFunc (guiMotionFunc );
1312 // call fgMainLoop() whenever there is
1313 // nothing else to do
1314 glutIdleFunc( fgIdleFunction );
1317 glutDisplayFunc( fgRenderFrame );
1324 int main( int argc, char **argv ) {
1326 #if defined( macintosh )
1327 freopen ("stdout.txt", "w", stdout );
1328 freopen ("stderr.txt", "w", stderr );
1329 argc = ccommand( &argv );
1333 _control87(MCW_EM, MCW_EM); /* defined in float.h */
1336 // set default log levels
1337 fglog().setLogLevels( FG_ALL, FG_INFO );
1339 FG_LOG( FG_GENERAL, FG_INFO, "FlightGear: Version "
1340 << FLIGHTGEAR_VERSION << endl );
1342 // seed the random number generater
1345 // Allocate global data structures. This needs to happen before
1346 // we parse command line options
1347 globals = new FGGlobals;
1349 SGRoute *route = new SGRoute;
1350 globals->set_route( route );
1352 FGOptions *options = new FGOptions;
1353 globals->set_options( options );
1355 FGViewer *pv = new FGViewer;
1356 globals->set_current_view( pv );
1357 // FGViewer *cv = new FGViewer;
1358 // globals->set_current_view( cv );
1360 // Scan the config file(s) and command line options to see if
1361 // fg_root was specified (ignore all other options for now)
1362 fgInitFGRoot(argc, argv);
1364 // cout << "1. airport_id = " << globals->get_options()->get_airport_id() << endl;
1366 // Read global preferences from $FG_ROOT/preferences.xml
1367 FGPath props_path(globals->get_options()->get_fg_root());
1368 props_path.append("preferences.xml");
1369 FG_LOG(FG_INPUT, FG_INFO, "Reading global preferences");
1370 if (!readPropertyList(props_path.str(), ¤t_properties)) {
1371 FG_LOG(FG_INPUT, FG_ALERT, "Failed to read global preferences from "
1372 << props_path.str());
1374 FG_LOG(FG_INPUT, FG_INFO, "Finished Reading global preferences");
1377 // cout << "2. airport_id = " << globals->get_options()->get_airport_id() << endl;
1379 // Initialize the Aircraft directory to "" (UIUC)
1382 // Load the configuration parameters
1383 if ( !fgInitConfig(argc, argv) ) {
1384 FG_LOG( FG_GENERAL, FG_ALERT, "Config option parsing failed ..." );
1388 // Initialize the Window/Graphics environment.
1389 if( !fgGlutInit(&argc, argv) ) {
1390 FG_LOG( FG_GENERAL, FG_ALERT, "GLUT initialization failed ..." );
1394 // Initialize the various GLUT Event Handlers.
1395 if( !fgGlutInitEvents() ) {
1396 FG_LOG( FG_GENERAL, FG_ALERT,
1397 "GLUT event handler initialization failed ..." );
1401 // Initialize ssg (from plib). Needs to come before we do any
1402 // other ssg stuff, but after opengl/glut has been initialized.
1405 // Initialize the user interface (we need to do this before
1406 // passing off control to glut and before fgInitGeneral to get our
1410 // set current_options lon/lat if an airport id is specified
1411 // cout << "3. airport_id = " << globals->get_options()->get_airport_id() << endl;
1412 if ( globals->get_options()->get_airport_id().length() ) {
1413 // fgSetPosFromAirportID( globals->get_options()->get_airport_id() );
1414 fgSetPosFromAirportIDandHdg( globals->get_options()->get_airport_id(),
1415 globals->get_options()->get_heading() );
1419 FGPath zone( globals->get_options()->get_fg_root() );
1420 zone.append( "Timezone" );
1421 SGTime *t = new SGTime( globals->get_options()->get_lon() * DEG_TO_RAD,
1422 globals->get_options()->get_lat() * DEG_TO_RAD,
1425 // Handle potential user specified time offsets
1426 time_t cur_time = t->get_cur_time();
1427 time_t currGMT = sgTimeGetGMT( gmtime(&cur_time) );
1428 time_t systemLocalTime = sgTimeGetGMT( localtime(&cur_time) );
1429 time_t aircraftLocalTime =
1430 sgTimeGetGMT( fgLocaltime(&cur_time, t->get_zonename() ) );
1432 // Okay, we now have six possible scenarios
1433 switch ( globals->get_options()->get_time_offset_type() ) {
1434 case FGOptions::FG_TIME_SYS_OFFSET:
1435 globals->set_warp( globals->get_options()->get_time_offset() );
1437 case FGOptions::FG_TIME_GMT_OFFSET:
1438 globals->set_warp( globals->get_options()->get_time_offset() -
1439 (currGMT - systemLocalTime) );
1441 case FGOptions::FG_TIME_LAT_OFFSET:
1442 globals->set_warp( globals->get_options()->get_time_offset() -
1443 (aircraftLocalTime - systemLocalTime) );
1445 case FGOptions::FG_TIME_SYS_ABSOLUTE:
1446 globals->set_warp( globals->get_options()->get_time_offset() -
1448 //printf("warp = %d\n", warp);
1450 case FGOptions::FG_TIME_GMT_ABSOLUTE:
1451 globals->set_warp( globals->get_options()->get_time_offset() -
1454 case FGOptions::FG_TIME_LAT_ABSOLUTE:
1455 globals->set_warp( globals->get_options()->get_time_offset() -
1456 (aircraftLocalTime - systemLocalTime) -
1460 FG_LOG( FG_GENERAL, FG_ALERT, "Unsupported type" );
1464 FG_LOG( FG_GENERAL, FG_INFO, "After time init, warp = "
1465 << globals->get_warp() );
1467 globals->set_warp_delta( 0 );
1469 t->update( 0.0, 0.0, globals->get_warp() );
1471 globals->set_time_params( t );
1473 // Do some quick general initializations
1474 if( !fgInitGeneral()) {
1475 FG_LOG( FG_GENERAL, FG_ALERT,
1476 "General initializations failed ..." );
1481 // some ssg test stuff (requires data from the plib source
1482 // distribution) specifically from the ssg tux example
1485 FGPath modelpath( globals->get_options()->get_fg_root() );
1486 // modelpath.append( "Models" );
1487 // modelpath.append( "Geometry" );
1489 FGPath texturepath( globals->get_options()->get_fg_root() );
1490 texturepath.append( "Models" );
1491 texturepath.append( "Textures" );
1493 ssgModelPath( (char *)modelpath.c_str() );
1494 ssgTexturePath( (char *)texturepath.c_str() );
1497 scene = new ssgRoot;
1498 scene->setName( "Scene" );
1500 // Initialize the sky
1501 FGPath ephem_data_path( globals->get_options()->get_fg_root() );
1502 ephem_data_path.append( "Astro" );
1503 SGEphemeris *ephem = new SGEphemeris( ephem_data_path.c_str() );
1504 ephem->update( globals->get_time_params()->getMjd(),
1505 globals->get_time_params()->getLst(),
1507 globals->set_ephem( ephem );
1509 FGPath sky_tex_path( globals->get_options()->get_fg_root() );
1510 sky_tex_path.append( "Textures" );
1511 sky_tex_path.append( "Sky" );
1513 thesky->texture_path( sky_tex_path.str() );
1515 thesky->build( 550.0, 550.0,
1516 globals->get_ephem()->getNumPlanets(),
1517 globals->get_ephem()->getPlanets(), 60000.0,
1518 globals->get_ephem()->getNumStars(),
1519 globals->get_ephem()->getStars(), 60000.0 );
1521 if ( globals->get_options()->get_clouds() == true ) {
1522 thesky->add_cloud_layer( 2600.0, 200.0, 50.0, 40000.0,
1523 SG_CLOUD_MOSTLY_SUNNY );
1524 thesky->add_cloud_layer( 6000.0, 20.0, 10.0, 40000.0,
1526 // thesky->add_cloud_layer( 1000.0, 200.0, 50.0,
1527 // SG_CLOUD_MOSTLY_SUNNY );
1528 // thesky->add_cloud_layer( 1800.0, 400.0, 100.0, SG_CLOUD_OVERCAST );
1529 // thesky->add_cloud_layer( 5000.0, 20.0, 10.0, SG_CLOUD_CIRRUS );
1532 // Initialize MagVar model
1533 SGMagVar *magvar = new SGMagVar();
1534 globals->set_mag( magvar );
1537 terrain = new ssgBranch;
1538 terrain->setName( "Terrain" );
1539 scene->addKid( terrain );
1541 // temporary visible aircraft "own ship"
1542 penguin_sel = new ssgSelector;
1543 penguin_pos = new ssgTransform;
1545 current_properties.getStringValue("/sim/model/path",
1546 "Models/Geometry/glider.ac");
1547 ssgEntity *tux_obj = ssgLoad((char *)(tux_path.c_str()));
1549 // align the model properly for FGFS
1550 ssgTransform *tux_align = new ssgTransform;
1551 tux_align->addKid(tux_obj);
1554 current_properties.getFloatValue("/sim/model/h-rotation", 0.0);
1556 current_properties.getFloatValue("/sim/model/p-rotation", 0.0);
1558 current_properties.getFloatValue("/sim/model/r-rotation", 0.0);
1559 sgMakeRotMat4(tux_matrix, h_rot, h_rot, r_rot);
1560 tux_align->setTransform(tux_matrix);
1562 penguin_pos->addKid( tux_align );
1563 penguin_sel->addKid( penguin_pos );
1564 ssgFlatten( tux_obj );
1565 ssgStripify( penguin_sel );
1566 penguin_sel->clrTraversalMaskBits( SSGTRAV_HOT );
1567 scene->addKid( penguin_sel );
1569 // $$$ begin - added VS Renganthan 17 Oct 2K
1571 // $$$ end - added VS Renganthan 17 Oct 2K
1573 #ifdef FG_NETWORK_OLK
1574 // Do the network intialization
1575 if ( globals->get_options()->get_network_olk() ) {
1576 printf("Multipilot mode %s\n", fg_net_init( scene ) );
1580 // build our custom render states
1581 fgBuildRenderStates();
1583 // pass control off to the master GLUT event handler
1586 // we never actually get here ... but to avoid compiler warnings,
1592 // $$$ end - added VS Renganathan, 15 Oct 2K
1593 void fgLoadDCS(void) {
1595 string obj_filename;
1597 FGPath tile_path( globals->get_options()->get_fg_root());
1598 tile_path.append( "Scenery" );
1599 tile_path.append( "Objects.txt" );
1600 fg_gzifstream in( tile_path.str() );
1601 if ( ! in.is_open() ) {
1602 FG_LOG( FG_TERRAIN, FG_ALERT, "Cannot open file: " << tile_path.str() );
1605 FGPath modelpath( globals->get_options()->get_fg_root() );
1606 modelpath.append( "Models" );
1607 modelpath.append( "Geometry" );
1609 FGPath texturepath( globals->get_options()->get_fg_root() );
1610 texturepath.append( "Models" );
1611 texturepath.append( "Textures" );
1613 ssgModelPath( (char *)modelpath.c_str() );
1614 ssgTexturePath( (char *)texturepath.c_str() );
1616 // while ( ! in.eof() ) {
1617 // in >> obj_filename >> obj_lat >> obj_lon >> obj_alt;
1618 // totalDCSobj = totalDCSobj+1;
1620 ship_sel = new ssgSelector;
1621 ship_pos = new ssgTransform;
1622 ssgEntity *ship_obj = ssgLoadOBJ( "saratoga.obj" );
1623 if ( ship_obj != NULL ) {
1624 ship_pos->addKid( ship_obj ); // add object to transform node
1625 ship_sel->addKid( ship_pos ); // add transform node to selector
1626 // ssgFlatten( ship_obj );
1627 // ssgStripify( ship_sel );
1628 ship_sel->clrTraversalMaskBits( SSGTRAV_HOT );
1629 scene->addKid( ship_sel ); //add selector node to root node
1631 FG_LOG( FG_TERRAIN, FG_ALERT, "Cannot open file: " << "saratoga.obj" );
1634 // if (in.eof()) break;
1641 void fgUpdateDCS (void) {
1643 // double eye_lat,eye_lon,eye_alt;
1644 static double obj_lat=15.377603*DEG_TO_RAD;
1645 static double obj_lon= 73.816436*DEG_TO_RAD;
1646 static double obj_alt=0.15;
1647 // static double obj_head;
1648 double sl_radius,obj_latgc;
1649 // float nresultmat[4][4];
1652 obj_lat = obj_lat + 0.0000001;
1654 // Instantaneous Geodetic Lat/Lon/Alt of moving object
1655 // obj_lon = current_aircraft.fdm_state->get_aux5()*DEG_TO_RAD;
1656 // obj_lat = current_aircraft.fdm_state->get_aux6()*DEG_TO_RAD;
1657 // obj_alt = current_aircraft.fdm_state->get_aux7();
1659 // Geodetic to Geocentric angles for rotation
1660 sgGeodToGeoc(obj_lat,obj_alt,&sl_radius,&obj_latgc);
1662 // moving object gbs-posn in cartesian coords
1663 Point3D obj_posn = Point3D( obj_lon,obj_lat,obj_alt);
1664 Point3D obj_pos = sgGeodToCart( obj_posn );
1666 // Translate moving object w.r.t eye
1667 Point3D Objtrans = obj_pos-scenery.center;
1672 // rotate dynamic objects for lat,lon & alt and other motion about its axes
1673 if ( ship_sel != NULL ) {
1674 ship_sel->select(1);
1677 sgMakeTransMat4( sgTRANS, bz[0],bz[1],bz[2]);
1679 sgVec3 ship_fwd,ship_rt,ship_up;
1680 sgSetVec3( ship_fwd, 1.0, 0.0, 0.0);//east,roll
1681 sgSetVec3( ship_rt, 0.0, 1.0, 0.0);//up,pitch
1682 sgSetVec3( ship_up, 0.0, 0.0, 1.0); //north,yaw
1684 sgMat4 sgROT_lon, sgROT_lat, sgROT_hdg;
1685 sgMakeRotMat4( sgROT_lon, obj_lon*RAD_TO_DEG, ship_up );
1686 sgMakeRotMat4( sgROT_lat, 90-obj_latgc*RAD_TO_DEG, ship_rt );
1687 sgMakeRotMat4( sgROT_hdg, 180.0, ship_up );
1690 sgCopyMat4( sgTUX, sgROT_hdg );
1691 sgPostMultMat4( sgTUX, sgROT_lat );
1692 sgPostMultMat4( sgTUX, sgROT_lon );
1693 sgPostMultMat4( sgTUX, sgTRANS );
1696 sgSetCoord( &shippos, sgTUX );
1697 ship_pos->setTransform( &shippos );
1701 // $$$ end - added VS Renganathan, 15 Oct 2K