2 * @file precipitation_mgr.cxx
3 * @author Nicolas VIVIEN
6 * @note Copyright (C) 2008 Nicolas VIVIEN
8 * @brief Precipitation manager
9 * This manager calculate the intensity of precipitation in function of the altitude,
10 * calculate the wind direction and velocity, then update the drawing of precipitation.
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of the
16 * License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include <osg/MatrixTransform>
31 #include <simgear/constants.h>
32 #include <simgear/math/SGMath.hxx>
33 #include <simgear/scene/sky/sky.hxx>
34 #include <simgear/scene/sky/cloud.hxx>
36 #include <Main/fg_props.hxx>
37 #include <Main/globals.hxx>
38 #include <Scenery/scenery.hxx>
40 #include "precipitation_mgr.hxx"
47 * @brief FGPrecipitation Manager constructor
49 * Build a new object to manage the precipitation object
51 FGPrecipitationMgr::FGPrecipitationMgr()
53 group = new osg::Group();
54 transform = new osg::MatrixTransform();
55 precipitation = new SGPrecipitation();
58 // By default, no precipitation
59 precipitation->setRainIntensity(0);
60 precipitation->setSnowIntensity(0);
62 transform->addChild(precipitation->build());
63 group->addChild(transform.get());
68 * @brief FGPrecipitaiton Manager destructor
70 FGPrecipitationMgr::~FGPrecipitationMgr()
76 * SGSubsystem initialization
78 void FGPrecipitationMgr::init()
80 // Read latitude and longitude position
81 SGGeod geod = SGGeod::fromDegM(fgGetDouble("/position/longitude-deg", 0.0),
82 fgGetDouble("/position/latitude-deg", 0.0),
84 osg::Matrix position(geod.makeZUpFrame());
85 // Move the precipitation object to player position
86 transform->setMatrix(position);
88 osg::Group* scenery = globals->get_scenery()->get_scene_graph();
89 scenery->addChild(getObject());
93 * @brief Get OSG precipitation object
95 * @returns A pointer on the OSG object (osg::Group *)
97 osg::Group * FGPrecipitationMgr::getObject(void)
99 return this->group.get();
104 * @brief Calculate the max alitutude with precipitation
106 * @returns Elevation max in meter
108 * This function permits you to know what is the altitude max where we can
109 * find precipitation. The value is returned in meter.
111 float FGPrecipitationMgr::getPrecipitationAtAltitudeMax(void)
118 // By default (not cloud layer)
119 max = SGCloudLayer::SG_MAX_CLOUD_COVERAGES;
122 // To avoid messing up
126 // For each cloud layer
127 for (i=0; i<thesky->get_cloud_layer_count(); i++) {
131 // Value for q are (meaning / thickness) :
135 // 2 : "scattered" / 600
136 // 1 : "broken" / 750
137 // 0 : "overcast" / 1000
138 q = thesky->get_cloud_layer(i)->getCoverage();
140 // Save the coverage max
143 result = thesky->get_cloud_layer(i)->getElevation_m();
152 * @brief Update the precipitation drawing
155 void FGPrecipitationMgr::update(double dt)
159 double rain_intensity;
160 double snow_intensity;
162 float altitudeAircraft;
163 float altitudeCloudLayer;
165 // Get the elevation of aicraft and of the cloud layer
166 altitudeAircraft = fgGetDouble("/position/altitude-ft", 0.0);
167 altitudeCloudLayer = this->getPrecipitationAtAltitudeMax() * SG_METER_TO_FEET;
169 if (altitudeAircraft > altitudeCloudLayer) {
170 // The aircraft is above the cloud layer
175 // The aircraft is bellow the cloud layer
176 rain_intensity = fgGetDouble("/environment/metar/rain-norm", 0.0);
177 snow_intensity = fgGetDouble("/environment/metar/snow-norm", 0.0);
180 // Get the current and dew temperature
181 dewtemp = fgGetDouble("/environment/dewpoint-degc", 0.0);
182 currtemp = fgGetDouble("/environment/temperature-degc", 0.0);
184 if (currtemp < dewtemp) {
185 // There is fog... and the weather is very steamy
186 if (rain_intensity == 0)
187 rain_intensity = 0.15;
190 // If the current temperature is below 0°C, we turn off the rain to snow...
192 precipitation->setFreezing(true);
194 precipitation->setFreezing(false);
197 // Set the wind property
198 precipitation->setWindProperty(
199 fgGetDouble("/environment/wind-from-heading-deg", 0.0),
200 fgGetDouble("/environment/wind-speed-kt", 0.0));
202 // Set the intensity of precipitation
203 precipitation->setRainIntensity(rain_intensity);
204 precipitation->setSnowIntensity(snow_intensity);
206 // Update the drawing...
207 precipitation->update();