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