]> git.mxchange.org Git - flightgear.git/blob - src/Environment/fgclouds.cxx
867798af467765c7aeec6959479dd77debc8c41a
[flightgear.git] / src / Environment / fgclouds.cxx
1 // Build a cloud layer based on metar
2 //
3 // Written by Harald JOHNSEN, started April 2005.
4 //
5 // Copyright (C) 2005  Harald JOHNSEN - hjohnsen@evc.net
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 //
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include <Main/fg_props.hxx>
28
29 #include <simgear/constants.h>
30 #include <simgear/sound/soundmgr_openal.hxx>
31 #include <simgear/scene/sky/sky.hxx>
32 #include <simgear/environment/visual_enviro.hxx>
33 #include <simgear/scene/sky/cloudfield.hxx>
34 #include <simgear/scene/sky/newcloud.hxx>
35 #include <simgear/math/sg_random.h>
36 #include <simgear/props/props_io.hxx>
37
38 #include <Main/globals.hxx>
39 #include <Airports/simple.hxx>
40 #include <Main/util.hxx>
41
42 #include "fgclouds.hxx"
43
44 extern SGSky *thesky;
45
46
47 FGClouds::FGClouds() :
48     snd_lightning(0),
49     clouds_3d_enabled(false)
50 {
51         update_event = 0;
52 }
53
54 FGClouds::~FGClouds() {
55 }
56
57 int FGClouds::get_update_event(void) const {
58         return update_event;
59 }
60
61 void FGClouds::set_update_event(int count) {
62         update_event = count;
63         buildCloudLayers();
64 }
65
66 void FGClouds::init(void) {
67         if( snd_lightning == NULL ) {
68                 snd_lightning = new SGSoundSample(globals->get_fg_root().c_str(), "Sounds/thunder.wav");
69                 snd_lightning->set_max_dist(7000.0f);
70                 snd_lightning->set_reference_dist(3000.0f);
71                 SGSoundMgr *soundMgr = globals->get_soundmgr();
72                 soundMgr->add( snd_lightning, "thunder" );
73                 sgEnviro.set_soundMgr( soundMgr );
74         }
75 }
76
77 void FGClouds::buildCloud(SGPropertyNode *cloud_def_root, SGPropertyNode *box_def_root, const string& name, sgVec3 pos, SGCloudField *layer) {
78         SGPropertyNode *box_def=NULL;
79         SGPropertyNode *cld_def=NULL;
80
81         SGPath texture_root = globals->get_fg_root();
82         texture_root.append("Textures");
83         texture_root.append("Sky");
84
85         box_def = box_def_root->getChild(name.c_str());
86   
87         string base_name = name.substr(0,2);
88         if( !box_def ) {
89                 if( name[2] == '-' ) {
90                         box_def = box_def_root->getChild(base_name.c_str());
91                 }
92                 if( !box_def )
93                         return;
94         }
95         
96         for(int i = 0; i < box_def->nChildren() ; i++) {
97                 SGPropertyNode *abox = box_def->getChild(i);
98                 if( strcmp(abox->getName(), "box") == 0) {
99                         double x = abox->getDoubleValue("x") + pos[0];
100                         double y = abox->getDoubleValue("y") + pos[1];
101                         double z = abox->getDoubleValue("z") + pos[2];
102                         SGVec3f newpos = SGVec3f(x, z, y);
103                         
104                         string type = abox->getStringValue("type", "cu-small");
105                                
106                         cld_def = cloud_def_root->getChild(type.c_str());
107                         if ( !cld_def ) return;
108                         
109                         double min_width = cld_def->getDoubleValue("min-cloud-width-m", 500.0);
110                         double max_width = cld_def->getDoubleValue("max-cloud-width-m", 1000.0);
111                         double min_height = cld_def->getDoubleValue("min-cloud-height-m", min_width);
112                         double max_height = cld_def->getDoubleValue("max-cloud-height-m", max_width);
113                         double min_sprite_width = cld_def->getDoubleValue("min-sprite-width-m", 200.0);
114                         double max_sprite_width = cld_def->getDoubleValue("max-sprite-width-m", min_sprite_width);
115                         double min_sprite_height = cld_def->getDoubleValue("min-sprite-height-m", min_sprite_width);
116                         double max_sprite_height = cld_def->getDoubleValue("max-sprite-height-m", max_sprite_width);
117                         int num_sprites = cld_def->getIntValue("num-sprites", 20);
118                         int num_textures_x = cld_def->getIntValue("num-textures-x", 1);
119                         int num_textures_y = cld_def->getIntValue("num-textures-y", 1);
120                         double bottom_shade = cld_def->getDoubleValue("bottom-shade", 1.0);
121                         string texture = cld_def->getStringValue("texture", "cl_cumulus.rgb");
122
123                         SGNewCloud *cld = 
124                                 new SGNewCloud(type,
125                                         texture_root, 
126                                         texture, 
127                                         min_width, 
128                                         max_width, 
129                                         min_height,
130                                         max_height,
131                                         min_sprite_width,
132                                         max_sprite_width,
133                                         min_sprite_height,
134                                         max_sprite_height,
135                                         bottom_shade,
136                                         num_sprites,
137                                         num_textures_x,
138                                         num_textures_y);
139                         layer->addCloud(newpos, cld);
140                 }
141         }
142 }
143
144 void FGClouds::buildLayer(int iLayer, const string& name, double alt, double coverage) {
145         struct {
146                 string name;
147                 double count;
148         } tCloudVariety[20];
149         int CloudVarietyCount = 0;
150         double totalCount = 0.0;
151         
152         SGPropertyNode *cloud_def_root = fgGetNode("/environment/cloudlayers/clouds", false);
153         SGPropertyNode *box_def_root   = fgGetNode("/environment/cloudlayers/boxes", false);
154         SGPropertyNode *layer_def_root = fgGetNode("/environment/cloudlayers/layers", false);
155         SGCloudField *layer = thesky->get_cloud_layer(iLayer)->get_layer3D();
156         layer->clear();
157         
158         // If we don't have the required properties, then render the cloud in 2D
159         if ((! clouds_3d_enabled) || coverage == 0.0 ||
160                 layer_def_root == NULL || cloud_def_root == NULL || box_def_root == NULL) {
161                         thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
162                         return;
163         }
164         
165         // If we can't find a definition for this cloud type, then render the cloud in 2D
166         SGPropertyNode *layer_def=NULL;
167         layer_def = layer_def_root->getChild(name.c_str());
168         if( !layer_def ) {
169                 if( name[2] == '-' ) {
170                         string base_name = name.substr(0,2);
171                         layer_def = layer_def_root->getChild(base_name.c_str());
172                 }
173                 if( !layer_def ) {
174                         thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
175                         return;
176                 }
177         }
178         
179         // At this point, we know we've got some 3D clouds to generate.
180         thesky->get_cloud_layer(iLayer)->set_enable3dClouds(true);
181
182         double grid_x_size = layer_def->getDoubleValue("grid-x-size", 1000.0);
183         double grid_y_size = layer_def->getDoubleValue("grid-y-size", 1000.0);
184         double grid_x_rand = layer_def->getDoubleValue("grid-x-rand", grid_x_size);
185         double grid_y_rand = layer_def->getDoubleValue("grid-y-rand", grid_y_size);
186         double grid_z_rand = layer_def->getDoubleValue("grid-z-rand");
187
188         for(int i = 0; i < layer_def->nChildren() ; i++) {
189                 SGPropertyNode *acloud = layer_def->getChild(i);
190                 if( strcmp(acloud->getName(), "cloud") == 0) {
191                         string cloud_name = acloud->getStringValue("name");
192                         tCloudVariety[CloudVarietyCount].name = cloud_name;
193                         double count = acloud->getDoubleValue("count", 1.0);
194                         tCloudVariety[CloudVarietyCount].count = count;
195                         int variety = 0;
196                         cloud_name = cloud_name + "-%d";
197                         char variety_name[50];
198                         do {
199                                 variety++;
200                                 snprintf(variety_name, sizeof(variety_name) - 1, cloud_name.c_str(), variety);
201                         } while( box_def_root->getChild(variety_name, 0, false) );
202
203                         totalCount += count;
204                         if( CloudVarietyCount < 20 )
205                                 CloudVarietyCount++;
206                 }
207         }
208         totalCount = 1.0 / totalCount;
209         
210         for(double px = 0.0; px < SGCloudField::fieldSize; px += grid_x_size) {
211                 for(double py = 0.0; py < SGCloudField::fieldSize; py += grid_y_size) {
212                         double x = px + grid_x_rand * (sg_random() - 0.5) - (SGCloudField::fieldSize / 2.0);
213                         double y = py + grid_y_rand * (sg_random() - 0.5) - (SGCloudField::fieldSize / 2.0);
214                         double z = grid_z_rand * (sg_random() - 0.5);
215                         
216                         if (sg_random() < coverage) {
217                                 double choice = sg_random();
218     
219                                 for(int i = 0; i < CloudVarietyCount ; i ++) {
220                                         choice -= tCloudVariety[i].count * totalCount;
221                                         if( choice <= 0.0 ) {
222                                                 sgVec3 pos={x,z,y};
223
224                                                 buildCloud(cloud_def_root, 
225                                                         box_def_root, 
226                                                         tCloudVariety[i].name, 
227                                                         pos, 
228                                                         layer);
229                                                 break;
230                                         }
231                                 }
232                         }
233                 }
234         }
235
236         // Now we've built any clouds, enable them and set the density (coverage)
237         //layer->setCoverage(coverage);
238         //layer->applyCoverage();
239         thesky->get_cloud_layer(iLayer)->set_enable3dClouds(clouds_3d_enabled);
240 }
241
242 void FGClouds::buildCloudLayers(void) {
243         SGPropertyNode *metar_root = fgGetNode("/environment", true);
244
245         //double wind_speed_kt   = metar_root->getDoubleValue("wind-speed-kt");
246         double temperature_degc  = metar_root->getDoubleValue("temperature-sea-level-degc");
247         double dewpoint_degc     = metar_root->getDoubleValue("dewpoint-sea-level-degc");
248         double pressure_mb              = metar_root->getDoubleValue("pressure-sea-level-inhg") * SG_INHG_TO_PA / 100.0;
249
250         double dewp = pow(10.0, 7.5 * dewpoint_degc / (237.7 + dewpoint_degc));
251         double temp = pow(10.0, 7.5 * temperature_degc / (237.7 + temperature_degc));
252         double rel_humidity = dewp * 100 / temp;
253
254         // formule d'Epsy, base d'un cumulus
255         double cumulus_base = 122.0 * (temperature_degc - dewpoint_degc);
256         double stratus_base = 100.0 * (100.0 - rel_humidity) * SG_FEET_TO_METER;
257
258         for(int iLayer = 0 ; iLayer < thesky->get_cloud_layer_count(); iLayer++) {
259                 SGPropertyNode *cloud_root = fgGetNode("/environment/clouds/layer", iLayer, true);
260
261                 double alt_ft = cloud_root->getDoubleValue("elevation-ft");
262                 double alt_m = alt_ft * SG_FEET_TO_METER;
263                 string coverage = cloud_root->getStringValue("coverage");
264                 
265                 double coverage_norm = 0.0;
266                 if( coverage == "few" )
267                         coverage_norm = 2.0/8.0;        // <1-2
268                 else if( coverage == "scattered" )
269                         coverage_norm = 4.0/8.0;        // 3-4
270                 else if( coverage == "broken" )
271                         coverage_norm = 6.0/8.0;        // 5-7
272                 else if( coverage == "overcast" )
273                         coverage_norm = 8.0/8.0;        // 8
274
275                 string layer_type = "nn";
276                 if( coverage == "cirrus" ) {
277                         layer_type = "ci";
278                 } else if( alt_ft > 16500 ) {
279 //                      layer_type = "ci|cs|cc";
280                         layer_type = "ci";
281                 } else if( alt_ft > 6500 ) {
282 //                      layer_type = "as|ac|ns";
283                         layer_type = "ac";
284                         if( pressure_mb < 1005.0 && coverage_norm >= 0.5 )
285                                 layer_type = "ns";
286                 } else {
287 //                      layer_type = "st|cu|cb|sc";
288                         if( cumulus_base * 0.80 < alt_m && cumulus_base * 1.20 > alt_m ) {
289                                 // +/- 20% from cumulus probable base
290                                 layer_type = "cu";
291                         } else if( stratus_base * 0.80 < alt_m && stratus_base * 1.40 > alt_m ) {
292                                 // +/- 20% from stratus probable base
293                                 layer_type = "st";
294                         } else {
295                                 // above formulae is far from perfect
296                                 if ( alt_ft < 2000 )
297                                         layer_type = "st";
298                                 else if( alt_ft < 4500 )
299                                         layer_type = "cu";
300                                 else
301                                         layer_type = "sc";
302                         }
303                 }
304                 
305                 buildLayer(iLayer, layer_type, alt_m, coverage_norm);
306         }
307 }
308
309 void FGClouds::set_3dClouds(bool enable)
310 {
311   if (enable != clouds_3d_enabled) {
312     clouds_3d_enabled = enable;
313     buildCloudLayers();
314   }
315 }
316
317 bool FGClouds::get_3dClouds() const {
318   return clouds_3d_enabled;
319 }
320