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 *soundMgr = globals->get_soundmgr();
72 soundMgr->add( snd_lightning, "thunder" );
73 sgEnviro.set_soundMgr( soundMgr );
77 void FGClouds::buildCloud(SGPropertyNode *cloud_def_root, SGPropertyNode *box_def_root, const string& name, sgVec3 pos, SGCloudField *layer) {
78 SGPropertyNode *box_def=NULL;
79 SGPropertyNode *cld_def=NULL;
81 SGPath texture_root = globals->get_fg_root();
82 texture_root.append("Textures");
83 texture_root.append("Sky");
85 box_def = box_def_root->getChild(name.c_str());
87 string base_name = name.substr(0,2);
89 if( name[2] == '-' ) {
90 box_def = box_def_root->getChild(base_name.c_str());
96 for(int i = 0; i < box_def->nChildren() ; i++) {
97 SGPropertyNode *abox = box_def->getChild(i);
98 if( strcmp(abox->getName(), "box") == 0) {
99 double x = abox->getDoubleValue("x") + pos[0];
100 double y = abox->getDoubleValue("y") + pos[1];
101 double z = abox->getDoubleValue("z") + pos[2];
102 SGVec3f newpos = SGVec3f(x, z, y);
104 string type = abox->getStringValue("type", "cu-small");
106 cld_def = cloud_def_root->getChild(type.c_str());
107 if ( !cld_def ) return;
109 double min_width = cld_def->getDoubleValue("min-cloud-width-m", 500.0);
110 double max_width = cld_def->getDoubleValue("max-cloud-width-m", 1000.0);
111 double min_height = cld_def->getDoubleValue("min-cloud-height-m", min_width);
112 double max_height = cld_def->getDoubleValue("max-cloud-height-m", max_width);
113 double min_sprite_width = cld_def->getDoubleValue("min-sprite-width-m", 200.0);
114 double max_sprite_width = cld_def->getDoubleValue("max-sprite-width-m", min_sprite_width);
115 double min_sprite_height = cld_def->getDoubleValue("min-sprite-height-m", min_sprite_width);
116 double max_sprite_height = cld_def->getDoubleValue("max-sprite-height-m", max_sprite_width);
117 int num_sprites = cld_def->getIntValue("num-sprites", 20);
118 int num_textures_x = cld_def->getIntValue("num-textures-x", 1);
119 int num_textures_y = cld_def->getIntValue("num-textures-y", 1);
120 double bottom_shade = cld_def->getDoubleValue("bottom-shade", 1.0);
121 string texture = cld_def->getStringValue("texture", "cl_cumulus.rgb");
139 layer->addCloud(newpos, cld);
144 void FGClouds::buildLayer(int iLayer, const string& name, double alt, double coverage) {
149 int CloudVarietyCount = 0;
150 double totalCount = 0.0;
152 SGPropertyNode *cloud_def_root = fgGetNode("/environment/cloudlayers/clouds", false);
153 SGPropertyNode *box_def_root = fgGetNode("/environment/cloudlayers/boxes", false);
154 SGPropertyNode *layer_def_root = fgGetNode("/environment/cloudlayers/layers", false);
155 SGCloudField *layer = thesky->get_cloud_layer(iLayer)->get_layer3D();
158 // If we don't have the required properties, then render the cloud in 2D
159 if ((! clouds_3d_enabled) || coverage == 0.0 ||
160 layer_def_root == NULL || cloud_def_root == NULL || box_def_root == NULL) {
161 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
165 // If we can't find a definition for this cloud type, then render the cloud in 2D
166 SGPropertyNode *layer_def=NULL;
167 layer_def = layer_def_root->getChild(name.c_str());
169 if( name[2] == '-' ) {
170 string base_name = name.substr(0,2);
171 layer_def = layer_def_root->getChild(base_name.c_str());
174 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
179 // At this point, we know we've got some 3D clouds to generate.
180 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(true);
182 double grid_x_size = layer_def->getDoubleValue("grid-x-size", 1000.0);
183 double grid_y_size = layer_def->getDoubleValue("grid-y-size", 1000.0);
184 double grid_x_rand = layer_def->getDoubleValue("grid-x-rand", grid_x_size);
185 double grid_y_rand = layer_def->getDoubleValue("grid-y-rand", grid_y_size);
186 double grid_z_rand = layer_def->getDoubleValue("grid-z-rand");
188 for(int i = 0; i < layer_def->nChildren() ; i++) {
189 SGPropertyNode *acloud = layer_def->getChild(i);
190 if( strcmp(acloud->getName(), "cloud") == 0) {
191 string cloud_name = acloud->getStringValue("name");
192 tCloudVariety[CloudVarietyCount].name = cloud_name;
193 double count = acloud->getDoubleValue("count", 1.0);
194 tCloudVariety[CloudVarietyCount].count = count;
196 cloud_name = cloud_name + "-%d";
197 char variety_name[50];
200 snprintf(variety_name, sizeof(variety_name) - 1, cloud_name.c_str(), variety);
201 } while( box_def_root->getChild(variety_name, 0, false) );
204 if( CloudVarietyCount < 20 )
208 totalCount = 1.0 / totalCount;
210 for(double px = 0.0; px < SGCloudField::fieldSize; px += grid_x_size) {
211 for(double py = 0.0; py < SGCloudField::fieldSize; py += grid_y_size) {
212 double x = px + grid_x_rand * (sg_random() - 0.5) - (SGCloudField::fieldSize / 2.0);
213 double y = py + grid_y_rand * (sg_random() - 0.5) - (SGCloudField::fieldSize / 2.0);
214 double z = grid_z_rand * (sg_random() - 0.5);
216 if (sg_random() < coverage) {
217 double choice = sg_random();
219 for(int i = 0; i < CloudVarietyCount ; i ++) {
220 choice -= tCloudVariety[i].count * totalCount;
221 if( choice <= 0.0 ) {
224 buildCloud(cloud_def_root,
226 tCloudVariety[i].name,
236 // Now we've built any clouds, enable them and set the density (coverage)
237 //layer->setCoverage(coverage);
238 //layer->applyCoverage();
239 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(clouds_3d_enabled);
242 void FGClouds::buildCloudLayers(void) {
243 SGPropertyNode *metar_root = fgGetNode("/environment", true);
245 //double wind_speed_kt = metar_root->getDoubleValue("wind-speed-kt");
246 double temperature_degc = metar_root->getDoubleValue("temperature-sea-level-degc");
247 double dewpoint_degc = metar_root->getDoubleValue("dewpoint-sea-level-degc");
248 double pressure_mb = metar_root->getDoubleValue("pressure-sea-level-inhg") * SG_INHG_TO_PA / 100.0;
250 double dewp = pow(10.0, 7.5 * dewpoint_degc / (237.7 + dewpoint_degc));
251 double temp = pow(10.0, 7.5 * temperature_degc / (237.7 + temperature_degc));
252 double rel_humidity = dewp * 100 / temp;
254 // formule d'Epsy, base d'un cumulus
255 double cumulus_base = 122.0 * (temperature_degc - dewpoint_degc);
256 double stratus_base = 100.0 * (100.0 - rel_humidity) * SG_FEET_TO_METER;
258 for(int iLayer = 0 ; iLayer < thesky->get_cloud_layer_count(); iLayer++) {
259 SGPropertyNode *cloud_root = fgGetNode("/environment/clouds/layer", iLayer, true);
261 double alt_ft = cloud_root->getDoubleValue("elevation-ft");
262 double alt_m = alt_ft * SG_FEET_TO_METER;
263 string coverage = cloud_root->getStringValue("coverage");
265 double coverage_norm = 0.0;
266 if( coverage == "few" )
267 coverage_norm = 2.0/8.0; // <1-2
268 else if( coverage == "scattered" )
269 coverage_norm = 4.0/8.0; // 3-4
270 else if( coverage == "broken" )
271 coverage_norm = 6.0/8.0; // 5-7
272 else if( coverage == "overcast" )
273 coverage_norm = 8.0/8.0; // 8
275 string layer_type = "nn";
276 if( coverage == "cirrus" ) {
278 } else if( alt_ft > 16500 ) {
279 // layer_type = "ci|cs|cc";
281 } else if( alt_ft > 6500 ) {
282 // layer_type = "as|ac|ns";
284 if( pressure_mb < 1005.0 && coverage_norm >= 0.5 )
287 // layer_type = "st|cu|cb|sc";
288 if( cumulus_base * 0.80 < alt_m && cumulus_base * 1.20 > alt_m ) {
289 // +/- 20% from cumulus probable base
291 } else if( stratus_base * 0.80 < alt_m && stratus_base * 1.40 > alt_m ) {
292 // +/- 20% from stratus probable base
295 // above formulae is far from perfect
298 else if( alt_ft < 4500 )
305 buildLayer(iLayer, layer_type, alt_m, coverage_norm);
309 void FGClouds::set_3dClouds(bool enable)
311 if (enable != clouds_3d_enabled) {
312 clouds_3d_enabled = enable;
317 bool FGClouds::get_3dClouds() const {
318 return clouds_3d_enabled;