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>
34 * @brief SGPrecipitation constructor
36 * Build a new OSG object from osgParticle.
38 SGPrecipitation::SGPrecipitation() :
39 _freeze(false), _snow_intensity(0.0), _rain_intensity(0.0), _clip_distance(5.0)
41 _precipitationEffect = new osgParticle::PrecipitationEffect;
46 * @brief Build and add the object "precipitationEffect"
48 * This function permits you to create an object precipitationEffect and initialize it.
49 * I define by default the color of water (for raining)
51 osg::Group* SGPrecipitation::build(void)
53 osg::Group* group = new osg::Group;
55 _precipitationEffect->snow(0);
56 _precipitationEffect->rain(0);
58 if (_clip_distance!=0.0)
60 osg::ref_ptr<osg::ClipNode> clipNode = new osg::ClipNode;
61 clipNode->addClipPlane( new osg::ClipPlane( 0 ) );
62 clipNode->getClipPlane(0)->setClipPlane( 0.0, 0.0, -1.0, -_clip_distance );
63 clipNode->setReferenceFrame(osg::ClipNode::ABSOLUTE_RF);
64 clipNode->addChild(_precipitationEffect.get());
66 group->addChild(clipNode.get());
70 group->addChild(_precipitationEffect.get());
78 * @brief Define the snow intensity
80 * This function permits you to define and change the snow intensity
81 * The param 'intensity' is normed (0 to 1).
83 void SGPrecipitation::setSnowIntensity(float intensity)
85 if (this->_snow_intensity < intensity-0.001)
86 this->_snow_intensity += 0.001;
87 else if (this->_snow_intensity > intensity+0.001)
88 this->_snow_intensity -= 0.001;
90 this->_snow_intensity = intensity;
95 * @brief Define the rain intensity
97 * This function permits you to define and change the rain intensity
98 * The param 'intensity' is normed (0 to 1).
100 void SGPrecipitation::setRainIntensity(float intensity)
102 if (this->_rain_intensity < intensity-0.001)
103 this->_rain_intensity += 0.001;
104 else if (this->_rain_intensity > intensity+0.001)
105 this->_rain_intensity -= 0.001;
107 this->_rain_intensity = intensity;
112 * @brief Freeze the rain to snow
114 * @param freeze Boolean
116 * This function permits you to turn off the rain to snow.
118 void SGPrecipitation::setFreezing(bool freeze)
120 this->_freeze = freeze;
125 * @brief Define the wind direction and speed
127 * This function permits you to define and change the wind direction
129 * After apply the MatrixTransform to the osg::Precipitation object,
130 * x points full south... From wind heading and speed, we can calculate
133 void SGPrecipitation::setWindProperty(double heading, double speed)
137 heading = (heading + 180) * SGD_DEGREES_TO_RADIANS;
138 speed = speed * SG_FEET_TO_METER;
140 x = -cos(heading) * speed;
141 y = sin(heading) * speed;
144 this->_wind_vec = osg::Vec3(x, y, z);
149 * @brief Update the precipitation effects
151 * This function permits you to update the precipitation effects.
152 * Be careful, if snow and rain intensity are greater than '0', snow effect
155 * The settings come from the osgParticule/PrecipitationEffect.cpp exemple.
157 bool SGPrecipitation::update(void)
160 if (this->_rain_intensity > 0)
161 this->_snow_intensity = this->_rain_intensity;
164 bool enabled = sgEnviro.get_precipitation_enable_state();
165 if (enabled && this->_snow_intensity > 0) {
166 _precipitationEffect->setWind(_wind_vec);
167 _precipitationEffect->setParticleSpeed( -0.75f - 0.25f*_snow_intensity);
169 _precipitationEffect->setParticleSize(0.02f + 0.03f*_snow_intensity);
170 _precipitationEffect->setMaximumParticleDensity(_snow_intensity * 7.2f);
171 _precipitationEffect->setCellSize(osg::Vec3(5.0f / (0.25f+_snow_intensity), 5.0f / (0.25f+_snow_intensity), 5.0f));
173 _precipitationEffect->setNearTransition(25.f);
174 _precipitationEffect->setFarTransition(100.0f - 60.0f*sqrtf(_snow_intensity));
176 _precipitationEffect->setParticleColor(osg::Vec4(0.85, 0.85, 0.85, 1.0) - osg::Vec4(0.1, 0.1, 0.1, 1.0) * _snow_intensity);
177 } else if (enabled && this->_rain_intensity > 0) {
178 _precipitationEffect->setWind(_wind_vec);
179 _precipitationEffect->setParticleSpeed( -2.0f + -5.0f*_rain_intensity);
181 _precipitationEffect->setParticleSize(0.01 + 0.02*_rain_intensity);
182 _precipitationEffect->setMaximumParticleDensity(_rain_intensity * 7.5f);
183 _precipitationEffect->setCellSize(osg::Vec3(5.0f / (0.25f+_rain_intensity), 5.0f / (0.25f+_rain_intensity), 5.0f));
185 _precipitationEffect->setNearTransition(25.f);
186 _precipitationEffect->setFarTransition(100.0f - 60.0f*sqrtf(_rain_intensity));
188 _precipitationEffect->setParticleColor( osg::Vec4(0x7A, 0xCE, 0xFF, 0x80));
190 _precipitationEffect->snow(0);
191 _precipitationEffect->rain(0);