]> git.mxchange.org Git - flightgear.git/blob - src/Environment/fgclouds.cxx
fix a pointer reference.
[flightgear.git] / src / Environment / fgclouds.cxx
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 <Main/fg_props.hxx>
28
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>
37
38 #include <Main/globals.hxx>
39 #include <Airports/simple.hxx>
40 #include <Main/util.hxx>
41
42 #include "fgclouds.hxx"
43
44 extern SGSky *thesky;
45
46
47 FGClouds::FGClouds() :
48     snd_lightning(0),
49     clouds_3d_enabled(false)
50 {
51         update_event = 0;
52 }
53
54 FGClouds::~FGClouds() {
55 }
56
57 int FGClouds::get_update_event(void) const {
58         return update_event;
59 }
60
61 void FGClouds::set_update_event(int count) {
62         update_event = count;
63         buildCloudLayers();
64 }
65
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 *smgr = globals->get_soundmgr();
72                 SGSampleGroup *sgr = smgr->find("weather", true);
73                 sgr->add( snd_lightning, "thunder" );
74                 sgEnviro.set_sampleGroup( sgr );
75         }
76 }
77
78 void FGClouds::buildCloud(SGPropertyNode *cloud_def_root, SGPropertyNode *box_def_root, const string& name, sgVec3 pos, SGCloudField *layer) {
79         SGPropertyNode *box_def=NULL;
80         SGPropertyNode *cld_def=NULL;
81
82         SGPath texture_root = globals->get_fg_root();
83         texture_root.append("Textures");
84         texture_root.append("Sky");
85
86         box_def = box_def_root->getChild(name.c_str());
87   
88         string base_name = name.substr(0,2);
89         if( !box_def ) {
90                 if( name[2] == '-' ) {
91                         box_def = box_def_root->getChild(base_name.c_str());
92                 }
93                 if( !box_def )
94                         return;
95         }
96         
97         for(int i = 0; i < box_def->nChildren() ; i++) {
98                 SGPropertyNode *abox = box_def->getChild(i);
99                 if( strcmp(abox->getName(), "box") == 0) {
100                         double x = abox->getDoubleValue("x") + pos[0];
101                         double y = abox->getDoubleValue("y") + pos[1];
102                         double z = abox->getDoubleValue("z") + pos[2];
103                         SGVec3f newpos = SGVec3f(x, z, y);
104                         
105                         string type = abox->getStringValue("type", "cu-small");
106                                
107                         cld_def = cloud_def_root->getChild(type.c_str());
108                         if ( !cld_def ) return;
109                         
110                         double min_width = cld_def->getDoubleValue("min-cloud-width-m", 500.0);
111                         double max_width = cld_def->getDoubleValue("max-cloud-width-m", 1000.0);
112                         double min_height = cld_def->getDoubleValue("min-cloud-height-m", min_width);
113                         double max_height = cld_def->getDoubleValue("max-cloud-height-m", max_width);
114                         double min_sprite_width = cld_def->getDoubleValue("min-sprite-width-m", 200.0);
115                         double max_sprite_width = cld_def->getDoubleValue("max-sprite-width-m", min_sprite_width);
116                         double min_sprite_height = cld_def->getDoubleValue("min-sprite-height-m", min_sprite_width);
117                         double max_sprite_height = cld_def->getDoubleValue("max-sprite-height-m", max_sprite_width);
118                         int num_sprites = cld_def->getIntValue("num-sprites", 20);
119                         int num_textures_x = cld_def->getIntValue("num-textures-x", 1);
120                         int num_textures_y = cld_def->getIntValue("num-textures-y", 1);
121                         double bottom_shade = cld_def->getDoubleValue("bottom-shade", 1.0);
122                         string texture = cld_def->getStringValue("texture", "cl_cumulus.rgb");
123
124                         SGNewCloud *cld = 
125                                 new SGNewCloud(type,
126                                         texture_root, 
127                                         texture, 
128                                         min_width, 
129                                         max_width, 
130                                         min_height,
131                                         max_height,
132                                         min_sprite_width,
133                                         max_sprite_width,
134                                         min_sprite_height,
135                                         max_sprite_height,
136                                         bottom_shade,
137                                         num_sprites,
138                                         num_textures_x,
139                                         num_textures_y);
140                         layer->addCloud(newpos, cld);
141                 }
142         }
143 }
144
145 void FGClouds::buildLayer(int iLayer, const string& name, double alt, double coverage) {
146         struct {
147                 string name;
148                 double count;
149         } tCloudVariety[20];
150         int CloudVarietyCount = 0;
151         double totalCount = 0.0;
152         
153         SGPropertyNode *cloud_def_root = fgGetNode("/environment/cloudlayers/clouds", false);
154         SGPropertyNode *box_def_root   = fgGetNode("/environment/cloudlayers/boxes", false);
155         SGPropertyNode *layer_def_root = fgGetNode("/environment/cloudlayers/layers", false);
156         SGCloudField *layer = thesky->get_cloud_layer(iLayer)->get_layer3D();
157         layer->clear();
158         
159         // If we don't have the required properties, then render the cloud in 2D
160         if ((! clouds_3d_enabled) || coverage == 0.0 ||
161                 layer_def_root == NULL || cloud_def_root == NULL || box_def_root == NULL) {
162                         thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
163                         return;
164         }
165         
166         // If we can't find a definition for this cloud type, then render the cloud in 2D
167         SGPropertyNode *layer_def=NULL;
168         layer_def = layer_def_root->getChild(name.c_str());
169         if( !layer_def ) {
170                 if( name[2] == '-' ) {
171                         string base_name = name.substr(0,2);
172                         layer_def = layer_def_root->getChild(base_name.c_str());
173                 }
174                 if( !layer_def ) {
175                         thesky->get_cloud_layer(iLayer)->set_enable3dClouds(false);
176                         return;
177                 }
178         }
179         
180         // At this point, we know we've got some 3D clouds to generate.
181         thesky->get_cloud_layer(iLayer)->set_enable3dClouds(true);
182
183         double grid_x_size = layer_def->getDoubleValue("grid-x-size", 1000.0);
184         double grid_y_size = layer_def->getDoubleValue("grid-y-size", 1000.0);
185         double grid_x_rand = layer_def->getDoubleValue("grid-x-rand", grid_x_size);
186         double grid_y_rand = layer_def->getDoubleValue("grid-y-rand", grid_y_size);
187         double grid_z_rand = layer_def->getDoubleValue("grid-z-rand");
188
189         for(int i = 0; i < layer_def->nChildren() ; i++) {
190                 SGPropertyNode *acloud = layer_def->getChild(i);
191                 if( strcmp(acloud->getName(), "cloud") == 0) {
192                         string cloud_name = acloud->getStringValue("name");
193                         tCloudVariety[CloudVarietyCount].name = cloud_name;
194                         double count = acloud->getDoubleValue("count", 1.0);
195                         tCloudVariety[CloudVarietyCount].count = count;
196                         int variety = 0;
197                         cloud_name = cloud_name + "-%d";
198                         char variety_name[50];
199                         do {
200                                 variety++;
201                                 snprintf(variety_name, sizeof(variety_name) - 1, cloud_name.c_str(), variety);
202                         } while( box_def_root->getChild(variety_name, 0, false) );
203
204                         totalCount += count;
205                         if( CloudVarietyCount < 20 )
206                                 CloudVarietyCount++;
207                 }
208         }
209         totalCount = 1.0 / totalCount;
210         
211         for(double px = 0.0; px < SGCloudField::fieldSize; px += grid_x_size) {
212                 for(double py = 0.0; py < SGCloudField::fieldSize; py += grid_y_size) {
213                         double x = px + grid_x_rand * (sg_random() - 0.5) - (SGCloudField::fieldSize / 2.0);
214                         double y = py + grid_y_rand * (sg_random() - 0.5) - (SGCloudField::fieldSize / 2.0);
215                         double z = grid_z_rand * (sg_random() - 0.5);
216                         
217                         if (sg_random() < coverage) {
218                                 double choice = sg_random();
219     
220                                 for(int i = 0; i < CloudVarietyCount ; i ++) {
221                                         choice -= tCloudVariety[i].count * totalCount;
222                                         if( choice <= 0.0 ) {
223                                                 sgVec3 pos={x,z,y};
224
225                                                 buildCloud(cloud_def_root, 
226                                                         box_def_root, 
227                                                         tCloudVariety[i].name, 
228                                                         pos, 
229                                                         layer);
230                                                 break;
231                                         }
232                                 }
233                         }
234                 }
235         }
236
237         // Now we've built any clouds, enable them and set the density (coverage)
238         //layer->setCoverage(coverage);
239         //layer->applyCoverage();
240         thesky->get_cloud_layer(iLayer)->set_enable3dClouds(clouds_3d_enabled);
241 }
242
243 void FGClouds::buildCloudLayers(void) {
244         SGPropertyNode *metar_root = fgGetNode("/environment", true);
245
246         //double wind_speed_kt   = metar_root->getDoubleValue("wind-speed-kt");
247         double temperature_degc  = metar_root->getDoubleValue("temperature-sea-level-degc");
248         double dewpoint_degc     = metar_root->getDoubleValue("dewpoint-sea-level-degc");
249         double pressure_mb              = metar_root->getDoubleValue("pressure-sea-level-inhg") * SG_INHG_TO_PA / 100.0;
250
251         double dewp = pow(10.0, 7.5 * dewpoint_degc / (237.7 + dewpoint_degc));
252         double temp = pow(10.0, 7.5 * temperature_degc / (237.7 + temperature_degc));
253         double rel_humidity = dewp * 100 / temp;
254
255         // formule d'Epsy, base d'un cumulus
256         double cumulus_base = 122.0 * (temperature_degc - dewpoint_degc);
257         double stratus_base = 100.0 * (100.0 - rel_humidity) * SG_FEET_TO_METER;
258
259         for(int iLayer = 0 ; iLayer < thesky->get_cloud_layer_count(); iLayer++) {
260                 SGPropertyNode *cloud_root = fgGetNode("/environment/clouds/layer", iLayer, true);
261
262                 double alt_ft = cloud_root->getDoubleValue("elevation-ft");
263                 double alt_m = alt_ft * SG_FEET_TO_METER;
264                 string coverage = cloud_root->getStringValue("coverage");
265                 
266                 double coverage_norm = 0.0;
267                 if( coverage == "few" )
268                         coverage_norm = 2.0/8.0;        // <1-2
269                 else if( coverage == "scattered" )
270                         coverage_norm = 4.0/8.0;        // 3-4
271                 else if( coverage == "broken" )
272                         coverage_norm = 6.0/8.0;        // 5-7
273                 else if( coverage == "overcast" )
274                         coverage_norm = 8.0/8.0;        // 8
275
276                 string layer_type = "nn";
277                 if( coverage == "cirrus" ) {
278                         layer_type = "ci";
279                 } else if( alt_ft > 16500 ) {
280 //                      layer_type = "ci|cs|cc";
281                         layer_type = "ci";
282                 } else if( alt_ft > 6500 ) {
283 //                      layer_type = "as|ac|ns";
284                         layer_type = "ac";
285                         if( pressure_mb < 1005.0 && coverage_norm >= 0.5 )
286                                 layer_type = "ns";
287                 } else {
288 //                      layer_type = "st|cu|cb|sc";
289                         if( cumulus_base * 0.80 < alt_m && cumulus_base * 1.20 > alt_m ) {
290                                 // +/- 20% from cumulus probable base
291                                 layer_type = "cu";
292                         } else if( stratus_base * 0.80 < alt_m && stratus_base * 1.40 > alt_m ) {
293                                 // +/- 20% from stratus probable base
294                                 layer_type = "st";
295                         } else {
296                                 // above formulae is far from perfect
297                                 if ( alt_ft < 2000 )
298                                         layer_type = "st";
299                                 else if( alt_ft < 4500 )
300                                         layer_type = "cu";
301                                 else
302                                         layer_type = "sc";
303                         }
304                 }
305                 
306                 buildLayer(iLayer, layer_type, alt_m, coverage_norm);
307         }
308 }
309
310 void FGClouds::set_3dClouds(bool enable)
311 {
312   if (enable != clouds_3d_enabled) {
313     clouds_3d_enabled = enable;
314     buildCloudLayers();
315   }
316 }
317
318 bool FGClouds::get_3dClouds() const {
319   return clouds_3d_enabled;
320 }
321