]> git.mxchange.org Git - simgear.git/blob - simgear/environment/precipitation.cxx
Terrasync: implement HTTP service lookup via DNS
[simgear.git] / simgear / environment / precipitation.cxx
1 /**
2  * @file precipitation.cxx
3  * @author Nicolas VIVIEN
4  * @date 2008-02-10
5  *
6  * @note Copyright (C) 2008 Nicolas VIVIEN
7  *
8  * @brief Precipitation effects to draw rain and snow.
9  *
10  * @par Licences
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.
15  *
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.
20  *
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.
24  *
25  */
26
27 #include "precipitation.hxx"
28 //#include "visual_enviro.hxx"
29
30 #include <simgear/constants.h>
31 #include <osg/ClipNode>
32 #include <simgear/scene/util/RenderConstants.hxx>
33
34 /**
35  * @brief SGPrecipitation constructor
36  *
37  * Build a new OSG object from osgParticle.
38  */
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)
41 {
42     _precipitationEffect = new osgParticle::PrecipitationEffect;
43 }
44
45 void SGPrecipitation::setEnabled( bool value )
46 {
47     _enabled = value;
48 }
49
50 void SGPrecipitation::setDropletExternal( bool value )
51 {
52     _droplet_external = value;
53 }
54
55 bool SGPrecipitation::getEnabled() const
56 {
57     return _enabled;
58 }
59
60 /**
61  * @brief Build and add the object "precipitationEffect"
62  *
63  * This function permits you to create an object precipitationEffect and initialize it.
64  * I define by default the color of water (for raining)
65  */
66 osg::Group* SGPrecipitation::build(void)
67 {
68     osg::Group* group = new osg::Group;
69
70     _precipitationEffect->snow(0);      
71     _precipitationEffect->rain(0);      
72
73     if (_clip_distance!=0.0)
74     {
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());
80
81         group->addChild(clipNode.get());
82     }
83     else
84     {
85         group->addChild(_precipitationEffect.get());
86     }
87
88     group->setNodeMask( ~(simgear::CASTSHADOW_BIT | simgear::MODELLIGHT_BIT) );
89
90     return group;
91 }
92
93
94 /**
95  * @brief Define the snow intensity
96  *
97  * This function permits you to define and change the snow intensity
98  * The param 'intensity' is normed (0 to 1).
99  */
100 void SGPrecipitation::setSnowIntensity(float intensity)
101 {
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;
106     else
107         this->_snow_intensity = intensity;
108 }
109
110
111 /**
112  * @brief Define the rain intensity
113  *
114  * This function permits you to define and change the rain intensity
115  * The param 'intensity' is normed (0 to 1).
116  */
117 void SGPrecipitation::setRainIntensity(float intensity)
118 {
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;
123     else
124         this->_rain_intensity = intensity;
125 }
126
127 /**
128  * @brief Define the rain droplet size
129  *
130  * This function permits you to define and change the rain droplet size
131  * which is used if external droplet size control is enabled
132  */
133
134 void SGPrecipitation::setRainDropletSize(float size)
135 {
136     _rain_droplet_size = size;
137 }
138
139
140 /**
141  * @brief Define the illumination multiplier
142  *
143  * This function permits you to define and change the rain droplet size
144  * which is used if external droplet size control is enabled
145  */
146
147 void SGPrecipitation::setIllumination(float illumination)
148 {
149     _illumination = illumination;
150 }
151
152 /**
153  * @brief Define the snow flake size
154  *
155  * This function permits you to define and change the snow flake size
156  * which is used if external droplet size control is enabled
157  */
158
159 void SGPrecipitation::setSnowFlakeSize(float size)
160 {
161     _snow_flake_size = size;
162 }
163
164
165 /**
166  * @brief Define the rain droplet size
167  *
168  * This function permits you to define and change the rain droplet size
169  * which is used if external droplet size control is enabled
170  */
171
172 void SGPrecipitation::setClipDistance(float distance)
173 {
174     _clip_distance = distance;
175 }
176
177 /**
178  * @brief Freeze the rain to snow
179  *
180  * @param freeze Boolean
181  *
182  * This function permits you to turn off the rain to snow.
183  */
184 void SGPrecipitation::setFreezing(bool freeze)
185 {
186     if ((this->_freeze)&&(!freeze)) // rain freezes suddenly, so we need to unfreeze
187         {
188         this->_rain_intensity = this->_snow_intensity;
189         this->_snow_intensity = 0.0;
190         }
191     this->_freeze = freeze;
192
193 }
194
195
196 /**
197  * @brief Define the wind direction and speed
198  *
199  * This function permits you to define and change the wind direction
200  *
201  * After apply the MatrixTransform to the osg::Precipitation object,
202  * x points full south... From wind heading and speed, we can calculate
203  * the wind vector.
204  */
205 void SGPrecipitation::setWindProperty(double heading, double speed)
206 {
207     double x, y, z;
208
209     heading = (heading + 180) * SGD_DEGREES_TO_RADIANS;
210     speed = speed * SG_FEET_TO_METER;
211
212     x = -cos(heading) * speed;
213     y = sin(heading) * speed;
214     z = 0;
215
216     this->_wind_vec = osg::Vec3(x, y, z);
217 }
218
219
220 /**
221  * @brief Update the precipitation effects
222  *
223  * This function permits you to update the precipitation effects.
224  * Be careful, if snow and rain intensity are greater than '0', snow effect
225  * will be first.
226  *
227  * The settings come from the osgParticule/PrecipitationEffect.cpp exemple.
228  */
229 bool SGPrecipitation::update(void)
230 {
231     if (this->_freeze) {
232         if (this->_rain_intensity > 0) {
233                 this->_snow_intensity = this->_rain_intensity;
234         }
235     }
236
237     if (_enabled && this->_snow_intensity > 0) {
238         _precipitationEffect->setWind(_wind_vec);
239
240                 
241         if(_droplet_external)
242                 {
243
244                 if ((_freeze) && (_rain_droplet_size > 0.03)) // this is hail or sleet
245                         {
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);
249                         }
250                 else if (_freeze) // this is snow from frozen small rain droplets
251                         {
252                         _precipitationEffect->setParticleSize(_rain_droplet_size*1.3f);
253                         _precipitationEffect->setParticleSpeed( -0.75f - 0.25f*_snow_intensity);
254                         _precipitationEffect->setMaximumParticleDensity(_snow_intensity * 10.0f);
255                         }
256                 else // this was snow in the first place
257                         {
258                         _precipitationEffect->setParticleSize(_snow_flake_size);
259                         _precipitationEffect->setParticleSpeed( -0.75f - 0.25f*_snow_intensity);
260                         _precipitationEffect->setMaximumParticleDensity(_snow_intensity * 7.2f);
261                         }
262                 }
263         else
264                 {
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);
268                 }
269
270
271         _precipitationEffect->setCellSize(osg::Vec3(5.0f / (0.25f+_snow_intensity), 5.0f / (0.25f+_snow_intensity), 5.0f));
272                 
273         _precipitationEffect->setNearTransition(25.f);
274         _precipitationEffect->setFarTransition(100.0f - 60.0f*sqrtf(_snow_intensity));
275                 
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) {
278
279         _precipitationEffect->setWind(_wind_vec);
280
281                 
282         if(_droplet_external)
283                 {
284                 _precipitationEffect->setParticleSize(_rain_droplet_size);
285                 _precipitationEffect->setParticleSpeed( -1.0f - 22.36f*sqrtf(_rain_droplet_size));
286                 }
287         else
288                 {
289                 _precipitationEffect->setParticleSize(0.01 + 0.02*_rain_intensity);
290                 _precipitationEffect->setParticleSpeed( -2.0f + -5.0f*_rain_intensity);
291                 }
292                 
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));
295                 
296         _precipitationEffect->setNearTransition(25.f);
297         _precipitationEffect->setFarTransition(100.0f - 60.0f*sqrtf(_rain_intensity));
298                 
299         _precipitationEffect->setParticleColor( osg::Vec4(0.64 * _illumination, 0.64 * _illumination, 0.64 * _illumination, 0.5));
300     } else {
301         _precipitationEffect->snow(0);  
302         _precipitationEffect->rain(0);  
303     }
304
305     return true;
306 }