]> git.mxchange.org Git - flightgear.git/blob - src/WeatherCM/FGLocalWeatherDatabase.h
dcd6c016bccc078df73deb769f5a1716569b1736
[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
70 /****************************************************************************/
71 /* DEFINES                                                                  */
72 /****************************************************************************/
73 FG_USING_STD(vector);
74 FG_USING_NAMESPACE(std);
75
76 /****************************************************************************/
77 /* CLASS DECLARATION                                                        */
78 /****************************************************************************/
79 class FGLocalWeatherDatabase
80 {
81 private:
82 protected:
83     FGGlobalWeatherDatabase *global;    //point to the global database
84
85     typedef vector<FGMicroWeather>       FGMicroWeatherList;
86     typedef FGMicroWeatherList::iterator FGMicroWeatherListIt;
87
88     typedef vector<sgVec2>      pointVector;
89     typedef vector<pointVector> tileVector;
90
91     /************************************************************************/
92     /* make tiles out of points on a 2D plane                               */
93     /************************************************************************/
94     void tileLocalWeather(const FGPhysicalProperties2DVector& EntryList);
95
96     FGMicroWeatherList WeatherAreas;
97
98     WeatherPrecision WeatherVisibility; //how far do I need to simulate the
99                                         //local weather? Unit: metres
100     sgVec3 last_known_position;
101
102
103     /************************************************************************/
104     /* return the index of the area with point p                            */
105     /************************************************************************/
106     unsigned int AreaWith(const sgVec2& p) const;
107     unsigned int AreaWith(const sgVec3& p) const
108     {
109         sgVec2 temp;
110         sgSetVec2(temp, p[0], p[1]);
111
112         return AreaWith(temp);
113     }
114
115 public:
116     static FGLocalWeatherDatabase *theFGLocalWeatherDatabase;  
117     
118     enum DatabaseWorkingType {
119         use_global,     //use global database for data
120         manual,         //use only user inputs
121         distant,        //use distant information, e.g. like LAN when used in
122                         //a multiplayer environment
123         random,         //generate weather randomly
124         default_mode    //use only default values
125     };
126
127     DatabaseWorkingType DatabaseStatus;
128
129     /************************************************************************/
130     /* Constructor and Destructor                                           */
131     /************************************************************************/
132     FGLocalWeatherDatabase(
133         const sgVec3&             posititon,
134         const WeatherPrecision    visibility = DEFAULT_WEATHER_VISIBILIY,
135         const DatabaseWorkingType type       = PREFERED_WORKING_TYPE);
136
137     FGLocalWeatherDatabase(
138         const WeatherPrecision    posititon_lat,
139         const WeatherPrecision    posititon_lon,
140         const WeatherPrecision    posititon_alt,
141         const WeatherPrecision    visibility = DEFAULT_WEATHER_VISIBILIY,
142         const DatabaseWorkingType type       = PREFERED_WORKING_TYPE)
143     {
144         sgVec3 position;
145         sgSetVec3( position, posititon_lat, posititon_lon, posititon_alt );
146
147         FGLocalWeatherDatabase( position, visibility, type );
148     }
149
150     ~FGLocalWeatherDatabase();
151
152     /************************************************************************/
153     /* reset the whole database                                             */
154     /************************************************************************/
155     void reset(const DatabaseWorkingType type = PREFERED_WORKING_TYPE);
156
157     /************************************************************************/
158     /* update the database. Since the last call we had dt seconds           */
159     /************************************************************************/
160     void update(const WeatherPrecision dt);                     //time has changed
161     void update(const sgVec3& p);                               //position has  changed
162     void update(const sgVec3& p, const WeatherPrecision dt);    //time and/or position has changed
163
164     /************************************************************************/
165     /* Get the physical properties on the specified point p                 */
166     /************************************************************************/
167     FGPhysicalProperties get(const sgVec2& p) const;
168     FGPhysicalProperty   get(const sgVec3& p) const;
169
170     WeatherPrecision     getAirDensity(const sgVec3& p) const;
171     
172     /************************************************************************/
173     /* Add a weather feature at the point p and surrounding area            */
174     /************************************************************************/
175
176     void addWind         (const WeatherPrecision alt, const sgVec3& x,          const sgVec2& p);
177     void addTurbulence   (const WeatherPrecision alt, const sgVec3& x,          const sgVec2& p);
178     void addTemperature  (const WeatherPrecision alt, const WeatherPrecision x, const sgVec2& p);
179     void addAirPressure  (const WeatherPrecision alt, const WeatherPrecision x, const sgVec2& p);
180     void addVaporPressure(const WeatherPrecision alt, const WeatherPrecision x, const sgVec2& p);
181     void addCloud        (const WeatherPrecision alt, const FGCloudItem& x,     const sgVec2& p);
182
183     void setSnowRainIntensity   (const WeatherPrecision x, const sgVec2& p);
184     void setSnowRainType        (const SnowRainType x,     const sgVec2& p);
185     void setLightningProbability(const WeatherPrecision x, const sgVec2& p);
186
187     void addProperties(const FGPhysicalProperties2D& x);    //add a property
188     void setProperties(const FGPhysicalProperties2D& x);    //change a property
189
190     /************************************************************************/
191     /* get/set weather visibility                                           */
192     /************************************************************************/
193     void             setWeatherVisibility(const WeatherPrecision visibility);
194     WeatherPrecision getWeatherVisibility(void) const;
195 };
196
197 extern FGLocalWeatherDatabase *WeatherDatabase;
198 void fgUpdateWeatherDatabase(void);
199
200 /****************************************************************************/
201 /* get/set weather visibility                                               */
202 /****************************************************************************/
203 void inline FGLocalWeatherDatabase::setWeatherVisibility(const WeatherPrecision visibility)
204 {
205     if (visibility >= MINIMUM_WEATHER_VISIBILIY)
206         WeatherVisibility = visibility;
207     else
208         WeatherVisibility = MINIMUM_WEATHER_VISIBILIY;
209
210     //This code doesn't belong here as this is the optical visibility and not
211     //the visibility of the weather database (that should be bigger...). The
212     //optical visibility should be calculated from the vapor pressure e.g.
213     //But for the sake of a smoother change from the old way to the new one...
214
215     GLfloat fog_exp_density;
216     GLfloat fog_exp2_density;
217     
218     // for GL_FOG_EXP
219     fog_exp_density = -log(0.01 / WeatherVisibility);
220     
221     // for GL_FOG_EXP2
222     fog_exp2_density = sqrt( -log(0.01) ) / WeatherVisibility;
223     
224     // Set correct opengl fog density
225     xglFogf (GL_FOG_DENSITY, fog_exp2_density);
226     
227     // FG_LOG( FG_INPUT, FG_DEBUG, "Fog density = " << w->fog_density );
228     //cerr << "FGLocalWeatherDatabase::setWeatherVisibility(" << visibility << "):\n";
229     //cerr << "Fog density = " << fog_exp_density << "\n";
230     
231
232 }
233
234 WeatherPrecision inline FGLocalWeatherDatabase::getWeatherVisibility(void) const
235 {
236     //cerr << "FGLocalWeatherDatabase::getWeatherVisibility() = " << WeatherVisibility << "\n";
237     return WeatherVisibility;
238 }
239
240
241 /****************************************************************************/
242 #endif /*FGLocalWeatherDatabase_H*/