1 // Build a cloud layer based on metar
3 // Written by Harald JOHNSEN, started April 2005.
5 // Copyright (C) 2005 Harald JOHNSEN - hjohnsen@evc.net
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.
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.
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.
28 #include <Main/fg_props.hxx>
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/structure/commands.hxx>
37 #include <simgear/math/sg_random.h>
38 #include <simgear/props/props_io.hxx>
40 #include <Main/globals.hxx>
41 #include <Airports/simple.hxx>
42 #include <Main/util.hxx>
44 #include "fgclouds.hxx"
49 FGClouds::FGClouds() :
51 clouds_3d_enabled(false),
61 int FGClouds::get_update_event(void) const {
65 void FGClouds::set_update_event(int count) {
70 void FGClouds::Init(void) {
71 if( snd_lightning == NULL ) {
72 snd_lightning = new SGSoundSample("Sounds/thunder.wav", SGPath());
73 snd_lightning->set_max_dist(7000.0f);
74 snd_lightning->set_reference_dist(3000.0f);
75 SGSoundMgr *smgr = globals->get_soundmgr();
76 SGSampleGroup *sgr = smgr->find("weather", true);
77 sgr->add( snd_lightning, "thunder" );
78 sgEnviro.set_sampleGroup( sgr );
81 globals->get_commands()->addCommand("add-cloud", do_add_3Dcloud);
82 globals->get_commands()->addCommand("del-cloud", do_delete_3Dcloud);
83 globals->get_commands()->addCommand("move-cloud", do_move_3Dcloud);
86 // Build an invidual cloud. Returns the extents of the cloud for coverage calculations
87 double FGClouds::buildCloud(SGPropertyNode *cloud_def_root, SGPropertyNode *box_def_root, const string& name, double grid_z_rand, SGCloudField *layer) {
88 SGPropertyNode *box_def=NULL;
89 SGPropertyNode *cld_def=NULL;
92 SGPath texture_root = globals->get_fg_root();
93 texture_root.append("Textures");
94 texture_root.append("Sky");
96 box_def = box_def_root->getChild(name.c_str());
98 string base_name = name.substr(0,2);
100 if( name[2] == '-' ) {
101 box_def = box_def_root->getChild(base_name.c_str());
107 double x = sg_random() * SGCloudField::fieldSize - (SGCloudField::fieldSize / 2.0);
108 double y = sg_random() * SGCloudField::fieldSize - (SGCloudField::fieldSize / 2.0);
109 double z = grid_z_rand * (sg_random() - 0.5);
111 float lon = fgGetNode("/position/longitude-deg", false)->getFloatValue();
112 float lat = fgGetNode("/position/latitude-deg", false)->getFloatValue();
116 for(int i = 0; i < box_def->nChildren() ; i++) {
117 SGPropertyNode *abox = box_def->getChild(i);
118 if( strcmp(abox->getName(), "box") == 0) {
120 string type = abox->getStringValue("type", "cu-small");
121 cld_def = cloud_def_root->getChild(type.c_str());
122 if ( !cld_def ) return 0.0;
124 double w = abox->getDoubleValue("width", 1000.0);
125 double h = abox->getDoubleValue("height", 1000.0);
126 int hdist = abox->getIntValue("hdist", 1);
127 int vdist = abox->getIntValue("vdist", 1);
129 double c = abox->getDoubleValue("count", 5);
130 int count = (int) (c + (sg_random() - 0.5) * c);
132 extent = max(w*w, extent);
134 for (int j = 0; j < count; j++) {
136 // Locate the clouds randomly in the defined space. The hdist and
137 // vdist values control the horizontal and vertical distribution
138 // by simply summing random components.
143 for (int k = 0; k < hdist; k++)
145 x += (sg_random() / hdist);
146 y += (sg_random() / hdist);
149 for (int k = 0; k < vdist; k++)
151 z += (sg_random() / vdist);
154 x = w * (x - 0.5) + pos[0]; // N/S
155 y = w * (y - 0.5) + pos[1]; // E/W
156 z = h * z + pos[2]; // Up/Down. pos[2] is the cloudbase
158 SGVec3f newpos = SGVec3f(x, y, z);
159 SGNewCloud cld = SGNewCloud(texture_root, cld_def);
161 //layer->addCloud(newpos, cld.genCloud());
162 layer->addCloud(lon, lat, z, x, y, index++, cld.genCloud());
167 // Return the maximum extent of the cloud
171 void FGClouds::buildLayer(int iLayer, const string& name, double coverage) {
176 int CloudVarietyCount = 0;
177 double totalCount = 0.0;
179 SGPropertyNode *cloud_def_root = fgGetNode("/environment/cloudlayers/clouds", false);
180 SGPropertyNode *box_def_root = fgGetNode("/environment/cloudlayers/boxes", false);
181 SGPropertyNode *layer_def_root = fgGetNode("/environment/cloudlayers/layers", false);
182 SGCloudField *layer = thesky->get_cloud_layer(iLayer)->get_layer3D();
185 // If we don't have the required properties, then render the cloud in 2D
186 if ((! clouds_3d_enabled) || coverage == 0.0 ||
187 layer_def_root == NULL || cloud_def_root == NULL || box_def_root == NULL) {
188 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
192 // If we can't find a definition for this cloud type, then render the cloud in 2D
193 SGPropertyNode *layer_def=NULL;
194 layer_def = layer_def_root->getChild(name.c_str());
196 if( name[2] == '-' ) {
197 string base_name = name.substr(0,2);
198 layer_def = layer_def_root->getChild(base_name.c_str());
201 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
206 // At this point, we know we've got some 3D clouds to generate.
207 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(true);
209 double grid_z_rand = layer_def->getDoubleValue("grid-z-rand");
211 for(int i = 0; i < layer_def->nChildren() ; i++) {
212 SGPropertyNode *acloud = layer_def->getChild(i);
213 if( strcmp(acloud->getName(), "cloud") == 0) {
214 string cloud_name = acloud->getStringValue("name");
215 tCloudVariety[CloudVarietyCount].name = cloud_name;
216 double count = acloud->getDoubleValue("count", 1.0);
217 tCloudVariety[CloudVarietyCount].count = count;
219 cloud_name = cloud_name + "-%d";
220 char variety_name[50];
223 snprintf(variety_name, sizeof(variety_name) - 1, cloud_name.c_str(), variety);
224 } while( box_def_root->getChild(variety_name, 0, false) );
227 if( CloudVarietyCount < 20 )
231 totalCount = 1.0 / totalCount;
233 // Determine how much cloud coverage we need in m^2.
234 double cov = coverage * SGCloudField::fieldSize * SGCloudField::fieldSize;
237 double choice = sg_random();
239 for(int i = 0; i < CloudVarietyCount ; i ++) {
240 choice -= tCloudVariety[i].count * totalCount;
241 if( choice <= 0.0 ) {
242 cov -= buildCloud(cloud_def_root,
244 tCloudVariety[i].name,
252 // Now we've built any clouds, enable them and set the density (coverage)
253 //layer->setCoverage(coverage);
254 //layer->applyCoverage();
255 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(clouds_3d_enabled);
258 void FGClouds::buildCloudLayers(void) {
259 SGPropertyNode *metar_root = fgGetNode("/environment", true);
261 //double wind_speed_kt = metar_root->getDoubleValue("wind-speed-kt");
262 double temperature_degc = metar_root->getDoubleValue("temperature-sea-level-degc");
263 double dewpoint_degc = metar_root->getDoubleValue("dewpoint-sea-level-degc");
264 double pressure_mb = metar_root->getDoubleValue("pressure-sea-level-inhg") * SG_INHG_TO_PA / 100.0;
265 double rel_humidity = metar_root->getDoubleValue("relative-humidity");
267 // formule d'Epsy, base d'un cumulus
268 double cumulus_base = 122.0 * (temperature_degc - dewpoint_degc);
269 double stratus_base = 100.0 * (100.0 - rel_humidity) * SG_FEET_TO_METER;
271 for(int iLayer = 0 ; iLayer < thesky->get_cloud_layer_count(); iLayer++) {
272 SGPropertyNode *cloud_root = fgGetNode("/environment/clouds/layer", iLayer, true);
274 double alt_ft = cloud_root->getDoubleValue("elevation-ft");
275 double alt_m = alt_ft * SG_FEET_TO_METER;
276 string coverage = cloud_root->getStringValue("coverage");
278 double coverage_norm = 0.0;
279 if( coverage == "few" )
280 coverage_norm = 2.0/8.0; // <1-2
281 else if( coverage == "scattered" )
282 coverage_norm = 4.0/8.0; // 3-4
283 else if( coverage == "broken" )
284 coverage_norm = 6.0/8.0; // 5-7
285 else if( coverage == "overcast" )
286 coverage_norm = 8.0/8.0; // 8
288 string layer_type = "nn";
290 if( coverage == "cirrus" ) {
292 } else if( alt_ft > 16500 ) {
293 // layer_type = "ci|cs|cc";
295 } else if( alt_ft > 6500 ) {
296 // layer_type = "as|ac|ns";
298 if( pressure_mb < 1005.0 && coverage_norm >= 0.5 )
301 // layer_type = "st|cu|cb|sc";
302 if( cumulus_base * 0.80 < alt_m && cumulus_base * 1.20 > alt_m ) {
303 // +/- 20% from cumulus probable base
305 } else if( stratus_base * 0.80 < alt_m && stratus_base * 1.40 > alt_m ) {
306 // +/- 20% from stratus probable base
309 // above formulae is far from perfect
312 else if( alt_ft < 4500 )
319 cloud_root->setStringValue("layer-type",layer_type);
320 buildLayer(iLayer, layer_type, coverage_norm);
324 void FGClouds::set_3dClouds(bool enable)
326 if (enable != clouds_3d_enabled) {
327 clouds_3d_enabled = enable;
332 bool FGClouds::get_3dClouds() const
334 return clouds_3d_enabled;
338 * Adds a 3D cloud to a cloud layer.
341 * layer - the layer index to add this cloud to. (Defaults to 0)
342 * index - the index for this cloud (to be used later)
343 * lon/lat/alt - the position for the cloud
344 * (Various) - cloud definition properties. See README.3DClouds
348 do_add_3Dcloud (const SGPropertyNode *arg)
350 int l = arg->getIntValue("layer", 0);
351 int index = arg->getIntValue("index", 0);
353 SGPath texture_root = globals->get_fg_root();
354 texture_root.append("Textures");
355 texture_root.append("Sky");
357 float lon = arg->getFloatValue("lon-deg", 0.0f);
358 float lat = arg->getFloatValue("lat-deg", 0.0f);
359 float alt = arg->getFloatValue("alt-ft", 0.0f);
361 // Adding a 3D cloud immediately makes this layer 3D.
362 thesky->get_cloud_layer(l)->set_enable3dClouds(true);
363 SGCloudField *layer = thesky->get_cloud_layer(l)->get_layer3D();
364 SGNewCloud cld = SGNewCloud(texture_root, arg);
365 return layer->addCloud(lon, lat, alt, index, cld.genCloud());
369 * Removes a 3D cloud from a cloud layer
373 * layer - the layer index to remove this cloud from. (defaults to 0)
374 * index - the cloud index
378 do_delete_3Dcloud (const SGPropertyNode *arg)
380 int l = arg->getIntValue("layer", 0);
381 int i = arg->getIntValue("index", 0);
383 SGCloudField *layer = thesky->get_cloud_layer(l)->get_layer3D();
384 return layer->deleteCloud(i);
388 * Move a cloud within a 3D layer
391 * layer - the layer index to add this cloud to. (Defaults to 0)
392 * index - the cloud index to move.
393 * lon/lat/alt - the position for the cloud
397 do_move_3Dcloud (const SGPropertyNode *arg)
399 int l = arg->getIntValue("layer", 0);
400 int i = arg->getIntValue("index", 0);
402 float lon = arg->getFloatValue("lon-deg", 0.0f);
403 float lat = arg->getFloatValue("lat-deg", 0.0f);
404 float alt = arg->getFloatValue("alt-ft", 0.0f);
406 SGCloudField *layer = thesky->get_cloud_layer(l)->get_layer3D();
407 return layer->repositionCloud(i, lon, lat, alt);