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