From f7fc455b0da6a6325b57098a93c05e93a6aad7fb Mon Sep 17 00:00:00 2001 From: curt Date: Fri, 19 Mar 2004 03:30:18 +0000 Subject: [PATCH] Jim Wilson: Remove some hardcoded dependencies between fdm, viewer and acmodel classes and replaced them with property references. Fix roll offset in viewer. --- src/FDM/YASim/YASim.cxx | 6 +-- src/FDM/flight.cxx | 17 ++------- src/Main/main.cxx | 47 +++++++++++++----------- src/Main/viewer.cxx | 81 +++++++++++++++++++++++++++++++++++++---- src/Main/viewer.hxx | 6 +++ src/Main/viewmgr.cxx | 50 +++++++++++++++++++++++++ src/Main/viewmgr.hxx | 4 ++ src/Model/acmodel.cxx | 2 +- 8 files changed, 164 insertions(+), 49 deletions(-) diff --git a/src/FDM/YASim/YASim.cxx b/src/FDM/YASim/YASim.cxx index 730d57e12..8e1e1e96b 100644 --- a/src/FDM/YASim/YASim.cxx +++ b/src/FDM/YASim/YASim.cxx @@ -2,13 +2,11 @@ #include #include #include -#include #include #include #include
#include
-#include #include "FGFDM.hpp" #include "Atmosphere.hpp" @@ -211,8 +209,8 @@ void YASim::copyToYASim(bool copyState) wind[1] = get_V_east_airmass() * FT2M * -1.0; wind[2] = get_V_down_airmass() * FT2M * -1.0; - // Get ground elevation from the FGinterface's FGlocation data - double ground = getACModel()->get3DModel()->getSGLocation()->get_cur_elev_m(); + // Get ground elevation + double ground = fgGetDouble("/position/ground-elev-m"); // cout << "YASIM: ground = " << ground << endl; float pressure = fgGetFloat("/environment/pressure-inhg") * INHG2PA; diff --git a/src/FDM/flight.cxx b/src/FDM/flight.cxx index 1f9d650c0..93125bba3 100644 --- a/src/FDM/flight.cxx +++ b/src/FDM/flight.cxx @@ -28,7 +28,6 @@ #include #include #include -#include #include #include @@ -36,7 +35,6 @@ #include #include
#include
-#include #include "flight.hxx" @@ -163,7 +161,6 @@ FGInterface::_setup () sin_latitude=cos_latitude=0; sin_longitude=cos_longitude=0; altitude_agl=0; - _acmodel = 0; } void @@ -187,12 +184,6 @@ FGInterface::common_init () // stamp(); // set_remainder( 0 ); - // linking in FGAircraft instance... - // FIXME: when using multiple instances, then there will be more than - // one model so get_aircraft_model will have to be indexed to the correct - // model. - _acmodel = globals->get_aircraft_model(); - // Set initial position SG_LOG( SG_FLIGHT, SG_INFO, "...initializing position..." ); set_Longitude( fgGetDouble("/sim/presets/longitude-deg") @@ -201,7 +192,7 @@ FGInterface::common_init () * SGD_DEGREES_TO_RADIANS ); double ground_elev_m = globals->get_scenery()->get_cur_elev(); double ground_elev_ft = ground_elev_m * SG_METER_TO_FEET; - _acmodel->get3DModel()->getSGLocation()->set_cur_elev_m( ground_elev_m ); + fgSetDouble("/position/ground-elev-m", ground_elev_m); _set_Runway_altitude ( ground_elev_ft ); if ( fgGetBool("/sim/presets/onground") || fgGetDouble("/sim/presets/altitude-ft") < ground_elev_ft ) { @@ -503,9 +494,7 @@ void FGInterface::_updateGeodeticPosition( double lat, double lon, double alt ) _set_Geodetic_Position( lat, lon, alt ); _set_Sea_level_radius( sl_radius * SG_METER_TO_FEET ); - if ( getACModel() != NULL ) { - _set_Runway_altitude( getACModel()->get3DModel()->getSGLocation()->get_cur_elev_m() * SG_METER_TO_FEET ); - } + _set_Runway_altitude( fgGetDouble("/position/ground-elev-m") * SG_METER_TO_FEET ); _set_sin_lat_geocentric( lat_geoc ); _set_cos_lat_geocentric( lat_geoc ); @@ -561,7 +550,7 @@ void FGInterface::_updateGeocentricPosition( double lat_geoc, double lon, _set_Geodetic_Position( lat_geod, lon, alt ); _set_Sea_level_radius( sl_radius2 * SG_METER_TO_FEET ); - _set_Runway_altitude( getACModel()->get3DModel()->getSGLocation()->get_cur_elev_m() * SG_METER_TO_FEET ); + _set_Runway_altitude( fgGetDouble("/position/ground-elev-m") * SG_METER_TO_FEET ); _set_sin_lat_geocentric( lat_geoc ); _set_cos_lat_geocentric( lat_geoc ); diff --git a/src/Main/main.cxx b/src/Main/main.cxx index e49ee74e1..8636e3b9d 100644 --- a/src/Main/main.cxx +++ b/src/Main/main.cxx @@ -367,8 +367,6 @@ void fgRenderFrame() { static const SGPropertyNode *groundlevel_nearplane = fgGetNode("/sim/current-view/ground-level-nearplane-m"); - FGViewer *current__view = globals->get_current_view(); - FGLight *l = (FGLight *)(globals->get_subsystem("lighting")); static double last_visibility = -9999; @@ -413,6 +411,8 @@ void fgRenderFrame() { // initializations and are running the main loop, so this will // now work without seg faulting the system. + FGViewer *current__view = globals->get_current_view(); + // calculate our current position in cartesian space globals->get_scenery()->set_center( globals->get_scenery()->get_next_center() ); @@ -980,11 +980,6 @@ static void fgMainLoop( void ) { globals->get_event_mgr()->update(delta_time_sec); - SGLocation * acmodel_location = 0; - if(cur_fdm_state->getACModel() != 0) { - acmodel_location = (SGLocation *) cur_fdm_state->getACModel()->get3DModel()->getSGLocation(); - } - SG_LOG( SG_ALL, SG_DEBUG, "Running Main Loop"); SG_LOG( SG_ALL, SG_DEBUG, "======= ==== ===="); @@ -1154,9 +1149,13 @@ static void fgMainLoop( void ) { double visibility_meters = fgGetDouble("/environment/visibility-m"); FGViewer *current_view = globals->get_current_view(); + // get the location data for the primary FDM (now hardcoded to ac model)... + SGLocation * acmodel_location = 0; + acmodel_location = (SGLocation *) globals->get_aircraft_model()->get3DModel()->getSGLocation(); + // update tile manager for FDM... - // ...only if location is different than the viewer (to avoid duplicating effort) - if( acmodel_location != current_view->getSGLocation() ) { + // ...only if location is different than the current-view location (to avoid duplicating effort) + if( !fgGetBool("/sim/current-view/config/from-model") ) { if( acmodel_location != 0 ) { globals->get_tile_mgr()->prep_ssg_nodes( acmodel_location, visibility_meters ); @@ -1167,6 +1166,7 @@ static void fgMainLoop( void ) { if ( globals->get_scenery()->get_cur_elev() > -9990 ) { acmodel_location-> set_cur_elev_m( globals->get_scenery()->get_cur_elev() ); + fgSetDouble("/position/ground-elev-m", globals->get_scenery()->get_cur_elev()); } acmodel_location-> set_tile_center( globals->get_scenery()->get_next_center() ); @@ -1190,10 +1190,11 @@ static void fgMainLoop( void ) { // If fdm location is same as viewer's then we didn't do the update for fdm location // above so we need to save the viewer results in the fdm SGLocation as well... - if( acmodel_location == current_view->getSGLocation() ) { + if( fgGetBool("/sim/current-view/config/from-model") ) { if( acmodel_location != 0 ) { if ( globals->get_scenery()->get_cur_elev() > -9990 ) { acmodel_location->set_cur_elev_m( globals->get_scenery()->get_cur_elev() ); + fgSetDouble("/position/ground-elev-m", globals->get_scenery()->get_cur_elev()); } acmodel_location->set_tile_center( globals->get_scenery()->get_next_center() ); } @@ -1332,28 +1333,30 @@ void fgReshape( int width, int height ) { view_h = height; } - // for all views - FGViewMgr *viewmgr = globals->get_viewmgr(); - for ( int i = 0; i < viewmgr->size(); ++i ) { - viewmgr->get_view(i)-> - set_aspect_ratio((float)view_h / (float)width); - } - glViewport( 0, (GLint)(height - view_h), (GLint)(width), (GLint)(view_h) ); fgSetInt("/sim/startup/xsize", width); fgSetInt("/sim/startup/ysize", height); guiInitMouse(width, height); - ssgSetFOV( viewmgr->get_current_view()->get_h_fov(), - viewmgr->get_current_view()->get_v_fov() ); + // for all views + FGViewMgr *viewmgr = globals->get_viewmgr(); + if (viewmgr) { + for ( int i = 0; i < viewmgr->size(); ++i ) { + viewmgr->get_view(i)-> + set_aspect_ratio((float)view_h / (float)width); + } - fgHUDReshape(); + ssgSetFOV( viewmgr->get_current_view()->get_h_fov(), + viewmgr->get_current_view()->get_v_fov() ); #ifdef FG_USE_CLOUDS_3D - sgClouds3d->Resize( viewmgr->get_current_view()->get_h_fov(), - viewmgr->get_current_view()->get_v_fov() ); + sgClouds3d->Resize( viewmgr->get_current_view()->get_h_fov(), + viewmgr->get_current_view()->get_v_fov() ); #endif + } + + fgHUDReshape(); } diff --git a/src/Main/viewer.cxx b/src/Main/viewer.cxx index 5c53c6af9..67f4cec18 100644 --- a/src/Main/viewer.cxx +++ b/src/Main/viewer.cxx @@ -57,7 +57,8 @@ // by considering them to be 3x3 until the very end -- NHV static void MakeVIEW_OFFSET( sgMat4 dst, const float angle1, const sgVec3 axis1, - const float angle2, const sgVec3 axis2 ) + const float angle2, const sgVec3 axis2, + const float angle3, const sgVec3 axis3 ) { // make rotmatrix1 from angle and axis float s = (float) sin ( angle1 ) ; @@ -101,20 +102,59 @@ static void MakeVIEW_OFFSET( sgMat4 dst, mat2[2][1] = tmp * axis2[1] - s * axis2[0] ; mat2[2][2] = tmp * axis2[2] + c ; + + // make rotmatrix3 from angle and axis (roll) + s = (float) sin ( angle3 ) ; + c = (float) cos ( angle3 ) ; + t = SG_ONE - c ; + + sgMat3 mat3; + tmp = t * axis3[0]; + mat3[0][0] = tmp * axis3[0] + c ; + mat3[0][1] = tmp * axis3[1] + s * axis3[2] ; + mat3[0][2] = tmp * axis3[2] - s * axis3[1] ; + + tmp = t * axis2[1]; + mat3[1][0] = tmp * axis3[0] - s * axis3[2] ; + mat3[1][1] = tmp * axis3[1] + c ; + mat3[1][2] = tmp * axis3[2] + s * axis3[0] ; + + tmp = t * axis3[2]; + mat3[2][0] = tmp * axis3[0] + s * axis3[1] ; + mat3[2][1] = tmp * axis3[1] - s * axis3[0] ; + mat3[2][2] = tmp * axis3[2] + c ; + + sgMat3 matt; + // multiply matrices for ( int j = 0 ; j < 3 ; j++ ) { - dst[0][j] = mat2[0][0] * mat1[0][j] + + matt[0][j] = mat2[0][0] * mat1[0][j] + mat2[0][1] * mat1[1][j] + mat2[0][2] * mat1[2][j]; - dst[1][j] = mat2[1][0] * mat1[0][j] + + matt[1][j] = mat2[1][0] * mat1[0][j] + mat2[1][1] * mat1[1][j] + mat2[1][2] * mat1[2][j]; - dst[2][j] = mat2[2][0] * mat1[0][j] + + matt[2][j] = mat2[2][0] * mat1[0][j] + mat2[2][1] * mat1[1][j] + mat2[2][2] * mat1[2][j]; } + + // multiply matrices + for ( int j = 0 ; j < 3 ; j++ ) { + dst[0][j] = mat3[0][0] * matt[0][j] + + mat3[0][1] * matt[1][j] + + mat3[0][2] * matt[2][j]; + + dst[1][j] = mat3[1][0] * matt[0][j] + + mat3[1][1] * matt[1][j] + + mat3[1][2] * matt[2][j]; + + dst[2][j] = mat3[2][0] * matt[0][j] + + mat3[2][1] * matt[1][j] + + mat3[2][2] * matt[2][j]; + } // fill in 4x4 matrix elements dst[0][3] = SG_ZERO; dst[1][3] = SG_ZERO; @@ -566,7 +606,6 @@ FGViewer::recalcLookFrom () sgSetVec3( right, LOCAL[1][0], LOCAL[1][1], LOCAL[1][2] ); sgSetVec3( forward, -LOCAL[0][0], -LOCAL[0][1], -LOCAL[0][2] ); - // Note that when in "lookfrom" view the "view up" vector is always applied // to the viewer. View up is based on verticle of the aircraft itself. (see // "LOCAL" matrix above) @@ -574,7 +613,8 @@ FGViewer::recalcLookFrom () // Orientation Offsets matrix MakeVIEW_OFFSET( VIEW_OFFSET, _heading_offset_deg * SG_DEGREES_TO_RADIANS, _view_up, - _pitch_offset_deg * SG_DEGREES_TO_RADIANS, right ); + _pitch_offset_deg * SG_DEGREES_TO_RADIANS, right, + _roll_offset_deg * SG_DEGREES_TO_RADIANS, forward ); // Make the VIEW matrix. sgSetVec4(VIEW[0], right[0], right[1], right[2],SG_ZERO); @@ -608,7 +648,7 @@ void FGViewer::recalcLookAt () { - sgVec3 right; + sgVec3 right, forward; sgVec3 eye_pos, at_pos; sgVec3 position_offset; // eye position offsets (xyz) sgVec3 target_position_offset; // target position offsets (xyz) @@ -653,6 +693,7 @@ FGViewer::recalcLookAt () // make sg vectors view up, right and forward vectors from LOCAL sgSetVec3( _view_up, LOCAL[2][0], LOCAL[2][1], LOCAL[2][2] ); sgSetVec3( right, LOCAL[1][0], LOCAL[1][1], LOCAL[1][2] ); + sgSetVec3( forward, -LOCAL[0][0], -LOCAL[0][1], -LOCAL[0][2] ); // rotate model or local matrix to get a matrix to apply Eye Position Offsets sgMat4 VIEW_UP; // L0 forward L1 right L2 up @@ -664,7 +705,8 @@ FGViewer::recalcLookAt () // get Orientation Offsets matrix MakeVIEW_OFFSET( VIEW_OFFSET, (_heading_offset_deg - 180) * SG_DEGREES_TO_RADIANS, _view_up, - _pitch_offset_deg * SG_DEGREES_TO_RADIANS, right ); + _pitch_offset_deg * SG_DEGREES_TO_RADIANS, right, + _roll_offset_deg * SG_DEGREES_TO_RADIANS, forward ); // add in the position offsets sgSetVec3( position_offset, _y_offset_m, _x_offset_m, _z_offset_m ); @@ -887,4 +929,27 @@ FGViewer::update (double dt) } } } + + + for ( i = 0; i < dt_ms; i++ ) { + if ( fabs( _goal_roll_offset_deg - _roll_offset_deg ) < 1 ) { + setRollOffset_deg( _goal_roll_offset_deg ); + break; + } else { + // move current_view.roll_offset_deg towards + // current_view.goal_roll_offset + if ( _goal_roll_offset_deg > _roll_offset_deg ) + { + incRollOffset_deg( 1.0 ); + } else { + incRollOffset_deg( -1.0 ); + } + if ( _roll_offset_deg > 90 ) { + setRollOffset_deg(90); + } else if ( _roll_offset_deg < -90 ) { + setRollOffset_deg( -90 ); + } + } + } + } diff --git a/src/Main/viewer.hxx b/src/Main/viewer.hxx index 26e581387..3f9c95e29 100644 --- a/src/Main/viewer.hxx +++ b/src/Main/viewer.hxx @@ -405,6 +405,12 @@ private: _pitch_offset_deg += amt; } + // add to _roll_offset_deg + inline void incRollOffset_deg( double amt ) { + set_dirty(); + _roll_offset_deg += amt; + } + inline void set_reverse_view_offset( bool val ) { _reverse_view_offset = val; } diff --git a/src/Main/viewmgr.cxx b/src/Main/viewmgr.cxx index 39e7ac95b..1d8dd6ab9 100644 --- a/src/Main/viewmgr.cxx +++ b/src/Main/viewmgr.cxx @@ -271,6 +271,14 @@ FGViewMgr::bind () &FGViewMgr::getGoalViewPitchOffset_deg, &FGViewMgr::setGoalViewPitchOffset_deg); fgSetArchivable("/sim/current-view/goal-pitch-offset-deg"); + fgTie("/sim/current-view/roll-offset-deg", this, + &FGViewMgr::getViewRollOffset_deg, + &FGViewMgr::setViewRollOffset_deg); + fgSetArchivable("/sim/current-view/roll-offset-deg"); + fgTie("/sim/current-view/goal-roll-offset-deg", this, + &FGViewMgr::getGoalViewRollOffset_deg, + &FGViewMgr::setGoalViewRollOffset_deg); + fgSetArchivable("/sim/current-view/goal-roll-offset-deg"); fgTie("/sim/current-view/view-number", this, &FGViewMgr::getView, &FGViewMgr::setView); @@ -435,6 +443,12 @@ FGViewMgr::copyToCurrent() fgSetDouble("/sim/current-view/config/default-field-of-view-deg", fgGetDouble(nodepath.c_str())); + nodepath = viewpath; + nodepath += "/config/from-model"; + fgSetBool("/sim/current-view/config/from-model", + fgGetBool(nodepath.c_str())); + + // copy view data fgSetDouble("/sim/current-view/x-offset-m", getViewXOffset_m()); fgSetDouble("/sim/current-view/y-offset-m", getViewYOffset_m()); @@ -457,6 +471,10 @@ FGViewMgr::copyToCurrent() get_current_view()->getTargetYOffset_m()); fgSetDouble("/sim/current-view/target-z-offset-m", get_current_view()->getTargetZOffset_m()); + + fgSetBool("/sim/current-view/internal", + get_current_view()->getInternal()); + } @@ -524,6 +542,38 @@ FGViewMgr::setGoalViewPitchOffset_deg (double tilt) view->setGoalPitchOffset_deg(tilt); } +double +FGViewMgr::getViewRollOffset_deg () const +{ + const FGViewer * view = get_current_view(); + return (view == 0 ? 0 : view->getRollOffset_deg()); +} + +void +FGViewMgr::setViewRollOffset_deg (double tilt) +{ + FGViewer * view = get_current_view(); + if (view != 0) { + view->setGoalRollOffset_deg(tilt); + view->setRollOffset_deg(tilt); + } +} + +double +FGViewMgr::getGoalViewRollOffset_deg () const +{ + const FGViewer * view = get_current_view(); + return (view == 0 ? 0 : view->getGoalRollOffset_deg()); +} + +void +FGViewMgr::setGoalViewRollOffset_deg (double tilt) +{ + FGViewer * view = get_current_view(); + if (view != 0) + view->setGoalRollOffset_deg(tilt); +} + double FGViewMgr::getViewXOffset_m () const { diff --git a/src/Main/viewmgr.hxx b/src/Main/viewmgr.hxx index b4a4fbf0e..f71635694 100644 --- a/src/Main/viewmgr.hxx +++ b/src/Main/viewmgr.hxx @@ -130,6 +130,10 @@ private: double getViewPitchOffset_deg () const; void setViewPitchOffset_deg (double tilt); double getGoalViewPitchOffset_deg () const; + void setGoalViewRollOffset_deg (double tilt); + double getViewRollOffset_deg () const; + void setViewRollOffset_deg (double tilt); + double getGoalViewRollOffset_deg () const; void setGoalViewPitchOffset_deg (double tilt); double getViewXOffset_m () const; void setViewXOffset_m (double x); diff --git a/src/Model/acmodel.cxx b/src/Model/acmodel.cxx index e209416c9..cb96dd7e5 100644 --- a/src/Model/acmodel.cxx +++ b/src/Model/acmodel.cxx @@ -92,7 +92,7 @@ void FGAircraftModel::update (double dt) { int view_number = globals->get_viewmgr()->get_current(); - int is_internal = globals->get_current_view()->getInternal(); + int is_internal = fgGetBool("/sim/current-view/internal"); if (view_number == 0 && !is_internal) { _aircraft->setVisible(false); -- 2.39.5