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.
29 #include <Main/fg_props.hxx>
31 #include <simgear/constants.h>
32 #include <simgear/sound/soundmgr_openal.hxx>
33 #include <simgear/scene/sky/sky.hxx>
34 //#include <simgear/environment/visual_enviro.hxx>
35 #include <simgear/scene/sky/cloudfield.hxx>
36 #include <simgear/scene/sky/newcloud.hxx>
37 #include <simgear/structure/commands.hxx>
38 #include <simgear/props/props_io.hxx>
40 #include <Main/globals.hxx>
41 #include <Main/util.hxx>
42 #include <Viewer/renderer.hxx>
43 #include <Airports/airport.hxx>
45 #include "fgclouds.hxx"
47 static bool do_delete_3Dcloud (const SGPropertyNode *arg);
48 static bool do_move_3Dcloud (const SGPropertyNode *arg);
49 static bool do_add_3Dcloud (const SGPropertyNode *arg);
51 // RNG seed to ensure cloud synchronization across multi-process
55 FGClouds::FGClouds() :
59 clouds_3d_enabled(false),
69 int FGClouds::get_update_event(void) const {
73 void FGClouds::set_update_event(int count) {
78 void FGClouds::Init(void) {
80 if( snd_lightning == NULL ) {
81 snd_lightning = new SGSoundSample("Sounds/thunder.wav", SGPath());
82 snd_lightning->set_max_dist(7000.0f);
83 snd_lightning->set_reference_dist(3000.0f);
84 SGSoundMgr *smgr = globals->get_soundmgr();
85 SGSampleGroup *sgr = smgr->find("weather", true);
86 sgr->add( snd_lightning, "thunder" );
90 mt_init_time_10(&seed);
92 globals->get_commands()->addCommand("add-cloud", do_add_3Dcloud);
93 globals->get_commands()->addCommand("del-cloud", do_delete_3Dcloud);
94 globals->get_commands()->addCommand("move-cloud", do_move_3Dcloud);
97 // Build an invidual cloud. Returns the extents of the cloud for coverage calculations
98 double FGClouds::buildCloud(SGPropertyNode *cloud_def_root, SGPropertyNode *box_def_root, const string& name, double grid_z_rand, SGCloudField *layer) {
99 SGPropertyNode *box_def=NULL;
100 SGPropertyNode *cld_def=NULL;
103 SGPath texture_root = globals->get_fg_root();
104 texture_root.append("Textures");
105 texture_root.append("Sky");
107 box_def = box_def_root->getChild(name.c_str());
109 string base_name = name.substr(0,2);
111 if( name[2] == '-' ) {
112 box_def = box_def_root->getChild(base_name.c_str());
118 double x = mt_rand(&seed) * SGCloudField::fieldSize - (SGCloudField::fieldSize / 2.0);
119 double y = mt_rand(&seed) * SGCloudField::fieldSize - (SGCloudField::fieldSize / 2.0);
120 double z = grid_z_rand * (mt_rand(&seed) - 0.5);
122 float lon = fgGetNode("/position/longitude-deg", false)->getFloatValue();
123 float lat = fgGetNode("/position/latitude-deg", false)->getFloatValue();
127 for(int i = 0; i < box_def->nChildren() ; i++) {
128 SGPropertyNode *abox = box_def->getChild(i);
129 if( strcmp(abox->getName(), "box") == 0) {
131 string type = abox->getStringValue("type", "cu-small");
132 cld_def = cloud_def_root->getChild(type.c_str());
133 if ( !cld_def ) return 0.0;
135 double w = abox->getDoubleValue("width", 1000.0);
136 double h = abox->getDoubleValue("height", 1000.0);
137 int hdist = abox->getIntValue("hdist", 1);
138 int vdist = abox->getIntValue("vdist", 1);
140 double c = abox->getDoubleValue("count", 5);
141 int count = (int) (c + (mt_rand(&seed) - 0.5) * c);
143 extent = std::max(w*w, extent);
145 for (int j = 0; j < count; j++) {
147 // Locate the clouds randomly in the defined space. The hdist and
148 // vdist values control the horizontal and vertical distribution
149 // by simply summing random components.
154 for (int k = 0; k < hdist; k++)
156 x += (mt_rand(&seed) / hdist);
157 y += (mt_rand(&seed) / hdist);
160 for (int k = 0; k < vdist; k++)
162 z += (mt_rand(&seed) / vdist);
165 x = w * (x - 0.5) + pos[0]; // N/S
166 y = w * (y - 0.5) + pos[1]; // E/W
167 z = h * z + pos[2]; // Up/Down. pos[2] is the cloudbase
169 //SGVec3f newpos = SGVec3f(x, y, z);
170 SGNewCloud cld(texture_root, cld_def, &seed);
172 //layer->addCloud(newpos, cld.genCloud());
173 layer->addCloud(lon, lat, z, x, y, index++, cld.genCloud());
178 // Return the maximum extent of the cloud
182 void FGClouds::buildLayer(int iLayer, const string& name, double coverage) {
187 int CloudVarietyCount = 0;
188 double totalCount = 0.0;
190 SGSky* thesky = globals->get_renderer()->getSky();
192 SGPropertyNode *cloud_def_root = fgGetNode("/environment/cloudlayers/clouds", false);
193 SGPropertyNode *box_def_root = fgGetNode("/environment/cloudlayers/boxes", false);
194 SGPropertyNode *layer_def_root = fgGetNode("/environment/cloudlayers/layers", false);
195 SGCloudField *layer = thesky->get_cloud_layer(iLayer)->get_layer3D();
198 // If we don't have the required properties, then render the cloud in 2D
199 if ((! clouds_3d_enabled) || coverage == 0.0 ||
200 layer_def_root == NULL || cloud_def_root == NULL || box_def_root == NULL) {
201 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
205 // If we can't find a definition for this cloud type, then render the cloud in 2D
206 SGPropertyNode *layer_def=NULL;
207 layer_def = layer_def_root->getChild(name.c_str());
209 if( name[2] == '-' ) {
210 string base_name = name.substr(0,2);
211 layer_def = layer_def_root->getChild(base_name.c_str());
214 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
219 // At this point, we know we've got some 3D clouds to generate.
220 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(true);
222 double grid_z_rand = layer_def->getDoubleValue("grid-z-rand");
224 for(int i = 0; i < layer_def->nChildren() ; i++) {
225 SGPropertyNode *acloud = layer_def->getChild(i);
226 if( strcmp(acloud->getName(), "cloud") == 0) {
227 string cloud_name = acloud->getStringValue("name");
228 tCloudVariety[CloudVarietyCount].name = cloud_name;
229 double count = acloud->getDoubleValue("count", 1.0);
230 tCloudVariety[CloudVarietyCount].count = count;
232 cloud_name = cloud_name + "-%d";
233 char variety_name[50];
236 snprintf(variety_name, sizeof(variety_name) - 1, cloud_name.c_str(), variety);
237 } while( box_def_root->getChild(variety_name, 0, false) );
240 if( CloudVarietyCount < 20 )
244 totalCount = 1.0 / totalCount;
246 // Determine how much cloud coverage we need in m^2.
247 double cov = coverage * SGCloudField::fieldSize * SGCloudField::fieldSize;
250 double choice = mt_rand(&seed);
252 for(int i = 0; i < CloudVarietyCount ; i ++) {
253 choice -= tCloudVariety[i].count * totalCount;
254 if( choice <= 0.0 ) {
255 cov -= buildCloud(cloud_def_root,
257 tCloudVariety[i].name,
265 // Now we've built any clouds, enable them and set the density (coverage)
266 //layer->setCoverage(coverage);
267 //layer->applyCoverage();
268 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(clouds_3d_enabled);
271 void FGClouds::buildCloudLayers(void) {
272 SGPropertyNode *metar_root = fgGetNode("/environment", true);
274 //double wind_speed_kt = metar_root->getDoubleValue("wind-speed-kt");
275 double temperature_degc = metar_root->getDoubleValue("temperature-sea-level-degc");
276 double dewpoint_degc = metar_root->getDoubleValue("dewpoint-sea-level-degc");
277 double pressure_mb = metar_root->getDoubleValue("pressure-sea-level-inhg") * SG_INHG_TO_PA / 100.0;
278 double rel_humidity = metar_root->getDoubleValue("relative-humidity");
280 // formule d'Epsy, base d'un cumulus
281 double cumulus_base = 122.0 * (temperature_degc - dewpoint_degc);
282 double stratus_base = 100.0 * (100.0 - rel_humidity) * SG_FEET_TO_METER;
284 SGSky* thesky = globals->get_renderer()->getSky();
285 for(int iLayer = 0 ; iLayer < thesky->get_cloud_layer_count(); iLayer++) {
286 SGPropertyNode *cloud_root = fgGetNode("/environment/clouds/layer", iLayer, true);
288 double alt_ft = cloud_root->getDoubleValue("elevation-ft");
289 double alt_m = alt_ft * SG_FEET_TO_METER;
290 string coverage = cloud_root->getStringValue("coverage");
292 double coverage_norm = 0.0;
293 if( coverage == "few" )
294 coverage_norm = 2.0/8.0; // <1-2
295 else if( coverage == "scattered" )
296 coverage_norm = 4.0/8.0; // 3-4
297 else if( coverage == "broken" )
298 coverage_norm = 6.0/8.0; // 5-7
299 else if( coverage == "overcast" )
300 coverage_norm = 8.0/8.0; // 8
302 string layer_type = "nn";
304 if( coverage == "cirrus" ) {
306 } else if( alt_ft > 16500 ) {
307 // layer_type = "ci|cs|cc";
309 } else if( alt_ft > 6500 ) {
310 // layer_type = "as|ac|ns";
312 if( pressure_mb < 1005.0 && coverage_norm >= 0.5 )
315 // layer_type = "st|cu|cb|sc";
316 if( cumulus_base * 0.80 < alt_m && cumulus_base * 1.20 > alt_m ) {
317 // +/- 20% from cumulus probable base
319 } else if( stratus_base * 0.80 < alt_m && stratus_base * 1.40 > alt_m ) {
320 // +/- 20% from stratus probable base
323 // above formulae is far from perfect
326 else if( alt_ft < 4500 )
333 cloud_root->setStringValue("layer-type",layer_type);
334 buildLayer(iLayer, layer_type, coverage_norm);
338 void FGClouds::set_3dClouds(bool enable)
340 if (enable != clouds_3d_enabled) {
341 clouds_3d_enabled = enable;
346 bool FGClouds::get_3dClouds() const
348 return clouds_3d_enabled;
352 * Adds a 3D cloud to a cloud layer.
355 * layer - the layer index to add this cloud to. (Defaults to 0)
356 * index - the index for this cloud (to be used later)
357 * lon/lat/alt - the position for the cloud
358 * (Various) - cloud definition properties. See README.3DClouds
362 do_add_3Dcloud (const SGPropertyNode *arg)
364 int l = arg->getIntValue("layer", 0);
365 int index = arg->getIntValue("index", 0);
367 SGPath texture_root = globals->get_fg_root();
368 texture_root.append("Textures");
369 texture_root.append("Sky");
371 float lon = arg->getFloatValue("lon-deg", 0.0f);
372 float lat = arg->getFloatValue("lat-deg", 0.0f);
373 float alt = arg->getFloatValue("alt-ft", 0.0f);
374 float x = arg->getFloatValue("x-offset-m", 0.0f);
375 float y = arg->getFloatValue("y-offset-m", 0.0f);
377 SGSky* thesky = globals->get_renderer()->getSky();
378 SGCloudField *layer = thesky->get_cloud_layer(l)->get_layer3D();
379 SGNewCloud cld(texture_root, arg, &seed);
380 bool success = layer->addCloud(lon, lat, alt, x, y, index, cld.genCloud());
382 // Adding a 3D cloud immediately makes this layer 3D.
383 thesky->get_cloud_layer(l)->set_enable3dClouds(true);
389 * Removes a 3D cloud from a cloud layer
393 * layer - the layer index to remove this cloud from. (defaults to 0)
394 * index - the cloud index
398 do_delete_3Dcloud (const SGPropertyNode *arg)
400 int l = arg->getIntValue("layer", 0);
401 int i = arg->getIntValue("index", 0);
403 SGSky* thesky = globals->get_renderer()->getSky();
404 SGCloudField *layer = thesky->get_cloud_layer(l)->get_layer3D();
405 return layer->deleteCloud(i);
409 * Move a cloud within a 3D layer
412 * layer - the layer index to add this cloud to. (Defaults to 0)
413 * index - the cloud index to move.
414 * lon/lat/alt - the position for the cloud
418 do_move_3Dcloud (const SGPropertyNode *arg)
420 int l = arg->getIntValue("layer", 0);
421 int i = arg->getIntValue("index", 0);
422 SGSky* thesky = globals->get_renderer()->getSky();
424 float lon = arg->getFloatValue("lon-deg", 0.0f);
425 float lat = arg->getFloatValue("lat-deg", 0.0f);
426 float alt = arg->getFloatValue("alt-ft", 0.0f);
427 float x = arg->getFloatValue("x-offset-m", 0.0f);
428 float y = arg->getFloatValue("y-offset-m", 0.0f);
430 SGCloudField *layer = thesky->get_cloud_layer(l)->get_layer3D();
431 return layer->repositionCloud(i, lon, lat, alt, x, y);