]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment.cxx
6fd317a744cf74bf8230a217c0b6c480b7425358
[flightgear.git] / src / Environment / environment.cxx
1 // environment.cxx -- routines to model the natural environment
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #ifdef HAVE_WINDOWS_H
29 #  include <windows.h>                     
30 #endif
31
32 #include <GL/glut.h>
33 #include <GL/gl.h>
34
35 #include <math.h>
36
37 #include <simgear/debug/logstream.hxx>
38 #include <simgear/math/sg_random.h>
39
40 #include <Main/fg_props.hxx>
41 #include <Aircraft/aircraft.hxx>
42 #include "environment.hxx"
43
44
45 // This is a record containing current environment info
46 FGEnvironment current_environment;
47
48
49 FGEnvironment::FGEnvironment()
50   : visibility_m(32000),
51     wind_from_heading_deg(0),
52     wind_speed_kt(0),
53     wind_from_north_fps(0),
54     wind_from_east_fps(0),
55     wind_from_down_fps(0),
56     fog_exp_density(0),
57     fog_exp2_density(0)
58 {
59 }
60
61
62 FGEnvironment::~FGEnvironment()
63 {
64 }
65
66
67 // Initialize the environment modeling subsystem
68 void FGEnvironment::init ()
69 {
70     SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
71 }
72
73
74 void
75 FGEnvironment::bind ()
76 {
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");
97 }
98
99 void
100 FGEnvironment::unbind ()
101 {
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");
108 }
109
110 void FGEnvironment::update (int dt)
111 {
112                                 // FIXME: the FDMs should update themselves
113   current_aircraft.fdm_state
114     ->set_Velocities_Local_Airmass(wind_from_north_fps,
115                                    wind_from_east_fps,
116                                    wind_from_down_fps);
117 }
118
119 void
120 FGEnvironment::set_visibility_m (double v)
121 {
122         glMatrixMode(GL_MODELVIEW);
123         // in meters
124         visibility_m = v;
125
126         // for GL_FOG_EXP
127         fog_exp_density = -log(0.01 / visibility_m);
128
129         // for GL_FOG_EXP2
130         fog_exp2_density = sqrt( -log(0.01) ) / visibility_m;
131
132         // Set correct opengl fog density
133         glFogf (GL_FOG_DENSITY, fog_exp2_density);
134         glFogi( GL_FOG_MODE, GL_EXP2 );
135
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 );
139 }
140
141 void
142 FGEnvironment::set_wind_from_heading_deg (double h)
143 {
144   wind_from_heading_deg = h;
145   _recalc_ne();
146 }
147
148 void
149 FGEnvironment::set_wind_speed_kt (double s)
150 {
151   wind_speed_kt = s;
152   _recalc_ne();
153 }
154
155 void
156 FGEnvironment::set_wind_from_north_fps (double n)
157 {
158   wind_from_north_fps = n;
159   _recalc_hdgspd();
160 }
161
162 void
163 FGEnvironment::set_wind_from_east_fps (double e)
164 {
165   wind_from_east_fps = e;
166   _recalc_hdgspd();
167 }
168
169 void
170 FGEnvironment::set_wind_from_down_fps (double d)
171 {
172   wind_from_down_fps = d;
173   _recalc_hdgspd();
174 }
175
176 void
177 FGEnvironment::_recalc_hdgspd ()
178 {
179   double angle_rad;
180
181   if (wind_from_east_fps == 0) {
182     angle_rad = (wind_from_north_fps >= 0 ? SGD_PI/2 : -SGD_PI/2);
183   } else {
184     angle_rad = atan(wind_from_north_fps/wind_from_east_fps);
185   }
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;
189   else
190     wind_from_heading_deg = 270 - wind_from_heading_deg;
191   if (angle_rad == 0)
192     wind_speed_kt = fabs(wind_from_east_fps
193                          * SG_METER_TO_NM * SG_FEET_TO_METER * 3600);
194   else
195     wind_speed_kt = (wind_from_north_fps / sin(angle_rad))
196       * SG_METER_TO_NM * SG_FEET_TO_METER * 3600;
197 }
198
199 void
200 FGEnvironment::_recalc_ne ()
201 {
202   double speed_fps =
203     wind_speed_kt * SG_NM_TO_METER * SG_METER_TO_FEET * (1.0/3600);
204
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);
209 }
210
211 // end of environment.cxx
212