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