From a0d90b9746e8deb331bd1075de0bf052357c5d16 Mon Sep 17 00:00:00 2001 From: ehofman Date: Mon, 14 Jul 2003 14:20:45 +0000 Subject: [PATCH] Framerate independent viewer fixes from Melchior FRANZ --- src/Main/viewer.cxx | 126 ++++++++++++++++++++++++++----------------- src/Main/viewer.hxx | 14 +++-- src/Main/viewmgr.cxx | 30 ++++++++--- 3 files changed, 110 insertions(+), 60 deletions(-) diff --git a/src/Main/viewer.cxx b/src/Main/viewer.cxx index 41f01e4ad..21e3d42f2 100644 --- a/src/Main/viewer.cxx +++ b/src/Main/viewer.cxx @@ -131,9 +131,10 @@ static void MakeVIEW_OFFSET( sgMat4 dst, //////////////////////////////////////////////////////////////////////// // Constructor... -FGViewer::FGViewer( fgViewType Type, bool from_model, int from_model_index, - bool at_model, int at_model_index, double at_model_damping, - double x_offset_m, double y_offset_m, double z_offset_m, +FGViewer::FGViewer( fgViewType Type, bool from_model, int from_model_index, + bool at_model, int at_model_index, + double damp_alt, double damp_roll, double damp_pitch, double damp_heading, + double x_offset_m, double y_offset_m, double z_offset_m, double heading_offset_deg, double pitch_offset_deg, double roll_offset_deg, double fov_deg, double target_x_offset_m, double target_y_offset_m, @@ -148,6 +149,10 @@ FGViewer::FGViewer( fgViewType Type, bool from_model, int from_model_index, _roll_deg(0), _pitch_deg(0), _heading_deg(0), + _damp_alt(0), + _damp_roll(0), + _damp_pitch(0), + _damp_heading(0), _scaling_type(FG_SCALING_MAX) { sgdZeroVec3(_absolute_view_pos); @@ -156,13 +161,14 @@ FGViewer::FGViewer( fgViewType Type, bool from_model, int from_model_index, _from_model_index = from_model_index; _at_model = at_model; _at_model_index = at_model_index; - - _damp = _damp_alt = 0.0; - if (at_model_damping > 0.0) { - _damp = 1 - 1.0 / pow(10, at_model_damping); - _damp_alt = _damp * _damp * _damp * _damp; - } else if (at_model_damping < 0.0) - _damp = 1 - 1.0 / pow(10, -at_model_damping); + if (damp_alt > 0.0) + _damp_alt = 1 - 1.0 / pow(10, fabs(damp_alt)); + if (damp_roll > 0.0) + _damp_roll = 1 - 1.0 / pow(10, fabs(damp_roll)); + if (damp_pitch > 0.0) + _damp_pitch = 1 - 1.0 / pow(10, fabs(damp_pitch)); + if (damp_heading > 0.0) + _damp_heading = 1 - 1.0 / pow(10, fabs(damp_heading)); _x_offset_m = x_offset_m; _y_offset_m = y_offset_m; @@ -502,44 +508,7 @@ FGViewer::recalcOurOwnLocation (SGLocation * location, double lon_deg, double la double roll_deg, double pitch_deg, double heading_deg) { // update from our own data... - if (_damp > 0.0) { - - static FGViewer *last = 0; - if (last != this) { - _damped_alt_ft = alt_ft; - _damped_roll_deg = roll_deg; - _damped_pitch_deg = pitch_deg; - _damped_heading_deg = heading_deg; - last = this; - } - - double d; - d = _damped_roll_deg - roll_deg; - if (d >= 180.0) - _damped_roll_deg -= 360.0; - else if (d < -180.0) - _damped_roll_deg += 360.0; - - d = _damped_pitch_deg - pitch_deg; - if (d >= 180.0) - _damped_pitch_deg -= 360.0; - else if (d < -180.0) - _damped_pitch_deg += 360.0; - - d = _damped_heading_deg - heading_deg; - if (d >= 180.0) - _damped_heading_deg -= 360.0; - else if (d < -180.0) - _damped_heading_deg += 360.0; - - d = 1.0 - _damp; - roll_deg = _damped_roll_deg = roll_deg * d + _damped_roll_deg * _damp; - pitch_deg = _damped_pitch_deg = pitch_deg * d + _damped_pitch_deg * _damp; - heading_deg = _damped_heading_deg = heading_deg * d + _damped_heading_deg * _damp; - - if (_damp_alt > 0.0) - alt_ft = _damped_alt_ft = alt_ft * (1 - _damp_alt) + _damped_alt_ft * _damp_alt; - } + dampEyeData(alt_ft, roll_deg, pitch_deg, heading_deg); location->setPosition( lon_deg, lat_deg, alt_ft ); location->setOrientation( roll_deg, pitch_deg, heading_deg ); sgCopyMat4(LOCAL, @@ -665,6 +634,7 @@ FGViewer::recalcLookAt () updateFromModelLocation(_location); } else { // update from our own data, just the rotation here... + recalcOurOwnLocation( _location, _lon_deg, _lat_deg, _alt_ft, _roll_deg, _pitch_deg, _heading_deg ); } @@ -752,6 +722,64 @@ FGViewer::copyLocationData() sgCopyVec3(_view_pos, _relative_view_pos); } +void +FGViewer::dampEyeData (double &alt_ft, double &roll_deg, double &pitch_deg, double &heading_deg) +{ + static FGViewer *last = 0; + if (last != this) { + _damp_sync = 0.0; + _damped_alt_ft = alt_ft; + _damped_roll_deg = roll_deg; + _damped_pitch_deg = pitch_deg; + _damped_heading_deg = heading_deg; + last = this; + return; + } + + if (_damp_sync < 0.01) { + alt_ft = _damped_alt_ft; + roll_deg = _damped_roll_deg; + pitch_deg = _damped_pitch_deg; + heading_deg = _damped_heading_deg; + return; + } + + while (_damp_sync >= 0.01) { + _damp_sync -= 0.01; + + double d; + if (_damp_alt > 0.0) + alt_ft = _damped_alt_ft = alt_ft * (1 - _damp_alt) + _damped_alt_ft * _damp_alt; + + if (_damp_roll > 0.0) { + d = _damped_roll_deg - roll_deg; + if (d >= 180.0) + _damped_roll_deg -= 360.0; + else if (d < -180.0) + _damped_roll_deg += 360.0; + roll_deg = _damped_roll_deg = roll_deg * (1 - _damp_roll) + _damped_roll_deg * _damp_roll; + } + + if (_damp_pitch > 0.0) { + d = _damped_pitch_deg - pitch_deg; + if (d >= 180.0) + _damped_pitch_deg -= 360.0; + else if (d < -180.0) + _damped_pitch_deg += 360.0; + pitch_deg = _damped_pitch_deg = pitch_deg * (1 - _damp_pitch) + _damped_pitch_deg * _damp_pitch; + } + + if (_damp_heading > 0.0) { + d = _damped_heading_deg - heading_deg; + if (d >= 180.0) + _damped_heading_deg -= 360.0; + else if (d < -180.0) + _damped_heading_deg += 360.0; + heading_deg = _damped_heading_deg = heading_deg * (1 - _damp_heading) + _damped_heading_deg * _damp_heading; + } + } +} + double FGViewer::get_h_fov() { @@ -800,6 +828,8 @@ FGViewer::get_v_fov() void FGViewer::update (double dt) { + _damp_sync += dt; + int i; int dt_ms = int(dt * 1000); for ( i = 0; i < dt_ms; i++ ) { diff --git a/src/Main/viewer.hxx b/src/Main/viewer.hxx index a07c3dad3..3ab9929b3 100644 --- a/src/Main/viewer.hxx +++ b/src/Main/viewer.hxx @@ -62,7 +62,8 @@ public: // Constructor FGViewer( fgViewType Type, bool from_model, int from_model_index, - bool at_model, int at_model_index, double damping, + bool at_model, int at_model_index, + double damp_alt, double damp_roll, double damp_pitch, double damp_heading, double x_offset_m, double y_offset_m, double z_offset_m, double heading_offset_deg, double pitch_offset_deg, double roll_offset_deg, double fov_deg, @@ -285,12 +286,16 @@ private: double _target_pitch_deg; double _target_heading_deg; - double _damp; + double _damp_sync; + double _damp_alt; + double _damp_roll; + double _damp_pitch; + double _damp_heading; + + double _damped_alt_ft; double _damped_roll_deg; double _damped_pitch_deg; double _damped_heading_deg; - double _damp_alt; - double _damped_alt_ft; // Position offsets from FDM origin. The X axis is positive // out the tail, Y is out the right wing, and Z is positive up. @@ -383,6 +388,7 @@ private: void updateAtModelLocation (SGLocation * location); void recalcOurOwnLocation (SGLocation * location, double lon_deg, double lat_deg, double alt_ft, double roll_deg, double pitch_deg, double heading_deg); + void dampEyeData (double &alt_ft, double &roll_deg, double &pitch_deg, double &heading_deg); // add to _heading_offset_deg inline void incHeadingOffset_deg( double amt ) { diff --git a/src/Main/viewmgr.cxx b/src/Main/viewmgr.cxx index 766ba8195..3ebcbbc08 100644 --- a/src/Main/viewmgr.cxx +++ b/src/Main/viewmgr.cxx @@ -51,7 +51,7 @@ FGViewMgr::init () bool at_model = false; int from_model_index = 0; int at_model_index = 0; - double at_model_damping; + double damp_alt, damp_roll, damp_pitch, damp_heading; double x_offset_m, y_offset_m, z_offset_m, fov_deg; double heading_offset_deg, pitch_offset_deg, roll_offset_deg; double target_x_offset_m, target_y_offset_m, target_z_offset_m; @@ -96,8 +96,20 @@ FGViewMgr::init () // view damping (0.0: no damping; <0.0: all but alt; >0.0: damp all) nodepath = viewpath; - nodepath += "/config/at-model-damping"; - at_model_damping = fgGetDouble(nodepath.c_str(), 0.0); + nodepath += "/config/at-model-alt-damping"; + damp_alt = fgGetDouble(nodepath.c_str(), 0.0); + + nodepath = viewpath; + nodepath += "/config/at-model-roll-damping"; + damp_roll = fgGetDouble(nodepath.c_str(), 0.0); + + nodepath = viewpath; + nodepath += "/config/at-model-pitch-damping"; + damp_pitch = fgGetDouble(nodepath.c_str(), 0.0); + + nodepath = viewpath; + nodepath += "/config/at-model-heading-damping"; + damp_heading = fgGetDouble(nodepath.c_str(), 0.0); } } @@ -144,15 +156,17 @@ FGViewMgr::init () // supporting two types now "lookat" = 1 and "lookfrom" = 0 if ( strcmp("lookat",strdata.c_str()) == 0 ) add_view(new FGViewer ( FG_LOOKAT, from_model, from_model_index, - at_model, at_model_index, at_model_damping, + at_model, at_model_index, + damp_alt, damp_roll, damp_pitch, damp_heading, x_offset_m, y_offset_m,z_offset_m, heading_offset_deg, pitch_offset_deg, roll_offset_deg, fov_deg, target_x_offset_m, target_y_offset_m, target_z_offset_m, near_m )); else - add_view(new FGViewer ( FG_LOOKFROM, from_model, from_model_index, false, - 0, 0.0, x_offset_m, y_offset_m, z_offset_m, + add_view(new FGViewer ( FG_LOOKFROM, from_model, from_model_index, + false, 0, 0.0, 0.0, 0.0, 0.0, + x_offset_m, y_offset_m, z_offset_m, heading_offset_deg, pitch_offset_deg, roll_offset_deg, fov_deg, 0, 0, 0, near_m )); } @@ -367,8 +381,8 @@ FGViewMgr::update (double dt) nodepath = viewpath; nodepath += "/config/target-heading-deg-path"; heading_deg = fgGetDouble(fgGetString(nodepath.c_str())); - - loop_view ->setTargetPosition(lon_deg, lat_deg, alt_ft); + + loop_view->setTargetPosition(lon_deg, lat_deg, alt_ft); loop_view->setTargetOrientation(roll_deg, pitch_deg, heading_deg); } else { loop_view->set_dirty();