]> git.mxchange.org Git - flightgear.git/blob - src/WeatherCM/FGLocalWeatherDatabase.h
b9a34bb4dce1e34003e5569fbefcdab07c899cdf
[flightgear.git] / src / WeatherCM / FGLocalWeatherDatabase.h
1 /*****************************************************************************
2
3  Header:       FGLocalWeatherDatabase.h 
4  Author:       Christian Mayer
5  Date started: 28.05.99
6
7  -------- Copyright (C) 1999 Christian Mayer (fgfs@christianmayer.de) --------
8
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  details.
18
19  You should have received a copy of the GNU General Public License along with
20  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21  Place - Suite 330, Boston, MA  02111-1307, USA.
22
23  Further information about the GNU General Public License can also be found on
24  the world wide web at http://www.gnu.org.
25
26 FUNCTIONAL DESCRIPTION
27 ------------------------------------------------------------------------------
28 Database for the local weather
29 This database is the only one that gets called from FG
30
31 HISTORY
32 ------------------------------------------------------------------------------
33 28.05.1999 Christian Mayer      Created
34 16.06.1999 Durk Talsma          Portability for Linux
35 20.06.1999 Christian Mayer      added lots of consts
36 30.06.1999 Christian Mayer      STL portability
37 11.10.1999 Christian Mayer      changed set<> to map<> on Bernie Bright's 
38                                 suggestion
39 19.10.1999 Christian Mayer      change to use PLIB's sg instead of Point[2/3]D
40                                 and lots of wee code cleaning
41 *****************************************************************************/
42
43 /****************************************************************************/
44 /* SENTRY                                                                   */
45 /****************************************************************************/
46 #ifndef FGLocalWeatherDatabase_H
47 #define FGLocalWeatherDatabase_H
48
49 /****************************************************************************/
50 /* INCLUDES                                                                 */
51 /****************************************************************************/
52 //This is only here for smoother code change. In the end the WD should be clean
53 //of *any* OpenGL:
54 #ifdef HAVE_WINDOWS_H
55 #  include <windows.h>
56 #endif
57 #include <GL/glut.h>
58 #include <XGL/xgl.h>
59
60 #include <vector>
61
62 #include "sg.h"
63
64 #include "FGPhysicalProperties.h"
65 #include "FGGlobalWeatherDatabase.h"
66 #include "FGMicroWeather.h"
67 #include "FGWeatherFeature.h"
68 #include "FGWeatherDefs.h"
69 #include "FGThunderstorm.h"
70
71 /****************************************************************************/
72 /* DEFINES                                                                  */
73 /****************************************************************************/
74 FG_USING_STD(vector);
75 FG_USING_NAMESPACE(std);
76
77 /****************************************************************************/
78 /* CLASS DECLARATION                                                        */
79 /****************************************************************************/
80 class FGLocalWeatherDatabase
81 {
82 private:
83 protected:
84     FGGlobalWeatherDatabase *global;    //point to the global database
85
86     typedef vector<FGMicroWeather>       FGMicroWeatherList;
87     typedef FGMicroWeatherList::iterator FGMicroWeatherListIt;
88
89     typedef vector<sgVec2>      pointVector;
90     typedef vector<pointVector> tileVector;
91
92     /************************************************************************/
93     /* make tiles out of points on a 2D plane                               */
94     /************************************************************************/
95     void tileLocalWeather(const FGPhysicalProperties2DVector& EntryList);
96
97     FGMicroWeatherList WeatherAreas;
98
99     WeatherPrecision WeatherVisibility; //how far do I need to simulate the
100                                         //local weather? Unit: metres
101     sgVec3 last_known_position;
102
103     bool Thunderstorm;                  //is there a thunderstorm near by?
104     FGThunderstorm *theThunderstorm;    //pointer to the thunderstorm.
105
106     /************************************************************************/
107     /* return the index of the area with point p                            */
108     /************************************************************************/
109     unsigned int AreaWith(const sgVec2& p) const;
110     unsigned int AreaWith(const sgVec3& p) const
111     {
112         sgVec2 temp;
113         sgSetVec2(temp, p[0], p[1]);
114
115         return AreaWith(temp);
116     }
117
118 public:
119     static FGLocalWeatherDatabase *theFGLocalWeatherDatabase;  
120     
121     enum DatabaseWorkingType {
122         use_global,     //use global database for data
123         use_internet,   //use the weather data that came from the internet
124         manual,         //use only user inputs
125         distant,        //use distant information, e.g. like LAN when used in
126                         //a multiplayer environment
127         random,         //generate weather randomly
128         default_mode    //use only default values
129     };
130
131     DatabaseWorkingType DatabaseStatus;
132
133     void init( const WeatherPrecision visibility, const DatabaseWorkingType type );
134
135     /************************************************************************/
136     /* Constructor and Destructor                                           */
137     /************************************************************************/
138     FGLocalWeatherDatabase(
139         const sgVec3&             position,
140         const WeatherPrecision    visibility = DEFAULT_WEATHER_VISIBILITY,
141         const DatabaseWorkingType type       = PREFERED_WORKING_TYPE)
142     {
143         sgCopyVec3( last_known_position, position );
144
145         init( visibility, type );
146
147         theFGLocalWeatherDatabase = this;
148     }
149
150     FGLocalWeatherDatabase(
151         const WeatherPrecision    position_lat,
152         const WeatherPrecision    position_lon,
153         const WeatherPrecision    position_alt,
154         const WeatherPrecision    visibility = DEFAULT_WEATHER_VISIBILITY,
155         const DatabaseWorkingType type       = PREFERED_WORKING_TYPE)
156     {
157         sgSetVec3( last_known_position, position_lat, position_lon, position_alt );
158
159         init( visibility, type );
160
161         theFGLocalWeatherDatabase = this;
162     }
163
164     ~FGLocalWeatherDatabase();
165
166     /************************************************************************/
167     /* reset the whole database                                             */
168     /************************************************************************/
169     void reset(const DatabaseWorkingType type = PREFERED_WORKING_TYPE);
170
171     /************************************************************************/
172     /* update the database. Since the last call we had dt seconds           */
173     /************************************************************************/
174     void update(const WeatherPrecision dt);                     //time has changed
175     void update(const sgVec3& p);                               //position has  changed
176     void update(const sgVec3& p, const WeatherPrecision dt);    //time and/or position has changed
177
178     /************************************************************************/
179     /* Get the physical properties on the specified point p                 */
180     /************************************************************************/
181     FGPhysicalProperties get(const sgVec2& p) const;
182     FGPhysicalProperty   get(const sgVec3& p) const;
183
184     WeatherPrecision     getAirDensity(const sgVec3& p) const;
185     
186     /************************************************************************/
187     /* Add a weather feature at the point p and surrounding area            */
188     /************************************************************************/
189
190     void addWind         (const WeatherPrecision alt, const sgVec3& x,          const sgVec2& p);
191     void addTurbulence   (const WeatherPrecision alt, const sgVec3& x,          const sgVec2& p);
192     void addTemperature  (const WeatherPrecision alt, const WeatherPrecision x, const sgVec2& p);
193     void addAirPressure  (const WeatherPrecision alt, const WeatherPrecision x, const sgVec2& p);
194     void addVaporPressure(const WeatherPrecision alt, const WeatherPrecision x, const sgVec2& p);
195     void addCloud        (const WeatherPrecision alt, const FGCloudItem& x,     const sgVec2& p);
196
197     void setSnowRainIntensity   (const WeatherPrecision x, const sgVec2& p);
198     void setSnowRainType        (const SnowRainType x,     const sgVec2& p);
199     void setLightningProbability(const WeatherPrecision x, const sgVec2& p);
200
201     void addProperties(const FGPhysicalProperties2D& x);    //add a property
202     void setProperties(const FGPhysicalProperties2D& x);    //change a property
203
204     /************************************************************************/
205     /* get/set weather visibility                                           */
206     /************************************************************************/
207     void             setWeatherVisibility(const WeatherPrecision visibility);
208     WeatherPrecision getWeatherVisibility(void) const;
209
210     /************************************************************************/
211     /* figure out if there's a thunderstorm that has to be taken care of    */
212     /************************************************************************/
213     void updateThunderstorm(const float dt)
214     {
215         if (Thunderstorm == false)
216             return;
217
218         theThunderstorm->update( dt );
219     }
220 };
221
222 extern FGLocalWeatherDatabase *WeatherDatabase;
223 void fgUpdateWeatherDatabase(void);
224
225 /****************************************************************************/
226 /* get/set weather visibility                                               */
227 /****************************************************************************/
228 void inline FGLocalWeatherDatabase::setWeatherVisibility(const WeatherPrecision visibility)
229 {
230     if (visibility >= MINIMUM_WEATHER_VISIBILITY)
231         WeatherVisibility = visibility;
232     else
233         WeatherVisibility = MINIMUM_WEATHER_VISIBILITY;
234
235 #if 0
236     //This code doesn't belong here as this is the optical visibility and not
237     //the visibility of the weather database (that should be bigger...). The
238     //optical visibility should be calculated from the vapor pressure e.g.
239     //But for the sake of a smoother change from the old way to the new one...
240
241     GLfloat fog_exp_density;
242     GLfloat fog_exp2_density;
243     
244     // for GL_FOG_EXP
245     fog_exp_density = -log(0.01 / WeatherVisibility);
246     
247     // for GL_FOG_EXP2
248     fog_exp2_density = sqrt( -log(0.01) ) / WeatherVisibility;
249     
250     // Set correct opengl fog density
251     xglFogf (GL_FOG_DENSITY, fog_exp2_density);
252     
253     // FG_LOG( FG_INPUT, FG_DEBUG, "Fog density = " << w->fog_density );
254     //cerr << "FGLocalWeatherDatabase::setWeatherVisibility(" << visibility << "):\n";
255     //cerr << "Fog density = " << fog_exp_density << "\n";
256 #endif
257 }
258
259 WeatherPrecision inline FGLocalWeatherDatabase::getWeatherVisibility(void) const
260 {
261     //cerr << "FGLocalWeatherDatabase::getWeatherVisibility() = " << WeatherVisibility << "\n";
262     return WeatherVisibility;
263 }
264
265
266 /****************************************************************************/
267 #endif /*FGLocalWeatherDatabase_H*/