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