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),
62 int FGClouds::get_update_event(void) const {
66 void FGClouds::set_update_event(int count) {
71 void FGClouds::Init(void)
73 mt_init_time_10(&seed);
75 globals->get_commands()->addCommand("add-cloud", this, &FGClouds::add3DCloud);
76 globals->get_commands()->addCommand("del-cloud", this, &FGClouds::delete3DCloud);
77 globals->get_commands()->addCommand("move-cloud", this, &FGClouds::move3DCloud);
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,
82 const std::string& name, double grid_z_rand, SGCloudField *layer)
84 SGPropertyNode *box_def=NULL;
85 SGPropertyNode *cld_def=NULL;
88 SGPath texture_root = globals->get_fg_root();
89 texture_root.append("Textures");
90 texture_root.append("Sky");
92 box_def = box_def_root->getChild(name.c_str());
94 string base_name = name.substr(0,2);
96 if( name[2] == '-' ) {
97 box_def = box_def_root->getChild(base_name.c_str());
103 double x = mt_rand(&seed) * SGCloudField::fieldSize - (SGCloudField::fieldSize / 2.0);
104 double y = mt_rand(&seed) * SGCloudField::fieldSize - (SGCloudField::fieldSize / 2.0);
105 double z = grid_z_rand * (mt_rand(&seed) - 0.5);
107 float lon = fgGetNode("/position/longitude-deg", false)->getFloatValue();
108 float lat = fgGetNode("/position/latitude-deg", false)->getFloatValue();
112 for(int i = 0; i < box_def->nChildren() ; i++) {
113 SGPropertyNode *abox = box_def->getChild(i);
114 if( strcmp(abox->getName(), "box") == 0) {
116 string type = abox->getStringValue("type", "cu-small");
117 cld_def = cloud_def_root->getChild(type.c_str());
118 if ( !cld_def ) return 0.0;
120 double w = abox->getDoubleValue("width", 1000.0);
121 double h = abox->getDoubleValue("height", 1000.0);
122 int hdist = abox->getIntValue("hdist", 1);
123 int vdist = abox->getIntValue("vdist", 1);
125 double c = abox->getDoubleValue("count", 5);
126 int count = (int) (c + (mt_rand(&seed) - 0.5) * c);
128 extent = std::max(w*w, extent);
130 for (int j = 0; j < count; j++) {
132 // Locate the clouds randomly in the defined space. The hdist and
133 // vdist values control the horizontal and vertical distribution
134 // by simply summing random components.
139 for (int k = 0; k < hdist; k++)
141 x += (mt_rand(&seed) / hdist);
142 y += (mt_rand(&seed) / hdist);
145 for (int k = 0; k < vdist; k++)
147 z += (mt_rand(&seed) / vdist);
150 x = w * (x - 0.5) + pos[0]; // N/S
151 y = w * (y - 0.5) + pos[1]; // E/W
152 z = h * z + pos[2]; // Up/Down. pos[2] is the cloudbase
154 //SGVec3f newpos = SGVec3f(x, y, z);
155 SGNewCloud cld(texture_root, cld_def, &seed);
157 //layer->addCloud(newpos, cld.genCloud());
158 layer->addCloud(lon, lat, z, x, y, index++, cld.genCloud());
163 // Return the maximum extent of the cloud
167 void FGClouds::buildLayer(int iLayer, const string& name, double coverage) {
172 int CloudVarietyCount = 0;
173 double totalCount = 0.0;
175 SGSky* thesky = globals->get_renderer()->getSky();
177 SGPropertyNode *cloud_def_root = fgGetNode("/environment/cloudlayers/clouds", false);
178 SGPropertyNode *box_def_root = fgGetNode("/environment/cloudlayers/boxes", false);
179 SGPropertyNode *layer_def_root = fgGetNode("/environment/cloudlayers/layers", false);
180 SGCloudField *layer = thesky->get_cloud_layer(iLayer)->get_layer3D();
183 // If we don't have the required properties, then render the cloud in 2D
184 if ((! clouds_3d_enabled) || coverage == 0.0 ||
185 layer_def_root == NULL || cloud_def_root == NULL || box_def_root == NULL) {
186 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
190 // If we can't find a definition for this cloud type, then render the cloud in 2D
191 SGPropertyNode *layer_def=NULL;
192 layer_def = layer_def_root->getChild(name.c_str());
194 if( name[2] == '-' ) {
195 string base_name = name.substr(0,2);
196 layer_def = layer_def_root->getChild(base_name.c_str());
199 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
204 // At this point, we know we've got some 3D clouds to generate.
205 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(true);
207 double grid_z_rand = layer_def->getDoubleValue("grid-z-rand");
209 for(int i = 0; i < layer_def->nChildren() ; i++) {
210 SGPropertyNode *acloud = layer_def->getChild(i);
211 if( strcmp(acloud->getName(), "cloud") == 0) {
212 string cloud_name = acloud->getStringValue("name");
213 tCloudVariety[CloudVarietyCount].name = cloud_name;
214 double count = acloud->getDoubleValue("count", 1.0);
215 tCloudVariety[CloudVarietyCount].count = count;
217 cloud_name = cloud_name + "-%d";
218 char variety_name[50];
221 snprintf(variety_name, sizeof(variety_name) - 1, cloud_name.c_str(), variety);
222 } while( box_def_root->getChild(variety_name, 0, false) );
225 if( CloudVarietyCount < 20 )
229 totalCount = 1.0 / totalCount;
231 // Determine how much cloud coverage we need in m^2.
232 double cov = coverage * SGCloudField::fieldSize * SGCloudField::fieldSize;
235 double choice = mt_rand(&seed);
237 for(int i = 0; i < CloudVarietyCount ; i ++) {
238 choice -= tCloudVariety[i].count * totalCount;
239 if( choice <= 0.0 ) {
240 cov -= buildCloud(cloud_def_root,
242 tCloudVariety[i].name,
250 // Now we've built any clouds, enable them and set the density (coverage)
251 //layer->setCoverage(coverage);
252 //layer->applyCoverage();
253 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(clouds_3d_enabled);
256 void FGClouds::buildCloudLayers(void) {
257 SGPropertyNode *metar_root = fgGetNode("/environment", true);
259 //double wind_speed_kt = metar_root->getDoubleValue("wind-speed-kt");
260 double temperature_degc = metar_root->getDoubleValue("temperature-sea-level-degc");
261 double dewpoint_degc = metar_root->getDoubleValue("dewpoint-sea-level-degc");
262 double pressure_mb = metar_root->getDoubleValue("pressure-sea-level-inhg") * SG_INHG_TO_PA / 100.0;
263 double rel_humidity = metar_root->getDoubleValue("relative-humidity");
265 // formule d'Epsy, base d'un cumulus
266 double cumulus_base = 122.0 * (temperature_degc - dewpoint_degc);
267 double stratus_base = 100.0 * (100.0 - rel_humidity) * SG_FEET_TO_METER;
269 SGSky* thesky = globals->get_renderer()->getSky();
270 for(int iLayer = 0 ; iLayer < thesky->get_cloud_layer_count(); iLayer++) {
271 SGPropertyNode *cloud_root = fgGetNode("/environment/clouds/layer", iLayer, true);
273 double alt_ft = cloud_root->getDoubleValue("elevation-ft");
274 double alt_m = alt_ft * SG_FEET_TO_METER;
275 string coverage = cloud_root->getStringValue("coverage");
277 double coverage_norm = 0.0;
278 if( coverage == "few" )
279 coverage_norm = 2.0/8.0; // <1-2
280 else if( coverage == "scattered" )
281 coverage_norm = 4.0/8.0; // 3-4
282 else if( coverage == "broken" )
283 coverage_norm = 6.0/8.0; // 5-7
284 else if( coverage == "overcast" )
285 coverage_norm = 8.0/8.0; // 8
287 string layer_type = "nn";
289 if( coverage == "cirrus" ) {
291 } else if( alt_ft > 16500 ) {
292 // layer_type = "ci|cs|cc";
294 } else if( alt_ft > 6500 ) {
295 // layer_type = "as|ac|ns";
297 if( pressure_mb < 1005.0 && coverage_norm >= 0.5 )
300 // layer_type = "st|cu|cb|sc";
301 if( cumulus_base * 0.80 < alt_m && cumulus_base * 1.20 > alt_m ) {
302 // +/- 20% from cumulus probable base
304 } else if( stratus_base * 0.80 < alt_m && stratus_base * 1.40 > alt_m ) {
305 // +/- 20% from stratus probable base
308 // above formulae is far from perfect
311 else if( alt_ft < 4500 )
318 cloud_root->setStringValue("layer-type",layer_type);
319 buildLayer(iLayer, layer_type, coverage_norm);
323 void FGClouds::set_3dClouds(bool enable)
325 if (enable != clouds_3d_enabled) {
326 clouds_3d_enabled = enable;
331 bool FGClouds::get_3dClouds() const
333 return clouds_3d_enabled;
337 * Adds a 3D cloud to a cloud layer.
340 * layer - the layer index to add this cloud to. (Defaults to 0)
341 * index - the index for this cloud (to be used later)
342 * lon/lat/alt - the position for the cloud
343 * (Various) - cloud definition properties. See README.3DClouds
346 bool FGClouds::add3DCloud(const SGPropertyNode *arg)
348 int l = arg->getIntValue("layer", 0);
349 int index = arg->getIntValue("index", 0);
351 SGPath texture_root = globals->get_fg_root();
352 texture_root.append("Textures");
353 texture_root.append("Sky");
355 float lon = arg->getFloatValue("lon-deg", 0.0f);
356 float lat = arg->getFloatValue("lat-deg", 0.0f);
357 float alt = arg->getFloatValue("alt-ft", 0.0f);
358 float x = arg->getFloatValue("x-offset-m", 0.0f);
359 float y = arg->getFloatValue("y-offset-m", 0.0f);
361 SGSky* thesky = globals->get_renderer()->getSky();
362 SGCloudField *layer = thesky->get_cloud_layer(l)->get_layer3D();
363 SGNewCloud cld(texture_root, arg, &seed);
364 bool success = layer->addCloud(lon, lat, alt, x, y, index, cld.genCloud());
366 // Adding a 3D cloud immediately makes this layer 3D.
367 thesky->get_cloud_layer(l)->set_enable3dClouds(true);
373 * Removes a 3D cloud from a cloud layer
377 * layer - the layer index to remove this cloud from. (defaults to 0)
378 * index - the cloud index
381 bool FGClouds::delete3DCloud(const SGPropertyNode *arg)
383 int l = arg->getIntValue("layer", 0);
384 int i = arg->getIntValue("index", 0);
386 SGSky* thesky = globals->get_renderer()->getSky();
387 SGCloudField *layer = thesky->get_cloud_layer(l)->get_layer3D();
388 return layer->deleteCloud(i);
392 * Move a cloud within a 3D layer
395 * layer - the layer index to add this cloud to. (Defaults to 0)
396 * index - the cloud index to move.
397 * lon/lat/alt - the position for the cloud
400 bool FGClouds::move3DCloud(const SGPropertyNode *arg)
402 int l = arg->getIntValue("layer", 0);
403 int i = arg->getIntValue("index", 0);
404 SGSky* thesky = globals->get_renderer()->getSky();
406 float lon = arg->getFloatValue("lon-deg", 0.0f);
407 float lat = arg->getFloatValue("lat-deg", 0.0f);
408 float alt = arg->getFloatValue("alt-ft", 0.0f);
409 float x = arg->getFloatValue("x-offset-m", 0.0f);
410 float y = arg->getFloatValue("y-offset-m", 0.0f);
412 SGCloudField *layer = thesky->get_cloud_layer(l)->get_layer3D();
413 return layer->repositionCloud(i, lon, lat, alt, x, y);