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 "fgclouds.hxx"
47 FGClouds::FGClouds() :
49 clouds_3d_enabled(false)
54 FGClouds::~FGClouds() {
57 int FGClouds::get_update_event(void) const {
61 void FGClouds::set_update_event(int count) {
66 void FGClouds::init(void) {
67 if( snd_lightning == NULL ) {
68 snd_lightning = new SGSoundSample(globals->get_fg_root().c_str(), "Sounds/thunder.wav");
69 snd_lightning->set_max_dist(7000.0f);
70 snd_lightning->set_reference_dist(3000.0f);
71 SGSoundMgr *smgr = globals->get_soundmgr();
72 SGSampleGroup *sgr = smgr->find("weather", true);
73 sgr->add( snd_lightning, "thunder" );
74 sgEnviro.set_sampleGroup( sgr );
78 // Build an invidual cloud. Returns the extents of the cloud for coverage calculations
79 double FGClouds::buildCloud(SGPropertyNode *cloud_def_root, SGPropertyNode *box_def_root, const string& name, double grid_z_rand, SGCloudField *layer) {
80 SGPropertyNode *box_def=NULL;
81 SGPropertyNode *cld_def=NULL;
84 SGPath texture_root = globals->get_fg_root();
85 texture_root.append("Textures");
86 texture_root.append("Sky");
88 box_def = box_def_root->getChild(name.c_str());
90 string base_name = name.substr(0,2);
92 if( name[2] == '-' ) {
93 box_def = box_def_root->getChild(base_name.c_str());
99 double x = sg_random() * SGCloudField::fieldSize - (SGCloudField::fieldSize / 2.0);
100 double y = sg_random() * SGCloudField::fieldSize - (SGCloudField::fieldSize / 2.0);
101 double z = grid_z_rand * (sg_random() - 0.5);
106 for(int i = 0; i < box_def->nChildren() ; i++) {
107 SGPropertyNode *abox = box_def->getChild(i);
108 if( strcmp(abox->getName(), "box") == 0) {
110 string type = abox->getStringValue("type", "cu-small");
111 cld_def = cloud_def_root->getChild(type.c_str());
112 if ( !cld_def ) return 0.0;
114 double w = abox->getDoubleValue("width", 1000.0);
115 double h = abox->getDoubleValue("height", 1000.0);
116 int hdist = abox->getIntValue("hdist", 1);
117 int vdist = abox->getIntValue("vdist", 1);
119 double c = abox->getDoubleValue("count", 5);
120 int count = (int) (c + (sg_random() - 0.5) * c);
122 extent = max(w*w, extent);
124 for (int j = 0; j < count; j++) {
126 // Locate the clouds randomly in the defined space. The hdist and
127 // vdist values control the horizontal and vertical distribution
128 // by simply summing random components.
133 for (int k = 0; k < hdist; k++)
135 x += (sg_random() / hdist);
136 y += (sg_random() / hdist);
139 for (int k = 0; k < vdist; k++)
141 z += (sg_random() / vdist);
144 x = w * (x - 0.5) + pos[0]; // N/S
145 y = w * (y - 0.5) + pos[1]; // E/W
146 z = h * z + pos[2]; // Up/Down. pos[2] is the cloudbase
148 SGVec3f newpos = SGVec3f(x, y, z);
150 double min_width = cld_def->getDoubleValue("min-cloud-width-m", 500.0);
151 double max_width = cld_def->getDoubleValue("max-cloud-width-m", 1000.0);
152 double min_height = cld_def->getDoubleValue("min-cloud-height-m", min_width);
153 double max_height = cld_def->getDoubleValue("max-cloud-height-m", max_width);
154 double min_sprite_width = cld_def->getDoubleValue("min-sprite-width-m", 200.0);
155 double max_sprite_width = cld_def->getDoubleValue("max-sprite-width-m", min_sprite_width);
156 double min_sprite_height = cld_def->getDoubleValue("min-sprite-height-m", min_sprite_width);
157 double max_sprite_height = cld_def->getDoubleValue("max-sprite-height-m", max_sprite_width);
158 int num_sprites = cld_def->getIntValue("num-sprites", 20);
159 int num_textures_x = cld_def->getIntValue("num-textures-x", 1);
160 int num_textures_y = cld_def->getIntValue("num-textures-y", 1);
161 double bottom_shade = cld_def->getDoubleValue("bottom-shade", 1.0);
162 string texture = cld_def->getStringValue("texture", "cu.png");
180 layer->addCloud(newpos, cld);
185 // Return the maximum extent of the cloud
189 void FGClouds::buildLayer(int iLayer, const string& name, double alt, double coverage) {
194 int CloudVarietyCount = 0;
195 double totalCount = 0.0;
197 SGPropertyNode *cloud_def_root = fgGetNode("/environment/cloudlayers/clouds", false);
198 SGPropertyNode *box_def_root = fgGetNode("/environment/cloudlayers/boxes", false);
199 SGPropertyNode *layer_def_root = fgGetNode("/environment/cloudlayers/layers", false);
200 SGCloudField *layer = thesky->get_cloud_layer(iLayer)->get_layer3D();
203 // If we don't have the required properties, then render the cloud in 2D
204 if ((! clouds_3d_enabled) || coverage == 0.0 ||
205 layer_def_root == NULL || cloud_def_root == NULL || box_def_root == NULL) {
206 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
210 // If we can't find a definition for this cloud type, then render the cloud in 2D
211 SGPropertyNode *layer_def=NULL;
212 layer_def = layer_def_root->getChild(name.c_str());
214 if( name[2] == '-' ) {
215 string base_name = name.substr(0,2);
216 layer_def = layer_def_root->getChild(base_name.c_str());
219 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
224 // At this point, we know we've got some 3D clouds to generate.
225 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(true);
227 double grid_z_rand = layer_def->getDoubleValue("grid-z-rand");
229 for(int i = 0; i < layer_def->nChildren() ; i++) {
230 SGPropertyNode *acloud = layer_def->getChild(i);
231 if( strcmp(acloud->getName(), "cloud") == 0) {
232 string cloud_name = acloud->getStringValue("name");
233 tCloudVariety[CloudVarietyCount].name = cloud_name;
234 double count = acloud->getDoubleValue("count", 1.0);
235 tCloudVariety[CloudVarietyCount].count = count;
237 cloud_name = cloud_name + "-%d";
238 char variety_name[50];
241 snprintf(variety_name, sizeof(variety_name) - 1, cloud_name.c_str(), variety);
242 } while( box_def_root->getChild(variety_name, 0, false) );
245 if( CloudVarietyCount < 20 )
249 totalCount = 1.0 / totalCount;
251 // Determine how much cloud coverage we need in m^2.
252 double cov = coverage * SGCloudField::fieldSize * SGCloudField::fieldSize;
255 double choice = sg_random();
257 for(int i = 0; i < CloudVarietyCount ; i ++) {
258 choice -= tCloudVariety[i].count * totalCount;
259 if( choice <= 0.0 ) {
260 cov -= buildCloud(cloud_def_root,
262 tCloudVariety[i].name,
271 // Now we've built any clouds, enable them and set the density (coverage)
272 //layer->setCoverage(coverage);
273 //layer->applyCoverage();
274 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(clouds_3d_enabled);
277 void FGClouds::buildCloudLayers(void) {
278 SGPropertyNode *metar_root = fgGetNode("/environment", true);
280 //double wind_speed_kt = metar_root->getDoubleValue("wind-speed-kt");
281 double temperature_degc = metar_root->getDoubleValue("temperature-sea-level-degc");
282 double dewpoint_degc = metar_root->getDoubleValue("dewpoint-sea-level-degc");
283 double pressure_mb = metar_root->getDoubleValue("pressure-sea-level-inhg") * SG_INHG_TO_PA / 100.0;
285 double dewp = pow(10.0, 7.5 * dewpoint_degc / (237.7 + dewpoint_degc));
286 double temp = pow(10.0, 7.5 * temperature_degc / (237.7 + temperature_degc));
287 double rel_humidity = dewp * 100 / temp;
289 // formule d'Epsy, base d'un cumulus
290 double cumulus_base = 122.0 * (temperature_degc - dewpoint_degc);
291 double stratus_base = 100.0 * (100.0 - rel_humidity) * SG_FEET_TO_METER;
293 for(int iLayer = 0 ; iLayer < thesky->get_cloud_layer_count(); iLayer++) {
294 SGPropertyNode *cloud_root = fgGetNode("/environment/clouds/layer", iLayer, true);
296 double alt_ft = cloud_root->getDoubleValue("elevation-ft");
297 double alt_m = alt_ft * SG_FEET_TO_METER;
298 string coverage = cloud_root->getStringValue("coverage");
300 double coverage_norm = 0.0;
301 if( coverage == "few" )
302 coverage_norm = 2.0/8.0; // <1-2
303 else if( coverage == "scattered" )
304 coverage_norm = 4.0/8.0; // 3-4
305 else if( coverage == "broken" )
306 coverage_norm = 6.0/8.0; // 5-7
307 else if( coverage == "overcast" )
308 coverage_norm = 8.0/8.0; // 8
310 string layer_type = "nn";
311 if( coverage == "cirrus" ) {
313 } else if( alt_ft > 16500 ) {
314 // layer_type = "ci|cs|cc";
316 } else if( alt_ft > 6500 ) {
317 // layer_type = "as|ac|ns";
319 if( pressure_mb < 1005.0 && coverage_norm >= 0.5 )
322 // layer_type = "st|cu|cb|sc";
323 if( cumulus_base * 0.80 < alt_m && cumulus_base * 1.20 > alt_m ) {
324 // +/- 20% from cumulus probable base
326 } else if( stratus_base * 0.80 < alt_m && stratus_base * 1.40 > alt_m ) {
327 // +/- 20% from stratus probable base
330 // above formulae is far from perfect
333 else if( alt_ft < 4500 )
340 buildLayer(iLayer, layer_type, alt_m, coverage_norm);
344 void FGClouds::set_3dClouds(bool enable)
346 if (enable != clouds_3d_enabled) {
347 clouds_3d_enabled = enable;
352 bool FGClouds::get_3dClouds() const {
353 return clouds_3d_enabled;