]> git.mxchange.org Git - flightgear.git/blob - src/Environment/fgclouds.cxx
Canvas Event: expose currentTarget to Nasal.
[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                         cloud_name = cloud_name + "-%d";
218                         char variety_name[50];
219                         do {
220                                 variety++;
221                                 snprintf(variety_name, sizeof(variety_name) - 1, cloud_name.c_str(), variety);
222                         } while( box_def_root->getChild(variety_name, 0, false) );
223
224                         totalCount += count;
225                         if( CloudVarietyCount < 20 )
226                                 CloudVarietyCount++;
227                 }
228         }
229         totalCount = 1.0 / totalCount;
230
231         // Determine how much cloud coverage we need in m^2.
232         double cov = coverage * SGCloudField::fieldSize * SGCloudField::fieldSize;
233
234         while (cov > 0.0f) {
235                 double choice = mt_rand(&seed);
236
237                 for(int i = 0; i < CloudVarietyCount ; i ++) {
238                         choice -= tCloudVariety[i].count * totalCount;
239                         if( choice <= 0.0 ) {
240                                 cov -= buildCloud(cloud_def_root,
241                                                 box_def_root,
242                                                 tCloudVariety[i].name,
243                                                 grid_z_rand,
244                                                 layer);
245                                 break;
246                         }
247                 }
248         }
249
250         // Now we've built any clouds, enable them and set the density (coverage)
251         //layer->setCoverage(coverage);
252         //layer->applyCoverage();
253         thesky->get_cloud_layer(iLayer)->set_enable3dClouds(clouds_3d_enabled);
254 }
255
256 void FGClouds::buildCloudLayers(void) {
257         SGPropertyNode *metar_root = fgGetNode("/environment", true);
258
259         //double wind_speed_kt   = metar_root->getDoubleValue("wind-speed-kt");
260         double temperature_degc  = metar_root->getDoubleValue("temperature-sea-level-degc");
261         double dewpoint_degc     = metar_root->getDoubleValue("dewpoint-sea-level-degc");
262         double pressure_mb       = metar_root->getDoubleValue("pressure-sea-level-inhg") * SG_INHG_TO_PA / 100.0;
263         double rel_humidity      = metar_root->getDoubleValue("relative-humidity");
264
265         // formule d'Epsy, base d'un cumulus
266         double cumulus_base = 122.0 * (temperature_degc - dewpoint_degc);
267         double stratus_base = 100.0 * (100.0 - rel_humidity) * SG_FEET_TO_METER;
268
269     SGSky* thesky = globals->get_renderer()->getSky();
270         for(int iLayer = 0 ; iLayer < thesky->get_cloud_layer_count(); iLayer++) {
271                 SGPropertyNode *cloud_root = fgGetNode("/environment/clouds/layer", iLayer, true);
272
273                 double alt_ft = cloud_root->getDoubleValue("elevation-ft");
274                 double alt_m = alt_ft * SG_FEET_TO_METER;
275                 string coverage = cloud_root->getStringValue("coverage");
276
277                 double coverage_norm = 0.0;
278                 if( coverage == "few" )
279                         coverage_norm = 2.0/8.0;        // <1-2
280                 else if( coverage == "scattered" )
281                         coverage_norm = 4.0/8.0;        // 3-4
282                 else if( coverage == "broken" )
283                         coverage_norm = 6.0/8.0;        // 5-7
284                 else if( coverage == "overcast" )
285                         coverage_norm = 8.0/8.0;        // 8
286
287                 string layer_type = "nn";
288
289                 if( coverage == "cirrus" ) {
290                         layer_type = "ci";
291                 } else if( alt_ft > 16500 ) {
292 //                      layer_type = "ci|cs|cc";
293                         layer_type = "ci";
294                 } else if( alt_ft > 6500 ) {
295 //                      layer_type = "as|ac|ns";
296                         layer_type = "ac";
297                         if( pressure_mb < 1005.0 && coverage_norm >= 0.5 )
298                                 layer_type = "ns";
299                 } else {
300 //                      layer_type = "st|cu|cb|sc";
301                         if( cumulus_base * 0.80 < alt_m && cumulus_base * 1.20 > alt_m ) {
302                                 // +/- 20% from cumulus probable base
303                                 layer_type = "cu";
304                         } else if( stratus_base * 0.80 < alt_m && stratus_base * 1.40 > alt_m ) {
305                                 // +/- 20% from stratus probable base
306                                 layer_type = "st";
307                         } else {
308                                 // above formulae is far from perfect
309                                 if ( alt_ft < 2000 )
310                                         layer_type = "st";
311                                 else if( alt_ft < 4500 )
312                                         layer_type = "cu";
313                                 else
314                                         layer_type = "sc";
315                         }
316                 }
317
318                 cloud_root->setStringValue("layer-type",layer_type);
319                 buildLayer(iLayer, layer_type, coverage_norm);
320         }
321 }
322
323 void FGClouds::set_3dClouds(bool enable)
324 {
325         if (enable != clouds_3d_enabled) {
326                 clouds_3d_enabled = enable;
327                 buildCloudLayers();
328         }
329 }
330
331 bool FGClouds::get_3dClouds() const
332 {
333         return clouds_3d_enabled;
334 }
335
336 /**
337  * Adds a 3D cloud to a cloud layer.
338  *
339  * Property arguments
340  * layer - the layer index to add this cloud to. (Defaults to 0)
341  * index - the index for this cloud (to be used later)
342  * lon/lat/alt - the position for the cloud
343  * (Various) - cloud definition properties. See README.3DClouds
344  *
345  */
346  bool FGClouds::add3DCloud(const SGPropertyNode *arg)
347  {
348    int l = arg->getIntValue("layer", 0);
349    int index = arg->getIntValue("index", 0);
350
351    SGPath texture_root = globals->get_fg_root();
352          texture_root.append("Textures");
353          texture_root.append("Sky");
354
355          float lon = arg->getFloatValue("lon-deg", 0.0f);
356          float lat = arg->getFloatValue("lat-deg", 0.0f);
357          float alt = arg->getFloatValue("alt-ft",  0.0f);
358          float x   = arg->getFloatValue("x-offset-m",  0.0f);
359          float y   = arg->getFloatValue("y-offset-m",  0.0f);
360
361    SGSky* thesky = globals->get_renderer()->getSky();
362    SGCloudField *layer = thesky->get_cloud_layer(l)->get_layer3D();
363    SGNewCloud cld(texture_root, arg, &seed);
364    bool success = layer->addCloud(lon, lat, alt, x, y, index, cld.genCloud());
365
366    // Adding a 3D cloud immediately makes this layer 3D.
367    thesky->get_cloud_layer(l)->set_enable3dClouds(true);
368
369    return success;
370  }
371
372  /**
373   * Removes a 3D cloud from a cloud layer
374   *
375   * Property arguments
376   *
377   * layer - the layer index to remove this cloud from. (defaults to 0)
378   * index - the cloud index
379   *
380   */
381  bool FGClouds::delete3DCloud(const SGPropertyNode *arg)
382  {
383    int l = arg->getIntValue("layer", 0);
384    int i = arg->getIntValue("index", 0);
385
386    SGSky* thesky = globals->get_renderer()->getSky();
387    SGCloudField *layer = thesky->get_cloud_layer(l)->get_layer3D();
388          return layer->deleteCloud(i);
389  }
390
391 /**
392  * Move a cloud within a 3D layer
393  *
394  * Property arguments
395  * layer - the layer index to add this cloud to. (Defaults to 0)
396  * index - the cloud index to move.
397  * lon/lat/alt - the position for the cloud
398  *
399  */
400 bool FGClouds::move3DCloud(const SGPropertyNode *arg)
401  {
402    int l = arg->getIntValue("layer", 0);
403    int i = arg->getIntValue("index", 0);
404       SGSky* thesky = globals->get_renderer()->getSky();
405      
406          float lon = arg->getFloatValue("lon-deg", 0.0f);
407          float lat = arg->getFloatValue("lat-deg", 0.0f);
408          float alt = arg->getFloatValue("alt-ft",  0.0f);
409          float x   = arg->getFloatValue("x-offset-m",  0.0f);
410          float y   = arg->getFloatValue("y-offset-m",  0.0f);
411
412    SGCloudField *layer = thesky->get_cloud_layer(l)->get_layer3D();
413          return layer->repositionCloud(i, lon, lat, alt, x, y);
414  }