2 * @file precipitation.cxx
3 * @author Nicolas VIVIEN
6 * @note Copyright (C) 2008 Nicolas VIVIEN
8 * @brief Precipitation effects to draw rain and snow.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of the
14 * License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include "precipitation.hxx"
28 //#include "visual_enviro.hxx"
30 #include <simgear/constants.h>
31 #include <osg/ClipNode>
32 #include <simgear/scene/util/RenderConstants.hxx>
35 * @brief SGPrecipitation constructor
37 * Build a new OSG object from osgParticle.
39 SGPrecipitation::SGPrecipitation() :
40 _freeze(false), _enabled(true), _droplet_external(false), _snow_intensity(0.0), _rain_intensity(0.0), _clip_distance(5.0), _rain_droplet_size(0.0), _snow_flake_size(0.0), _illumination(1.0)
42 _precipitationEffect = new osgParticle::PrecipitationEffect;
45 void SGPrecipitation::setEnabled( bool value )
50 void SGPrecipitation::setDropletExternal( bool value )
52 _droplet_external = value;
55 bool SGPrecipitation::getEnabled() const
61 * @brief Build and add the object "precipitationEffect"
63 * This function permits you to create an object precipitationEffect and initialize it.
64 * I define by default the color of water (for raining)
66 osg::Group* SGPrecipitation::build(void)
68 osg::Group* group = new osg::Group;
70 _precipitationEffect->snow(0);
71 _precipitationEffect->rain(0);
73 if (_clip_distance!=0.0)
75 osg::ref_ptr<osg::ClipNode> clipNode = new osg::ClipNode;
76 clipNode->addClipPlane( new osg::ClipPlane( 0 ) );
77 clipNode->getClipPlane(0)->setClipPlane( 0.0, 0.0, -1.0, -_clip_distance );
78 clipNode->setReferenceFrame(osg::ClipNode::ABSOLUTE_RF);
79 clipNode->addChild(_precipitationEffect.get());
81 group->addChild(clipNode.get());
85 group->addChild(_precipitationEffect.get());
88 group->setNodeMask( ~(simgear::CASTSHADOW_BIT | simgear::MODELLIGHT_BIT) );
95 * @brief Define the snow intensity
97 * This function permits you to define and change the snow intensity
98 * The param 'intensity' is normed (0 to 1).
100 void SGPrecipitation::setSnowIntensity(float intensity)
102 if (this->_snow_intensity < intensity-0.001)
103 this->_snow_intensity += 0.001;
104 else if (this->_snow_intensity > intensity+0.001)
105 this->_snow_intensity -= 0.001;
107 this->_snow_intensity = intensity;
112 * @brief Define the rain intensity
114 * This function permits you to define and change the rain intensity
115 * The param 'intensity' is normed (0 to 1).
117 void SGPrecipitation::setRainIntensity(float intensity)
119 if (this->_rain_intensity < intensity-0.001)
120 this->_rain_intensity += 0.001;
121 else if (this->_rain_intensity > intensity+0.001)
122 this->_rain_intensity -= 0.001;
124 this->_rain_intensity = intensity;
128 * @brief Define the rain droplet size
130 * This function permits you to define and change the rain droplet size
131 * which is used if external droplet size control is enabled
134 void SGPrecipitation::setRainDropletSize(float size)
136 _rain_droplet_size = size;
141 * @brief Define the illumination multiplier
143 * This function permits you to define and change the rain droplet size
144 * which is used if external droplet size control is enabled
147 void SGPrecipitation::setIllumination(float illumination)
149 _illumination = illumination;
153 * @brief Define the snow flake size
155 * This function permits you to define and change the snow flake size
156 * which is used if external droplet size control is enabled
159 void SGPrecipitation::setSnowFlakeSize(float size)
161 _snow_flake_size = size;
166 * @brief Define the rain droplet size
168 * This function permits you to define and change the rain droplet size
169 * which is used if external droplet size control is enabled
172 void SGPrecipitation::setClipDistance(float distance)
174 _clip_distance = distance;
178 * @brief Freeze the rain to snow
180 * @param freeze Boolean
182 * This function permits you to turn off the rain to snow.
184 void SGPrecipitation::setFreezing(bool freeze)
186 if ((this->_freeze)&&(!freeze)) // rain freezes suddenly, so we need to unfreeze
188 this->_rain_intensity = this->_snow_intensity;
189 this->_snow_intensity = 0.0;
191 this->_freeze = freeze;
197 * @brief Define the wind direction and speed
199 * This function permits you to define and change the wind direction
201 * After apply the MatrixTransform to the osg::Precipitation object,
202 * x points full south... From wind heading and speed, we can calculate
205 void SGPrecipitation::setWindProperty(double heading, double speed)
209 heading = (heading + 180) * SGD_DEGREES_TO_RADIANS;
210 speed = speed * SG_FEET_TO_METER;
212 x = -cos(heading) * speed;
213 y = sin(heading) * speed;
216 this->_wind_vec = osg::Vec3(x, y, z);
221 * @brief Update the precipitation effects
223 * This function permits you to update the precipitation effects.
224 * Be careful, if snow and rain intensity are greater than '0', snow effect
227 * The settings come from the osgParticule/PrecipitationEffect.cpp exemple.
229 bool SGPrecipitation::update(void)
232 if (this->_rain_intensity > 0) {
233 this->_snow_intensity = this->_rain_intensity;
237 if (_enabled && this->_snow_intensity > 0) {
238 _precipitationEffect->setWind(_wind_vec);
241 if(_droplet_external)
244 if ((_freeze) && (_rain_droplet_size > 0.03)) // this is hail or sleet
246 _precipitationEffect->setParticleSize(_rain_droplet_size*1.5f);
247 _precipitationEffect->setParticleSpeed( -1.0f - 22.36f*sqrtf(_rain_droplet_size));
248 _precipitationEffect->setMaximumParticleDensity(_snow_intensity * 4.8f);
250 else if (_freeze) // this is snow from frozen small rain droplets
252 _precipitationEffect->setParticleSize(_rain_droplet_size*1.3f);
253 _precipitationEffect->setParticleSpeed( -0.75f - 0.25f*_snow_intensity);
254 _precipitationEffect->setMaximumParticleDensity(_snow_intensity * 10.0f);
256 else // this was snow in the first place
258 _precipitationEffect->setParticleSize(_snow_flake_size);
259 _precipitationEffect->setParticleSpeed( -0.75f - 0.25f*_snow_intensity);
260 _precipitationEffect->setMaximumParticleDensity(_snow_intensity * 7.2f);
265 _precipitationEffect->setMaximumParticleDensity(_snow_intensity * 7.2f);
266 _precipitationEffect->setParticleSize(0.02f + 0.03f*_snow_intensity);
267 _precipitationEffect->setParticleSpeed( -0.75f - 0.25f*_snow_intensity);
271 _precipitationEffect->setCellSize(osg::Vec3(5.0f / (0.25f+_snow_intensity), 5.0f / (0.25f+_snow_intensity), 5.0f));
273 _precipitationEffect->setNearTransition(25.f);
274 _precipitationEffect->setFarTransition(100.0f - 60.0f*sqrtf(_snow_intensity));
276 _precipitationEffect->setParticleColor(osg::Vec4(0.85 * _illumination, 0.85 * _illumination, 0.85 * _illumination, 1.0) - osg::Vec4(0.1, 0.1, 0.1, 1.0) * _snow_intensity);
277 } else if (_enabled && this->_rain_intensity > 0) {
279 _precipitationEffect->setWind(_wind_vec);
282 if(_droplet_external)
284 _precipitationEffect->setParticleSize(_rain_droplet_size);
285 _precipitationEffect->setParticleSpeed( -1.0f - 22.36f*sqrtf(_rain_droplet_size));
289 _precipitationEffect->setParticleSize(0.01 + 0.02*_rain_intensity);
290 _precipitationEffect->setParticleSpeed( -2.0f + -5.0f*_rain_intensity);
293 _precipitationEffect->setMaximumParticleDensity(_rain_intensity * 7.5f);
294 _precipitationEffect->setCellSize(osg::Vec3(5.0f / (0.25f+_rain_intensity), 5.0f / (0.25f+_rain_intensity), 5.0f));
296 _precipitationEffect->setNearTransition(25.f);
297 _precipitationEffect->setFarTransition(100.0f - 60.0f*sqrtf(_rain_intensity));
299 _precipitationEffect->setParticleColor( osg::Vec4(0.64 * _illumination, 0.64 * _illumination, 0.64 * _illumination, 0.5));
301 _precipitationEffect->snow(0);
302 _precipitationEffect->rain(0);