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.
33 #include <osg/MatrixTransform>
35 #include <simgear/constants.h>
36 #include <simgear/math/SGMath.hxx>
37 #include <simgear/scene/sky/sky.hxx>
38 #include <simgear/scene/sky/cloud.hxx>
39 #include <simgear/environment/visual_enviro.hxx>
41 #include <Main/fg_props.hxx>
42 #include <Main/globals.hxx>
43 #include <Scenery/scenery.hxx>
45 #include "precipitation_mgr.hxx"
51 * @brief FGPrecipitation Manager constructor
53 * Build a new object to manage the precipitation object
55 FGPrecipitationMgr::FGPrecipitationMgr()
57 group = new osg::Group();
58 transform = new osg::MatrixTransform();
59 precipitation = new SGPrecipitation();
62 // By default, no precipitation
63 precipitation->setRainIntensity(0);
64 precipitation->setSnowIntensity(0);
65 transform->addChild(precipitation->build());
66 group->addChild(transform.get());
71 * @brief FGPrecipitaiton Manager destructor
73 FGPrecipitationMgr::~FGPrecipitationMgr()
79 * SGSubsystem initialization
81 void FGPrecipitationMgr::init()
83 // Read latitude and longitude position
84 SGGeod geod = SGGeod::fromDegM(fgGetDouble("/position/longitude-deg", 0.0),
85 fgGetDouble("/position/latitude-deg", 0.0),
87 osg::Matrix position(geod.makeZUpFrame());
88 // Move the precipitation object to player position
89 transform->setMatrix(position);
91 osg::Group* scenery = globals->get_scenery()->get_scene_graph();
92 scenery->addChild(getObject());
93 fgGetNode("environment/params/precipitation-level-ft", true);
96 void FGPrecipitationMgr::setPrecipitationLevel(double a)
98 fgSetDouble("environment/params/precipitation-level-ft",a);
102 * @brief Get OSG precipitation object
104 * @returns A pointer on the OSG object (osg::Group *)
106 osg::Group * FGPrecipitationMgr::getObject(void)
108 return this->group.get();
112 * @brief Calculate the max alitutude with precipitation
114 * @returns Elevation max in meter
116 * This function permits you to know what is the altitude max where we can
117 * find precipitation. The value is returned in meter.
119 float FGPrecipitationMgr::getPrecipitationAtAltitudeMax(void)
125 SGPropertyNode *boundaryNode, *boundaryEntry;
128 // By default (not cloud layer)
129 max = SGCloudLayer::SG_MAX_CLOUD_COVERAGES;
132 // To avoid messing up
136 // For each cloud layer
137 for (i=0; i<thesky->get_cloud_layer_count(); i++) {
141 // Value for q are (meaning / thickness) :
145 // 2 : "scattered" / 600
146 // 1 : "broken" / 750
147 // 0 : "overcast" / 1000
148 q = thesky->get_cloud_layer(i)->getCoverage();
150 // Save the coverage max
153 result = thesky->get_cloud_layer(i)->getElevation_m();
158 // If we haven't found clouds layers, we read the bounday layers table.
163 // Read boundary layers node
164 boundaryNode = fgGetNode("/environment/config/boundary");
166 if (boundaryNode != NULL) {
169 // For each boundary layers
170 while ( ( boundaryEntry = boundaryNode->getNode( "entry", i ) ) != NULL ) {
171 elev = boundaryEntry->getDoubleValue( "elevation-ft" );
180 // Convert the result in meter
181 result = result * SG_FEET_TO_METER;
188 * @brief Update the precipitation drawing
190 * To seem real, we stop the precipitation above the cloud or boundary layer.
191 * If METAR information doesn't give us this altitude, we will see precipitations
193 * Moreover, below 0°C we change rain into snow.
195 void FGPrecipitationMgr::update(double dt)
199 double rain_intensity;
200 double snow_intensity;
202 float altitudeAircraft;
203 float altitudeCloudLayer;
205 altitudeCloudLayer = this->getPrecipitationAtAltitudeMax() * SG_METER_TO_FEET;
206 setPrecipitationLevel(altitudeCloudLayer);
208 // Does the user enable the precipitation ?
209 if (!sgEnviro.get_precipitation_enable_state()) {
210 // Disable precipitations
211 precipitation->setRainIntensity(0);
212 precipitation->setSnowIntensity(0);
214 // Update the drawing...
215 precipitation->update();
221 // Get the elevation of aicraft and of the cloud layer
222 altitudeAircraft = fgGetDouble("/position/altitude-ft", 0.0);
224 if ((altitudeCloudLayer > 0) && (altitudeAircraft > altitudeCloudLayer)) {
225 // The aircraft is above the cloud layer
230 // The aircraft is bellow the cloud layer
231 rain_intensity = fgGetDouble("/environment/metar/rain-norm", 0.0);
232 snow_intensity = fgGetDouble("/environment/metar/snow-norm", 0.0);
235 // Get the current and dew temperature
236 dewtemp = fgGetDouble("/environment/dewpoint-degc", 0.0);
237 currtemp = fgGetDouble("/environment/temperature-degc", 0.0);
239 if (currtemp < dewtemp) {
240 // There is fog... and the weather is very steamy
241 if (rain_intensity == 0)
242 rain_intensity = 0.15;
245 // If the current temperature is below 0°C, we turn off the rain to snow...
247 precipitation->setFreezing(true);
249 precipitation->setFreezing(false);
252 // Set the wind property
253 precipitation->setWindProperty(
254 fgGetDouble("/environment/wind-from-heading-deg", 0.0),
255 fgGetDouble("/environment/wind-speed-kt", 0.0));
257 // Set the intensity of precipitation
258 precipitation->setRainIntensity(rain_intensity);
259 precipitation->setSnowIntensity(snow_intensity);
261 // Update the drawing...
262 precipitation->update();