From 76ea9a7968eddac27772bfed21227e6404775ddd Mon Sep 17 00:00:00 2001
From: Torsten Dreyer <Torsten@t3r.de>
Date: Fri, 6 May 2011 14:12:17 +0200
Subject: [PATCH 1/2] Remove unused class SGEnviro

This class and their source files visual_enviro.[h|c]xx were
unused since OSG transition. It's only functionality was to
keep a variable for the enable-state of rendering of precipitation.
This has now been moved to SGPrecipitation.
This patch also removes all references to this class and
visual_enviro.hxx. Wxradar needs a new implementation for cloud
echoes which probably should be part of fgclouds.

See also SimGear commit 4e6159aa07f2817c09fad72db64d713e47f6f3da
---
 src/Environment/environment_mgr.cxx   | 18 ++++++------------
 src/Environment/fgclouds.cxx          |  7 +++++--
 src/Environment/fgclouds.hxx          |  2 ++
 src/Environment/precipitation_mgr.cxx | 16 ++++++++++++++--
 src/Environment/precipitation_mgr.hxx |  5 ++++-
 src/Instrumentation/wxradar.cxx       |  9 ++++++---
 src/Instrumentation/wxradar.hxx       |  4 ++--
 src/Main/renderer.cxx                 |  2 --
 8 files changed, 39 insertions(+), 24 deletions(-)

diff --git a/src/Environment/environment_mgr.cxx b/src/Environment/environment_mgr.cxx
index c6451eef1..e48039029 100644
--- a/src/Environment/environment_mgr.cxx
+++ b/src/Environment/environment_mgr.cxx
@@ -27,7 +27,6 @@
 #include <simgear/constants.h>
 #include <simgear/debug/logstream.hxx>
 #include <simgear/scene/sky/sky.hxx>
-#include <simgear/environment/visual_enviro.hxx>
 #include <simgear/scene/model/particles.hxx>
 
 #include <Main/main.hxx>
@@ -122,9 +121,9 @@ FGEnvironmentMgr::bind ()
           &FGClouds::get_update_event,
           &FGClouds::set_update_event);
 
-  _tiedProperties.Tie("turbulence/use-cloud-turbulence", &sgEnviro,
-          &SGEnviro::get_turbulence_enable_state,
-          &SGEnviro::set_turbulence_enable_state);
+//  _tiedProperties.Tie("turbulence/use-cloud-turbulence", &sgEnviro,
+//          &SGEnviro::get_turbulence_enable_state,
+//          &SGEnviro::set_turbulence_enable_state);
 
   for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
       SGPropertyNode_ptr layerNode = fgGetNode("/environment/clouds",true)->getChild("layer", i, true );
@@ -176,15 +175,10 @@ FGEnvironmentMgr::bind ()
           &SGSky::get_3dCloudVisRange,
           &SGSky::set_3dCloudVisRange);
 
-  _tiedProperties.Tie("precipitation-enable", &sgEnviro,
-          &SGEnviro::get_precipitation_enable_state,
-          &SGEnviro::set_precipitation_enable_state);
+//  _tiedProperties.Tie("lightning-enable", &sgEnviro,
+//          &SGEnviro::get_lightning_enable_state,
+//          &SGEnviro::set_lightning_enable_state);
 
-  _tiedProperties.Tie("lightning-enable", &sgEnviro,
-          &SGEnviro::get_lightning_enable_state,
-          &SGEnviro::set_lightning_enable_state);
-
-  sgEnviro.config(fgGetNode("/sim/rendering/precipitation"));
 }
 
 void
diff --git a/src/Environment/fgclouds.cxx b/src/Environment/fgclouds.cxx
index c57c4ee1d..d5d9fbbc0 100644
--- a/src/Environment/fgclouds.cxx
+++ b/src/Environment/fgclouds.cxx
@@ -30,7 +30,7 @@
 #include <simgear/constants.h>
 #include <simgear/sound/soundmgr_openal.hxx>
 #include <simgear/scene/sky/sky.hxx>
-#include <simgear/environment/visual_enviro.hxx>
+//#include <simgear/environment/visual_enviro.hxx>
 #include <simgear/scene/sky/cloudfield.hxx>
 #include <simgear/scene/sky/newcloud.hxx>
 #include <simgear/structure/commands.hxx>
@@ -47,7 +47,9 @@ extern SGSky *thesky;
 
 
 FGClouds::FGClouds() :
+#if 0
     snd_lightning(0),
+#endif
     clouds_3d_enabled(false),
     index(0)
 {
@@ -68,6 +70,7 @@ void FGClouds::set_update_event(int count) {
 }
 
 void FGClouds::Init(void) {
+#if 0
 	if( snd_lightning == NULL ) {
 		snd_lightning = new SGSoundSample("Sounds/thunder.wav", SGPath());
 		snd_lightning->set_max_dist(7000.0f);
@@ -75,8 +78,8 @@ void FGClouds::Init(void) {
 		SGSoundMgr *smgr = globals->get_soundmgr();
 		SGSampleGroup *sgr = smgr->find("weather", true);
 		sgr->add( snd_lightning, "thunder" );
-		sgEnviro.set_sampleGroup( sgr );
 	}
+#endif
 
 	globals->get_commands()->addCommand("add-cloud", do_add_3Dcloud);
 	globals->get_commands()->addCommand("del-cloud", do_delete_3Dcloud);
diff --git a/src/Environment/fgclouds.hxx b/src/Environment/fgclouds.hxx
index 9d26419ff..61ccaccfe 100644
--- a/src/Environment/fgclouds.hxx
+++ b/src/Environment/fgclouds.hxx
@@ -45,7 +45,9 @@ private:
 	void buildCloudLayers(void);
 
 	int update_event;
+#if 0
 	SGSoundSample *snd_lightning;
+#endif
 	bool clouds_3d_enabled;
   int index;
 
diff --git a/src/Environment/precipitation_mgr.cxx b/src/Environment/precipitation_mgr.cxx
index 727087f83..1f3140547 100644
--- a/src/Environment/precipitation_mgr.cxx
+++ b/src/Environment/precipitation_mgr.cxx
@@ -36,7 +36,6 @@
 #include <simgear/math/SGMath.hxx>
 #include <simgear/scene/sky/sky.hxx>
 #include <simgear/scene/sky/cloud.hxx>
-#include <simgear/environment/visual_enviro.hxx>
 
 #include <Main/fg_props.hxx>
 #include <Main/globals.hxx>
@@ -93,6 +92,19 @@ void FGPrecipitationMgr::init()
     fgGetNode("environment/params/precipitation-level-ft", true);
 }
 
+void FGPrecipitationMgr::bind ()
+{
+  _tiedProperties.setRoot( fgGetNode("/sim/rendering", true ) );
+  _tiedProperties.Tie("precipitation-enable", precipitation.get(),
+          &SGPrecipitation::getEnabled,
+          &SGPrecipitation::setEnabled);
+}
+
+void FGPrecipitationMgr::unbind ()
+{
+  _tiedProperties.Untie();
+}
+
 void FGPrecipitationMgr::setPrecipitationLevel(double a)
 {
     fgSetDouble("environment/params/precipitation-level-ft",a);
@@ -206,7 +218,7 @@ void FGPrecipitationMgr::update(double dt)
     setPrecipitationLevel(altitudeCloudLayer);
 
 	// Does the user enable the precipitation ?
-	if (!sgEnviro.get_precipitation_enable_state()) {
+	if (!precipitation->getEnabled() ) {
 		// Disable precipitations
 	    precipitation->setRainIntensity(0);
 	    precipitation->setSnowIntensity(0);
diff --git a/src/Environment/precipitation_mgr.hxx b/src/Environment/precipitation_mgr.hxx
index 69a0ecfaa..449f7f47c 100644
--- a/src/Environment/precipitation_mgr.hxx
+++ b/src/Environment/precipitation_mgr.hxx
@@ -31,7 +31,7 @@
 
 #include <simgear/structure/subsystem_mgr.hxx>
 #include <simgear/environment/precipitation.hxx>
-
+#include <simgear/props/tiedpropertylist.hxx>
 
 class FGPrecipitationMgr : public SGSubsystem
 {
@@ -40,12 +40,15 @@ private:
     osg::ref_ptr<osg::MatrixTransform> transform;
     osg::ref_ptr<SGPrecipitation> precipitation;
     float getPrecipitationAtAltitudeMax(void);
+    simgear::TiedPropertyList _tiedProperties;
 
 public:
     FGPrecipitationMgr();
     virtual ~FGPrecipitationMgr();
 
     // SGSubsystem methods
+    virtual void bind ();
+    virtual void unbind ();
     virtual void init ();
     virtual void update (double dt);
     
diff --git a/src/Instrumentation/wxradar.cxx b/src/Instrumentation/wxradar.cxx
index d88d0cde9..84d9f84fe 100644
--- a/src/Instrumentation/wxradar.cxx
+++ b/src/Instrumentation/wxradar.cxx
@@ -38,7 +38,6 @@
 
 #include <simgear/constants.h>
 #include <simgear/misc/sg_path.hxx>
-#include <simgear/environment/visual_enviro.hxx>
 #include <simgear/scene/model/model.hxx>
 #include <simgear/structure/exception.hxx>
 #include <simgear/misc/sg_path.hxx>
@@ -137,7 +136,7 @@ wxRadarBg::init ()
     SGPath tpath = globals->resolve_aircraft_path(path);
 
     // no mipmap or else alpha will mix with pixels on the border of shapes, ruining the effect
-    _wxEcho = SGLoadTexture2D(tpath, false, false);
+    _wxEcho = SGLoadTexture2D(tpath, NULL, false, false);
 
 
     _Instrument->setFloatValue("trk", 0.0);
@@ -472,13 +471,16 @@ void
 wxRadarBg::update_weather()
 {
     string modeButton = _Instrument->getStringValue("mode", "WX");
-    _radarEchoBuffer = *sgEnviro.get_radar_echo();
+// FIXME: implementation of radar echoes missing
+//    _radarEchoBuffer = *sgEnviro.get_radar_echo();
 
     // pretend we have a scan angle bigger then the FOV
     // TODO:check real fov, enlarge if < nn, and do clipping if > mm
 //    const float fovFactor = 1.45f;
     _Instrument->setStringValue("status", modeButton.c_str());
 
+// FIXME: implementation of radar echoes missing
+#if 0
     list_of_SGWxRadarEcho *radarEcho = &_radarEchoBuffer;
     list_of_SGWxRadarEcho::iterator iradarEcho, end = radarEcho->end();
     const float LWClevel[] = { 0.1f, 0.5f, 2.1f };
@@ -558,6 +560,7 @@ wxRadarBg::update_weather()
             addQuad(_vertices, _texCoords, m, texBase);
         }
     }
+#endif
 }
 
 
diff --git a/src/Instrumentation/wxradar.hxx b/src/Instrumentation/wxradar.hxx
index f962045e1..2b696430a 100644
--- a/src/Instrumentation/wxradar.hxx
+++ b/src/Instrumentation/wxradar.hxx
@@ -30,7 +30,6 @@
 
 #include <simgear/props/props.hxx>
 #include <simgear/structure/subsystem_mgr.hxx>
-#include <simgear/environment/visual_enviro.hxx>
 
 #include <vector>
 #include <string>
@@ -148,7 +147,8 @@ private:
     float _font_size;
     float _font_spacing;
 
-    list_of_SGWxRadarEcho _radarEchoBuffer;
+// FIXME: implementation of radar echoes missing
+//    list_of_SGWxRadarEcho _radarEchoBuffer;
 
     void update_weather();
     void update_aircraft();
diff --git a/src/Main/renderer.cxx b/src/Main/renderer.cxx
index c88003f1e..fe713ab62 100644
--- a/src/Main/renderer.cxx
+++ b/src/Main/renderer.cxx
@@ -82,8 +82,6 @@
 #include <simgear/screen/jpgfactory.hxx>
 #endif
 
-#include <simgear/environment/visual_enviro.hxx>
-
 #include <Time/light.hxx>
 #include <Time/light.hxx>
 #include <Cockpit/panel.hxx>

From 209a93c4ea09064e02a03c130b57439112a44d5f Mon Sep 17 00:00:00 2001
From: Torsten Dreyer <Torsten@t3r.de>
Date: Sun, 8 May 2011 15:50:33 +0200
Subject: [PATCH 2/2] Expose JSBSim turbulence models

---
 src/FDM/JSBSim/JSBSim.cxx | 73 ++++++++++++++++++++++++++++++++-------
 src/FDM/JSBSim/JSBSim.hxx |  4 +++
 2 files changed, 64 insertions(+), 13 deletions(-)

diff --git a/src/FDM/JSBSim/JSBSim.cxx b/src/FDM/JSBSim/JSBSim.cxx
index e2d98b673..4c6ad3f04 100644
--- a/src/FDM/JSBSim/JSBSim.cxx
+++ b/src/FDM/JSBSim/JSBSim.cxx
@@ -114,12 +114,38 @@ private:
   FGJSBsim* mInterface;
 };
 
+// FG uses a squared normalized magnitude for turbulence
+// this lookup table maps fg's severity levels 
+// none(0), light(1/3), moderate(2/3) and severe(3/3)
+// to the POE table indexes 0, 3, 4 and 7
+class FGTurbulenceSeverityTable : public FGTable {
+public:
+  FGTurbulenceSeverityTable() : FGTable(4) {
+    *this << (0.0/9.0) << 0.0;
+    *this << (1.0/9.0) << 3.0;
+    *this << (4.0/9.0) << 4.0;
+    *this << (9.0/9.0) << 7.0;
+  }
+};
+
 /******************************************************************************/
+std::map<std::string,int> FGJSBsim::TURBULENCE_TYPE_NAMES;
+
+static FGTurbulenceSeverityTable TurbulenceSeverityTable;
 
 FGJSBsim::FGJSBsim( double dt )
   : FGInterface(dt), got_wire(false)
 {
     bool result;
+    if( TURBULENCE_TYPE_NAMES.empty() ) {
+        TURBULENCE_TYPE_NAMES["ttNone"]     = FGAtmosphere::ttNone;
+        TURBULENCE_TYPE_NAMES["ttStandard"] = FGAtmosphere::ttStandard;
+        TURBULENCE_TYPE_NAMES["ttBerndt"]   = FGAtmosphere::ttBerndt;
+        TURBULENCE_TYPE_NAMES["ttCulp"]     = FGAtmosphere::ttCulp;
+        TURBULENCE_TYPE_NAMES["ttMilspec"]  = FGAtmosphere::ttMilspec;
+        TURBULENCE_TYPE_NAMES["ttTustin"]   = FGAtmosphere::ttTustin;
+    }
+
                                 // Set up the debugging level
                                 // FIXME: this will not respond to
                                 // runtime changes
@@ -282,8 +308,10 @@ FGJSBsim::FGJSBsim( double dt )
     temperature = fgGetNode("/environment/temperature-degc",true);
     pressure = fgGetNode("/environment/pressure-inhg",true);
     density = fgGetNode("/environment/density-slugft3",true);
+    ground_wind = fgGetNode("/environment/config/boundary/entry[0]/wind-speed-kt",true);
     turbulence_gain = fgGetNode("/environment/turbulence/magnitude-norm",true);
     turbulence_rate = fgGetNode("/environment/turbulence/rate-hz",true);
+    turbulence_model = fgGetNode("/environment/params/jsbsim-turbulence-model",true);
 
     wind_from_north= fgGetNode("/environment/wind-from-north-fps",true);
     wind_from_east = fgGetNode("/environment/wind-from-east-fps" ,true);
@@ -331,10 +359,12 @@ void FGJSBsim::init()
                   9.0/5.0*(temperature->getDoubleValue()+273.15) );
       Atmosphere->SetExPressure(pressure->getDoubleValue()*70.726566);
       Atmosphere->SetExDensity(density->getDoubleValue());
-      Atmosphere->SetTurbType(FGAtmosphere::ttCulp);
-      Atmosphere->SetTurbGain(turbulence_gain->getDoubleValue());
-      Atmosphere->SetTurbRate(turbulence_rate->getDoubleValue());
-
+      // initialize to no turbulence, these values get set in the update loop
+      Atmosphere->SetTurbType(FGAtmosphere::ttNone);
+      Atmosphere->SetTurbGain(0.0);
+      Atmosphere->SetTurbRate(0.0);
+      Atmosphere->SetWindspeed20ft(0.0);
+      Atmosphere->SetProbabilityOfExceedence(0.0);
     } else {
       Atmosphere->UseInternal();
     }
@@ -351,14 +381,12 @@ void FGJSBsim::init()
      << ", " << fdmex->GetAtmosphere()->GetDensity() );
 
 // deprecate egt_degf for egt-degf to have consistent naming
-// TODO: raise log-level to ALERT in summer 2010, 
-// remove alias in fall 2010, 
-// remove this code in winter 2010
+// TODO: remove this by end of 2011
     for (unsigned int i=0; i < Propulsion->GetNumEngines(); i++) {
       SGPropertyNode * node = fgGetNode("engines/engine", i, true);
       SGPropertyNode * egtn = node->getNode( "egt_degf" );
       if( egtn != NULL ) {
-        SG_LOG(SG_FLIGHT,SG_WARN,
+        SG_LOG(SG_FLIGHT,SG_ALERT,
                "Aircraft uses deprecated node egt_degf. Please upgrade to egt-degf");
         node->getNode("egt-degf", true)->alias( egtn );
       }
@@ -590,7 +618,6 @@ void FGJSBsim::resume()
 
 bool FGJSBsim::copy_to_JSBsim()
 {
-    double tmp;
     unsigned int i;
 
     // copy control positions into the JSBsim structure
@@ -686,11 +713,31 @@ bool FGJSBsim::copy_to_JSBsim()
     Atmosphere->SetExPressure(pressure->getDoubleValue()*70.726566);
     Atmosphere->SetExDensity(density->getDoubleValue());
 
-    tmp = turbulence_gain->getDoubleValue();
-    //Atmosphere->SetTurbGain(tmp * tmp * 100.0);
+    Atmosphere->SetTurbType((FGAtmosphere::tType)TURBULENCE_TYPE_NAMES[turbulence_model->getStringValue()]);
+    switch( Atmosphere->GetTurbType() ) {
+        case FGAtmosphere::ttStandard:
+        case FGAtmosphere::ttCulp:
+        case FGAtmosphere::ttBerndt: {
+            double tmp = turbulence_gain->getDoubleValue();
+            Atmosphere->SetTurbGain(tmp * tmp * 100.0);
+            Atmosphere->SetTurbRate(turbulence_rate->getDoubleValue());
+            break;
+        }
+        case FGAtmosphere::ttMilspec:
+        case FGAtmosphere::ttTustin: {
+            // milspec turbulence: 3=light, 4=moderate, 6=severe turbulence
+            // turbulence_gain normalized: 0: none, 1/3: light, 2/3: moderate, 3/3: severe
+            double tmp = turbulence_gain->getDoubleValue();
+            Atmosphere->SetProbabilityOfExceedence(
+              round(TurbulenceSeverityTable.GetValue( tmp ) )
+            );
+            Atmosphere->SetWindspeed20ft(ground_wind->getDoubleValue());
+            break;
+        }
 
-    tmp = turbulence_rate->getDoubleValue();
-    //Atmosphere->SetTurbRate(tmp);
+        default:
+            break;
+    }
 
     Atmosphere->SetWindNED( -wind_from_north->getDoubleValue(),
                             -wind_from_east->getDoubleValue(),
diff --git a/src/FDM/JSBSim/JSBSim.hxx b/src/FDM/JSBSim/JSBSim.hxx
index ca52f9223..14766c6f6 100644
--- a/src/FDM/JSBSim/JSBSim.hxx
+++ b/src/FDM/JSBSim/JSBSim.hxx
@@ -271,8 +271,10 @@ private:
     SGPropertyNode_ptr temperature;
     SGPropertyNode_ptr pressure;
     SGPropertyNode_ptr density;
+    SGPropertyNode_ptr ground_wind;
     SGPropertyNode_ptr turbulence_gain;
     SGPropertyNode_ptr turbulence_rate;
+    SGPropertyNode_ptr turbulence_model;
 
     SGPropertyNode_ptr wind_from_north;
     SGPropertyNode_ptr wind_from_east;
@@ -280,6 +282,8 @@ private:
 
     SGPropertyNode_ptr slaved;
 
+    static std::map<std::string,int> TURBULENCE_TYPE_NAMES;
+
     double last_hook_tip[3];
     double last_hook_root[3];
     JSBSim::FGColumnVector3 hook_root_struct;