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.
27 #include <Main/fg_props.hxx>
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>
38 #include <Main/globals.hxx>
39 #include <Airports/simple.hxx>
40 #include <Main/util.hxx>
42 #include "environment_ctrl.hxx"
43 #include "environment_mgr.hxx"
44 #include "fgmetar.hxx"
45 #include "fgclouds.hxx"
50 FGClouds::FGClouds(FGEnvironmentCtrl * controller) :
52 _controller(controller),
53 station_elevation_ft(0.0),
54 clouds_3d_enabled(false),
55 last_scenario( "unset" ),
56 last_env_config( new SGPropertyNode ),
57 last_env_clouds( new SGPropertyNode )
62 FGClouds::~FGClouds() {
65 int FGClouds::get_update_event(void) const {
69 void FGClouds::set_update_event(int count) {
74 void FGClouds::init(void) {
75 if( snd_lightning == NULL ) {
76 snd_lightning = new SGSoundSample(globals->get_fg_root().c_str(), "Sounds/thunder.wav");
77 snd_lightning->set_max_dist(7000.0f);
78 snd_lightning->set_reference_dist(3000.0f);
79 SGSoundMgr *soundMgr = globals->get_soundmgr();
80 soundMgr->add( snd_lightning, "thunder" );
81 sgEnviro.set_soundMgr( soundMgr );
85 void FGClouds::buildCloud(SGPropertyNode *cloud_def_root, SGPropertyNode *box_def_root, const string& name, sgVec3 pos, SGCloudField *layer) {
86 SGPropertyNode *box_def=NULL;
87 SGPropertyNode *cld_def=NULL;
89 SGPath texture_root = globals->get_fg_root();
90 texture_root.append("Textures");
91 texture_root.append("Sky");
93 box_def = box_def_root->getChild(name.c_str());
95 string base_name = name.substr(0,2);
97 if( name[2] == '-' ) {
98 box_def = box_def_root->getChild(base_name.c_str());
104 for(int i = 0; i < box_def->nChildren() ; i++) {
105 SGPropertyNode *abox = box_def->getChild(i);
106 if( strcmp(abox->getName(), "box") == 0) {
107 double x = abox->getDoubleValue("x") + pos[0];
108 double y = abox->getDoubleValue("y") + pos[1];
109 double z = abox->getDoubleValue("z") + pos[2];
110 SGVec3f newpos = SGVec3f(x, z, y);
112 string type = abox->getStringValue("type", "cu-small");
114 cld_def = cloud_def_root->getChild(type.c_str());
115 if ( !cld_def ) return;
117 double min_width = cld_def->getDoubleValue("min-cloud-width-m", 500.0);
118 double max_width = cld_def->getDoubleValue("max-cloud-width-m", 1000.0);
119 double min_height = cld_def->getDoubleValue("min-cloud-height-m", min_width);
120 double max_height = cld_def->getDoubleValue("max-cloud-height-m", max_width);
121 double min_sprite_width = cld_def->getDoubleValue("min-sprite-width-m", 200.0);
122 double max_sprite_width = cld_def->getDoubleValue("max-sprite-width-m", min_sprite_width);
123 double min_sprite_height = cld_def->getDoubleValue("min-sprite-height-m", min_sprite_width);
124 double max_sprite_height = cld_def->getDoubleValue("max-sprite-height-m", max_sprite_width);
125 int num_sprites = cld_def->getIntValue("num-sprites", 20);
126 int num_textures_x = cld_def->getIntValue("num-textures-x", 1);
127 int num_textures_y = cld_def->getIntValue("num-textures-y", 1);
128 double bottom_shade = cld_def->getDoubleValue("bottom-shade", 1.0);
129 string texture = cld_def->getStringValue("texture", "cl_cumulus.rgb");
147 layer->addCloud(newpos, cld);
152 void FGClouds::buildLayer(int iLayer, const string& name, double alt, double coverage) {
157 int CloudVarietyCount = 0;
158 double totalCount = 0.0;
160 SGPropertyNode *cloud_def_root = fgGetNode("/environment/cloudlayers/clouds", false);
161 SGPropertyNode *box_def_root = fgGetNode("/environment/cloudlayers/boxes", false);
162 SGPropertyNode *layer_def_root = fgGetNode("/environment/cloudlayers/layers", false);
163 SGCloudField *layer = thesky->get_cloud_layer(iLayer)->get_layer3D();
166 // If we don't have the required properties, then render the cloud in 2D
167 if ((! clouds_3d_enabled) || coverage == 0.0 ||
168 layer_def_root == NULL || cloud_def_root == NULL || box_def_root == NULL)
170 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
174 // If we can't find a definition for this cloud type, then render the cloud in 2D
175 SGPropertyNode *layer_def=NULL;
176 layer_def = layer_def_root->getChild(name.c_str());
178 if( name[2] == '-' ) {
179 string base_name = name.substr(0,2);
180 layer_def = layer_def_root->getChild(base_name.c_str());
184 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
189 // At this point, we know we've got some 3D clouds to generate.
190 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(true);
192 double grid_x_size = layer_def->getDoubleValue("grid-x-size", 1000.0);
193 double grid_y_size = layer_def->getDoubleValue("grid-y-size", 1000.0);
194 double grid_x_rand = layer_def->getDoubleValue("grid-x-rand", grid_x_size);
195 double grid_y_rand = layer_def->getDoubleValue("grid-y-rand", grid_y_size);
196 double grid_z_rand = layer_def->getDoubleValue("grid-z-rand");
198 for(int i = 0; i < layer_def->nChildren() ; i++) {
199 SGPropertyNode *acloud = layer_def->getChild(i);
200 if( strcmp(acloud->getName(), "cloud") == 0) {
201 string cloud_name = acloud->getStringValue("name");
202 tCloudVariety[CloudVarietyCount].name = cloud_name;
203 double count = acloud->getDoubleValue("count", 1.0);
204 tCloudVariety[CloudVarietyCount].count = count;
206 cloud_name = cloud_name + "-%d";
207 char variety_name[50];
210 snprintf(variety_name, sizeof(variety_name) - 1, cloud_name.c_str(), variety);
211 } while( box_def_root->getChild(variety_name, 0, false) );
214 if( CloudVarietyCount < 20 )
218 totalCount = 1.0 / totalCount;
220 for(double px = 0.0; px < SGCloudField::fieldSize; px += grid_x_size) {
221 for(double py = 0.0; py < SGCloudField::fieldSize; py += grid_y_size) {
222 double x = px + grid_x_rand * (sg_random() - 0.5) - (SGCloudField::fieldSize / 2.0);
223 double y = py + grid_y_rand * (sg_random() - 0.5) - (SGCloudField::fieldSize / 2.0);
224 double z = grid_z_rand * (sg_random() - 0.5);
226 if (sg_random() < coverage)
228 double choice = sg_random();
230 for(int i = 0; i < CloudVarietyCount ; i ++) {
231 choice -= tCloudVariety[i].count * totalCount;
232 if( choice <= 0.0 ) {
235 buildCloud(cloud_def_root,
237 tCloudVariety[i].name,
247 // Now we've built any clouds, enable them and set the density (coverage)
248 //layer->setCoverage(coverage);
249 //layer->applyCoverage();
250 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(clouds_3d_enabled);
253 void FGClouds::buildCloudLayers(void) {
254 SGPropertyNode *metar_root = fgGetNode("/environment", true);
256 //double wind_speed_kt = metar_root->getDoubleValue("wind-speed-kt");
257 double temperature_degc = metar_root->getDoubleValue("temperature-sea-level-degc");
258 double dewpoint_degc = metar_root->getDoubleValue("dewpoint-sea-level-degc");
259 double pressure_mb = metar_root->getDoubleValue("pressure-sea-level-inhg") * SG_INHG_TO_PA / 100.0;
261 double dewp = pow(10.0, 7.5 * dewpoint_degc / (237.7 + dewpoint_degc));
262 double temp = pow(10.0, 7.5 * temperature_degc / (237.7 + temperature_degc));
263 double rel_humidity = dewp * 100 / temp;
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;
269 for(int iLayer = 0 ; iLayer < thesky->get_cloud_layer_count(); iLayer++) {
270 SGPropertyNode *cloud_root = fgGetNode("/environment/clouds/layer", iLayer, true);
272 double alt_ft = cloud_root->getDoubleValue("elevation-ft");
273 double alt_m = alt_ft * SG_FEET_TO_METER;
274 string coverage = cloud_root->getStringValue("coverage");
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
287 string layer_type = "nn";
288 if( coverage == "cirrus" ) {
290 } else if( alt_ft > 16500 ) {
291 // layer_type = "ci|cs|cc";
293 } else if( alt_ft > 6500 ) {
294 // layer_type = "as|ac|ns";
296 if( pressure_mb < 1005.0 && coverage_norm >= 0.5 )
299 // layer_type = "st|cu|cb|sc";
300 if( cumulus_base * 0.80 < alt_m && cumulus_base * 1.20 > alt_m ) {
301 // +/- 20% from cumulus probable base
303 } else if( stratus_base * 0.80 < alt_m && stratus_base * 1.40 > alt_m ) {
304 // +/- 20% from stratus probable base
307 // above formulae is far from perfect
310 else if( alt_ft < 4500 )
317 buildLayer(iLayer, layer_type, alt_m, coverage_norm);
321 // copy from FGMetarEnvironmentCtrl until better
323 FGClouds::update_metar_properties( const FGMetar *m )
327 fgSetString("/environment/metar/station-id", m->getId());
328 fgSetDouble("/environment/metar/min-visibility-m",
329 m->getMinVisibility().getVisibility_m() );
330 fgSetDouble("/environment/metar/max-visibility-m",
331 m->getMaxVisibility().getVisibility_m() );
333 SGPropertyNode *metar = fgGetNode("/environment/metar", true);
334 const SGMetarVisibility *dirvis = m->getDirVisibility();
336 for (i = 0; i < 8; i++, dirvis++) {
337 SGPropertyNode *vis = metar->getChild("visibility", i, true);
338 double v = dirvis->getVisibility_m();
340 vis->setDoubleValue("min-m", v);
341 vis->setDoubleValue("max-m", v);
344 fgSetInt("/environment/metar/base-wind-range-from",
345 m->getWindRangeFrom() );
346 fgSetInt("/environment/metar/base-wind-range-to",
347 m->getWindRangeTo() );
348 fgSetDouble("/environment/metar/base-wind-speed-kt",
349 m->getWindSpeed_kt() );
350 fgSetDouble("/environment/metar/gust-wind-speed-kt",
351 m->getGustSpeed_kt() );
352 fgSetDouble("/environment/metar/temperature-degc",
353 m->getTemperature_C() );
354 fgSetDouble("/environment/metar/dewpoint-degc",
355 m->getDewpoint_C() );
356 fgSetDouble("/environment/metar/rel-humidity-norm",
357 m->getRelHumidity() );
358 fgSetDouble("/environment/metar/pressure-inhg",
359 m->getPressure_inHg() );
361 vector<SGMetarCloud> cv = m->getClouds();
362 vector<SGMetarCloud>::const_iterator cloud, cloud_end = cv.end();
364 // Load into both the METAR and environment properties to stop interpolation
365 SGPropertyNode *metar_clouds = fgGetNode("/environment/metar/clouds", true);
366 SGPropertyNode *clouds = fgGetNode("/environment/clouds", true);
368 for (i = 0, cloud = cv.begin(); i < FGEnvironmentMgr::MAX_CLOUD_LAYERS; i++) {
369 const char *coverage_string[5] = { "clear", "few", "scattered", "broken", "overcast" };
370 const double thickness_value[5] = { 0, 65, 600, 750, 1000 };
372 const char *coverage = "clear";
373 double elevation = -9999.0;
374 double thickness = 0.0;
375 const double span = 40000.0;
377 if (cloud != cloud_end) {
378 int c = cloud->getCoverage();
379 coverage = coverage_string[c];
380 elevation = cloud->getAltitude_ft() + station_elevation_ft;
381 thickness = thickness_value[c];
385 SGPropertyNode *layer;
386 layer = metar_clouds->getChild("layer", i, true);
387 layer->setStringValue("coverage", coverage);
388 layer->setDoubleValue("elevation-ft", elevation);
389 layer->setDoubleValue("thickness-ft", thickness);
390 layer->setDoubleValue("span-m", span);
392 layer = clouds->getChild("layer", i, true);
393 layer->setStringValue("coverage", coverage);
394 layer->setDoubleValue("elevation-ft", elevation);
395 layer->setDoubleValue("thickness-ft", thickness);
396 layer->setDoubleValue("span-m", span);
399 fgSetDouble("/environment/metar/rain-norm", m->getRain());
400 fgSetDouble("/environment/metar/hail-norm", m->getHail());
401 fgSetDouble("/environment/metar/snow-norm", m->getSnow());
402 fgSetBool("/environment/metar/snow-cover", m->getSnowCover());
406 FGClouds::update_env_config ()
408 fgSetupWind( fgGetDouble("/environment/metar/base-wind-range-from"),
409 fgGetDouble("/environment/metar/base-wind-range-to"),
410 fgGetDouble("/environment/metar/base-wind-speed-kt"),
411 fgGetDouble("/environment/metar/gust-wind-speed-kt") );
413 fgDefaultWeatherValue( "visibility-m",
414 fgGetDouble("/environment/metar/min-visibility-m") );
416 set_temp_at_altitude( fgGetDouble("/environment/metar/temperature-degc"),
417 station_elevation_ft );
418 set_dewpoint_at_altitude( fgGetDouble("/environment/metar/dewpoint-degc"),
419 station_elevation_ft );
421 fgDefaultWeatherValue( "pressure-sea-level-inhg",
422 fgGetDouble("/environment/metar/pressure-inhg") );
425 void FGClouds::setLayer( int iLayer, float alt_ft, const string& coverage, const string& layer_type ) {
426 double coverage_norm = 0.0;
427 if( coverage == "few" )
428 coverage_norm = 2.0/8.0; // <1-2
429 else if( coverage == "scattered" )
430 coverage_norm = 4.0/8.0; // 3-4
431 else if( coverage == "broken" )
432 coverage_norm = 6.0/8.0; // 5-7
433 else if( coverage == "overcast" )
434 coverage_norm = 8.0/8.0; // 8
436 buildLayer(iLayer, layer_type, station_elevation_ft + alt_ft * SG_FEET_TO_METER, coverage_norm);
439 void FGClouds::buildScenario( const string& scenario ) {
441 string station = fgGetString("/environment/metar/station-id", "XXXX");
443 // fetch station elevation if exists
444 if( station == "XXXX" )
445 station_elevation_ft = fgGetDouble("/position/ground-elev-m", 0.0) * SG_METER_TO_FEET;
447 const FGAirport* a = FGAirport::findByIdent(station);
448 station_elevation_ft = (a ? a->getElevation() : 0.0);
451 for(int iLayer = 0 ; iLayer < thesky->get_cloud_layer_count(); iLayer++) {
452 thesky->get_cloud_layer(iLayer)
453 ->setCoverage(SGCloudLayer::SG_CLOUD_CLEAR);
454 thesky->get_cloud_layer(iLayer)->get_layer3D()->clear();
457 station += " 011000Z ";
458 if( scenario == "Fair weather" ) {
459 fakeMetar = "15003KT 12SM SCT041 FEW200 20/08 Q1015 NOSIG";
460 //setLayer(0, 3300.0, "scattered", "cu");
461 } else if( scenario == "Thunderstorm" ) {
462 fakeMetar = "15012KT 08SM TSRA SCT040 BKN070 20/12 Q0995";
463 setLayer(0, 4000.0, "scattered", "cb");
464 setLayer(1, 7000.0, "scattered", "ns");
467 FGMetar *m = new FGMetar( station + fakeMetar );
468 update_metar_properties( m );
470 // propagate aloft tables
471 //_controller->reinit();
473 fgSetString("/environment/metar/last-metar", m->getData());
474 // TODO:de-activate real metar updates
475 if( scenario == "Fair weather" ) {
476 fgSetString("/environment/clouds/layer[1]/coverage", "cirrus");
480 void FGClouds::set_scenario(const char * sc) {
482 scenario = string(sc);
484 // if(!rebuild_required && (scenario == last_scenario))
487 if( last_scenario == "none" ) {
488 // save clouds and weather conditions
489 SGPropertyNode *param = fgGetNode("/environment/config", true);
490 copyProperties( param, last_env_config );
491 param = fgGetNode("/environment/clouds", true);
492 copyProperties( param, last_env_clouds );
495 if( scenario == "METAR" ) {
496 // needed here to compute station_elevation_ft
497 buildScenario( scenario );
498 string realMetar = fgGetString("/environment/metar/real-metar", "");
500 if( realMetar != "" ) {
501 fgSetString("/environment/metar/last-metar", realMetar.c_str());
502 FGMetar *m = new FGMetar( realMetar );
503 update_metar_properties( m );
505 // propagate aloft tables
506 _controller->reinit();
510 else if( scenario == "none" ) {
511 // restore clouds and weather conditions
512 SGPropertyNode *param = fgGetNode("/environment/config", true);
513 copyProperties( last_env_config, param );
514 param = fgGetNode("/environment/clouds", true);
515 copyProperties( last_env_clouds, param );
516 fgSetDouble("/environment/metar/rain-norm", 0.0);
517 fgSetDouble("/environment/metar/snow-norm", 0.0);
518 // update_env_config();
519 // propagate aloft tables
520 _controller->reinit();
524 buildScenario( scenario );
525 _controller->reinit();
529 last_scenario = scenario;
531 if( snd_lightning == NULL )
535 const char * FGClouds::get_scenario(void) const
537 return scenario.c_str();
540 void FGClouds::set_3dClouds(bool enable)
542 if (enable != clouds_3d_enabled) {
543 clouds_3d_enabled = enable;
548 bool FGClouds::get_3dClouds() const {
549 return clouds_3d_enabled;