]> git.mxchange.org Git - simgear.git/blob - simgear/environment/precipitation.cxx
Merge branch 'timoore/effects-anim-rebase' into next
[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
32 /**
33  * @brief SGPrecipitation constructor
34  *
35  * Build a new OSG object from osgParticle.
36  */
37 SGPrecipitation::SGPrecipitation() :
38     _freeze(false), _snow_intensity(0.0), _rain_intensity(0.0)
39 {
40     _precipitationEffect = new osgParticle::PrecipitationEffect;
41 }
42
43
44 /**
45  * @brief Build and add the object "precipitationEffect"
46  *
47  * This function permits you to create an object precipitationEffect and initialize it.
48  * I define by default the color of water (for raining)
49  */
50 osg::Group* SGPrecipitation::build(void)
51 {
52     osg::Group* group = new osg::Group;
53
54     _precipitationEffect->snow(0);      
55     _precipitationEffect->rain(0);      
56
57     group->addChild(_precipitationEffect.get());
58
59     return group;
60 }
61
62
63 /**
64  * @brief Define the snow intensity
65  *
66  * This function permits you to define and change the snow intensity
67  * The param 'intensity' is normed (0 to 1).
68  */
69 void SGPrecipitation::setSnowIntensity(float intensity)
70 {
71     if (this->_snow_intensity < intensity-0.001)
72         this->_snow_intensity += 0.001;
73     else if (this->_snow_intensity > intensity+0.001)
74         this->_snow_intensity -= 0.001;
75     else
76         this->_snow_intensity = intensity;
77 }
78
79
80 /**
81  * @brief Define the rain intensity
82  *
83  * This function permits you to define and change the rain intensity
84  * The param 'intensity' is normed (0 to 1).
85  */
86 void SGPrecipitation::setRainIntensity(float intensity)
87 {
88     if (this->_rain_intensity < intensity-0.001)
89         this->_rain_intensity += 0.001;
90     else if (this->_rain_intensity > intensity+0.001)
91         this->_rain_intensity -= 0.001;
92     else
93         this->_rain_intensity = intensity;
94 }
95
96
97 /** 
98  * @brief Freeze the rain to snow
99  * 
100  * @param freeze Boolean 
101  * 
102  * This function permits you to turn off the rain to snow.
103  */
104 void SGPrecipitation::setFreezing(bool freeze)
105 {
106     this->_freeze = freeze;
107 }
108
109
110 /**
111  * @brief Define the wind direction and speed
112  *
113  * This function permits you to define and change the wind direction
114  * 
115  * After apply the MatrixTransform to the osg::Precipitation object,
116  * x points full south... From wind heading and speed, we can calculate
117  * the wind vector.
118  */
119 void SGPrecipitation::setWindProperty(double heading, double speed)
120 {
121     double x, y, z;
122
123     heading = (heading + 180) * SGD_DEGREES_TO_RADIANS;
124     speed = speed * SG_FEET_TO_METER;
125
126     x = -cos(heading) * speed;
127     y = sin(heading) * speed;
128     z = 0;
129
130     this->_wind_vec = osg::Vec3(x, y, z);
131 }
132
133
134 /**
135  * @brief Update the precipitation effects
136  *
137  * This function permits you to update the precipitation effects.
138  * Be careful, if snow and rain intensity are greater than '0', snow effect
139  * will be first.
140  *
141  * The settings come from the osgParticule/PrecipitationEffect.cpp exemple.
142  */
143 bool SGPrecipitation::update(void)
144 {
145     if (this->_freeze) {
146         if (this->_rain_intensity > 0) 
147             this->_snow_intensity = this->_rain_intensity;
148     }
149
150     bool enabled = sgEnviro.get_precipitation_enable_state();
151     if (enabled && this->_snow_intensity > 0) {
152         _precipitationEffect->setWind(_wind_vec);
153         _precipitationEffect->setParticleSpeed( -0.75f - 0.25f*_snow_intensity);
154                 
155         _precipitationEffect->setParticleSize(0.02f + 0.03f*_snow_intensity);
156         _precipitationEffect->setMaximumParticleDensity(_snow_intensity * 7.2f);
157         _precipitationEffect->setCellSize(osg::Vec3(5.0f / (0.25f+_snow_intensity), 5.0f / (0.25f+_snow_intensity), 5.0f));
158                 
159         _precipitationEffect->setNearTransition(25.f);
160         _precipitationEffect->setFarTransition(100.0f - 60.0f*sqrtf(_snow_intensity));
161                 
162         _precipitationEffect->setParticleColor(osg::Vec4(0.85, 0.85, 0.85, 1.0) - osg::Vec4(0.1, 0.1, 0.1, 1.0) * _snow_intensity);
163     } else if (enabled && this->_rain_intensity > 0) {
164         _precipitationEffect->setWind(_wind_vec);
165         _precipitationEffect->setParticleSpeed( -2.0f + -5.0f*_rain_intensity);
166                 
167         _precipitationEffect->setParticleSize(0.01 + 0.02*_rain_intensity);
168         _precipitationEffect->setMaximumParticleDensity(_rain_intensity * 7.5f);
169         _precipitationEffect->setCellSize(osg::Vec3(5.0f / (0.25f+_rain_intensity), 5.0f / (0.25f+_rain_intensity), 5.0f));
170                 
171         _precipitationEffect->setNearTransition(25.f);
172         _precipitationEffect->setFarTransition(100.0f - 60.0f*sqrtf(_rain_intensity));
173                 
174         _precipitationEffect->setParticleColor( osg::Vec4(0x7A, 0xCE, 0xFF, 0x80));
175     } else {
176         _precipitationEffect->snow(0);  
177         _precipitationEffect->rain(0);  
178     }
179
180     return true;
181 }