]> git.mxchange.org Git - flightgear.git/blob - src/Environment/fgclouds.cxx
f58d324f407e373620a0e7a2a3368606ee195377
[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 <Main/fg_props.hxx>
28
29 #include <simgear/constants.h>
30 #include <simgear/sound/soundmgr_openal.hxx>
31 #include <simgear/scene/sky/sky.hxx>
32 #include <simgear/environment/visual_enviro.hxx>
33 #include <simgear/scene/sky/cloudfield.hxx>
34 #include <simgear/scene/sky/newcloud.hxx>
35 #include <simgear/math/sg_random.h>
36 #include <simgear/props/props_io.hxx>
37
38 #include <Main/globals.hxx>
39 #include <Airports/simple.hxx>
40 #include <Main/util.hxx>
41
42 #include "fgclouds.hxx"
43
44 extern SGSky *thesky;
45
46
47 FGClouds::FGClouds() :
48     snd_lightning(0),
49     clouds_3d_enabled(false)
50 {
51         update_event = 0;
52 }
53
54 FGClouds::~FGClouds() {
55 }
56
57 int FGClouds::get_update_event(void) const {
58         return update_event;
59 }
60
61 void FGClouds::set_update_event(int count) {
62         update_event = count;
63         buildCloudLayers();
64 }
65
66 void FGClouds::init(void) {
67         if( snd_lightning == NULL ) {
68                 snd_lightning = new SGSoundSample(globals->get_fg_root().c_str(), "Sounds/thunder.wav");
69                 snd_lightning->set_max_dist(7000.0f);
70                 snd_lightning->set_reference_dist(3000.0f);
71                 SGSoundMgr *smgr = globals->get_soundmgr();
72                 SGSampleGroup *sgr = smgr->find("weather", true);
73                 sgr->add( snd_lightning, "thunder" );
74                 sgEnviro.set_sampleGroup( sgr );
75         }
76 }
77
78 // Build an invidual cloud. Returns the extents of the cloud for coverage calculations
79 double FGClouds::buildCloud(SGPropertyNode *cloud_def_root, SGPropertyNode *box_def_root, const string& name, double grid_z_rand, SGCloudField *layer) {
80         SGPropertyNode *box_def=NULL;
81         SGPropertyNode *cld_def=NULL;
82         double extent = 0.0;
83
84         SGPath texture_root = globals->get_fg_root();
85         texture_root.append("Textures");
86         texture_root.append("Sky");
87
88         box_def = box_def_root->getChild(name.c_str());
89   
90         string base_name = name.substr(0,2);
91         if( !box_def ) {
92                 if( name[2] == '-' ) {
93                         box_def = box_def_root->getChild(base_name.c_str());
94                 }
95                 if( !box_def )
96                         return 0.0;
97         }
98
99         double x = sg_random() * SGCloudField::fieldSize - (SGCloudField::fieldSize / 2.0);
100         double y = sg_random() * SGCloudField::fieldSize - (SGCloudField::fieldSize / 2.0);
101         double z = grid_z_rand * (sg_random() - 0.5);
102                 
103         SGVec3f pos(x,y,z);
104         
105         for(int i = 0; i < box_def->nChildren() ; i++) {
106                 SGPropertyNode *abox = box_def->getChild(i);
107                 if( strcmp(abox->getName(), "box") == 0) {
108
109                         string type = abox->getStringValue("type", "cu-small");
110                         cld_def = cloud_def_root->getChild(type.c_str());
111                         if ( !cld_def ) return 0.0;
112                         
113                         double w = abox->getDoubleValue("width", 1000.0);
114                         double h = abox->getDoubleValue("height", 1000.0);
115                         int hdist = abox->getIntValue("hdist", 1);
116                         int vdist = abox->getIntValue("vdist", 1);
117
118                         double c = abox->getDoubleValue("count", 5);
119                         int count = (int) (c + (sg_random() - 0.5) * c);
120
121                         extent = max(w*w, extent);
122
123                         for (int j = 0; j < count; j++) {
124
125                                 // Locate the clouds randomly in the defined space. The hdist and
126                                 // vdist values control the horizontal and vertical distribution
127                                 // by simply summing random components.
128                                 double x = 0.0;
129                                 double y = 0.0;
130                                 double z = 0.0;
131
132                                 for (int k = 0; k < hdist; k++)
133                                 {
134                                         x += (sg_random() / hdist);
135                                         y += (sg_random() / hdist);
136                                 }
137
138                                 for (int k = 0; k < vdist; k++)
139                                 {
140                                         z += (sg_random() / vdist);
141                                 }
142
143                                 x = w * (x - 0.5) + pos[0]; // N/S
144                                 y = w * (y - 0.5) + pos[1]; // E/W
145                                 z = h * z + pos[2]; // Up/Down. pos[2] is the cloudbase
146
147                                 SGVec3f newpos = SGVec3f(x, y, z);
148
149                                 double min_width = cld_def->getDoubleValue("min-cloud-width-m", 500.0);
150                                 double max_width = cld_def->getDoubleValue("max-cloud-width-m", 1000.0);
151                                 double min_height = cld_def->getDoubleValue("min-cloud-height-m", min_width);
152                                 double max_height = cld_def->getDoubleValue("max-cloud-height-m", max_width);
153                                 double min_sprite_width = cld_def->getDoubleValue("min-sprite-width-m", 200.0);
154                                 double max_sprite_width = cld_def->getDoubleValue("max-sprite-width-m", min_sprite_width);
155                                 double min_sprite_height = cld_def->getDoubleValue("min-sprite-height-m", min_sprite_width);
156                                 double max_sprite_height = cld_def->getDoubleValue("max-sprite-height-m", max_sprite_width);
157                                 int num_sprites = cld_def->getIntValue("num-sprites", 20);
158                                 int num_textures_x = cld_def->getIntValue("num-textures-x", 1);
159                                 int num_textures_y = cld_def->getIntValue("num-textures-y", 1);
160                                 double bottom_shade = cld_def->getDoubleValue("bottom-shade", 1.0);
161                                 string texture = cld_def->getStringValue("texture", "cu.png");
162
163                                 SGNewCloud *cld = 
164                                         new SGNewCloud(type,
165                                                 texture_root, 
166                                                 texture, 
167                                                 min_width, 
168                                                 max_width, 
169                                                 min_height,
170                                                 max_height,
171                                                 min_sprite_width,
172                                                 max_sprite_width,
173                                                 min_sprite_height,
174                                                 max_sprite_height,
175                                                 bottom_shade,
176                                                 num_sprites,
177                                                 num_textures_x,
178                                                 num_textures_y);
179                                 layer->addCloud(newpos, cld);
180                         }
181                 }
182         }
183
184         // Return the maximum extent of the cloud
185         return extent;
186 }
187
188 void FGClouds::buildLayer(int iLayer, const string& name, double alt, double coverage) {
189         struct {
190                 string name;
191                 double count;
192         } tCloudVariety[20];
193         int CloudVarietyCount = 0;
194         double totalCount = 0.0;
195         
196         SGPropertyNode *cloud_def_root = fgGetNode("/environment/cloudlayers/clouds", false);
197         SGPropertyNode *box_def_root   = fgGetNode("/environment/cloudlayers/boxes", false);
198         SGPropertyNode *layer_def_root = fgGetNode("/environment/cloudlayers/layers", false);
199         SGCloudField *layer = thesky->get_cloud_layer(iLayer)->get_layer3D();
200         layer->clear();
201         
202         // If we don't have the required properties, then render the cloud in 2D
203         if ((! clouds_3d_enabled) || coverage == 0.0 ||
204                 layer_def_root == NULL || cloud_def_root == NULL || box_def_root == NULL) {
205                         thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
206                         return;
207         }
208         
209         // If we can't find a definition for this cloud type, then render the cloud in 2D
210         SGPropertyNode *layer_def=NULL;
211         layer_def = layer_def_root->getChild(name.c_str());
212         if( !layer_def ) {
213                 if( name[2] == '-' ) {
214                         string base_name = name.substr(0,2);
215                         layer_def = layer_def_root->getChild(base_name.c_str());
216                 }
217                 if( !layer_def ) {
218                         thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
219                         return;
220                 }
221         }
222
223         // At this point, we know we've got some 3D clouds to generate.
224         thesky->get_cloud_layer(iLayer)->set_enable3dClouds(true);
225
226         double grid_z_rand = layer_def->getDoubleValue("grid-z-rand");
227
228         for(int i = 0; i < layer_def->nChildren() ; i++) {
229                 SGPropertyNode *acloud = layer_def->getChild(i);
230                 if( strcmp(acloud->getName(), "cloud") == 0) {
231                         string cloud_name = acloud->getStringValue("name");
232                         tCloudVariety[CloudVarietyCount].name = cloud_name;
233                         double count = acloud->getDoubleValue("count", 1.0);
234                         tCloudVariety[CloudVarietyCount].count = count;
235                         int variety = 0;
236                         cloud_name = cloud_name + "-%d";
237                         char variety_name[50];
238                         do {
239                                 variety++;
240                                 snprintf(variety_name, sizeof(variety_name) - 1, cloud_name.c_str(), variety);
241                         } while( box_def_root->getChild(variety_name, 0, false) );
242
243                         totalCount += count;
244                         if( CloudVarietyCount < 20 )
245                                 CloudVarietyCount++;
246                 }
247         }
248         totalCount = 1.0 / totalCount;
249
250         // Determine how much cloud coverage we need in m^2.
251         double cov = coverage * SGCloudField::fieldSize * SGCloudField::fieldSize;
252
253         while (cov > 0.0f) {
254                 double choice = sg_random();
255     
256                 for(int i = 0; i < CloudVarietyCount ; i ++) {
257                         choice -= tCloudVariety[i].count * totalCount;
258                         if( choice <= 0.0 ) {
259                                 cov -= buildCloud(cloud_def_root,
260                                                 box_def_root,
261                                                 tCloudVariety[i].name,
262                                                 grid_z_rand,
263                                                 layer);
264                                 break;
265                         }
266                 }
267                 
268         }
269
270         // Now we've built any clouds, enable them and set the density (coverage)
271         //layer->setCoverage(coverage);
272         //layer->applyCoverage();
273         thesky->get_cloud_layer(iLayer)->set_enable3dClouds(clouds_3d_enabled);
274 }
275
276 void FGClouds::buildCloudLayers(void) {
277         SGPropertyNode *metar_root = fgGetNode("/environment", true);
278
279         //double wind_speed_kt   = metar_root->getDoubleValue("wind-speed-kt");
280         double temperature_degc  = metar_root->getDoubleValue("temperature-sea-level-degc");
281         double dewpoint_degc     = metar_root->getDoubleValue("dewpoint-sea-level-degc");
282         double pressure_mb              = metar_root->getDoubleValue("pressure-sea-level-inhg") * SG_INHG_TO_PA / 100.0;
283
284         double dewp = pow(10.0, 7.5 * dewpoint_degc / (237.7 + dewpoint_degc));
285         double temp = pow(10.0, 7.5 * temperature_degc / (237.7 + temperature_degc));
286         double rel_humidity = dewp * 100 / temp;
287
288         // formule d'Epsy, base d'un cumulus
289         double cumulus_base = 122.0 * (temperature_degc - dewpoint_degc);
290         double stratus_base = 100.0 * (100.0 - rel_humidity) * SG_FEET_TO_METER;
291
292         for(int iLayer = 0 ; iLayer < thesky->get_cloud_layer_count(); iLayer++) {
293                 SGPropertyNode *cloud_root = fgGetNode("/environment/clouds/layer", iLayer, true);
294
295                 double alt_ft = cloud_root->getDoubleValue("elevation-ft");
296                 double alt_m = alt_ft * SG_FEET_TO_METER;
297                 string coverage = cloud_root->getStringValue("coverage");
298                 
299                 double coverage_norm = 0.0;
300                 if( coverage == "few" )
301                         coverage_norm = 2.0/8.0;        // <1-2
302                 else if( coverage == "scattered" )
303                         coverage_norm = 4.0/8.0;        // 3-4
304                 else if( coverage == "broken" )
305                         coverage_norm = 6.0/8.0;        // 5-7
306                 else if( coverage == "overcast" )
307                         coverage_norm = 8.0/8.0;        // 8
308
309                 string layer_type = "nn";
310                 if( coverage == "cirrus" ) {
311                         layer_type = "ci";
312                 } else if( alt_ft > 16500 ) {
313 //                      layer_type = "ci|cs|cc";
314                         layer_type = "ci";
315                 } else if( alt_ft > 6500 ) {
316 //                      layer_type = "as|ac|ns";
317                         layer_type = "ac";
318                         if( pressure_mb < 1005.0 && coverage_norm >= 0.5 )
319                                 layer_type = "ns";
320                 } else {
321 //                      layer_type = "st|cu|cb|sc";
322                         if( cumulus_base * 0.80 < alt_m && cumulus_base * 1.20 > alt_m ) {
323                                 // +/- 20% from cumulus probable base
324                                 layer_type = "cu";
325                         } else if( stratus_base * 0.80 < alt_m && stratus_base * 1.40 > alt_m ) {
326                                 // +/- 20% from stratus probable base
327                                 layer_type = "st";
328                         } else {
329                                 // above formulae is far from perfect
330                                 if ( alt_ft < 2000 )
331                                         layer_type = "st";
332                                 else if( alt_ft < 4500 )
333                                         layer_type = "cu";
334                                 else
335                                         layer_type = "sc";
336                         }
337                 }
338                 
339                 buildLayer(iLayer, layer_type, alt_m, coverage_norm);
340         }
341 }
342
343 void FGClouds::set_3dClouds(bool enable)
344 {
345   if (enable != clouds_3d_enabled) {
346     clouds_3d_enabled = enable;
347     buildCloudLayers();
348   }
349 }
350
351 bool FGClouds::get_3dClouds() const {
352   return clouds_3d_enabled;
353 }
354