33#include <simgear/constants.h>
34#include <simgear/sound/soundmgr.hxx>
35#include <simgear/scene/sky/sky.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>
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");
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);
85double 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( abox->getNameString() ==
"box" ) {
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++) {
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];
155 y = w * (y - 0.5) + pos[1];
159 SGNewCloud cld(texture_root, cld_def, &
seed);
162 layer->addCloud(lon, lat, z, x, y, index++, cld.genCloud());
171void FGClouds::buildLayer(
int iLayer,
const string&
name,
double coverage) {
176 int CloudVarietyCount = 0;
177 double totalCount = 0.0;
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();
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);
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);
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( 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;
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;
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,
256 thesky->get_cloud_layer(iLayer)->set_enable3dClouds(clouds_3d_enabled);
259void FGClouds::buildCloudLayers(
void) {
260 SGPropertyNode *metar_root =
fgGetNode(
"/environment",
true);
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");
269 double cumulus_base = 122.0 * (temperature_degc - dewpoint_degc);
270 double stratus_base = 100.0 * (100.0 - rel_humidity) * SG_FEET_TO_METER;
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;
283 else if( coverage ==
"scattered" )
284 coverage_norm = 4.0/8.0;
285 else if( coverage ==
"broken" )
286 coverage_norm = 6.0/8.0;
287 else if( coverage ==
"overcast" )
288 coverage_norm = 8.0/8.0;
290 string layer_type =
"nn";
292 if( coverage ==
"cirrus" ) {
294 }
else if( alt_ft > 16500 ) {
297 }
else if( alt_ft > 6500 ) {
300 if( pressure_mb < 1005.0 && coverage_norm >= 0.5 )
304 if( cumulus_base * 0.80 < alt_m && cumulus_base * 1.20 > alt_m ) {
307 }
else if( stratus_base * 0.80 < alt_m && stratus_base * 1.40 > alt_m ) {
314 else if( alt_ft < 4500 )
321 cloud_root->setStringValue(
"layer-type",layer_type);
322 buildLayer(iLayer, layer_type, coverage_norm);
328 if (enable != clouds_3d_enabled) {
329 clouds_3d_enabled = enable;
336 return clouds_3d_enabled;
349 bool FGClouds::add3DCloud(
const SGPropertyNode *arg, SGPropertyNode * root)
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());
370 thesky->get_cloud_layer(l)->set_enable3dClouds(
true);
384 bool FGClouds::delete3DCloud(
const SGPropertyNode *arg, SGPropertyNode * root)
386 int l = arg->getIntValue(
"layer", 0);
387 int i = arg->getIntValue(
"index", 0);
390 SGCloudField *layer = thesky->get_cloud_layer(l)->get_layer3D();
391 return layer->deleteCloud(
i);
403bool FGClouds::move3DCloud(
const SGPropertyNode *arg, SGPropertyNode * root)
405 int l = arg->getIntValue(
"layer", 0);
406 int i = arg->getIntValue(
"index", 0);
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);
void set_update_event(int count)
bool get_3dClouds() const
void set_3dClouds(bool enable)
int get_update_event(void) const
virtual FGRenderer * get_renderer() const
SGPropertyNode * fgGetNode(const char *path, bool create)
Get a property node.