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/constants.h>
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/scene/sky/sky.hxx>
30 #include <simgear/scene/model/particles.hxx>
31 #include <simgear/structure/event_mgr.hxx>
33 #include <Main/main.hxx>
34 #include <Main/fg_props.hxx>
35 #include <Viewer/renderer.hxx>
36 #include <FDM/flight.hxx>
38 #include "environment.hxx"
39 #include "environment_mgr.hxx"
40 #include "environment_ctrl.hxx"
41 #include "realwx_ctrl.hxx"
42 #include "fgclouds.hxx"
43 #include "precipitation_mgr.hxx"
44 #include "ridge_lift.hxx"
45 #include "terrainsampler.hxx"
46 #include "Airports/airport.hxx"
47 #include "gravity.hxx"
48 #include "magvarmanager.hxx"
50 class FG3DCloudsListener : public SGPropertyChangeListener {
52 FG3DCloudsListener( FGClouds * fgClouds );
53 virtual ~FG3DCloudsListener();
55 virtual void valueChanged (SGPropertyNode * node);
59 SGPropertyNode_ptr _enableNode;
62 FG3DCloudsListener::FG3DCloudsListener( FGClouds * fgClouds ) :
65 _enableNode = fgGetNode( "/sim/rendering/clouds3d-enable", true );
66 _enableNode->addChangeListener( this );
68 valueChanged( _enableNode );
71 FG3DCloudsListener::~FG3DCloudsListener()
73 _enableNode->removeChangeListener( this );
76 void FG3DCloudsListener::valueChanged( SGPropertyNode * node )
78 _fgClouds->set_3dClouds( _enableNode->getBoolValue() );
81 FGEnvironmentMgr::FGEnvironmentMgr () :
82 _environment(new FGEnvironment()),
83 fgClouds(new FGClouds()),
84 _cloudLayersDirty(true),
85 _3dCloudsEnableListener(new FG3DCloudsListener(fgClouds) ),
86 _sky(globals->get_renderer()->getSky())
88 set_subsystem("controller", Environment::LayerInterpolateController::createInstance( fgGetNode("/environment/config", true ) ));
89 set_subsystem("realwx", Environment::RealWxController::createInstance( fgGetNode("/environment/realwx", true ) ), 1.0 );
91 set_subsystem("precipitation", new FGPrecipitationMgr);
92 set_subsystem("terrainsampler", Environment::TerrainSampler::createInstance( fgGetNode("/environment/terrain", true ) ));
93 set_subsystem("ridgelift", new FGRidgeLift);
95 set_subsystem("magvar", new FGMagVarManager);
98 FGEnvironmentMgr::~FGEnvironmentMgr ()
100 remove_subsystem( "ridgelift" );
101 remove_subsystem( "terrainsampler" );
102 remove_subsystem("precipitation");
103 remove_subsystem("realwx");
104 remove_subsystem("controller");
105 remove_subsystem("magvar");
110 delete _3dCloudsEnableListener;
113 SGSubsystem::InitStatus FGEnvironmentMgr::incrementalInit()
116 InitStatus r = SGSubsystemGroup::incrementalInit();
117 if (r == INIT_DONE) {
119 globals->get_event_mgr()->addTask("updateClosestAirport", this,
120 &FGEnvironmentMgr::updateClosestAirport, 30 );
127 FGEnvironmentMgr::shutdown()
129 globals->get_event_mgr()->removeTask("updateClosestAirport");
130 SGSubsystemGroup::shutdown();
134 FGEnvironmentMgr::reinit ()
136 SG_LOG( SG_ENVIRONMENT, SG_INFO, "Reinitializing environment subsystem");
137 SGSubsystemGroup::reinit();
141 FGEnvironmentMgr::bind ()
143 SGSubsystemGroup::bind();
144 _environment->Tie( fgGetNode("/environment", true ) );
146 _tiedProperties.setRoot( fgGetNode( "/environment", true ) );
148 _tiedProperties.Tie( "effective-visibility-m", _sky,
149 &SGSky::get_visibility );
151 _tiedProperties.Tie("rebuild-layers", fgClouds,
152 &FGClouds::get_update_event,
153 &FGClouds::set_update_event);
155 // _tiedProperties.Tie("turbulence/use-cloud-turbulence", &sgEnviro,
156 // &SGEnviro::get_turbulence_enable_state,
157 // &SGEnviro::set_turbulence_enable_state);
159 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
160 SGPropertyNode_ptr layerNode = fgGetNode("/environment/clouds",true)->getChild("layer", i, true );
162 _tiedProperties.Tie( layerNode->getNode("span-m",true), this, i,
163 &FGEnvironmentMgr::get_cloud_layer_span_m,
164 &FGEnvironmentMgr::set_cloud_layer_span_m);
166 _tiedProperties.Tie( layerNode->getNode("elevation-ft",true), this, i,
167 &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
168 &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
170 _tiedProperties.Tie( layerNode->getNode("thickness-ft",true), this, i,
171 &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
172 &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
174 _tiedProperties.Tie( layerNode->getNode("transition-ft",true), this, i,
175 &FGEnvironmentMgr::get_cloud_layer_transition_ft,
176 &FGEnvironmentMgr::set_cloud_layer_transition_ft);
178 _tiedProperties.Tie( layerNode->getNode("coverage",true), this, i,
179 &FGEnvironmentMgr::get_cloud_layer_coverage,
180 &FGEnvironmentMgr::set_cloud_layer_coverage);
182 _tiedProperties.Tie( layerNode->getNode("coverage-type",true), this, i,
183 &FGEnvironmentMgr::get_cloud_layer_coverage_type,
184 &FGEnvironmentMgr::set_cloud_layer_coverage_type);
186 _tiedProperties.Tie( layerNode->getNode( "visibility-m",true), this, i,
187 &FGEnvironmentMgr::get_cloud_layer_visibility_m,
188 &FGEnvironmentMgr::set_cloud_layer_visibility_m);
190 _tiedProperties.Tie( layerNode->getNode( "alpha",true), this, i,
191 &FGEnvironmentMgr::get_cloud_layer_maxalpha,
192 &FGEnvironmentMgr::set_cloud_layer_maxalpha);
195 _tiedProperties.setRoot( fgGetNode("/sim/rendering", true ) );
197 _tiedProperties.Tie( "clouds3d-density", _sky,
198 &SGSky::get_3dCloudDensity,
199 &SGSky::set_3dCloudDensity);
201 _tiedProperties.Tie("clouds3d-vis-range", _sky,
202 &SGSky::get_3dCloudVisRange,
203 &SGSky::set_3dCloudVisRange);
205 _tiedProperties.Tie("clouds3d-impostor-range", _sky,
206 &SGSky::get_3dCloudImpostorDistance,
207 &SGSky::set_3dCloudImpostorDistance);
209 _tiedProperties.Tie("clouds3d-lod1-range", _sky,
210 &SGSky::get_3dCloudLoD1Range,
211 &SGSky::set_3dCloudLoD1Range);
213 _tiedProperties.Tie("clouds3d-lod2-range", _sky,
214 &SGSky::get_3dCloudLoD2Range,
215 &SGSky::set_3dCloudLoD2Range);
217 _tiedProperties.Tie("clouds3d-wrap", _sky,
218 &SGSky::get_3dCloudWrap,
219 &SGSky::set_3dCloudWrap);
221 _tiedProperties.Tie("clouds3d-use-impostors", _sky,
222 &SGSky::get_3dCloudUseImpostors,
223 &SGSky::set_3dCloudUseImpostors);
228 FGEnvironmentMgr::unbind ()
230 _tiedProperties.Untie();
231 _environment->Untie();
232 SGSubsystemGroup::unbind();
236 FGEnvironmentMgr::update (double dt)
238 SGSubsystemGroup::update(dt);
240 SGGeod aircraftPos(globals->get_aircraft_position());
241 _environment->set_elevation_ft( aircraftPos.getElevationFt() );
243 simgear::Particles::setWindFrom( _environment->get_wind_from_heading_deg(),
244 _environment->get_wind_speed_kt() );
245 if( _cloudLayersDirty ) {
246 _cloudLayersDirty = false;
247 fgClouds->set_update_event( fgClouds->get_update_event()+1 );
251 fgSetDouble( "/environment/gravitational-acceleration-mps2",
252 Environment::Gravity::instance()->getGravity(aircraftPos));
256 FGEnvironmentMgr::updateClosestAirport()
258 SG_LOG(SG_ENVIRONMENT, SG_DEBUG, "FGEnvironmentMgr::update: updating closest airport");
260 SGGeod pos = globals->get_aircraft_position();
261 FGAirport * nearestAirport = FGAirport::findClosest(pos, 100.0);
262 if( nearestAirport == NULL )
264 SG_LOG(SG_ENVIRONMENT,SG_WARN,"FGEnvironmentMgr::update: No airport within 100NM range");
268 const string currentId = fgGetString("/sim/airport/closest-airport-id", "");
269 if (currentId != nearestAirport->ident())
271 SG_LOG(SG_ENVIRONMENT, SG_INFO, "FGEnvironmentMgr::updateClosestAirport: selected:" << nearestAirport->ident());
272 fgSetString("/sim/airport/closest-airport-id",
273 nearestAirport->ident().c_str());
280 FGEnvironmentMgr::getEnvironment () const
282 return *_environment;
286 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
288 // Always returns the same environment
289 // for now; we'll make it interesting
291 FGEnvironment env = *_environment;
292 env.set_elevation_ft(alt);
297 FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const
299 // Always returns the same environment
300 // for now; we'll make it interesting
302 FGEnvironment env = *_environment;
303 env.set_elevation_ft(aPos.getElevationFt());
309 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
311 return _sky->get_cloud_layer(index)->getSpan_m();
315 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
317 _sky->get_cloud_layer(index)->setSpan_m(span_m);
321 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
323 return _sky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
327 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
329 FGEnvironment env = *_environment;
330 env.set_elevation_ft(elevation_ft);
332 _sky->get_cloud_layer(index)
333 ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
335 _sky->get_cloud_layer(index)
336 ->setSpeed(env.get_wind_speed_kt() * 0.5151); // 1 kt = 0.5151 m/s
338 _sky->get_cloud_layer(index)
339 ->setDirection(env.get_wind_from_heading_deg());
343 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
345 return _sky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
349 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
351 _sky->get_cloud_layer(index)
352 ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
356 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
358 return _sky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
362 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
363 double transition_ft)
365 _sky->get_cloud_layer(index)
366 ->setTransition_m(transition_ft * SG_FEET_TO_METER);
370 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
372 return _sky->get_cloud_layer(index)->getCoverageString().c_str();
376 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
377 const char * coverage_name)
379 if( _sky->get_cloud_layer(index)->getCoverageString() == coverage_name )
382 _sky->get_cloud_layer(index)->setCoverageString(coverage_name);
383 _cloudLayersDirty = true;
387 FGEnvironmentMgr::get_cloud_layer_coverage_type (int index) const
389 return _sky->get_cloud_layer(index)->getCoverage();
393 FGEnvironmentMgr::get_cloud_layer_visibility_m (int index) const
395 return _sky->get_cloud_layer(index)->getVisibility_m();
399 FGEnvironmentMgr::set_cloud_layer_visibility_m (int index, double visibility_m)
401 _sky->get_cloud_layer(index)->setVisibility_m(visibility_m);
405 FGEnvironmentMgr::get_cloud_layer_maxalpha (int index ) const
407 return _sky->get_cloud_layer(index)->getMaxAlpha();
411 FGEnvironmentMgr::set_cloud_layer_maxalpha (int index, double maxalpha)
413 _sky->get_cloud_layer(index)->setMaxAlpha(maxalpha);
417 FGEnvironmentMgr::set_cloud_layer_coverage_type (int index, int type )
419 if( type < 0 || type >= SGCloudLayer::SG_MAX_CLOUD_COVERAGES ) {
420 SG_LOG(SG_ENVIRONMENT,SG_WARN,"Unknown cloud layer type " << type << " ignored" );
424 if( static_cast<SGCloudLayer::Coverage>(type) == _sky->get_cloud_layer(index)->getCoverage() )
427 _sky->get_cloud_layer(index)->setCoverage(static_cast<SGCloudLayer::Coverage>(type));
428 _cloudLayersDirty = true;
432 // end of environment-mgr.cxx