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) :
51 station_elevation_ft(0.0),
52 _controller( controller ),
54 last_scenario( "unset" ),
55 last_env_config( new SGPropertyNode() ),
56 last_env_clouds( new SGPropertyNode() )
60 FGClouds::~FGClouds() {
63 int FGClouds::get_update_event(void) const {
66 void FGClouds::set_update_event(int count) {
71 void FGClouds::init(void) {
72 if( snd_lightning == NULL ) {
73 snd_lightning = new SGSoundSample(globals->get_fg_root().c_str(), "Sounds/thunder.wav");
74 snd_lightning->set_max_dist(7000.0f);
75 snd_lightning->set_reference_dist(3000.0f);
76 SGSoundMgr *soundMgr = globals->get_soundmgr();
77 soundMgr->add( snd_lightning, "thunder" );
78 sgEnviro.set_soundMgr( soundMgr );
82 void FGClouds::buildCloud(SGPropertyNode *cloud_def_root, SGPropertyNode *box_def_root, const string& name, sgVec3 pos, SGCloudField *layer) {
83 SGPropertyNode *box_def=NULL;
84 SGPropertyNode *cld_def=NULL;
86 SGPath texture_root = globals->get_fg_root();
87 texture_root.append("Textures");
88 texture_root.append("Sky");
90 box_def = box_def_root->getChild(name.c_str());
92 string base_name = name.substr(0,2);
94 if( name[2] == '-' ) {
95 box_def = box_def_root->getChild(base_name.c_str());
101 for(int i = 0; i < box_def->nChildren() ; i++) {
102 SGPropertyNode *abox = box_def->getChild(i);
103 if( strcmp(abox->getName(), "box") == 0) {
104 double x = abox->getDoubleValue("x") + pos[0];
105 double y = abox->getDoubleValue("y") + pos[1];
106 double z = abox->getDoubleValue("z") + pos[2];
107 SGVec3f newpos = SGVec3f(x, z, y);
109 string type = abox->getStringValue("type", "cu-small");
111 cld_def = cloud_def_root->getChild(type.c_str());
112 if ( !cld_def ) return;
114 double min_width = cld_def->getDoubleValue("min-cloud-width-m", 500.0);
115 double max_width = cld_def->getDoubleValue("max-cloud-width-m", 1000.0);
116 double min_height = cld_def->getDoubleValue("min-cloud-height-m", min_width);
117 double max_height = cld_def->getDoubleValue("max-cloud-height-m", max_width);
118 double min_sprite_width = cld_def->getDoubleValue("min-sprite-width-m", 200.0);
119 double max_sprite_width = cld_def->getDoubleValue("max-sprite-width-m", min_sprite_width);
120 double min_sprite_height = cld_def->getDoubleValue("min-sprite-height-m", min_sprite_width);
121 double max_sprite_height = cld_def->getDoubleValue("max-sprite-height-m", max_sprite_width);
122 int num_sprites = cld_def->getIntValue("num-sprites", 20);
123 int num_textures_x = cld_def->getIntValue("num-textures-x", 1);
124 int num_textures_y = cld_def->getIntValue("num-textures-y", 1);
125 double bottom_shade = cld_def->getDoubleValue("bottom-shade", 1.0);
126 string texture = cld_def->getStringValue("texture", "cl_cumulus.rgb");
144 layer->addCloud(newpos, cld);
149 void FGClouds::buildLayer(int iLayer, const string& name, double alt, double coverage) {
154 int CloudVarietyCount = 0;
155 double totalCount = 0.0;
157 SGPropertyNode *cloud_def_root = fgGetNode("/environment/cloudlayers/clouds", false);
158 SGPropertyNode *box_def_root = fgGetNode("/environment/cloudlayers/boxes", false);
159 SGPropertyNode *layer_def_root = fgGetNode("/environment/cloudlayers/layers", false);
160 SGCloudField *layer = thesky->get_cloud_layer(iLayer)->get_layer3D();
163 // If we don't have the required properties, then render the cloud in 2D
164 if ((! clouds_3d_enabled) || coverage == 0.0 ||
165 layer_def_root == NULL || cloud_def_root == NULL || box_def_root == NULL)
167 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
171 // If we can't find a definition for this cloud type, then render the cloud in 2D
172 SGPropertyNode *layer_def=NULL;
173 layer_def = layer_def_root->getChild(name.c_str());
175 if( name[2] == '-' ) {
176 string base_name = name.substr(0,2);
177 layer_def = layer_def_root->getChild(base_name.c_str());
181 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
186 // At this point, we know we've got some 3D clouds to generate.
187 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(true);
189 double grid_x_size = layer_def->getDoubleValue("grid-x-size", 1000.0);
190 double grid_y_size = layer_def->getDoubleValue("grid-y-size", 1000.0);
191 double grid_x_rand = layer_def->getDoubleValue("grid-x-rand", grid_x_size);
192 double grid_y_rand = layer_def->getDoubleValue("grid-y-rand", grid_y_size);
193 double grid_z_rand = layer_def->getDoubleValue("grid-z-rand");
195 for(int i = 0; i < layer_def->nChildren() ; i++) {
196 SGPropertyNode *acloud = layer_def->getChild(i);
197 if( strcmp(acloud->getName(), "cloud") == 0) {
198 string cloud_name = acloud->getStringValue("name");
199 tCloudVariety[CloudVarietyCount].name = cloud_name;
200 double count = acloud->getDoubleValue("count", 1.0);
201 tCloudVariety[CloudVarietyCount].count = count;
203 cloud_name = cloud_name + "-%d";
204 char variety_name[50];
207 snprintf(variety_name, sizeof(variety_name), cloud_name.c_str(), variety);
208 } while( box_def_root->getChild(variety_name, 0, false) );
211 if( CloudVarietyCount < 20 )
215 totalCount = 1.0 / totalCount;
217 for(double px = 0.0; px < SGCloudField::fieldSize; px += grid_x_size) {
218 for(double py = 0.0; py < SGCloudField::fieldSize; py += grid_y_size) {
219 double x = px + grid_x_rand * (sg_random() - 0.5) - (SGCloudField::fieldSize / 2.0);
220 double y = py + grid_y_rand * (sg_random() - 0.5) - (SGCloudField::fieldSize / 2.0);
221 double z = grid_z_rand * (sg_random() - 0.5);
223 if (sg_random() < coverage)
225 double choice = sg_random();
227 for(int i = 0; i < CloudVarietyCount ; i ++) {
228 choice -= tCloudVariety[i].count * totalCount;
229 if( choice <= 0.0 ) {
232 buildCloud(cloud_def_root,
234 tCloudVariety[i].name,
244 // Now we've built any clouds, enable them and set the density (coverage)
245 //layer->setCoverage(coverage);
246 //layer->applyCoverage();
247 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(clouds_3d_enabled);
250 void FGClouds::buildCloudLayers(void) {
251 SGPropertyNode *metar_root = fgGetNode("/environment", true);
253 double wind_speed_kt = metar_root->getDoubleValue("wind-speed-kt");
254 double temperature_degc = metar_root->getDoubleValue("temperature-sea-level-degc");
255 double dewpoint_degc = metar_root->getDoubleValue("dewpoint-sea-level-degc");
256 double pressure_mb = metar_root->getDoubleValue("pressure-sea-level-inhg") * SG_INHG_TO_PA / 100.0;
258 double dewp = pow(10.0, 7.5 * dewpoint_degc / (237.7 + dewpoint_degc));
259 double temp = pow(10.0, 7.5 * temperature_degc / (237.7 + temperature_degc));
260 double rel_humidity = dewp * 100 / temp;
262 // formule d'Epsy, base d'un cumulus
263 double cumulus_base = 122.0 * (temperature_degc - dewpoint_degc);
264 double stratus_base = 100.0 * (100.0 - rel_humidity) * SG_FEET_TO_METER;
266 bool cu_seen = false;
268 for(int iLayer = 0 ; iLayer < thesky->get_cloud_layer_count(); iLayer++) {
269 SGPropertyNode *cloud_root = fgGetNode("/environment/clouds/layer", iLayer, true);
271 double alt_ft = cloud_root->getDoubleValue("elevation-ft");
272 double alt_m = alt_ft * SG_FEET_TO_METER;
273 string coverage = cloud_root->getStringValue("coverage");
276 double coverage_norm = 0.0;
277 if( coverage == "few" )
278 coverage_norm = 2.0/8.0; // <1-2
279 else if( coverage == "scattered" )
280 coverage_norm = 4.0/8.0; // 3-4
281 else if( coverage == "broken" )
282 coverage_norm = 6.0/8.0; // 5-7
283 else if( coverage == "overcast" )
284 coverage_norm = 8.0/8.0; // 8
286 string layer_type = "nn";
287 if( coverage == "cirrus" ) {
289 } else if( alt_ft > 16500 ) {
290 // layer_type = "ci|cs|cc";
292 } else if( alt_ft > 6500 ) {
293 // layer_type = "as|ac|ns";
295 if( pressure_mb < 1005.0 && coverage_norm >= 0.5 )
298 // layer_type = "st|cu|cb|sc";
299 if( cumulus_base * 0.80 < alt_m && cumulus_base * 1.20 > alt_m ) {
300 // +/- 20% from cumulus probable base
302 } else if( stratus_base * 0.80 < alt_m && stratus_base * 1.40 > alt_m ) {
303 // +/- 20% from stratus probable base
306 // above formulae is far from perfect
309 else if( alt_ft < 4500 )
316 buildLayer(iLayer, layer_type, alt_m, coverage_norm);
320 // copy from FGMetarEnvironmentCtrl until better
322 FGClouds::update_metar_properties( const FGMetar *m )
329 fgSetString("/environment/metar/station-id", m->getId());
330 fgSetDouble("/environment/metar/min-visibility-m",
331 m->getMinVisibility().getVisibility_m() );
332 fgSetDouble("/environment/metar/max-visibility-m",
333 m->getMaxVisibility().getVisibility_m() );
335 const SGMetarVisibility *dirvis = m->getDirVisibility();
336 for (i = 0; i < 8; i++, dirvis++) {
337 const char *min = "/environment/metar/visibility[%d]/min-m";
338 const char *max = "/environment/metar/visibility[%d]/max-m";
340 d = dirvis->getVisibility_m();
342 snprintf(s, 128, min, i);
344 snprintf(s, 128, max, i);
348 fgSetInt("/environment/metar/base-wind-range-from",
349 m->getWindRangeFrom() );
350 fgSetInt("/environment/metar/base-wind-range-to",
351 m->getWindRangeTo() );
352 fgSetDouble("/environment/metar/base-wind-speed-kt",
353 m->getWindSpeed_kt() );
354 fgSetDouble("/environment/metar/gust-wind-speed-kt",
355 m->getGustSpeed_kt() );
356 fgSetDouble("/environment/metar/temperature-degc",
357 m->getTemperature_C() );
358 fgSetDouble("/environment/metar/dewpoint-degc",
359 m->getDewpoint_C() );
360 fgSetDouble("/environment/metar/rel-humidity-norm",
361 m->getRelHumidity() );
362 fgSetDouble("/environment/metar/pressure-inhg",
363 m->getPressure_inHg() );
365 vector<SGMetarCloud> cv = m->getClouds();
366 vector<SGMetarCloud>::const_iterator cloud;
368 // Load into both the METAR and environment properties to stop interpolation
369 const char *cl[] = {"/environment/metar/clouds/layer[%i]",
370 "/environment/clouds/layer[%i]"};
372 for (j = 0; j < 2; j++)
374 for (i = 0, cloud = cv.begin(); cloud != cv.end(); cloud++, i++) {
375 const char *coverage_string[5] =
376 { "clear", "few", "scattered", "broken", "overcast" };
377 const double thickness[5] = { 0, 65, 600,750, 1000};
380 snprintf(s, 128, cl[j], i);
381 strncat(s, "/coverage", 128);
382 q = cloud->getCoverage();
383 fgSetString(s, coverage_string[q] );
385 snprintf(s, 128, cl[j], i);
386 strncat(s, "/elevation-ft", 128);
387 fgSetDouble(s, cloud->getAltitude_ft() + station_elevation_ft);
389 snprintf(s, 128, cl[j], i);
390 strncat(s, "/thickness-ft", 128);
391 fgSetDouble(s, thickness[q]);
393 snprintf(s, 128, cl[j], i);
394 strncat(s, "/span-m", 128);
395 fgSetDouble(s, 40000.0);
398 for (; i < FGEnvironmentMgr::MAX_CLOUD_LAYERS; i++) {
401 snprintf(s, 128, cl[j], i);
402 strncat(s, "/coverage", 128);
403 fgSetString(s, "clear");
405 snprintf(s, 128, cl[j], i);
406 strncat(s, "/elevation-ft", 128);
407 fgSetDouble(s, -9999);
409 snprintf(s, 128, cl[j], i);
410 strncat(s, "/thickness-ft", 128);
413 snprintf(s, 128, cl[j], i);
414 strncat(s, "/span-m", 128);
415 fgSetDouble(s, 40000.0);
419 fgSetDouble("/environment/metar/rain-norm", m->getRain());
420 fgSetDouble("/environment/metar/hail-norm", m->getHail());
421 fgSetDouble("/environment/metar/snow-norm", m->getSnow());
422 fgSetBool("/environment/metar/snow-cover", m->getSnowCover());
426 FGClouds::update_env_config ()
428 fgSetupWind( fgGetDouble("/environment/metar/base-wind-range-from"),
429 fgGetDouble("/environment/metar/base-wind-range-to"),
430 fgGetDouble("/environment/metar/base-wind-speed-kt"),
431 fgGetDouble("/environment/metar/gust-wind-speed-kt") );
433 fgDefaultWeatherValue( "visibility-m",
434 fgGetDouble("/environment/metar/min-visibility-m") );
436 set_temp_at_altitude( fgGetDouble("/environment/metar/temperature-degc"),
437 station_elevation_ft );
438 set_dewpoint_at_altitude( fgGetDouble("/environment/metar/dewpoint-degc"),
439 station_elevation_ft );
441 fgDefaultWeatherValue( "pressure-sea-level-inhg",
442 fgGetDouble("/environment/metar/pressure-inhg") );
445 void FGClouds::setLayer( int iLayer, float alt_ft, const string& coverage, const string& layer_type ) {
446 double coverage_norm = 0.0;
447 if( coverage == "few" )
448 coverage_norm = 2.0/8.0; // <1-2
449 else if( coverage == "scattered" )
450 coverage_norm = 4.0/8.0; // 3-4
451 else if( coverage == "broken" )
452 coverage_norm = 6.0/8.0; // 5-7
453 else if( coverage == "overcast" )
454 coverage_norm = 8.0/8.0; // 8
456 buildLayer(iLayer, layer_type, station_elevation_ft + alt_ft * SG_FEET_TO_METER, coverage_norm);
459 void FGClouds::buildScenario( const string& scenario ) {
461 string station = fgGetString("/environment/metar/station-id", "XXXX");
463 // fetch station elevation if exists
464 if( station == "XXXX" )
465 station_elevation_ft = fgGetDouble("/position/ground-elev-m", 0.0);
467 const FGAirport* a = FGAirport::findByIdent(station);
468 station_elevation_ft = (a ? a->getElevation() : 0.0);
471 for(int iLayer = 0 ; iLayer < thesky->get_cloud_layer_count(); iLayer++) {
472 thesky->get_cloud_layer(iLayer)
473 ->setCoverage(SGCloudLayer::SG_CLOUD_CLEAR);
474 thesky->get_cloud_layer(iLayer)->get_layer3D()->clear();
477 station += " 011000Z ";
478 if( scenario == "Fair weather" ) {
479 fakeMetar = "15003KT 12SM SCT041 FEW200 20/08 Q1015 NOSIG";
480 //setLayer(0, 3300.0, "scattered", "cu");
481 } else if( scenario == "Thunderstorm" ) {
482 fakeMetar = "15012KT 08SM TSRA SCT040 BKN070 20/12 Q0995";
483 setLayer(0, 4000.0, "scattered", "cb");
484 setLayer(1, 7000.0, "scattered", "ns");
487 FGMetar *m = new FGMetar( station + fakeMetar );
488 update_metar_properties( m );
490 // propagate aloft tables
491 //_controller->reinit();
493 fgSetString("/environment/metar/last-metar", m->getData());
494 // TODO:de-activate real metar updates
495 if( scenario == "Fair weather" ) {
496 fgSetString("/environment/clouds/layer[1]/coverage", "cirrus");
500 void FGClouds::set_scenario(const char * sc) {
502 scenario = string(sc);
504 // if(!rebuild_required && (scenario == last_scenario))
507 if( last_scenario == "none" ) {
508 // save clouds and weather conditions
509 SGPropertyNode *param = fgGetNode("/environment/config", true);
510 copyProperties( param, last_env_config );
511 param = fgGetNode("/environment/clouds", true);
512 copyProperties( param, last_env_clouds );
515 if( scenario == "METAR" ) {
516 string realMetar = fgGetString("/environment/metar/real-metar", "");
518 if( realMetar != "" ) {
519 fgSetString("/environment/metar/last-metar", realMetar.c_str());
520 FGMetar *m = new FGMetar( realMetar );
521 update_metar_properties( m );
523 // propagate aloft tables
524 _controller->reinit();
528 else if( scenario == "none" ) {
529 // restore clouds and weather conditions
530 SGPropertyNode *param = fgGetNode("/environment/config", true);
531 copyProperties( last_env_config, param );
532 param = fgGetNode("/environment/clouds", true);
533 copyProperties( last_env_clouds, param );
534 fgSetDouble("/environment/metar/rain-norm", 0.0);
535 fgSetDouble("/environment/metar/snow-norm", 0.0);
536 // update_env_config();
537 // propagate aloft tables
538 _controller->reinit();
542 buildScenario( scenario );
543 _controller->reinit();
547 last_scenario = scenario;
549 if( snd_lightning == NULL )
553 const char * FGClouds::get_scenario(void) const
555 return scenario.c_str();
558 void FGClouds::set_3dClouds(bool enable)
560 if (enable != clouds_3d_enabled) {
561 clouds_3d_enabled = enable;
566 bool FGClouds::get_3dClouds() const {
567 return clouds_3d_enabled;