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 "fgclouds.hxx"
31 #include <Main/fg_props.hxx>
33 #include <simgear/constants.h>
34 #include <simgear/sound/soundmgr_openal.hxx>
35 #include <simgear/scene/sky/sky.hxx>
36 //#include <simgear/environment/visual_enviro.hxx>
37 #include <simgear/scene/sky/cloudfield.hxx>
38 #include <simgear/scene/sky/newcloud.hxx>
39 #include <simgear/structure/commands.hxx>
40 #include <simgear/props/props_io.hxx>
42 #include <Main/globals.hxx>
43 #include <Main/util.hxx>
44 #include <Viewer/renderer.hxx>
45 #include <Airports/airport.hxx>
47 // RNG seed to ensure cloud synchronization across multi-process
51 FGClouds::FGClouds() :
52 clouds_3d_enabled(false),
60 globals->get_commands()->removeCommand("add-cloud");
61 globals->get_commands()->removeCommand("del-cloud");
62 globals->get_commands()->removeCommand("move-cloud");
66 int FGClouds::get_update_event(void) const {
70 void FGClouds::set_update_event(int count) {
75 void FGClouds::Init(void)
77 mt_init_time_10(&seed);
79 globals->get_commands()->addCommand("add-cloud", this, &FGClouds::add3DCloud);
80 globals->get_commands()->addCommand("del-cloud", this, &FGClouds::delete3DCloud);
81 globals->get_commands()->addCommand("move-cloud", this, &FGClouds::move3DCloud);
84 // Build an invidual cloud. Returns the extents of the cloud for coverage calculations
85 double FGClouds::buildCloud(SGPropertyNode *cloud_def_root, SGPropertyNode *box_def_root,
86 const std::string& name, double grid_z_rand, SGCloudField *layer)
88 SGPropertyNode *box_def=NULL;
89 SGPropertyNode *cld_def=NULL;
92 SGPath texture_root = globals->get_fg_root();
93 texture_root.append("Textures");
94 texture_root.append("Sky");
96 box_def = box_def_root->getChild(name.c_str());
98 string base_name = name.substr(0,2);
100 if( name[2] == '-' ) {
101 box_def = box_def_root->getChild(base_name.c_str());
107 double x = mt_rand(&seed) * SGCloudField::fieldSize - (SGCloudField::fieldSize / 2.0);
108 double y = mt_rand(&seed) * SGCloudField::fieldSize - (SGCloudField::fieldSize / 2.0);
109 double z = grid_z_rand * (mt_rand(&seed) - 0.5);
111 float lon = fgGetNode("/position/longitude-deg", false)->getFloatValue();
112 float lat = fgGetNode("/position/latitude-deg", false)->getFloatValue();
116 for(int i = 0; i < box_def->nChildren() ; i++) {
117 SGPropertyNode *abox = box_def->getChild(i);
118 if( strcmp(abox->getName(), "box") == 0) {
120 string type = abox->getStringValue("type", "cu-small");
121 cld_def = cloud_def_root->getChild(type.c_str());
122 if ( !cld_def ) return 0.0;
124 double w = abox->getDoubleValue("width", 1000.0);
125 double h = abox->getDoubleValue("height", 1000.0);
126 int hdist = abox->getIntValue("hdist", 1);
127 int vdist = abox->getIntValue("vdist", 1);
129 double c = abox->getDoubleValue("count", 5);
130 int count = (int) (c + (mt_rand(&seed) - 0.5) * c);
132 extent = std::max(w*w, extent);
134 for (int j = 0; j < count; j++) {
136 // Locate the clouds randomly in the defined space. The hdist and
137 // vdist values control the horizontal and vertical distribution
138 // by simply summing random components.
143 for (int k = 0; k < hdist; k++)
145 x += (mt_rand(&seed) / hdist);
146 y += (mt_rand(&seed) / hdist);
149 for (int k = 0; k < vdist; k++)
151 z += (mt_rand(&seed) / vdist);
154 x = w * (x - 0.5) + pos[0]; // N/S
155 y = w * (y - 0.5) + pos[1]; // E/W
156 z = h * z + pos[2]; // Up/Down. pos[2] is the cloudbase
158 //SGVec3f newpos = SGVec3f(x, y, z);
159 SGNewCloud cld(texture_root, cld_def, &seed);
161 //layer->addCloud(newpos, cld.genCloud());
162 layer->addCloud(lon, lat, z, x, y, index++, cld.genCloud());
167 // Return the maximum extent of the cloud
171 void FGClouds::buildLayer(int iLayer, const string& name, double coverage) {
176 int CloudVarietyCount = 0;
177 double totalCount = 0.0;
179 SGSky* thesky = globals->get_renderer()->getSky();
181 SGPropertyNode *cloud_def_root = fgGetNode("/environment/cloudlayers/clouds", false);
182 SGPropertyNode *box_def_root = fgGetNode("/environment/cloudlayers/boxes", false);
183 SGPropertyNode *layer_def_root = fgGetNode("/environment/cloudlayers/layers", false);
184 SGCloudField *layer = thesky->get_cloud_layer(iLayer)->get_layer3D();
187 // If we don't have the required properties, then render the cloud in 2D
188 if ((! clouds_3d_enabled) || coverage == 0.0 ||
189 layer_def_root == NULL || cloud_def_root == NULL || box_def_root == NULL) {
190 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
194 // If we can't find a definition for this cloud type, then render the cloud in 2D
195 SGPropertyNode *layer_def=NULL;
196 layer_def = layer_def_root->getChild(name.c_str());
198 if( name[2] == '-' ) {
199 string base_name = name.substr(0,2);
200 layer_def = layer_def_root->getChild(base_name.c_str());
203 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
208 // At this point, we know we've got some 3D clouds to generate.
209 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(true);
211 double grid_z_rand = layer_def->getDoubleValue("grid-z-rand");
213 for(int i = 0; i < layer_def->nChildren() ; i++) {
214 SGPropertyNode *acloud = layer_def->getChild(i);
215 if( strcmp(acloud->getName(), "cloud") == 0) {
216 string cloud_name = acloud->getStringValue("name");
217 tCloudVariety[CloudVarietyCount].name = cloud_name;
218 double count = acloud->getDoubleValue("count", 1.0);
219 tCloudVariety[CloudVarietyCount].count = count;
221 char variety_name[50];
224 snprintf(variety_name, sizeof(variety_name) - 1, "%s-%d", cloud_name.c_str(), variety);
225 } while( box_def_root->getChild(variety_name, 0, false) );
228 if( CloudVarietyCount < 20 )
232 totalCount = 1.0 / totalCount;
234 // Determine how much cloud coverage we need in m^2.
235 double cov = coverage * SGCloudField::fieldSize * SGCloudField::fieldSize;
238 double choice = mt_rand(&seed);
240 for(int i = 0; i < CloudVarietyCount ; i ++) {
241 choice -= tCloudVariety[i].count * totalCount;
242 if( choice <= 0.0 ) {
243 cov -= buildCloud(cloud_def_root,
245 tCloudVariety[i].name,
253 // Now we've built any clouds, enable them and set the density (coverage)
254 //layer->setCoverage(coverage);
255 //layer->applyCoverage();
256 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(clouds_3d_enabled);
259 void FGClouds::buildCloudLayers(void) {
260 SGPropertyNode *metar_root = fgGetNode("/environment", true);
262 //double wind_speed_kt = metar_root->getDoubleValue("wind-speed-kt");
263 double temperature_degc = metar_root->getDoubleValue("temperature-sea-level-degc");
264 double dewpoint_degc = metar_root->getDoubleValue("dewpoint-sea-level-degc");
265 double pressure_mb = metar_root->getDoubleValue("pressure-sea-level-inhg") * SG_INHG_TO_PA / 100.0;
266 double rel_humidity = metar_root->getDoubleValue("relative-humidity");
268 // formule d'Epsy, base d'un cumulus
269 double cumulus_base = 122.0 * (temperature_degc - dewpoint_degc);
270 double stratus_base = 100.0 * (100.0 - rel_humidity) * SG_FEET_TO_METER;
272 SGSky* thesky = globals->get_renderer()->getSky();
273 for(int iLayer = 0 ; iLayer < thesky->get_cloud_layer_count(); iLayer++) {
274 SGPropertyNode *cloud_root = fgGetNode("/environment/clouds/layer", iLayer, true);
276 double alt_ft = cloud_root->getDoubleValue("elevation-ft");
277 double alt_m = alt_ft * SG_FEET_TO_METER;
278 string coverage = cloud_root->getStringValue("coverage");
280 double coverage_norm = 0.0;
281 if( coverage == "few" )
282 coverage_norm = 2.0/8.0; // <1-2
283 else if( coverage == "scattered" )
284 coverage_norm = 4.0/8.0; // 3-4
285 else if( coverage == "broken" )
286 coverage_norm = 6.0/8.0; // 5-7
287 else if( coverage == "overcast" )
288 coverage_norm = 8.0/8.0; // 8
290 string layer_type = "nn";
292 if( coverage == "cirrus" ) {
294 } else if( alt_ft > 16500 ) {
295 // layer_type = "ci|cs|cc";
297 } else if( alt_ft > 6500 ) {
298 // layer_type = "as|ac|ns";
300 if( pressure_mb < 1005.0 && coverage_norm >= 0.5 )
303 // layer_type = "st|cu|cb|sc";
304 if( cumulus_base * 0.80 < alt_m && cumulus_base * 1.20 > alt_m ) {
305 // +/- 20% from cumulus probable base
307 } else if( stratus_base * 0.80 < alt_m && stratus_base * 1.40 > alt_m ) {
308 // +/- 20% from stratus probable base
311 // above formulae is far from perfect
314 else if( alt_ft < 4500 )
321 cloud_root->setStringValue("layer-type",layer_type);
322 buildLayer(iLayer, layer_type, coverage_norm);
326 void FGClouds::set_3dClouds(bool enable)
328 if (enable != clouds_3d_enabled) {
329 clouds_3d_enabled = enable;
334 bool FGClouds::get_3dClouds() const
336 return clouds_3d_enabled;
340 * Adds a 3D cloud to a cloud layer.
343 * layer - the layer index to add this cloud to. (Defaults to 0)
344 * index - the index for this cloud (to be used later)
345 * lon/lat/alt - the position for the cloud
346 * (Various) - cloud definition properties. See README.3DClouds
349 bool FGClouds::add3DCloud(const SGPropertyNode *arg)
351 int l = arg->getIntValue("layer", 0);
352 int index = arg->getIntValue("index", 0);
354 SGPath texture_root = globals->get_fg_root();
355 texture_root.append("Textures");
356 texture_root.append("Sky");
358 float lon = arg->getFloatValue("lon-deg", 0.0f);
359 float lat = arg->getFloatValue("lat-deg", 0.0f);
360 float alt = arg->getFloatValue("alt-ft", 0.0f);
361 float x = arg->getFloatValue("x-offset-m", 0.0f);
362 float y = arg->getFloatValue("y-offset-m", 0.0f);
364 SGSky* thesky = globals->get_renderer()->getSky();
365 SGCloudField *layer = thesky->get_cloud_layer(l)->get_layer3D();
366 SGNewCloud cld(texture_root, arg, &seed);
367 bool success = layer->addCloud(lon, lat, alt, x, y, index, cld.genCloud());
369 // Adding a 3D cloud immediately makes this layer 3D.
370 thesky->get_cloud_layer(l)->set_enable3dClouds(true);
376 * Removes a 3D cloud from a cloud layer
380 * layer - the layer index to remove this cloud from. (defaults to 0)
381 * index - the cloud index
384 bool FGClouds::delete3DCloud(const SGPropertyNode *arg)
386 int l = arg->getIntValue("layer", 0);
387 int i = arg->getIntValue("index", 0);
389 SGSky* thesky = globals->get_renderer()->getSky();
390 SGCloudField *layer = thesky->get_cloud_layer(l)->get_layer3D();
391 return layer->deleteCloud(i);
395 * Move a cloud within a 3D layer
398 * layer - the layer index to add this cloud to. (Defaults to 0)
399 * index - the cloud index to move.
400 * lon/lat/alt - the position for the cloud
403 bool FGClouds::move3DCloud(const SGPropertyNode *arg)
405 int l = arg->getIntValue("layer", 0);
406 int i = arg->getIntValue("index", 0);
407 SGSky* thesky = globals->get_renderer()->getSky();
409 float lon = arg->getFloatValue("lon-deg", 0.0f);
410 float lat = arg->getFloatValue("lat-deg", 0.0f);
411 float alt = arg->getFloatValue("alt-ft", 0.0f);
412 float x = arg->getFloatValue("x-offset-m", 0.0f);
413 float y = arg->getFloatValue("y-offset-m", 0.0f);
415 SGCloudField *layer = thesky->get_cloud_layer(l)->get_layer3D();
416 return layer->repositionCloud(i, lon, lat, alt, x, y);