1 // environment.cxx -- routines to model the natural environment
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.
37 #include <simgear/debug/logstream.hxx>
38 #include <simgear/math/sg_random.h>
40 #include <Main/fg_props.hxx>
41 #include <Aircraft/aircraft.hxx>
42 #include "environment.hxx"
45 // This is a record containing current environment info
46 FGEnvironment current_environment;
49 FGEnvironment::FGEnvironment()
50 : visibility_m(32000),
51 wind_from_heading_deg(0),
53 wind_from_north_fps(0),
54 wind_from_east_fps(0),
55 wind_from_down_fps(0),
62 FGEnvironment::~FGEnvironment()
67 // Initialize the environment modeling subsystem
68 void FGEnvironment::init ()
70 SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
75 FGEnvironment::bind ()
77 fgTie("/environment/visibility-m", this,
78 &FGEnvironment::get_visibility_m, &FGEnvironment::set_visibility_m);
79 fgSetArchivable("/environment/visibility-m");
80 fgTie("/environment/wind-from-heading-deg", this,
81 &FGEnvironment::get_wind_from_heading_deg,
82 &FGEnvironment::set_wind_from_heading_deg);
83 fgTie("/environment/wind-speed-kt", this,
84 &FGEnvironment::get_wind_speed_kt, &FGEnvironment::set_wind_speed_kt);
85 fgTie("/environment/wind-from-north-fps", this,
86 &FGEnvironment::get_wind_from_north_fps,
87 &FGEnvironment::set_wind_from_north_fps);
88 fgSetArchivable("/environment/wind-from-north-fps");
89 fgTie("/environment/wind-from-east-fps", this,
90 &FGEnvironment::get_wind_from_east_fps,
91 &FGEnvironment::set_wind_from_east_fps);
92 fgSetArchivable("/environment/wind-from-east-fps");
93 fgTie("/environment/wind-from-down-fps", this,
94 &FGEnvironment::get_wind_from_down_fps,
95 &FGEnvironment::set_wind_from_down_fps);
96 fgSetArchivable("/environment/wind-from-down-fps");
100 FGEnvironment::unbind ()
102 fgUntie("/environment/visibility-m");
103 fgUntie("/environment/wind-from-heading-deg");
104 fgUntie("/environment/wind-speed-kt");
105 fgUntie("/environment/wind-from-north-fps");
106 fgUntie("/environment/wind-from-east-fps");
107 fgUntie("/environment/wind-from-down-fps");
110 void FGEnvironment::update (int dt)
112 // FIXME: the FDMs should update themselves
113 current_aircraft.fdm_state
114 ->set_Velocities_Local_Airmass(wind_from_north_fps,
120 FGEnvironment::set_visibility_m (double v)
122 glMatrixMode(GL_MODELVIEW);
127 fog_exp_density = -log(0.01 / visibility_m);
130 fog_exp2_density = sqrt( -log(0.01) ) / visibility_m;
132 // Set correct opengl fog density
133 glFogf (GL_FOG_DENSITY, fog_exp2_density);
134 glFogi( GL_FOG_MODE, GL_EXP2 );
136 // SG_LOG( SG_INPUT, SG_DEBUG, "Fog density = " << fog_density );
137 // SG_LOG( SG_INPUT, SG_INFO,
138 // "Fog exp2 density = " << fog_exp2_density );
142 FGEnvironment::set_wind_from_heading_deg (double h)
144 wind_from_heading_deg = h;
149 FGEnvironment::set_wind_speed_kt (double s)
156 FGEnvironment::set_wind_from_north_fps (double n)
158 wind_from_north_fps = n;
163 FGEnvironment::set_wind_from_east_fps (double e)
165 wind_from_east_fps = e;
170 FGEnvironment::set_wind_from_down_fps (double d)
172 wind_from_down_fps = d;
177 FGEnvironment::_recalc_hdgspd ()
181 if (wind_from_east_fps == 0) {
182 angle_rad = (wind_from_north_fps >= 0 ? SGD_PI/2 : -SGD_PI/2);
184 angle_rad = atan(wind_from_north_fps/wind_from_east_fps);
186 wind_from_heading_deg = angle_rad * SGD_RADIANS_TO_DEGREES;
187 if (wind_from_east_fps >= 0)
188 wind_from_heading_deg = 90 - wind_from_heading_deg;
190 wind_from_heading_deg = 270 - wind_from_heading_deg;
192 wind_speed_kt = fabs(wind_from_east_fps
193 * SG_METER_TO_NM * SG_FEET_TO_METER * 3600);
195 wind_speed_kt = (wind_from_north_fps / sin(angle_rad))
196 * SG_METER_TO_NM * SG_FEET_TO_METER * 3600;
200 FGEnvironment::_recalc_ne ()
203 wind_speed_kt * SG_NM_TO_METER * SG_METER_TO_FEET * (1.0/3600);
205 wind_from_north_fps = speed_fps *
206 cos(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
207 wind_from_east_fps = speed_fps *
208 sin(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
211 // end of environment.cxx