]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/cloud.hxx
Added some OSG headers for the correct evaluation of the OSG_VERSION_LESS_THAN macro.
[simgear.git] / simgear / scene / sky / cloud.hxx
1 /**
2  * \file cloud.hxx
3  * Provides a class to model a single cloud layer
4  */
5
6 // Written by Curtis Olson, started June 2000.
7 //
8 // Copyright (C) 2000  Curtis L. Olson  - http://www.flightgear.org/~curt
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Library General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // Library General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23 //
24 // $Id$
25
26
27 #ifndef _SG_CLOUD_HXX_
28 #define _SG_CLOUD_HXX_
29
30 #include <simgear/compiler.h>
31 #include <simgear/misc/sg_path.hxx>
32 #include <simgear/math/SGMath.hxx>
33 #include <simgear/structure/SGReferenced.hxx>
34
35 #include <string>
36
37 #include <osg/ref_ptr>
38 #include <osg/Array>
39 #include <osg/Geode>
40 #include <osg/Group>
41 #include <osg/MatrixTransform>
42 #include <osg/Switch>
43
44 class SGCloudField;
45
46 /**
47  * A class layer to model a single cloud layer
48  */
49 class SGCloudLayer : public SGReferenced {
50 public:
51
52     /**
53      * This is the list of available cloud coverages/textures
54      */
55     enum Coverage {
56         SG_CLOUD_OVERCAST = 0,
57         SG_CLOUD_BROKEN,
58         SG_CLOUD_SCATTERED,
59         SG_CLOUD_FEW,
60         SG_CLOUD_CIRRUS,
61         SG_CLOUD_CLEAR,
62         SG_MAX_CLOUD_COVERAGES
63     };
64
65     static const std::string SG_CLOUD_OVERCAST_STRING; // "overcast"
66     static const std::string SG_CLOUD_BROKEN_STRING; // "broken"
67     static const std::string SG_CLOUD_SCATTERED_STRING; // "scattered"
68     static const std::string SG_CLOUD_FEW_STRING; // "few"
69     static const std::string SG_CLOUD_CIRRUS_STRING; // "cirrus"
70     static const std::string SG_CLOUD_CLEAR_STRING; // "clear"
71
72     /**
73      * Constructor
74      * @param tex_path the path to the set of cloud textures
75      */
76     SGCloudLayer( const std::string &tex_path );
77
78     /**
79      * Destructor
80      */
81     ~SGCloudLayer( void );
82
83     /** get the cloud span (in meters) */
84     float getSpan_m () const;
85     /**
86      * set the cloud span
87      * @param span_m the cloud span in meters
88      */
89     void setSpan_m (float span_m);
90
91     /** get the layer elevation (in meters) */
92     float getElevation_m () const;
93     /**
94      * set the layer elevation.  Note that this specifies the bottom
95      * of the cloud layer.  The elevation of the top of the layer is
96      * elevation_m + thickness_m.
97      * @param elevation_m the layer elevation in meters
98      * @param set_span defines whether it is allowed to adjust the span
99      */
100     void setElevation_m (float elevation_m, bool set_span = true);
101
102     /** get the layer thickness */
103     float getThickness_m () const;
104     /**
105      * set the layer thickness.
106      * @param thickness_m the layer thickness in meters.
107      */
108     void setThickness_m (float thickness_m);
109
110     /** get the layer visibility */
111     float getVisibility_m() const;
112     /**
113      * set the layer visibility 
114      * @param visibility_m the layer minimum visibility in meters.
115      */
116     void setVisibility_m(float visibility_m);
117
118
119
120     /**
121      * get the transition/boundary layer depth in meters.  This
122      * allows gradual entry/exit from the cloud layer via adjusting
123      * visibility.
124      */
125     float getTransition_m () const;
126
127     /**
128      * set the transition layer size in meters
129      * @param transition_m the transition layer size in meters
130      */
131     void setTransition_m (float transition_m);
132
133     /** get coverage type */
134     Coverage getCoverage () const;
135
136     /**
137      * set coverage type
138      * @param coverage the coverage type
139      */
140     void setCoverage (Coverage coverage);
141
142     /** get coverage as string */
143     const std::string & getCoverageString() const;
144
145     /** get coverage as string */
146     static const std::string & getCoverageString( Coverage coverage );
147
148     /** get coverage type from string */
149     static Coverage getCoverageType( const std::string & coverage );
150
151     /** set coverage as string */
152     void setCoverageString( const std::string & coverage );
153
154     /**
155      * set the cloud movement direction
156      * @param dir the cloud movement direction
157      */
158     inline void setDirection(float dir) { 
159         // cout << "cloud dir = " << dir << endl;
160         direction = dir;
161     }
162
163     /** get the cloud movement direction */
164     inline float getDirection() { return direction; }
165
166     /**
167      * set the cloud movement speed 
168      * @param sp the cloud movement speed
169      */
170     inline void setSpeed(float sp) {
171         // cout << "cloud speed = " << sp << endl;
172         speed = sp;
173     }
174
175     /** get the cloud movement speed */
176     inline float getSpeed() { return speed; }
177
178     /**
179      * set the alpha component of the cloud base color.  Normally this
180      * should be 1.0, but you can set it anywhere in the range of 0.0
181      * to 1.0 to fade a cloud layer in or out.
182      * @param alpha cloud alpha value (0.0 to 1.0)
183      */
184     inline void setAlpha( float alpha ) {
185         if ( alpha < 0.0 ) { alpha = 0.0; }
186         if ( alpha > max_alpha ) { alpha = max_alpha; }
187         cloud_alpha = alpha;
188     }
189
190     inline void setMaxAlpha( float alpha ) {
191         if ( alpha < 0.0 ) { alpha = 0.0; }
192         if ( alpha > 1.0 ) { alpha = 1.0; }
193         max_alpha = alpha;
194     }
195
196     inline float getMaxAlpha() const {
197         return max_alpha;
198     }
199
200     /** build the cloud object */
201     void rebuild();
202
203     /** Enable/disable 3D clouds in this layer */
204     void set_enable3dClouds(bool enable);
205
206     /**
207      * repaint the cloud colors based on the specified fog_color
208      * @param fog_color the fog color
209      */
210     bool repaint( const SGVec3f& fog_color );
211
212     /**
213      * reposition the cloud layer at the specified origin and
214      * orientation.
215      * @param p position vector
216      * @param up the local up vector
217      * @param lon TODO
218      * @param lat TODO
219      * @param alt TODO
220      * @param dt the time elapsed since the last call
221      */
222     bool reposition( const SGVec3f& p,
223                      const SGVec3f& up,
224                      double lon, double lat, double alt,
225                      double dt = 0.0 );
226
227     osg::Switch* getNode() { return cloud_root.get(); }
228
229     /** return the 3D layer cloud associated with this 2D layer */
230     SGCloudField *get_layer3D(void) { return layer3D; }
231
232 protected:
233     void setTextureOffset(const osg::Vec2& offset);
234 private:
235
236     osg::ref_ptr<osg::Switch> cloud_root;
237     osg::ref_ptr<osg::Switch> layer_root;
238     osg::ref_ptr<osg::Group> group_top, group_bottom;
239     osg::ref_ptr<osg::MatrixTransform> layer_transform;
240     osg::ref_ptr<osg::Geode> layer[4];
241
242     float cloud_alpha;          // 1.0 = drawn fully, 0.0 faded out completely
243
244     osg::ref_ptr<osg::Vec4Array> cl[4];
245     osg::ref_ptr<osg::Vec3Array> vl[4];
246     osg::ref_ptr<osg::Vec2Array> tl[4];
247     osg::ref_ptr<osg::Vec3Array> tl2[4];
248
249     // height above sea level (meters)
250     SGPath texture_path;
251     float layer_span;
252     float layer_asl;
253     float layer_thickness;
254     float layer_transition;
255     float layer_visibility;
256     Coverage layer_coverage;
257     float scale;
258     float speed;
259     float direction;
260
261     // for handling texture coordinates to simulate cloud movement
262     // from winds, and to simulate the clouds being tied to ground
263     // position, not view position
264     // double xoff, yoff;
265     SGGeod last_pos;
266     double last_course;
267     double max_alpha;
268
269     osg::Vec2 base;
270
271     SGCloudField *layer3D;
272
273 };
274
275 #endif // _SG_CLOUD_HXX_