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