]> git.mxchange.org Git - flightgear.git/commitdiff
cleanup of precipitation contribution
authortimoore <timoore>
Tue, 4 Mar 2008 09:03:54 +0000 (09:03 +0000)
committertimoore <timoore>
Tue, 4 Mar 2008 09:03:54 +0000 (09:03 +0000)
Reindent to Stroustrup style.

Make FGPrecipitationMgr an SGSubsystem and remove all references to it
in main.cxx and renderer.cxx.

Use SGGeod::makeZUpFrame instead of a private function in
tileentry.cxx. Rewrite that function, WorldCoordinate, to use
makeZUpFrame too.

src/Environment/environment_mgr.cxx
src/Environment/environment_mgr.hxx
src/Environment/precipitation_mgr.cxx
src/Environment/precipitation_mgr.hxx
src/Main/main.cxx
src/Main/renderer.cxx
src/Scenery/tileentry.cxx

index b029316e1a597728b4774046f47708e6668a7b06..98178b5b25739290491d373354024412655074eb 100644 (file)
@@ -36,6 +36,7 @@
 #include "environment_ctrl.hxx"
 #include "environment_mgr.hxx"
 #include "fgclouds.hxx"
+#include "precipitation_mgr.hxx"
 
 class SGSky;
 extern SGSky *thesky;
@@ -54,14 +55,18 @@ FGEnvironmentMgr::FGEnvironmentMgr ()
   _controller->setEnvironment(_environment);
   set_subsystem("controller", _controller, 0.5);
   fgClouds = new FGClouds( _controller );
+  _precipitationManager = new FGPrecipitationMgr;
+  set_subsystem("precipitation", _precipitationManager);
 }
 
 FGEnvironmentMgr::~FGEnvironmentMgr ()
 {
   delete _environment;
   remove_subsystem("controller");
+  remove_subsystem("precipitation");
   delete _controller;
   delete fgClouds;
+  delete _precipitationManager;
 }
 
 void
index a0633b170a898be44939e96c24b647648a49e6e5..e45e353309adba6b5e15c08a76bf73e05f61022d 100644 (file)
@@ -35,6 +35,7 @@
 class FGEnvironment;
 class FGEnvironmentCtrl;
 class FGClouds;
+class FGPrecipitationMgr;
 
 /**
  * Manage environment information.
@@ -86,6 +87,7 @@ private:
 
   FGEnvironment * _environment;        // always the same, for now
   FGEnvironmentCtrl * _controller; // always the same, for now
+  FGPrecipitationMgr* _precipitationManager;
 
   FGClouds *fgClouds;
 };
index fcabe72ba351c085592cbd5e338b335e1b11fbbf..b6e6e6e3183b7e32365c7af7fb90c2510a9fcf5c 100644 (file)
  *   along with this program; if not, write to the Free Software
  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  *
- * @par CVS
- *   $Id$
  */
 
-#include <osgDB/ReadFile>
-#include <osgDB/FileUtils>
-#include <osgUtil/Optimizer>
-#include <osgUtil/CullVisitor>
-#include <osgViewer/Viewer>
-
-#include <osg/Depth>
-#include <osg/Stencil>
-#include <osg/ClipPlane>
-#include <osg/ClipNode>
 #include <osg/MatrixTransform>
-#include <osgUtil/TransformCallback>
 
 #include <simgear/constants.h>
+#include <simgear/math/SGMath.hxx>
 #include <simgear/scene/sky/sky.hxx>
 #include <simgear/scene/sky/cloud.hxx>
 
 #include <Main/fg_props.hxx>
+#include <Main/globals.hxx>
+#include <Scenery/scenery.hxx>
 
 #include "precipitation_mgr.hxx"
 
@@ -53,9 +43,6 @@
 extern SGSky *thesky;
 
 
-void WorldCoordinate( osg::Matrix&, double,
-                             double, double, double);
-
 /** 
  * @brief FGPrecipitation Manager constructor 
  *
@@ -63,32 +50,17 @@ void WorldCoordinate( osg::Matrix&, double,
  */
 FGPrecipitationMgr::FGPrecipitationMgr()
 {      
-       osg::Matrix position;
-       double latitude, longitude;
-
-
-       group = new osg::Group();
-       transform = new osg::MatrixTransform();
-       precipitation = new SGPrecipitation();
-
-
-       // By default, none precipitation
-       precipitation->setRainIntensity(0);
-       precipitation->setSnowIntensity(0);
-
-
-       // Read latitude and longitude position
-       latitude = fgGetDouble("/position/latitude-deg", 0.0);
-       longitude = fgGetDouble("/position/longitude-deg", 0.0);
-
-       WorldCoordinate(position, latitude, longitude, 0, 0);
+    group = new osg::Group();
+    transform = new osg::MatrixTransform();
+    precipitation = new SGPrecipitation();
 
 
-       // Move the precipitation object to player position
-       transform->setMatrix(position);
-       transform->addChild(precipitation->build());
+    // By default, no precipitation
+    precipitation->setRainIntensity(0);
+    precipitation->setSnowIntensity(0);
 
-       group->addChild(transform);
+    transform->addChild(precipitation->build());
+    group->addChild(transform.get());
 }
 
 
@@ -97,9 +69,25 @@ FGPrecipitationMgr::FGPrecipitationMgr()
  */
 FGPrecipitationMgr::~FGPrecipitationMgr()
 {
-       delete precipitation;
+
 }
 
+/**
+ * SGSubsystem initialization
+ */
+void FGPrecipitationMgr::init()
+{
+    // Read latitude and longitude position
+    SGGeod geod = SGGeod::fromDegM(fgGetDouble("/position/longitude-deg", 0.0),
+                                   fgGetDouble("/position/latitude-deg", 0.0),
+                                   0.0);
+    osg::Matrix position(geod.makeZUpFrame());
+    // Move the precipitation object to player position
+    transform->setMatrix(position);
+    // Add to scene graph
+    osg::Group* scenery = globals->get_scenery()->get_scene_graph();
+    scenery->addChild(getObject());
+}
 
 /** 
  * @brief Get OSG precipitation object
@@ -108,7 +96,7 @@ FGPrecipitationMgr::~FGPrecipitationMgr()
  */
 osg::Group * FGPrecipitationMgr::getObject(void)
 {
-       return this->group;
+    return this->group.get();
 }
 
 
@@ -122,107 +110,99 @@ osg::Group * FGPrecipitationMgr::getObject(void)
  */
 float FGPrecipitationMgr::getPrecipitationAtAltitudeMax(void)
 {
-       int i;
-       int max;
-       float result;
-
-
-       // By default (not cloud layer)
-       max = SGCloudLayer::SG_MAX_CLOUD_COVERAGES;
-       result = 0;
-
-       // To avoid messing up
-       if (thesky == NULL)
-               return result;
-
-       // For each cloud layer
-       for (i=0; i<thesky->get_cloud_layer_count(); i++) {
-               int q;
-
-               // Get coverage
-               // Value for q are (meaning / thickness) :
-               //   5 : "clear"                / 0
-               //   4 : "cirrus"       / ??
-               //   3 : "few"                  / 65
-               //   2 : "scattered"    / 600
-               //   1 : "broken"               / 750
-               //   0 : "overcast"             / 1000
-               q = thesky->get_cloud_layer(i)->getCoverage();
-
-               // Save the coverage max
-               if (q < max) {
-                       max = q;
-                       result = thesky->get_cloud_layer(i)->getElevation_m();
-               }
-       }
-
-       return result;
+    int i;
+    int max;
+    float result;
+
+
+    // By default (not cloud layer)
+    max = SGCloudLayer::SG_MAX_CLOUD_COVERAGES;
+    result = 0;
+
+    // To avoid messing up
+    if (thesky == NULL)
+        return result;
+
+    // For each cloud layer
+    for (i=0; i<thesky->get_cloud_layer_count(); i++) {
+        int q;
+
+        // Get coverage
+        // Value for q are (meaning / thickness) :
+        //   5 : "clear"               / 0
+        //   4 : "cirrus"       / ??
+        //   3 : "few"                         / 65
+        //   2 : "scattered"   / 600
+        //   1 : "broken"              / 750
+        //   0 : "overcast"            / 1000
+        q = thesky->get_cloud_layer(i)->getCoverage();
+
+        // Save the coverage max
+        if (q < max) {
+            max = q;
+            result = thesky->get_cloud_layer(i)->getElevation_m();
+        }
+    }
+
+    return result;
 }
 
 
 /** 
  * @brief Update the precipitation drawing
  * 
- * @returns true is all is OK
- *
- * This function permits you to update the precipitation drawing.
- * This function is called by the renderer.
  */
-bool FGPrecipitationMgr::update(void)
+void FGPrecipitationMgr::update(double dt)
 {
-       double dewtemp;
-       double currtemp;
-       double rain_intensity;
-       double snow_intensity;
-
-       float altitudeAircraft;
-       float altitudeCloudLayer;
-
-       // Get the elevation of aicraft and of the cloud layer
-       altitudeAircraft = fgGetDouble("/position/altitude-ft", 0.0);
-       altitudeCloudLayer = this->getPrecipitationAtAltitudeMax() * SG_METER_TO_FEET;
-
-       if (altitudeAircraft > altitudeCloudLayer) {
-               // The aircraft is above the cloud layer
-               rain_intensity = 0;
-               snow_intensity = 0;
-       }
-       else {
-               // The aircraft is bellow the cloud layer
-               rain_intensity = fgGetDouble("/environment/metar/rain-norm", 0.0);
-               snow_intensity = fgGetDouble("/environment/metar/snow-norm", 0.0);
-       }
-
-       // Get the current and dew temperature
-       dewtemp = fgGetDouble("/environment/dewpoint-degc", 0.0);
-       currtemp = fgGetDouble("/environment/temperature-degc", 0.0);
-
-       if (currtemp < dewtemp) {
-               // There is fog... and the weather is very steamy
-               if (rain_intensity == 0)
-                       rain_intensity = 0.15;
-       }
-
-       // If the current temperature is below 0°C, we turn off the rain to snow...
-       if (currtemp < 0)
-               precipitation->setFreezing(true);
-       else
-               precipitation->setFreezing(false);
-
-
-       // Set the wind property
-       precipitation->setWindProperty(
-               fgGetDouble("/environment/wind-from-heading-deg", 0.0),
-               fgGetDouble("/environment/wind-speed-kt", 0.0));
-
-       // Set the intensity of precipitation
-       precipitation->setRainIntensity(rain_intensity);
-       precipitation->setSnowIntensity(snow_intensity);
-
-       // Update the drawing...
-       precipitation->update();
-
-       return true;
+    double dewtemp;
+    double currtemp;
+    double rain_intensity;
+    double snow_intensity;
+
+    float altitudeAircraft;
+    float altitudeCloudLayer;
+
+    // Get the elevation of aicraft and of the cloud layer
+    altitudeAircraft = fgGetDouble("/position/altitude-ft", 0.0);
+    altitudeCloudLayer = this->getPrecipitationAtAltitudeMax() * SG_METER_TO_FEET;
+
+    if (altitudeAircraft > altitudeCloudLayer) {
+        // The aircraft is above the cloud layer
+        rain_intensity = 0;
+        snow_intensity = 0;
+    }
+    else {
+        // The aircraft is bellow the cloud layer
+        rain_intensity = fgGetDouble("/environment/metar/rain-norm", 0.0);
+        snow_intensity = fgGetDouble("/environment/metar/snow-norm", 0.0);
+    }
+
+    // Get the current and dew temperature
+    dewtemp = fgGetDouble("/environment/dewpoint-degc", 0.0);
+    currtemp = fgGetDouble("/environment/temperature-degc", 0.0);
+
+    if (currtemp < dewtemp) {
+        // There is fog... and the weather is very steamy
+        if (rain_intensity == 0)
+            rain_intensity = 0.15;
+    }
+
+    // If the current temperature is below 0°C, we turn off the rain to snow...
+    if (currtemp < 0)
+        precipitation->setFreezing(true);
+    else
+        precipitation->setFreezing(false);
+
+
+    // Set the wind property
+    precipitation->setWindProperty(
+        fgGetDouble("/environment/wind-from-heading-deg", 0.0),
+        fgGetDouble("/environment/wind-speed-kt", 0.0));
+
+    // Set the intensity of precipitation
+    precipitation->setRainIntensity(rain_intensity);
+    precipitation->setSnowIntensity(snow_intensity);
+
+    // Update the drawing...
+    precipitation->update();
 }
-
-
index 2d2457976bff202b7cbc6d362fe3ed5dd22f0a79..6f2ab8dd782d4d94e18bebee532158c76361e9b4 100644 (file)
  *   along with this program; if not, write to the Free Software
  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  *
- * @par CVS
- *   $Id$
  */
 
 #ifndef _PRECIPITATION_MGR_HXX
 #define _PRECIPITATION_MGR_HXX
 
+#include <simgear/structure/subsystem_mgr.hxx>
 #include <simgear/environment/precipitation.hxx>
 
-#include "precipitation_mgr.hxx"
-
-
-class FGPrecipitationMgr {
+class FGPrecipitationMgr : public SGSubsystem
+{
 private:
-       osg::Group *group;
-       osg::MatrixTransform *transform;
-       SGPrecipitation *precipitation;
-       float getPrecipitationAtAltitudeMax(void);
+    osg::ref_ptr<osg::Group> group;
+    osg::ref_ptr<osg::MatrixTransform> transform;
+    osg::ref_ptr<SGPrecipitation> precipitation;
+    float getPrecipitationAtAltitudeMax(void);
 
 
 public:
-       FGPrecipitationMgr();
-       ~FGPrecipitationMgr();
-       bool update(void);
-       osg::Group * getObject(void);
+    FGPrecipitationMgr();
+    virtual ~FGPrecipitationMgr();
+
+    // SGSubsystem methods
+    virtual void init ();
+    virtual void update (double dt);
+    
+    osg::Group * getObject(void);
 };
 
 #endif
index dd29fb8b85cab46a8213043388324dd6c1b67694..615ed6c548091e32eb7473cbf64ca64c6809183a 100644 (file)
@@ -71,7 +71,6 @@
 #include <Time/tmp.hxx>
 #include <Time/fg_timer.hxx>
 #include <Environment/environment_mgr.hxx>
-#include <Environment/precipitation_mgr.hxx>
 #include <GUI/new_gui.hxx>
 #include <MultiPlayer/multiplaymgr.hxx>
 
@@ -82,9 +81,6 @@
 #include "main.hxx"
 
 
-extern FGPrecipitationMgr *fgPrecipitationMgr;
-
-
 static double real_delta_time_sec = 0.0;
 double delta_time_sec = 0.0;
 extern float init_volume;
@@ -850,11 +846,6 @@ static void fgIdleFunction ( void ) {
                        globals->get_ephem()->getStars(),
                        fgGetNode("/environment", true));
 
-
-               // Precipitation Manager...
-               fgPrecipitationMgr = new FGPrecipitationMgr();
-
-
         // Initialize MagVar model
         SGMagVar *magvar = new SGMagVar();
         globals->set_mag( magvar );
index 3d5cc51a56d2b4cf9037e37d92c583d3cdb26880..a67a832f3df2d60aac2a069c45ac1e2b0ac61737 100644 (file)
@@ -387,9 +387,6 @@ public:
 // Sky structures
 SGSky *thesky;
 
-// Precipitations
-FGPrecipitationMgr *fgPrecipitationMgr;
-
 static osg::ref_ptr<osg::FrameStamp> mFrameStamp = new osg::FrameStamp;
 static osg::ref_ptr<SGUpdateVisitor> mUpdateVisitor= new SGUpdateVisitor;
 
@@ -505,11 +502,6 @@ FGRenderer::init( void ) {
     stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
     stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
 
-
-       // Add Precipitation object
-       sceneGroup->addChild( fgPrecipitationMgr->getObject() );
-
-
     // need to update the light on every frame
     osg::LightSource* lightSource = new osg::LightSource;
     lightSource->setUpdateCallback(new FGLightSourceUpdateCallback);
@@ -788,20 +780,6 @@ FGRenderer::update( bool refresh_camera_settings ) {
 //         fgGetDouble("/velocities/airspeed-kt", 0.0)
 //                        * cos( fgGetDouble("/orientation/pitch-deg", 0.0)
 //                                * SGD_DEGREES_TO_RADIANS);
-       // TODO:find the real view speed, not the AC one
-//     sgEnviro.drawPrecipitation(
-//         fgGetDouble("/environment/metar/rain-norm", 0.0),
-//         fgGetDouble("/environment/metar/snow-norm", 0.0),
-//         fgGetDouble("/environment/metar/hail-norm", 0.0),
-//         current__view->getPitch_deg() + current__view->getPitchOffset_deg(),
-//         current__view->getRoll_deg() + current__view->getRollOffset_deg(),
-//         - current__view->getHeadingOffset_deg(),
-//                current_view_origin_airspeed_horiz_kt
-//                );
-
-       // Update precipitation informations...
-       fgPrecipitationMgr->update();
-
 
     // OSGFIXME
 //     if( is_internal )
index bc89446117f677d35559d69be7da883b5697c0a8..354a10971deacf7cabe653b498ecb91fab79fe6b 100644 (file)
@@ -52,6 +52,7 @@
 #include <simgear/math/polar3d.hxx>
 #include <simgear/math/sg_geodesy.hxx>
 #include <simgear/math/sg_random.h>
+#include <simgear/math/SGMath.hxx>
 #include <simgear/misc/sgstream.hxx>
 #include <simgear/scene/material/mat.hxx>
 #include <simgear/scene/material/matlib.hxx>
@@ -165,43 +166,15 @@ FGTileEntry::~FGTileEntry ()
 {
 }
 
-void WorldCoordinate( osg::Matrix& obj_pos, double lat,
-                             double lon, double elev, double hdg )
+static void WorldCoordinate(osg::Matrix& obj_pos, double lat,
+                            double lon, double elev, double hdg)
 {
-    double lon_rad = lon * SGD_DEGREES_TO_RADIANS;
-    double lat_rad = lat * SGD_DEGREES_TO_RADIANS;
-    double hdg_rad = hdg * SGD_DEGREES_TO_RADIANS;
-
-    // setup transforms
-    Point3D geod( lon_rad, lat_rad, elev );
-    Point3D world_pos = sgGeodToCart( geod );
-
-    double sin_lat = sin( lat_rad );
-    double cos_lat = cos( lat_rad );
-    double cos_lon = cos( lon_rad );
-    double sin_lon = sin( lon_rad );
-    double sin_hdg = sin( hdg_rad ) ;
-    double cos_hdg = cos( hdg_rad ) ;
-
-    obj_pos(0, 0) =  cos_hdg * sin_lat * cos_lon - sin_hdg * sin_lon;
-    obj_pos(0, 1) =  cos_hdg * sin_lat * sin_lon + sin_hdg * cos_lon;
-    obj_pos(0, 2) = -cos_hdg * cos_lat;
-    obj_pos(0, 3) =  SG_ZERO;
-
-    obj_pos(1, 0) = -sin_hdg * sin_lat * cos_lon - cos_hdg * sin_lon;
-    obj_pos(1, 1) = -sin_hdg * sin_lat * sin_lon + cos_hdg * cos_lon;
-    obj_pos(1, 2) =  sin_hdg * cos_lat;
-    obj_pos(1, 3) =  SG_ZERO;
-
-    obj_pos(2, 0) = cos_lat * cos_lon;
-    obj_pos(2, 1) = cos_lat * sin_lon;
-    obj_pos(2, 2) = sin_lat;
-    obj_pos(2, 3) =  SG_ZERO;
-
-    obj_pos(3, 0) = world_pos.x();
-    obj_pos(3, 1) = world_pos.y();
-    obj_pos(3, 2) = world_pos.z();
-    obj_pos(3, 3) = SG_ONE ;
+    SGGeod geod = SGGeod::fromDegM(lon, lat, elev);
+    obj_pos = geod.makeZUpFrame();
+    // hdg is not a compass heading, but a counter-clockwise rotation
+    // around the Z axis
+    obj_pos.preMult(osg::Matrix::rotate(hdg * SGD_DEGREES_TO_RADIANS,
+                                        0.0, 0.0, 1.0));
 }