FlightGear next
fgclouds.cxx
Go to the documentation of this file.
1// Build a cloud layer based on metar
2//
3// Written by Harald JOHNSEN, started April 2005.
4//
5// Copyright (C) 2005 Harald JOHNSEN - hjohnsen@evc.net
6//
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.
11//
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.
16//
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.
20//
21//
22
23#ifdef HAVE_CONFIG_H
24# include "config.h"
25#endif
26
27#include "fgclouds.hxx"
28
29#include <cstring>
30#include <cstdio>
31#include <Main/fg_props.hxx>
32
33#include <simgear/constants.h>
34#include <simgear/sound/soundmgr.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>
41
42#include <Main/globals.hxx>
43#include <Main/util.hxx>
44#include <Viewer/renderer.hxx>
45#include <Airports/airport.hxx>
46
47// RNG seed to ensure cloud synchronization across multi-process
48// deployments
49static mt seed;
50
52 clouds_3d_enabled(false),
53 index(0)
54{
55 update_event = 0;
56}
57
59{
60 globals->get_commands()->removeCommand("add-cloud");
61 globals->get_commands()->removeCommand("del-cloud");
62 globals->get_commands()->removeCommand("move-cloud");
63
64}
65
67 return update_event;
68}
69
71 update_event = count;
72 buildCloudLayers();
73}
74
76{
77 mt_init_time_10(&seed);
78
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);
82}
83
84// Build an invidual cloud. Returns the extents of the cloud for coverage calculations
85double FGClouds::buildCloud(SGPropertyNode *cloud_def_root, SGPropertyNode *box_def_root,
86 const std::string& name, double grid_z_rand, SGCloudField *layer)
87{
88 SGPropertyNode *box_def=NULL;
89 SGPropertyNode *cld_def=NULL;
90 double extent = 0.0;
91
92 SGPath texture_root = globals->get_fg_root();
93 texture_root.append("Textures");
94 texture_root.append("Sky");
95
96 box_def = box_def_root->getChild(name.c_str());
97
98 string base_name = name.substr(0,2);
99 if( !box_def ) {
100 if( name[2] == '-' ) {
101 box_def = box_def_root->getChild(base_name.c_str());
102 }
103 if( !box_def )
104 return 0.0;
105 }
106
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);
110
111 float lon = fgGetNode("/position/longitude-deg", false)->getFloatValue();
112 float lat = fgGetNode("/position/latitude-deg", false)->getFloatValue();
113
114 SGVec3f pos(x,y,z);
115
116 for(int i = 0; i < box_def->nChildren() ; i++) {
117 SGPropertyNode *abox = box_def->getChild(i);
118 if( abox->getNameString() == "box" ) {
119
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;
123
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);
128
129 double c = abox->getDoubleValue("count", 5);
130 int count = (int) (c + (mt_rand(&seed) - 0.5) * c);
131
132 extent = std::max(w*w, extent);
133
134 for (int j = 0; j < count; j++) {
135
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.
139 double x = 0.0;
140 double y = 0.0;
141 double z = 0.0;
142
143 for (int k = 0; k < hdist; k++)
144 {
145 x += (mt_rand(&seed) / hdist);
146 y += (mt_rand(&seed) / hdist);
147 }
148
149 for (int k = 0; k < vdist; k++)
150 {
151 z += (mt_rand(&seed) / vdist);
152 }
153
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
157
158 //SGVec3f newpos = SGVec3f(x, y, z);
159 SGNewCloud cld(texture_root, cld_def, &seed);
160
161 //layer->addCloud(newpos, cld.genCloud());
162 layer->addCloud(lon, lat, z, x, y, index++, cld.genCloud());
163 }
164 }
165 }
166
167 // Return the maximum extent of the cloud
168 return extent;
169}
170
171void FGClouds::buildLayer(int iLayer, const string& name, double coverage) {
172 struct {
173 string name;
174 double count;
175 } tCloudVariety[20];
176 int CloudVarietyCount = 0;
177 double totalCount = 0.0;
178
179 SGSky* thesky = globals->get_renderer()->getSky();
180
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();
185 layer->clear();
186
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);
191 return;
192 }
193
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());
197 if( !layer_def ) {
198 if( name[2] == '-' ) {
199 string base_name = name.substr(0,2);
200 layer_def = layer_def_root->getChild(base_name.c_str());
201 }
202 if( !layer_def ) {
203 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
204 return;
205 }
206 }
207
208 // At this point, we know we've got some 3D clouds to generate.
209 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(true);
210
211 double grid_z_rand = layer_def->getDoubleValue("grid-z-rand");
212
213 for(int i = 0; i < layer_def->nChildren() ; i++) {
214 SGPropertyNode *acloud = layer_def->getChild(i);
215 if( acloud->getNameString() == "cloud" ) {
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;
220 int variety = 0;
221 char variety_name[50];
222 do {
223 variety++;
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) );
226
227 totalCount += count;
228 if( CloudVarietyCount < 20 )
229 CloudVarietyCount++;
230 }
231 }
232 totalCount = 1.0 / totalCount;
233
234 // Determine how much cloud coverage we need in m^2.
235 double cov = coverage * SGCloudField::fieldSize * SGCloudField::fieldSize;
236
237 while (cov > 0.0f) {
238 double choice = mt_rand(&seed);
239
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,
244 box_def_root,
245 tCloudVariety[i].name,
246 grid_z_rand,
247 layer);
248 break;
249 }
250 }
251 }
252
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);
257}
258
259void FGClouds::buildCloudLayers(void) {
260 SGPropertyNode *metar_root = fgGetNode("/environment", true);
261
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");
267
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;
271
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);
275
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");
279
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
289
290 string layer_type = "nn";
291
292 if( coverage == "cirrus" ) {
293 layer_type = "ci";
294 } else if( alt_ft > 16500 ) {
295// layer_type = "ci|cs|cc";
296 layer_type = "ci";
297 } else if( alt_ft > 6500 ) {
298// layer_type = "as|ac|ns";
299 layer_type = "ac";
300 if( pressure_mb < 1005.0 && coverage_norm >= 0.5 )
301 layer_type = "ns";
302 } else {
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
306 layer_type = "cu";
307 } else if( stratus_base * 0.80 < alt_m && stratus_base * 1.40 > alt_m ) {
308 // +/- 20% from stratus probable base
309 layer_type = "st";
310 } else {
311 // above formulae is far from perfect
312 if ( alt_ft < 2000 )
313 layer_type = "st";
314 else if( alt_ft < 4500 )
315 layer_type = "cu";
316 else
317 layer_type = "sc";
318 }
319 }
320
321 cloud_root->setStringValue("layer-type",layer_type);
322 buildLayer(iLayer, layer_type, coverage_norm);
323 }
324}
325
326void FGClouds::set_3dClouds(bool enable)
327{
328 if (enable != clouds_3d_enabled) {
329 clouds_3d_enabled = enable;
330 buildCloudLayers();
331 }
332}
333
335{
336 return clouds_3d_enabled;
337}
338
349 bool FGClouds::add3DCloud(const SGPropertyNode *arg, SGPropertyNode * root)
350 {
351 int l = arg->getIntValue("layer", 0);
352 int index = arg->getIntValue("index", 0);
353
354 SGPath texture_root = globals->get_fg_root();
355 texture_root.append("Textures");
356 texture_root.append("Sky");
357
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);
363
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());
368
369 // Adding a 3D cloud immediately makes this layer 3D.
370 thesky->get_cloud_layer(l)->set_enable3dClouds(true);
371
372 return success;
373 }
374
384 bool FGClouds::delete3DCloud(const SGPropertyNode *arg, SGPropertyNode * root)
385 {
386 int l = arg->getIntValue("layer", 0);
387 int i = arg->getIntValue("index", 0);
388
389 SGSky* thesky = globals->get_renderer()->getSky();
390 SGCloudField *layer = thesky->get_cloud_layer(l)->get_layer3D();
391 return layer->deleteCloud(i);
392 }
393
403bool FGClouds::move3DCloud(const SGPropertyNode *arg, SGPropertyNode * root)
404 {
405 int l = arg->getIntValue("layer", 0);
406 int i = arg->getIntValue("index", 0);
407 SGSky* thesky = globals->get_renderer()->getSky();
408
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);
414
415 SGCloudField *layer = thesky->get_cloud_layer(l)->get_layer3D();
416 return layer->repositionCloud(i, lon, lat, alt, x, y);
417 }
#define i(x)
void set_update_event(int count)
Definition fgclouds.cxx:70
bool get_3dClouds() const
Definition fgclouds.cxx:334
void Init(void)
Definition fgclouds.cxx:75
void set_3dClouds(bool enable)
Definition fgclouds.cxx:326
int get_update_event(void) const
Definition fgclouds.cxx:66
virtual FGRenderer * get_renderer() const
Definition globals.cxx:572
SGSky * getSky() const
Definition renderer.cxx:856
const char * name
static mt seed
Definition fgclouds.cxx:49
FGGlobals * globals
Definition globals.cxx:142
SGPropertyNode * fgGetNode(const char *path, bool create)
Get a property node.
Definition proptest.cpp:27