]> git.mxchange.org Git - flightgear.git/blob - src/Environment/fgclouds.cxx
e7273a7148ed6b44c05c13c74d6dba536c6d0431
[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 <cstring>
28 #include <Main/fg_props.hxx>
29
30 #include <simgear/constants.h>
31 #include <simgear/sound/soundmgr_openal.hxx>
32 #include <simgear/scene/sky/sky.hxx>
33 //#include <simgear/environment/visual_enviro.hxx>
34 #include <simgear/scene/sky/cloudfield.hxx>
35 #include <simgear/scene/sky/newcloud.hxx>
36 #include <simgear/structure/commands.hxx>
37 #include <simgear/math/sg_random.h>
38 #include <simgear/props/props_io.hxx>
39
40 #include <Main/globals.hxx>
41 #include <Airports/simple.hxx>
42 #include <Main/util.hxx>
43
44 #include "fgclouds.hxx"
45
46 extern SGSky *thesky;
47
48 static bool do_delete_3Dcloud (const SGPropertyNode *arg);
49 static bool do_move_3Dcloud (const SGPropertyNode *arg);
50 static bool do_add_3Dcloud (const SGPropertyNode *arg);
51
52 FGClouds::FGClouds() :
53 #if 0
54     snd_lightning(0),
55 #endif
56     clouds_3d_enabled(false),
57     index(0)
58 {
59         update_event = 0;
60 }
61
62 FGClouds::~FGClouds()
63 {
64 }
65
66 int FGClouds::get_update_event(void) const {
67         return update_event;
68 }
69
70 void FGClouds::set_update_event(int count) {
71         update_event = count;
72         buildCloudLayers();
73 }
74
75 void FGClouds::Init(void) {
76 #if 0
77         if( snd_lightning == NULL ) {
78                 snd_lightning = new SGSoundSample("Sounds/thunder.wav", SGPath());
79                 snd_lightning->set_max_dist(7000.0f);
80                 snd_lightning->set_reference_dist(3000.0f);
81                 SGSoundMgr *smgr = globals->get_soundmgr();
82                 SGSampleGroup *sgr = smgr->find("weather", true);
83                 sgr->add( snd_lightning, "thunder" );
84         }
85 #endif
86
87         globals->get_commands()->addCommand("add-cloud", do_add_3Dcloud);
88         globals->get_commands()->addCommand("del-cloud", do_delete_3Dcloud);
89         globals->get_commands()->addCommand("move-cloud", do_move_3Dcloud);
90 }
91
92 // Build an invidual cloud. Returns the extents of the cloud for coverage calculations
93 double FGClouds::buildCloud(SGPropertyNode *cloud_def_root, SGPropertyNode *box_def_root, const string& name, double grid_z_rand, SGCloudField *layer) {
94         SGPropertyNode *box_def=NULL;
95         SGPropertyNode *cld_def=NULL;
96         double extent = 0.0;
97
98         SGPath texture_root = globals->get_fg_root();
99         texture_root.append("Textures");
100         texture_root.append("Sky");
101
102         box_def = box_def_root->getChild(name.c_str());
103
104         string base_name = name.substr(0,2);
105         if( !box_def ) {
106                 if( name[2] == '-' ) {
107                         box_def = box_def_root->getChild(base_name.c_str());
108                 }
109                 if( !box_def )
110                         return 0.0;
111         }
112
113         double x = sg_random() * SGCloudField::fieldSize - (SGCloudField::fieldSize / 2.0);
114         double y = sg_random() * SGCloudField::fieldSize - (SGCloudField::fieldSize / 2.0);
115         double z = grid_z_rand * (sg_random() - 0.5);
116
117         float lon = fgGetNode("/position/longitude-deg", false)->getFloatValue();
118         float lat = fgGetNode("/position/latitude-deg", false)->getFloatValue();
119
120         SGVec3f pos(x,y,z);
121
122         for(int i = 0; i < box_def->nChildren() ; i++) {
123                 SGPropertyNode *abox = box_def->getChild(i);
124                 if( strcmp(abox->getName(), "box") == 0) {
125
126                         string type = abox->getStringValue("type", "cu-small");
127                         cld_def = cloud_def_root->getChild(type.c_str());
128                         if ( !cld_def ) return 0.0;
129
130                         double w = abox->getDoubleValue("width", 1000.0);
131                         double h = abox->getDoubleValue("height", 1000.0);
132                         int hdist = abox->getIntValue("hdist", 1);
133                         int vdist = abox->getIntValue("vdist", 1);
134
135                         double c = abox->getDoubleValue("count", 5);
136                         int count = (int) (c + (sg_random() - 0.5) * c);
137
138                         extent = std::max(w*w, extent);
139
140                         for (int j = 0; j < count; j++) {
141
142                                 // Locate the clouds randomly in the defined space. The hdist and
143                                 // vdist values control the horizontal and vertical distribution
144                                 // by simply summing random components.
145                                 double x = 0.0;
146                                 double y = 0.0;
147                                 double z = 0.0;
148
149                                 for (int k = 0; k < hdist; k++)
150                                 {
151                                         x += (sg_random() / hdist);
152                                         y += (sg_random() / hdist);
153                                 }
154
155                                 for (int k = 0; k < vdist; k++)
156                                 {
157                                         z += (sg_random() / vdist);
158                                 }
159
160                                 x = w * (x - 0.5) + pos[0]; // N/S
161                                 y = w * (y - 0.5) + pos[1]; // E/W
162                                 z = h * z + pos[2]; // Up/Down. pos[2] is the cloudbase
163
164                                 //SGVec3f newpos = SGVec3f(x, y, z);
165                                 SGNewCloud cld = SGNewCloud(texture_root, cld_def);
166
167                                 //layer->addCloud(newpos, cld.genCloud());
168                                 layer->addCloud(lon, lat, z, x, y, index++, cld.genCloud());
169                         }
170                 }
171         }
172
173         // Return the maximum extent of the cloud
174         return extent;
175 }
176
177 void FGClouds::buildLayer(int iLayer, const string& name, double coverage) {
178         struct {
179                 string name;
180                 double count;
181         } tCloudVariety[20];
182         int CloudVarietyCount = 0;
183         double totalCount = 0.0;
184
185         SGPropertyNode *cloud_def_root = fgGetNode("/environment/cloudlayers/clouds", false);
186         SGPropertyNode *box_def_root   = fgGetNode("/environment/cloudlayers/boxes", false);
187         SGPropertyNode *layer_def_root = fgGetNode("/environment/cloudlayers/layers", false);
188         SGCloudField *layer = thesky->get_cloud_layer(iLayer)->get_layer3D();
189         layer->clear();
190
191         // If we don't have the required properties, then render the cloud in 2D
192         if ((! clouds_3d_enabled) || coverage == 0.0 ||
193                 layer_def_root == NULL || cloud_def_root == NULL || box_def_root == NULL) {
194                         thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
195                         return;
196         }
197
198         // If we can't find a definition for this cloud type, then render the cloud in 2D
199         SGPropertyNode *layer_def=NULL;
200         layer_def = layer_def_root->getChild(name.c_str());
201         if( !layer_def ) {
202                 if( name[2] == '-' ) {
203                         string base_name = name.substr(0,2);
204                         layer_def = layer_def_root->getChild(base_name.c_str());
205                 }
206                 if( !layer_def ) {
207                         thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
208                         return;
209                 }
210         }
211
212         // At this point, we know we've got some 3D clouds to generate.
213         thesky->get_cloud_layer(iLayer)->set_enable3dClouds(true);
214
215         double grid_z_rand = layer_def->getDoubleValue("grid-z-rand");
216
217         for(int i = 0; i < layer_def->nChildren() ; i++) {
218                 SGPropertyNode *acloud = layer_def->getChild(i);
219                 if( strcmp(acloud->getName(), "cloud") == 0) {
220                         string cloud_name = acloud->getStringValue("name");
221                         tCloudVariety[CloudVarietyCount].name = cloud_name;
222                         double count = acloud->getDoubleValue("count", 1.0);
223                         tCloudVariety[CloudVarietyCount].count = count;
224                         int variety = 0;
225                         cloud_name = cloud_name + "-%d";
226                         char variety_name[50];
227                         do {
228                                 variety++;
229                                 snprintf(variety_name, sizeof(variety_name) - 1, cloud_name.c_str(), variety);
230                         } while( box_def_root->getChild(variety_name, 0, false) );
231
232                         totalCount += count;
233                         if( CloudVarietyCount < 20 )
234                                 CloudVarietyCount++;
235                 }
236         }
237         totalCount = 1.0 / totalCount;
238
239         // Determine how much cloud coverage we need in m^2.
240         double cov = coverage * SGCloudField::fieldSize * SGCloudField::fieldSize;
241
242         while (cov > 0.0f) {
243                 double choice = sg_random();
244
245                 for(int i = 0; i < CloudVarietyCount ; i ++) {
246                         choice -= tCloudVariety[i].count * totalCount;
247                         if( choice <= 0.0 ) {
248                                 cov -= buildCloud(cloud_def_root,
249                                                 box_def_root,
250                                                 tCloudVariety[i].name,
251                                                 grid_z_rand,
252                                                 layer);
253                                 break;
254                         }
255                 }
256         }
257
258         // Now we've built any clouds, enable them and set the density (coverage)
259         //layer->setCoverage(coverage);
260         //layer->applyCoverage();
261         thesky->get_cloud_layer(iLayer)->set_enable3dClouds(clouds_3d_enabled);
262 }
263
264 void FGClouds::buildCloudLayers(void) {
265         SGPropertyNode *metar_root = fgGetNode("/environment", true);
266
267         //double wind_speed_kt   = metar_root->getDoubleValue("wind-speed-kt");
268         double temperature_degc  = metar_root->getDoubleValue("temperature-sea-level-degc");
269         double dewpoint_degc     = metar_root->getDoubleValue("dewpoint-sea-level-degc");
270         double pressure_mb       = metar_root->getDoubleValue("pressure-sea-level-inhg") * SG_INHG_TO_PA / 100.0;
271         double rel_humidity      = metar_root->getDoubleValue("relative-humidity");
272
273         // formule d'Epsy, base d'un cumulus
274         double cumulus_base = 122.0 * (temperature_degc - dewpoint_degc);
275         double stratus_base = 100.0 * (100.0 - rel_humidity) * SG_FEET_TO_METER;
276
277         for(int iLayer = 0 ; iLayer < thesky->get_cloud_layer_count(); iLayer++) {
278                 SGPropertyNode *cloud_root = fgGetNode("/environment/clouds/layer", iLayer, true);
279
280                 double alt_ft = cloud_root->getDoubleValue("elevation-ft");
281                 double alt_m = alt_ft * SG_FEET_TO_METER;
282                 string coverage = cloud_root->getStringValue("coverage");
283
284                 double coverage_norm = 0.0;
285                 if( coverage == "few" )
286                         coverage_norm = 2.0/8.0;        // <1-2
287                 else if( coverage == "scattered" )
288                         coverage_norm = 4.0/8.0;        // 3-4
289                 else if( coverage == "broken" )
290                         coverage_norm = 6.0/8.0;        // 5-7
291                 else if( coverage == "overcast" )
292                         coverage_norm = 8.0/8.0;        // 8
293
294                 string layer_type = "nn";
295
296                 if( coverage == "cirrus" ) {
297                         layer_type = "ci";
298                 } else if( alt_ft > 16500 ) {
299 //                      layer_type = "ci|cs|cc";
300                         layer_type = "ci";
301                 } else if( alt_ft > 6500 ) {
302 //                      layer_type = "as|ac|ns";
303                         layer_type = "ac";
304                         if( pressure_mb < 1005.0 && coverage_norm >= 0.5 )
305                                 layer_type = "ns";
306                 } else {
307 //                      layer_type = "st|cu|cb|sc";
308                         if( cumulus_base * 0.80 < alt_m && cumulus_base * 1.20 > alt_m ) {
309                                 // +/- 20% from cumulus probable base
310                                 layer_type = "cu";
311                         } else if( stratus_base * 0.80 < alt_m && stratus_base * 1.40 > alt_m ) {
312                                 // +/- 20% from stratus probable base
313                                 layer_type = "st";
314                         } else {
315                                 // above formulae is far from perfect
316                                 if ( alt_ft < 2000 )
317                                         layer_type = "st";
318                                 else if( alt_ft < 4500 )
319                                         layer_type = "cu";
320                                 else
321                                         layer_type = "sc";
322                         }
323                 }
324
325                 cloud_root->setStringValue("layer-type",layer_type);
326                 buildLayer(iLayer, layer_type, coverage_norm);
327         }
328 }
329
330 void FGClouds::set_3dClouds(bool enable)
331 {
332         if (enable != clouds_3d_enabled) {
333                 clouds_3d_enabled = enable;
334                 buildCloudLayers();
335         }
336 }
337
338 bool FGClouds::get_3dClouds() const
339 {
340         return clouds_3d_enabled;
341 }
342
343 /**
344  * Adds a 3D cloud to a cloud layer.
345  *
346  * Property arguments
347  * layer - the layer index to add this cloud to. (Defaults to 0)
348  * index - the index for this cloud (to be used later)
349  * lon/lat/alt - the position for the cloud
350  * (Various) - cloud definition properties. See README.3DClouds
351  *
352  */
353  static bool
354  do_add_3Dcloud (const SGPropertyNode *arg)
355  {
356    int l = arg->getIntValue("layer", 0);
357    int index = arg->getIntValue("index", 0);
358
359    SGPath texture_root = globals->get_fg_root();
360          texture_root.append("Textures");
361          texture_root.append("Sky");
362
363          float lon = arg->getFloatValue("lon-deg", 0.0f);
364          float lat = arg->getFloatValue("lat-deg", 0.0f);
365          float alt = arg->getFloatValue("alt-ft",  0.0f);
366          float x   = arg->getFloatValue("x-offset-m",  0.0f);
367          float y   = arg->getFloatValue("y-offset-m",  0.0f);
368
369
370    SGCloudField *layer = thesky->get_cloud_layer(l)->get_layer3D();
371    SGNewCloud cld = SGNewCloud(texture_root, arg);
372          bool success = layer->addCloud(lon, lat, alt, x, y, index, cld.genCloud());
373
374    // Adding a 3D cloud immediately makes this layer 3D.
375    thesky->get_cloud_layer(l)->set_enable3dClouds(true);
376
377    return success;
378  }
379
380  /**
381   * Removes a 3D cloud from a cloud layer
382   *
383   * Property arguments
384   *
385   * layer - the layer index to remove this cloud from. (defaults to 0)
386   * index - the cloud index
387   *
388   */
389  static bool
390  do_delete_3Dcloud (const SGPropertyNode *arg)
391  {
392    int l = arg->getIntValue("layer", 0);
393    int i = arg->getIntValue("index", 0);
394
395    SGCloudField *layer = thesky->get_cloud_layer(l)->get_layer3D();
396          return layer->deleteCloud(i);
397  }
398
399 /**
400  * Move a cloud within a 3D layer
401  *
402  * Property arguments
403  * layer - the layer index to add this cloud to. (Defaults to 0)
404  * index - the cloud index to move.
405  * lon/lat/alt - the position for the cloud
406  *
407  */
408  static bool
409  do_move_3Dcloud (const SGPropertyNode *arg)
410  {
411    int l = arg->getIntValue("layer", 0);
412    int i = arg->getIntValue("index", 0);
413
414          float lon = arg->getFloatValue("lon-deg", 0.0f);
415          float lat = arg->getFloatValue("lat-deg", 0.0f);
416          float alt = arg->getFloatValue("alt-ft",  0.0f);
417          float x   = arg->getFloatValue("x-offset-m",  0.0f);
418          float y   = arg->getFloatValue("y-offset-m",  0.0f);
419
420    SGCloudField *layer = thesky->get_cloud_layer(l)->get_layer3D();
421          return layer->repositionCloud(i, lon, lat, alt, x, y);
422  }