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., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <simgear/debug/logstream.hxx>
24 #include <simgear/scene/sky/sky.hxx>
25 #include <simgear/environment/visual_enviro.hxx>
27 #include <Main/main.hxx>
28 #include <Main/fg_props.hxx>
29 #include <Aircraft/aircraft.hxx>
31 #include "environment.hxx"
32 #include "environment_ctrl.hxx"
33 #include "environment_mgr.hxx"
34 #include "fgclouds.hxx"
41 FGEnvironmentMgr::FGEnvironmentMgr ()
42 : _environment(new FGEnvironment)
45 if (fgGetBool("/environment/params/real-world-weather-fetch") == true)
46 _controller = new FGMetarEnvironmentCtrl;
48 _controller = new FGInterpolateEnvironmentCtrl;
50 _controller->setEnvironment(_environment);
51 set_subsystem("controller", _controller, 0.5);
52 fgClouds = new FGClouds( _controller );
55 FGEnvironmentMgr::~FGEnvironmentMgr ()
63 FGEnvironmentMgr::init ()
65 SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
66 SGSubsystemGroup::init();
71 FGEnvironmentMgr::reinit ()
73 SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
74 SGSubsystemGroup::reinit();
79 FGEnvironmentMgr::bind ()
81 fgTie("/environment/visibility-m", _environment,
82 &FGEnvironment::get_visibility_m, &FGEnvironment::set_visibility_m);
83 fgSetArchivable("/environment/visibility-m");
84 fgTie("/environment/temperature-sea-level-degc", _environment,
85 &FGEnvironment::get_temperature_sea_level_degc,
86 &FGEnvironment::set_temperature_sea_level_degc);
87 fgSetArchivable("/environment/temperature-sea-level-degc");
88 fgTie("/environment/temperature-degc", _environment,
89 &FGEnvironment::get_temperature_degc); // FIXME: read-only for now
90 fgTie("/environment/temperature-degf", _environment,
91 &FGEnvironment::get_temperature_degf); // FIXME: read-only for now
92 fgTie("/environment/dewpoint-sea-level-degc", _environment,
93 &FGEnvironment::get_dewpoint_sea_level_degc,
94 &FGEnvironment::set_dewpoint_sea_level_degc);
95 fgSetArchivable("/environment/dewpoint-sea-level-degc");
96 fgTie("/environment/dewpoint-degc", _environment,
97 &FGEnvironment::get_dewpoint_degc); // FIXME: read-only for now
98 fgTie("/environment/pressure-sea-level-inhg", _environment,
99 &FGEnvironment::get_pressure_sea_level_inhg,
100 &FGEnvironment::set_pressure_sea_level_inhg);
101 fgSetArchivable("/environment/pressure-sea-level-inhg");
102 fgTie("/environment/pressure-inhg", _environment,
103 &FGEnvironment::get_pressure_inhg); // FIXME: read-only for now
104 fgTie("/environment/density-slugft3", _environment,
105 &FGEnvironment::get_density_slugft3); // read-only
106 fgTie("/environment/wind-from-heading-deg", _environment,
107 &FGEnvironment::get_wind_from_heading_deg,
108 &FGEnvironment::set_wind_from_heading_deg);
109 fgTie("/environment/wind-speed-kt", _environment,
110 &FGEnvironment::get_wind_speed_kt, &FGEnvironment::set_wind_speed_kt);
111 fgTie("/environment/wind-from-north-fps", _environment,
112 &FGEnvironment::get_wind_from_north_fps,
113 &FGEnvironment::set_wind_from_north_fps);
114 fgSetArchivable("/environment/wind-from-north-fps");
115 fgTie("/environment/wind-from-east-fps", _environment,
116 &FGEnvironment::get_wind_from_east_fps,
117 &FGEnvironment::set_wind_from_east_fps);
118 fgSetArchivable("/environment/wind-from-east-fps");
119 fgTie("/environment/wind-from-down-fps", _environment,
120 &FGEnvironment::get_wind_from_down_fps,
121 &FGEnvironment::set_wind_from_down_fps);
122 fgSetArchivable("/environment/wind-from-down-fps");
123 fgTie("/environment/turbulence/magnitude-norm", _environment,
124 &FGEnvironment::get_turbulence_magnitude_norm,
125 &FGEnvironment::set_turbulence_magnitude_norm);
126 fgSetArchivable("/environment/turbulence/magnitude-norm");
127 fgTie("/environment/turbulence/rate-hz", _environment,
128 &FGEnvironment::get_turbulence_rate_hz,
129 &FGEnvironment::set_turbulence_rate_hz);
130 fgSetArchivable("/environment/turbulence/rate-hz");
132 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
134 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
136 &FGEnvironmentMgr::get_cloud_layer_span_m,
137 &FGEnvironmentMgr::set_cloud_layer_span_m);
138 fgSetArchivable(buf);
139 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
141 &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
142 &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
143 fgSetArchivable(buf);
144 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
146 &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
147 &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
148 fgSetArchivable(buf);
149 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
151 &FGEnvironmentMgr::get_cloud_layer_transition_ft,
152 &FGEnvironmentMgr::set_cloud_layer_transition_ft);
153 fgSetArchivable(buf);
154 sprintf(buf, "/environment/clouds/layer[%d]/coverage", i);
156 &FGEnvironmentMgr::get_cloud_layer_coverage,
157 &FGEnvironmentMgr::set_cloud_layer_coverage);
158 fgSetArchivable(buf);
160 fgTie("/sim/rendering/clouds3d-enable", &sgEnviro,
161 &SGEnviro::get_clouds_enable_state,
162 &SGEnviro::set_clouds_enable_state);
163 fgTie("/sim/rendering/clouds3d-vis-range", &sgEnviro,
164 &SGEnviro::get_clouds_visibility,
165 &SGEnviro::set_clouds_visibility);
166 fgTie("/sim/rendering/clouds3d-density", &sgEnviro,
167 &SGEnviro::get_clouds_density,
168 &SGEnviro::set_clouds_density);
169 fgTie("/sim/rendering/clouds3d-cache-size", &sgEnviro,
170 &SGEnviro::get_clouds_CacheSize,
171 &SGEnviro::set_clouds_CacheSize);
172 fgTie("/sim/rendering/clouds3d-cache-resolution", &sgEnviro,
173 &SGEnviro::get_CacheResolution,
174 &SGEnviro::set_CacheResolution);
175 fgTie("/sim/rendering/precipitation-enable", &sgEnviro,
176 &SGEnviro::get_precipitation_enable_state,
177 &SGEnviro::set_precipitation_enable_state);
178 fgTie("/environment/rebuild_layers", fgClouds,
179 &FGClouds::get_update_event,
180 &FGClouds::set_update_event);
181 fgTie("/sim/rendering/lightning-enable", &sgEnviro,
182 &SGEnviro::get_lightning_enable_state,
183 &SGEnviro::set_lightning_enable_state);
184 fgTie("/environment/turbulence/use-cloud-turbulence", &sgEnviro,
185 &SGEnviro::get_turbulence_enable_state,
186 &SGEnviro::set_turbulence_enable_state);
190 FGEnvironmentMgr::unbind ()
192 fgUntie("/environment/visibility-m");
193 fgUntie("/environment/temperature-sea-level-degc");
194 fgUntie("/environment/temperature-degc");
195 fgUntie("/environment/dewpoint-sea-level-degc");
196 fgUntie("/environment/dewpoint-degc");
197 fgUntie("/environment/pressure-sea-level-inhg");
198 fgUntie("/environment/pressure-inhg");
199 fgUntie("/environment/density-inhg");
200 fgUntie("/environment/wind-from-north-fps");
201 fgUntie("/environment/wind-from-east-fps");
202 fgUntie("/environment/wind-from-down-fps");
203 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
205 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
207 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
209 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
211 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
213 sprintf(buf, "/environment/clouds/layer[%d]/type", i);
216 fgUntie("/sim/rendering/clouds3d-enable");
217 fgUntie("/sim/rendering/clouds3d-vis-range");
218 fgUntie("/sim/rendering/clouds3d-density");
219 fgUntie("/sim/rendering/clouds3d-cache-size");
220 fgUntie("/sim/rendering/clouds3d-cache-resolution");
221 fgUntie("/sim/rendering/precipitation-enable");
222 fgUntie("/environment/rebuild_layers");
223 fgUntie("/sim/rendering/lightning-enable");
224 fgUntie("/environment/turbulence/use-cloud-turbulence");
228 FGEnvironmentMgr::update (double dt)
230 SGSubsystemGroup::update(dt);
232 // FIXME: the FDMs should update themselves
233 current_aircraft.fdm_state
234 ->set_Velocities_Local_Airmass(_environment->get_wind_from_north_fps(),
235 _environment->get_wind_from_east_fps(),
236 _environment->get_wind_from_down_fps());
237 _environment->set_elevation_ft(fgGetDouble("/position/altitude-ft"));
243 FGEnvironmentMgr::getEnvironment () const
245 return *_environment;
249 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
251 // Always returns the same environment
252 // for now; we'll make it interesting
254 FGEnvironment env = *_environment;
255 env.set_elevation_ft(alt);
260 FGEnvironmentMgr::_update_fdm () const
263 // Pass atmosphere on to FDM
264 // FIXME: have FDMs read properties directly.
266 if (fgGetBool("/environment/params/control-fdm-atmosphere")) {
267 // convert from Rankine to Celsius
269 ->set_Static_temperature((9.0/5.0)
270 * (_environment->get_temperature_degc() + 273.15));
271 // convert from inHG to PSF
273 ->set_Static_pressure(_environment->get_pressure_inhg() * 70.726566);
274 // keep in slugs/ft^3
276 ->set_Density(_environment->get_density_slugft3());
281 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
283 return thesky->get_cloud_layer(index)->getSpan_m();
287 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
289 thesky->get_cloud_layer(index)->setSpan_m(span_m);
293 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
295 return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
299 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
301 FGEnvironment env = *_environment;
302 env.set_elevation_ft(elevation_ft);
304 thesky->get_cloud_layer(index)
305 ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
307 thesky->get_cloud_layer(index)
308 ->setSpeed(env.get_wind_speed_kt() * 0.5151); // 1 kt = 0.5151 m/s
310 thesky->get_cloud_layer(index)
311 ->setDirection(env.get_wind_from_heading_deg());
315 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
317 return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
321 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
323 thesky->get_cloud_layer(index)
324 ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
328 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
330 return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
334 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
335 double transition_ft)
337 thesky->get_cloud_layer(index)
338 ->setTransition_m(transition_ft * SG_FEET_TO_METER);
342 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
344 switch (thesky->get_cloud_layer(index)->getCoverage()) {
345 case SGCloudLayer::SG_CLOUD_OVERCAST:
347 case SGCloudLayer::SG_CLOUD_BROKEN:
349 case SGCloudLayer::SG_CLOUD_SCATTERED:
351 case SGCloudLayer::SG_CLOUD_FEW:
353 case SGCloudLayer::SG_CLOUD_CIRRUS:
355 case SGCloudLayer::SG_CLOUD_CLEAR:
363 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
364 const char * coverage_name)
366 SGCloudLayer::Coverage coverage;
367 if (!strcmp(coverage_name, "overcast"))
368 coverage = SGCloudLayer::SG_CLOUD_OVERCAST;
369 else if (!strcmp(coverage_name, "broken"))
370 coverage = SGCloudLayer::SG_CLOUD_BROKEN;
371 else if (!strcmp(coverage_name, "scattered"))
372 coverage = SGCloudLayer::SG_CLOUD_SCATTERED;
373 else if (!strcmp(coverage_name, "few"))
374 coverage = SGCloudLayer::SG_CLOUD_FEW;
375 else if (!strcmp(coverage_name, "cirrus"))
376 coverage = SGCloudLayer::SG_CLOUD_CIRRUS;
377 else if (!strcmp(coverage_name, "clear"))
378 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
380 SG_LOG(SG_INPUT, SG_WARN, "Unknown cloud type " << coverage_name);
381 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
383 thesky->get_cloud_layer(index)->setCoverage(coverage);
388 // end of environment-mgr.cxx