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