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/math/sg_random.h>
37 #include <simgear/props/props_io.hxx>
39 #include <Main/globals.hxx>
40 #include <Airports/simple.hxx>
41 #include <Main/util.hxx>
43 #include "fgclouds.hxx"
48 FGClouds::FGClouds() :
50 clouds_3d_enabled(false)
59 int FGClouds::get_update_event(void) const {
63 void FGClouds::set_update_event(int count) {
68 void FGClouds::init(void) {
69 if( snd_lightning == NULL ) {
70 snd_lightning = new SGSoundSample("Sounds/thunder.wav", SGPath());
71 snd_lightning->set_max_dist(7000.0f);
72 snd_lightning->set_reference_dist(3000.0f);
73 SGSoundMgr *smgr = globals->get_soundmgr();
74 SGSampleGroup *sgr = smgr->find("weather", true);
75 sgr->add( snd_lightning, "thunder" );
76 sgEnviro.set_sampleGroup( sgr );
80 // Build an invidual cloud. Returns the extents of the cloud for coverage calculations
81 double FGClouds::buildCloud(SGPropertyNode *cloud_def_root, SGPropertyNode *box_def_root, const string& name, double grid_z_rand, SGCloudField *layer) {
82 SGPropertyNode *box_def=NULL;
83 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 double x = sg_random() * SGCloudField::fieldSize - (SGCloudField::fieldSize / 2.0);
102 double y = sg_random() * SGCloudField::fieldSize - (SGCloudField::fieldSize / 2.0);
103 double z = grid_z_rand * (sg_random() - 0.5);
107 for(int i = 0; i < box_def->nChildren() ; i++) {
108 SGPropertyNode *abox = box_def->getChild(i);
109 if( strcmp(abox->getName(), "box") == 0) {
111 string type = abox->getStringValue("type", "cu-small");
112 cld_def = cloud_def_root->getChild(type.c_str());
113 if ( !cld_def ) return 0.0;
115 double w = abox->getDoubleValue("width", 1000.0);
116 double h = abox->getDoubleValue("height", 1000.0);
117 int hdist = abox->getIntValue("hdist", 1);
118 int vdist = abox->getIntValue("vdist", 1);
120 double c = abox->getDoubleValue("count", 5);
121 int count = (int) (c + (sg_random() - 0.5) * c);
123 extent = max(w*w, extent);
125 for (int j = 0; j < count; j++) {
127 // Locate the clouds randomly in the defined space. The hdist and
128 // vdist values control the horizontal and vertical distribution
129 // by simply summing random components.
134 for (int k = 0; k < hdist; k++)
136 x += (sg_random() / hdist);
137 y += (sg_random() / hdist);
140 for (int k = 0; k < vdist; k++)
142 z += (sg_random() / vdist);
145 x = w * (x - 0.5) + pos[0]; // N/S
146 y = w * (y - 0.5) + pos[1]; // E/W
147 z = h * z + pos[2]; // Up/Down. pos[2] is the cloudbase
149 SGVec3f newpos = SGVec3f(x, y, z);
151 double min_width = cld_def->getDoubleValue("min-cloud-width-m", 500.0);
152 double max_width = cld_def->getDoubleValue("max-cloud-width-m", 1000.0);
153 double min_height = cld_def->getDoubleValue("min-cloud-height-m", min_width);
154 double max_height = cld_def->getDoubleValue("max-cloud-height-m", max_width);
155 double min_sprite_width = cld_def->getDoubleValue("min-sprite-width-m", 200.0);
156 double max_sprite_width = cld_def->getDoubleValue("max-sprite-width-m", min_sprite_width);
157 double min_sprite_height = cld_def->getDoubleValue("min-sprite-height-m", min_sprite_width);
158 double max_sprite_height = cld_def->getDoubleValue("max-sprite-height-m", max_sprite_width);
159 int num_sprites = cld_def->getIntValue("num-sprites", 20);
160 int num_textures_x = cld_def->getIntValue("num-textures-x", 1);
161 int num_textures_y = cld_def->getIntValue("num-textures-y", 1);
162 double bottom_shade = cld_def->getDoubleValue("bottom-shade", 1.0);
163 string texture = cld_def->getStringValue("texture", "cu.png");
181 layer->addCloud(newpos, cld);
186 // Return the maximum extent of the cloud
190 void FGClouds::buildLayer(int iLayer, const string& name, double coverage) {
195 int CloudVarietyCount = 0;
196 double totalCount = 0.0;
198 SGPropertyNode *cloud_def_root = fgGetNode("/environment/cloudlayers/clouds", false);
199 SGPropertyNode *box_def_root = fgGetNode("/environment/cloudlayers/boxes", false);
200 SGPropertyNode *layer_def_root = fgGetNode("/environment/cloudlayers/layers", false);
201 SGCloudField *layer = thesky->get_cloud_layer(iLayer)->get_layer3D();
204 // If we don't have the required properties, then render the cloud in 2D
205 if ((! clouds_3d_enabled) || coverage == 0.0 ||
206 layer_def_root == NULL || cloud_def_root == NULL || box_def_root == NULL) {
207 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
211 // If we can't find a definition for this cloud type, then render the cloud in 2D
212 SGPropertyNode *layer_def=NULL;
213 layer_def = layer_def_root->getChild(name.c_str());
215 if( name[2] == '-' ) {
216 string base_name = name.substr(0,2);
217 layer_def = layer_def_root->getChild(base_name.c_str());
220 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
225 // At this point, we know we've got some 3D clouds to generate.
226 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(true);
228 double grid_z_rand = layer_def->getDoubleValue("grid-z-rand");
230 for(int i = 0; i < layer_def->nChildren() ; i++) {
231 SGPropertyNode *acloud = layer_def->getChild(i);
232 if( strcmp(acloud->getName(), "cloud") == 0) {
233 string cloud_name = acloud->getStringValue("name");
234 tCloudVariety[CloudVarietyCount].name = cloud_name;
235 double count = acloud->getDoubleValue("count", 1.0);
236 tCloudVariety[CloudVarietyCount].count = count;
238 cloud_name = cloud_name + "-%d";
239 char variety_name[50];
242 snprintf(variety_name, sizeof(variety_name) - 1, cloud_name.c_str(), variety);
243 } while( box_def_root->getChild(variety_name, 0, false) );
246 if( CloudVarietyCount < 20 )
250 totalCount = 1.0 / totalCount;
252 // Determine how much cloud coverage we need in m^2.
253 double cov = coverage * SGCloudField::fieldSize * SGCloudField::fieldSize;
256 double choice = sg_random();
258 for(int i = 0; i < CloudVarietyCount ; i ++) {
259 choice -= tCloudVariety[i].count * totalCount;
260 if( choice <= 0.0 ) {
261 cov -= buildCloud(cloud_def_root,
263 tCloudVariety[i].name,
272 // Now we've built any clouds, enable them and set the density (coverage)
273 //layer->setCoverage(coverage);
274 //layer->applyCoverage();
275 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(clouds_3d_enabled);
278 void FGClouds::buildCloudLayers(void) {
279 SGPropertyNode *metar_root = fgGetNode("/environment", true);
281 //double wind_speed_kt = metar_root->getDoubleValue("wind-speed-kt");
282 double temperature_degc = metar_root->getDoubleValue("temperature-sea-level-degc");
283 double dewpoint_degc = metar_root->getDoubleValue("dewpoint-sea-level-degc");
284 double pressure_mb = metar_root->getDoubleValue("pressure-sea-level-inhg") * SG_INHG_TO_PA / 100.0;
285 double rel_humidity = metar_root->getDoubleValue("relative-humidity");
287 // formule d'Epsy, base d'un cumulus
288 double cumulus_base = 122.0 * (temperature_degc - dewpoint_degc);
289 double stratus_base = 100.0 * (100.0 - rel_humidity) * SG_FEET_TO_METER;
291 for(int iLayer = 0 ; iLayer < thesky->get_cloud_layer_count(); iLayer++) {
292 SGPropertyNode *cloud_root = fgGetNode("/environment/clouds/layer", iLayer, true);
294 double alt_ft = cloud_root->getDoubleValue("elevation-ft");
295 double alt_m = alt_ft * SG_FEET_TO_METER;
296 string coverage = cloud_root->getStringValue("coverage");
298 double coverage_norm = 0.0;
299 if( coverage == "few" )
300 coverage_norm = 2.0/8.0; // <1-2
301 else if( coverage == "scattered" )
302 coverage_norm = 4.0/8.0; // 3-4
303 else if( coverage == "broken" )
304 coverage_norm = 6.0/8.0; // 5-7
305 else if( coverage == "overcast" )
306 coverage_norm = 8.0/8.0; // 8
308 string layer_type = "nn";
310 if( coverage == "cirrus" ) {
312 } else if( alt_ft > 16500 ) {
313 // layer_type = "ci|cs|cc";
315 } else if( alt_ft > 6500 ) {
316 // layer_type = "as|ac|ns";
318 if( pressure_mb < 1005.0 && coverage_norm >= 0.5 )
321 // layer_type = "st|cu|cb|sc";
322 if( cumulus_base * 0.80 < alt_m && cumulus_base * 1.20 > alt_m ) {
323 // +/- 20% from cumulus probable base
325 } else if( stratus_base * 0.80 < alt_m && stratus_base * 1.40 > alt_m ) {
326 // +/- 20% from stratus probable base
329 // above formulae is far from perfect
332 else if( alt_ft < 4500 )
339 cloud_root->setStringValue("layer-type",layer_type);
340 buildLayer(iLayer, layer_type, 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
354 return clouds_3d_enabled;