]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_mgr.cxx
Mark's dynamic sun color changes.
[flightgear.git] / src / Environment / environment_mgr.cxx
1 // environment-mgr.cxx -- manager for natural environment information.
2 //
3 // Written by David Megginson, started February 2002.
4 //
5 // Copyright (C) 2002  David Megginson - david@megginson.com
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 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/scene/sky/sky.hxx>
29 #include <simgear/environment/visual_enviro.hxx>
30
31 #include <Main/main.hxx>
32 #include <Main/fg_props.hxx>
33 #include <Aircraft/aircraft.hxx>
34
35 #include "environment.hxx"
36 #include "environment_ctrl.hxx"
37 #include "environment_mgr.hxx"
38 #include "fgclouds.hxx"
39
40 class SGSky;
41 extern SGSky *thesky;
42
43
44
45 FGEnvironmentMgr::FGEnvironmentMgr ()
46   : _environment(new FGEnvironment)
47 {
48
49    if (fgGetBool("/environment/params/real-world-weather-fetch") == true)
50        _controller = new FGMetarEnvironmentCtrl;
51    else
52        _controller = new FGInterpolateEnvironmentCtrl;
53
54   _controller->setEnvironment(_environment);
55   set_subsystem("controller", _controller, 0.5);
56   fgClouds = new FGClouds( _controller );
57 }
58
59 FGEnvironmentMgr::~FGEnvironmentMgr ()
60 {
61   delete _environment;
62   remove_subsystem("controller");
63   delete _controller;
64   delete fgClouds;
65 }
66
67 void
68 FGEnvironmentMgr::init ()
69 {
70   SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
71   SGSubsystemGroup::init();
72   _update_fdm();
73 }
74
75 void
76 FGEnvironmentMgr::reinit ()
77 {
78   SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
79   SGSubsystemGroup::reinit();
80   _update_fdm();
81 }
82
83 void
84 FGEnvironmentMgr::bind ()
85 {
86   fgTie("/environment/visibility-m", _environment,
87         &FGEnvironment::get_visibility_m, &FGEnvironment::set_visibility_m);
88   fgSetArchivable("/environment/visibility-m");
89   fgTie("/environment/temperature-sea-level-degc", _environment,
90         &FGEnvironment::get_temperature_sea_level_degc,
91         &FGEnvironment::set_temperature_sea_level_degc);
92   fgSetArchivable("/environment/temperature-sea-level-degc");
93   fgTie("/environment/temperature-degc", _environment,
94         &FGEnvironment::get_temperature_degc); // FIXME: read-only for now
95   fgTie("/environment/temperature-degf", _environment,
96         &FGEnvironment::get_temperature_degf); // FIXME: read-only for now
97   fgTie("/environment/dewpoint-sea-level-degc", _environment,
98         &FGEnvironment::get_dewpoint_sea_level_degc,
99         &FGEnvironment::set_dewpoint_sea_level_degc);
100   fgSetArchivable("/environment/dewpoint-sea-level-degc");
101   fgTie("/environment/dewpoint-degc", _environment,
102         &FGEnvironment::get_dewpoint_degc); // FIXME: read-only for now
103   fgTie("/environment/pressure-sea-level-inhg", _environment,
104         &FGEnvironment::get_pressure_sea_level_inhg,
105         &FGEnvironment::set_pressure_sea_level_inhg);
106   fgSetArchivable("/environment/pressure-sea-level-inhg");
107   fgTie("/environment/pressure-inhg", _environment,
108         &FGEnvironment::get_pressure_inhg); // FIXME: read-only for now
109   fgTie("/environment/density-slugft3", _environment,
110         &FGEnvironment::get_density_slugft3); // read-only
111   fgTie("/environment/relative-humidity", _environment,
112         &FGEnvironment::get_relative_humidity); //ro
113   fgTie("/environment/atmosphere/density-tropo-avg", _environment,
114         &FGEnvironment::get_density_tropo_avg_kgm3); //ro
115   fgTie("/environment/atmosphere/altitude-half-to-sun", _environment,
116         &FGEnvironment::get_altitude_half_to_sun_m, 
117         &FGEnvironment::set_altitude_half_to_sun_m);
118   fgTie("/environment/atmosphere/altitude-troposphere-top", _environment,
119         &FGEnvironment::get_altitude_tropo_top_m,
120         &FGEnvironment::set_altitude_tropo_top_m);
121   fgTie("/environment/wind-from-heading-deg", _environment,
122         &FGEnvironment::get_wind_from_heading_deg,
123         &FGEnvironment::set_wind_from_heading_deg);
124   fgTie("/environment/wind-speed-kt", _environment,
125         &FGEnvironment::get_wind_speed_kt, &FGEnvironment::set_wind_speed_kt);
126   fgTie("/environment/wind-from-north-fps", _environment,
127         &FGEnvironment::get_wind_from_north_fps,
128         &FGEnvironment::set_wind_from_north_fps);
129   fgSetArchivable("/environment/wind-from-north-fps");
130   fgTie("/environment/wind-from-east-fps", _environment,
131         &FGEnvironment::get_wind_from_east_fps,
132         &FGEnvironment::set_wind_from_east_fps);
133   fgSetArchivable("/environment/wind-from-east-fps");
134   fgTie("/environment/wind-from-down-fps", _environment,
135         &FGEnvironment::get_wind_from_down_fps,
136         &FGEnvironment::set_wind_from_down_fps);
137   fgSetArchivable("/environment/wind-from-down-fps");
138   fgTie("/environment/turbulence/magnitude-norm", _environment,
139         &FGEnvironment::get_turbulence_magnitude_norm,
140         &FGEnvironment::set_turbulence_magnitude_norm);
141   fgSetArchivable("/environment/turbulence/magnitude-norm");
142   fgTie("/environment/turbulence/rate-hz", _environment,
143         &FGEnvironment::get_turbulence_rate_hz,
144         &FGEnvironment::set_turbulence_rate_hz);
145   fgSetArchivable("/environment/turbulence/rate-hz");
146
147   for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
148     char buf[128];
149     sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
150     fgTie(buf, this, i,
151           &FGEnvironmentMgr::get_cloud_layer_span_m,
152           &FGEnvironmentMgr::set_cloud_layer_span_m);
153     fgSetArchivable(buf);
154     sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
155     fgTie(buf, this, i,
156           &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
157           &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
158     fgSetArchivable(buf);
159     sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
160     fgTie(buf, this, i,
161           &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
162           &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
163     fgSetArchivable(buf);
164     sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
165     fgTie(buf, this, i,
166           &FGEnvironmentMgr::get_cloud_layer_transition_ft,
167           &FGEnvironmentMgr::set_cloud_layer_transition_ft);
168     fgSetArchivable(buf);
169     sprintf(buf, "/environment/clouds/layer[%d]/coverage", i);
170     fgTie(buf, this, i,
171           &FGEnvironmentMgr::get_cloud_layer_coverage,
172           &FGEnvironmentMgr::set_cloud_layer_coverage);
173     fgSetArchivable(buf);
174   }
175   fgTie("/sim/rendering/clouds3d-enable", &sgEnviro,
176           &SGEnviro::get_clouds_enable_state,
177           &SGEnviro::set_clouds_enable_state);
178   fgTie("/sim/rendering/clouds3d-vis-range", &sgEnviro,
179           &SGEnviro::get_clouds_visibility,
180           &SGEnviro::set_clouds_visibility);
181   fgTie("/sim/rendering/clouds3d-density", &sgEnviro,
182           &SGEnviro::get_clouds_density,
183           &SGEnviro::set_clouds_density);
184   fgTie("/sim/rendering/clouds3d-cache-size", &sgEnviro,
185           &SGEnviro::get_clouds_CacheSize,
186           &SGEnviro::set_clouds_CacheSize);
187   fgTie("/sim/rendering/clouds3d-cache-resolution", &sgEnviro,
188           &SGEnviro::get_CacheResolution,
189           &SGEnviro::set_CacheResolution);
190   fgTie("/sim/rendering/precipitation-enable", &sgEnviro,
191           &SGEnviro::get_precipitation_enable_state,
192           &SGEnviro::set_precipitation_enable_state);
193   fgTie("/environment/rebuild_layers", fgClouds,
194       &FGClouds::get_update_event,
195       &FGClouds::set_update_event);
196   fgTie("/sim/rendering/lightning-enable", &sgEnviro,
197       &SGEnviro::get_lightning_enable_state,
198       &SGEnviro::set_lightning_enable_state);
199   fgTie("/environment/turbulence/use-cloud-turbulence", &sgEnviro,
200       &SGEnviro::get_turbulence_enable_state,
201       &SGEnviro::set_turbulence_enable_state);
202   sgEnviro.config(fgGetNode("/sim/rendering/precipitation"));
203 }
204
205 void
206 FGEnvironmentMgr::unbind ()
207 {
208   fgUntie("/environment/visibility-m");
209   fgUntie("/environment/temperature-sea-level-degc");
210   fgUntie("/environment/temperature-degc");
211   fgUntie("/environment/dewpoint-sea-level-degc");
212   fgUntie("/environment/dewpoint-degc");
213   fgUntie("/environment/pressure-sea-level-inhg");
214   fgUntie("/environment/pressure-inhg");
215   fgUntie("/environment/density-inhg");
216   fgUntie("/environment/relative_humidity");
217   fgUntie("/environment/atmosphere/density_tropo_avg");
218   fgUntie("/environment/wind-from-north-fps");
219   fgUntie("/environment/wind-from-east-fps");
220   fgUntie("/environment/wind-from-down-fps");
221   fgUntie("/environment/atmosphere/altitude_half_to_sun");
222   fgUntie("/environment/atmosphere/altitude_troposphere_top");
223   for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
224     char buf[128];
225     sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
226     fgUntie(buf);
227     sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
228     fgUntie(buf);
229     sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
230     fgUntie(buf);
231     sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
232     fgUntie(buf);
233     sprintf(buf, "/environment/clouds/layer[%d]/type", i);
234     fgUntie(buf);
235   }
236   fgUntie("/sim/rendering/clouds3d-enable");
237   fgUntie("/sim/rendering/clouds3d-vis-range");
238   fgUntie("/sim/rendering/clouds3d-density");
239   fgUntie("/sim/rendering/clouds3d-cache-size");
240   fgUntie("/sim/rendering/clouds3d-cache-resolution");
241   fgUntie("/sim/rendering/precipitation-enable");
242   fgUntie("/environment/rebuild_layers");
243   fgUntie("/sim/rendering/lightning-enable");
244   fgUntie("/environment/turbulence/use-cloud-turbulence");
245 }
246
247 void
248 FGEnvironmentMgr::update (double dt)
249 {
250   SGSubsystemGroup::update(dt);
251
252                                 // FIXME: the FDMs should update themselves
253   current_aircraft.fdm_state
254     ->set_Velocities_Local_Airmass(_environment->get_wind_from_north_fps(),
255                                    _environment->get_wind_from_east_fps(),
256                                    _environment->get_wind_from_down_fps());
257   _environment->set_elevation_ft(fgGetDouble("/position/altitude-ft"));
258
259   _update_fdm();
260 }
261
262 FGEnvironment
263 FGEnvironmentMgr::getEnvironment () const
264 {
265   return *_environment;
266 }
267
268 FGEnvironment
269 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
270 {
271                                 // Always returns the same environment
272                                 // for now; we'll make it interesting
273                                 // later.
274   FGEnvironment env = *_environment;
275   env.set_elevation_ft(alt);
276   return env;
277 }
278
279 void
280 FGEnvironmentMgr::_update_fdm () const
281 {
282   //
283   // Pass atmosphere on to FDM
284   // FIXME: have FDMs read properties directly.
285   //
286   if (fgGetBool("/environment/params/control-fdm-atmosphere")) {
287                                 // convert from Rankine to Celsius
288     cur_fdm_state
289       ->set_Static_temperature((9.0/5.0)
290                                * (_environment->get_temperature_degc() + 273.15));
291                                 // convert from inHG to PSF
292     cur_fdm_state
293       ->set_Static_pressure(_environment->get_pressure_inhg() * 70.726566);
294                                 // keep in slugs/ft^3
295     cur_fdm_state
296       ->set_Density(_environment->get_density_slugft3());
297   }
298 }
299
300 double
301 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
302 {
303   return thesky->get_cloud_layer(index)->getSpan_m();
304 }
305
306 void
307 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
308 {
309   thesky->get_cloud_layer(index)->setSpan_m(span_m);
310 }
311
312 double
313 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
314 {
315   return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
316 }
317
318 void
319 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
320 {
321   FGEnvironment env = *_environment;
322   env.set_elevation_ft(elevation_ft);
323
324   thesky->get_cloud_layer(index)
325     ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
326
327   thesky->get_cloud_layer(index)
328     ->setSpeed(env.get_wind_speed_kt() * 0.5151);       // 1 kt = 0.5151 m/s
329
330   thesky->get_cloud_layer(index)
331     ->setDirection(env.get_wind_from_heading_deg());
332 }
333
334 double
335 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
336 {
337   return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
338 }
339
340 void
341 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
342 {
343   thesky->get_cloud_layer(index)
344     ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
345 }
346
347 double
348 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
349 {
350   return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
351 }
352
353 void
354 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
355                                                  double transition_ft)
356 {
357   thesky->get_cloud_layer(index)
358     ->setTransition_m(transition_ft * SG_FEET_TO_METER);
359 }
360
361 const char *
362 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
363 {
364   switch (thesky->get_cloud_layer(index)->getCoverage()) {
365   case SGCloudLayer::SG_CLOUD_OVERCAST:
366     return "overcast";
367   case SGCloudLayer::SG_CLOUD_BROKEN:
368     return "broken";
369   case SGCloudLayer::SG_CLOUD_SCATTERED:
370     return "scattered";
371   case SGCloudLayer::SG_CLOUD_FEW:
372     return "few";
373   case SGCloudLayer::SG_CLOUD_CIRRUS:
374     return "cirrus";
375   case SGCloudLayer::SG_CLOUD_CLEAR:
376     return "clear";
377   default:
378     return "unknown";
379   }
380 }
381
382 void
383 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
384                                             const char * coverage_name)
385 {
386   SGCloudLayer::Coverage coverage;
387   if (!strcmp(coverage_name, "overcast"))
388     coverage = SGCloudLayer::SG_CLOUD_OVERCAST;
389   else if (!strcmp(coverage_name, "broken"))
390     coverage = SGCloudLayer::SG_CLOUD_BROKEN;
391   else if (!strcmp(coverage_name, "scattered"))
392     coverage = SGCloudLayer::SG_CLOUD_SCATTERED;
393   else if (!strcmp(coverage_name, "few"))
394     coverage = SGCloudLayer::SG_CLOUD_FEW;
395   else if (!strcmp(coverage_name, "cirrus"))
396     coverage = SGCloudLayer::SG_CLOUD_CIRRUS;
397   else if (!strcmp(coverage_name, "clear"))
398     coverage = SGCloudLayer::SG_CLOUD_CLEAR;
399   else {
400     SG_LOG(SG_INPUT, SG_WARN, "Unknown cloud type " << coverage_name);
401     coverage = SGCloudLayer::SG_CLOUD_CLEAR;
402   }
403   thesky->get_cloud_layer(index)->setCoverage(coverage);
404 }
405
406
407
408 // end of environment-mgr.cxx