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