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