1 // environment-mgr.cxx -- manager for natural environment information.
3 // Written by David Megginson, started February 2002.
5 // Copyright (C) 2002 David Megginson - david@megginson.com
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.
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.
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.
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/scene/sky/sky.hxx>
29 #include <simgear/environment/visual_enviro.hxx>
31 #include <Main/main.hxx>
32 #include <Main/fg_props.hxx>
33 #include <Aircraft/aircraft.hxx>
35 #include "environment.hxx"
36 #include "environment_ctrl.hxx"
37 #include "environment_mgr.hxx"
38 #include "fgclouds.hxx"
45 FGEnvironmentMgr::FGEnvironmentMgr ()
46 : _environment(new FGEnvironment)
49 if (fgGetBool("/environment/params/real-world-weather-fetch") == true)
50 _controller = new FGMetarEnvironmentCtrl;
52 _controller = new FGInterpolateEnvironmentCtrl;
54 _controller->setEnvironment(_environment);
55 set_subsystem("controller", _controller, 0.5);
56 fgClouds = new FGClouds( _controller );
59 FGEnvironmentMgr::~FGEnvironmentMgr ()
62 remove_subsystem("controller");
68 FGEnvironmentMgr::init ()
70 SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
71 SGSubsystemGroup::init();
76 FGEnvironmentMgr::reinit ()
78 SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
79 SGSubsystemGroup::reinit();
84 FGEnvironmentMgr::bind ()
86 fgTie("/environment/visibility-m", _environment,
87 &FGEnvironment::get_visibility_m, &FGEnvironment::set_visibility_m);
88 fgSetArchivable("/environment/visibility-m");
89 fgTie("/environment/effective-visibility-m", thesky,
90 &SGSky::get_visibility );
91 fgTie("/environment/temperature-sea-level-degc", _environment,
92 &FGEnvironment::get_temperature_sea_level_degc,
93 &FGEnvironment::set_temperature_sea_level_degc);
94 fgSetArchivable("/environment/temperature-sea-level-degc");
95 fgTie("/environment/temperature-degc", _environment,
96 &FGEnvironment::get_temperature_degc); // FIXME: read-only for now
97 fgTie("/environment/temperature-degf", _environment,
98 &FGEnvironment::get_temperature_degf); // FIXME: read-only for now
99 fgTie("/environment/dewpoint-sea-level-degc", _environment,
100 &FGEnvironment::get_dewpoint_sea_level_degc,
101 &FGEnvironment::set_dewpoint_sea_level_degc);
102 fgSetArchivable("/environment/dewpoint-sea-level-degc");
103 fgTie("/environment/dewpoint-degc", _environment,
104 &FGEnvironment::get_dewpoint_degc); // FIXME: read-only for now
105 fgTie("/environment/pressure-sea-level-inhg", _environment,
106 &FGEnvironment::get_pressure_sea_level_inhg,
107 &FGEnvironment::set_pressure_sea_level_inhg);
108 fgSetArchivable("/environment/pressure-sea-level-inhg");
109 fgTie("/environment/pressure-inhg", _environment,
110 &FGEnvironment::get_pressure_inhg); // FIXME: read-only for now
111 fgTie("/environment/density-slugft3", _environment,
112 &FGEnvironment::get_density_slugft3); // read-only
113 fgTie("/environment/relative-humidity", _environment,
114 &FGEnvironment::get_relative_humidity); //ro
115 fgTie("/environment/atmosphere/density-tropo-avg", _environment,
116 &FGEnvironment::get_density_tropo_avg_kgm3); //ro
117 fgTie("/environment/atmosphere/altitude-half-to-sun", _environment,
118 &FGEnvironment::get_altitude_half_to_sun_m,
119 &FGEnvironment::set_altitude_half_to_sun_m);
120 fgTie("/environment/atmosphere/altitude-troposphere-top", _environment,
121 &FGEnvironment::get_altitude_tropo_top_m,
122 &FGEnvironment::set_altitude_tropo_top_m);
123 fgTie("/environment/wind-from-heading-deg", _environment,
124 &FGEnvironment::get_wind_from_heading_deg,
125 &FGEnvironment::set_wind_from_heading_deg);
126 fgTie("/environment/wind-speed-kt", _environment,
127 &FGEnvironment::get_wind_speed_kt, &FGEnvironment::set_wind_speed_kt);
128 fgTie("/environment/wind-from-north-fps", _environment,
129 &FGEnvironment::get_wind_from_north_fps,
130 &FGEnvironment::set_wind_from_north_fps);
131 fgSetArchivable("/environment/wind-from-north-fps");
132 fgTie("/environment/wind-from-east-fps", _environment,
133 &FGEnvironment::get_wind_from_east_fps,
134 &FGEnvironment::set_wind_from_east_fps);
135 fgSetArchivable("/environment/wind-from-east-fps");
136 fgTie("/environment/wind-from-down-fps", _environment,
137 &FGEnvironment::get_wind_from_down_fps,
138 &FGEnvironment::set_wind_from_down_fps);
139 fgSetArchivable("/environment/wind-from-down-fps");
140 fgTie("/environment/turbulence/magnitude-norm", _environment,
141 &FGEnvironment::get_turbulence_magnitude_norm,
142 &FGEnvironment::set_turbulence_magnitude_norm);
143 fgSetArchivable("/environment/turbulence/magnitude-norm");
144 fgTie("/environment/turbulence/rate-hz", _environment,
145 &FGEnvironment::get_turbulence_rate_hz,
146 &FGEnvironment::set_turbulence_rate_hz);
147 fgSetArchivable("/environment/turbulence/rate-hz");
149 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
151 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
153 &FGEnvironmentMgr::get_cloud_layer_span_m,
154 &FGEnvironmentMgr::set_cloud_layer_span_m);
155 fgSetArchivable(buf);
156 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
158 &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
159 &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
160 fgSetArchivable(buf);
161 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
163 &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
164 &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
165 fgSetArchivable(buf);
166 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
168 &FGEnvironmentMgr::get_cloud_layer_transition_ft,
169 &FGEnvironmentMgr::set_cloud_layer_transition_ft);
170 fgSetArchivable(buf);
171 sprintf(buf, "/environment/clouds/layer[%d]/coverage", i);
173 &FGEnvironmentMgr::get_cloud_layer_coverage,
174 &FGEnvironmentMgr::set_cloud_layer_coverage);
175 fgSetArchivable(buf);
177 fgTie("/sim/rendering/clouds3d-enable", &sgEnviro,
178 &SGEnviro::get_clouds_enable_state,
179 &SGEnviro::set_clouds_enable_state);
180 fgTie("/sim/rendering/clouds3d-vis-range", &sgEnviro,
181 &SGEnviro::get_clouds_visibility,
182 &SGEnviro::set_clouds_visibility);
183 fgTie("/sim/rendering/clouds3d-density", &sgEnviro,
184 &SGEnviro::get_clouds_density,
185 &SGEnviro::set_clouds_density);
186 fgTie("/sim/rendering/clouds3d-cache-size", &sgEnviro,
187 &SGEnviro::get_clouds_CacheSize,
188 &SGEnviro::set_clouds_CacheSize);
189 fgTie("/sim/rendering/clouds3d-cache-resolution", &sgEnviro,
190 &SGEnviro::get_CacheResolution,
191 &SGEnviro::set_CacheResolution);
192 fgTie("/sim/rendering/precipitation-enable", &sgEnviro,
193 &SGEnviro::get_precipitation_enable_state,
194 &SGEnviro::set_precipitation_enable_state);
195 fgTie("/environment/rebuild-layers", fgClouds,
196 &FGClouds::get_update_event,
197 &FGClouds::set_update_event);
198 fgTie("/sim/rendering/lightning-enable", &sgEnviro,
199 &SGEnviro::get_lightning_enable_state,
200 &SGEnviro::set_lightning_enable_state);
201 fgTie("/environment/turbulence/use-cloud-turbulence", &sgEnviro,
202 &SGEnviro::get_turbulence_enable_state,
203 &SGEnviro::set_turbulence_enable_state);
204 sgEnviro.config(fgGetNode("/sim/rendering/precipitation"));
208 FGEnvironmentMgr::unbind ()
210 fgUntie("/environment/visibility-m");
211 fgUntie("/environment/effective-visibility-m");
212 fgUntie("/environment/temperature-sea-level-degc");
213 fgUntie("/environment/temperature-degc");
214 fgUntie("/environment/dewpoint-sea-level-degc");
215 fgUntie("/environment/dewpoint-degc");
216 fgUntie("/environment/pressure-sea-level-inhg");
217 fgUntie("/environment/pressure-inhg");
218 fgUntie("/environment/density-inhg");
219 fgUntie("/environment/relative-humidity");
220 fgUntie("/environment/atmosphere/density-tropo-avg");
221 fgUntie("/environment/wind-from-north-fps");
222 fgUntie("/environment/wind-from-east-fps");
223 fgUntie("/environment/wind-from-down-fps");
224 fgUntie("/environment/atmosphere/altitude-half-to-sun");
225 fgUntie("/environment/atmosphere/altitude-troposphere-top");
226 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
228 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
230 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
232 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
234 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
236 sprintf(buf, "/environment/clouds/layer[%d]/type", i);
239 fgUntie("/sim/rendering/clouds3d-enable");
240 fgUntie("/sim/rendering/clouds3d-vis-range");
241 fgUntie("/sim/rendering/clouds3d-density");
242 fgUntie("/sim/rendering/clouds3d-cache-size");
243 fgUntie("/sim/rendering/clouds3d-cache-resolution");
244 fgUntie("/sim/rendering/precipitation-enable");
245 fgUntie("/environment/rebuild-layers");
246 fgUntie("/sim/rendering/lightning-enable");
247 fgUntie("/environment/turbulence/use-cloud-turbulence");
251 FGEnvironmentMgr::update (double dt)
253 SGSubsystemGroup::update(dt);
255 // FIXME: the FDMs should update themselves
256 current_aircraft.fdm_state
257 ->set_Velocities_Local_Airmass(_environment->get_wind_from_north_fps(),
258 _environment->get_wind_from_east_fps(),
259 _environment->get_wind_from_down_fps());
260 _environment->set_elevation_ft(fgGetDouble("/position/altitude-ft"));
266 FGEnvironmentMgr::getEnvironment () const
268 return *_environment;
272 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
274 // Always returns the same environment
275 // for now; we'll make it interesting
277 FGEnvironment env = *_environment;
278 env.set_elevation_ft(alt);
283 FGEnvironmentMgr::_update_fdm () const
286 // Pass atmosphere on to FDM
287 // FIXME: have FDMs read properties directly.
289 if (fgGetBool("/environment/params/control-fdm-atmosphere")) {
290 // convert from Rankine to Celsius
292 ->set_Static_temperature((9.0/5.0)
293 * (_environment->get_temperature_degc() + 273.15));
294 // convert from inHG to PSF
296 ->set_Static_pressure(_environment->get_pressure_inhg() * 70.726566);
297 // keep in slugs/ft^3
299 ->set_Density(_environment->get_density_slugft3());
304 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
306 return thesky->get_cloud_layer(index)->getSpan_m();
310 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
312 thesky->get_cloud_layer(index)->setSpan_m(span_m);
316 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
318 return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
322 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
324 FGEnvironment env = *_environment;
325 env.set_elevation_ft(elevation_ft);
327 thesky->get_cloud_layer(index)
328 ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
330 thesky->get_cloud_layer(index)
331 ->setSpeed(env.get_wind_speed_kt() * 0.5151); // 1 kt = 0.5151 m/s
333 thesky->get_cloud_layer(index)
334 ->setDirection(env.get_wind_from_heading_deg());
338 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
340 return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
344 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
346 thesky->get_cloud_layer(index)
347 ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
351 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
353 return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
357 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
358 double transition_ft)
360 thesky->get_cloud_layer(index)
361 ->setTransition_m(transition_ft * SG_FEET_TO_METER);
365 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
367 switch (thesky->get_cloud_layer(index)->getCoverage()) {
368 case SGCloudLayer::SG_CLOUD_OVERCAST:
370 case SGCloudLayer::SG_CLOUD_BROKEN:
372 case SGCloudLayer::SG_CLOUD_SCATTERED:
374 case SGCloudLayer::SG_CLOUD_FEW:
376 case SGCloudLayer::SG_CLOUD_CIRRUS:
378 case SGCloudLayer::SG_CLOUD_CLEAR:
386 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
387 const char * coverage_name)
389 SGCloudLayer::Coverage coverage;
390 if (!strcmp(coverage_name, "overcast"))
391 coverage = SGCloudLayer::SG_CLOUD_OVERCAST;
392 else if (!strcmp(coverage_name, "broken"))
393 coverage = SGCloudLayer::SG_CLOUD_BROKEN;
394 else if (!strcmp(coverage_name, "scattered"))
395 coverage = SGCloudLayer::SG_CLOUD_SCATTERED;
396 else if (!strcmp(coverage_name, "few"))
397 coverage = SGCloudLayer::SG_CLOUD_FEW;
398 else if (!strcmp(coverage_name, "cirrus"))
399 coverage = SGCloudLayer::SG_CLOUD_CIRRUS;
400 else if (!strcmp(coverage_name, "clear"))
401 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
403 SG_LOG(SG_INPUT, SG_WARN, "Unknown cloud type " << coverage_name);
404 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
406 thesky->get_cloud_layer(index)->setCoverage(coverage);
411 // end of environment-mgr.cxx