]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/cloud.hxx
Work around apparent OSG 3.2.0 normal binding bug.
[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 using std::string;
37
38 #include <osg/ref_ptr>
39 #include <osg/Array>
40 #include <osg/Geode>
41 #include <osg/Group>
42 #include <osg/MatrixTransform>
43 #include <osg/Switch>
44
45 class SGCloudField;
46
47 /**
48  * A class layer to model a single cloud layer
49  */
50 class SGCloudLayer : public SGReferenced {
51 public:
52
53     /**
54      * This is the list of available cloud coverages/textures
55      */
56     enum Coverage {
57         SG_CLOUD_OVERCAST = 0,
58         SG_CLOUD_BROKEN,
59         SG_CLOUD_SCATTERED,
60         SG_CLOUD_FEW,
61         SG_CLOUD_CIRRUS,
62         SG_CLOUD_CLEAR,
63         SG_MAX_CLOUD_COVERAGES
64     };
65
66     static const std::string SG_CLOUD_OVERCAST_STRING; // "overcast"
67     static const std::string SG_CLOUD_BROKEN_STRING; // "broken"
68     static const std::string SG_CLOUD_SCATTERED_STRING; // "scattered"
69     static const std::string SG_CLOUD_FEW_STRING; // "few"
70     static const std::string SG_CLOUD_CIRRUS_STRING; // "cirrus"
71     static const std::string SG_CLOUD_CLEAR_STRING; // "clear"
72
73     /**
74      * Constructor
75      * @param tex_path the path to the set of cloud textures
76      */
77     SGCloudLayer( const string &tex_path );
78
79     /**
80      * Destructor
81      */
82     ~SGCloudLayer( void );
83
84     /** get the cloud span (in meters) */
85     float getSpan_m () const;
86     /**
87      * set the cloud span
88      * @param span_m the cloud span in meters
89      */
90     void setSpan_m (float span_m);
91
92     /** get the layer elevation (in meters) */
93     float getElevation_m () const;
94     /**
95      * set the layer elevation.  Note that this specifies the bottom
96      * of the cloud layer.  The elevation of the top of the layer is
97      * elevation_m + thickness_m.
98      * @param elevation_m the layer elevation in meters
99      * @param set_span defines whether it is allowed to adjust the span
100      */
101     void setElevation_m (float elevation_m, bool set_span = true);
102
103     /** get the layer thickness */
104     float getThickness_m () const;
105     /**
106      * set the layer thickness.
107      * @param thickness_m the layer thickness in meters.
108      */
109     void setThickness_m (float thickness_m);
110
111     /** get the layer visibility */
112     float getVisibility_m() const;
113     /**
114      * set the layer visibility 
115      * @param visibility_m the layer minimum visibility in meters.
116      */
117     void setVisibility_m(float visibility_m);
118
119
120
121     /**
122      * get the transition/boundary layer depth in meters.  This
123      * allows gradual entry/exit from the cloud layer via adjusting
124      * visibility.
125      */
126     float getTransition_m () const;
127
128     /**
129      * set the transition layer size in meters
130      * @param transition_m the transition layer size in meters
131      */
132     void setTransition_m (float transition_m);
133
134     /** get coverage type */
135     Coverage getCoverage () const;
136
137     /**
138      * set coverage type
139      * @param coverage the coverage type
140      */
141     void setCoverage (Coverage coverage);
142
143     /** get coverage as string */
144     const string & getCoverageString() const;
145
146     /** get coverage as string */
147     static const string & getCoverageString( Coverage coverage );
148
149     /** get coverage type from string */
150     static Coverage getCoverageType( const std::string & coverage );
151
152     /** set coverage as string */
153     void setCoverageString( const string & coverage );
154
155     /**
156      * set the cloud movement direction
157      * @param dir the cloud movement direction
158      */
159     inline void setDirection(float dir) { 
160         // cout << "cloud dir = " << dir << endl;
161         direction = dir;
162     }
163
164     /** get the cloud movement direction */
165     inline float getDirection() { return direction; }
166
167     /**
168      * set the cloud movement speed 
169      * @param sp the cloud movement speed
170      */
171     inline void setSpeed(float sp) {
172         // cout << "cloud speed = " << sp << endl;
173         speed = sp;
174     }
175
176     /** get the cloud movement speed */
177     inline float getSpeed() { return speed; }
178
179     /**
180      * set the alpha component of the cloud base color.  Normally this
181      * should be 1.0, but you can set it anywhere in the range of 0.0
182      * to 1.0 to fade a cloud layer in or out.
183      * @param alpha cloud alpha value (0.0 to 1.0)
184      */
185     inline void setAlpha( float alpha ) {
186         if ( alpha < 0.0 ) { alpha = 0.0; }
187         if ( alpha > max_alpha ) { alpha = max_alpha; }
188         cloud_alpha = alpha;
189     }
190
191     inline void setMaxAlpha( float alpha ) {
192         if ( alpha < 0.0 ) { alpha = 0.0; }
193         if ( alpha > 1.0 ) { alpha = 1.0; }
194         max_alpha = alpha;
195     }
196
197     inline float getMaxAlpha() const {
198         return max_alpha;
199     }
200
201     /** build the cloud object */
202     void rebuild();
203
204     /** Enable/disable 3D clouds in this layer */
205     void set_enable3dClouds(bool enable);
206
207     /**
208      * repaint the cloud colors based on the specified fog_color
209      * @param fog_color the fog color
210      */
211     bool repaint( const SGVec3f& fog_color );
212
213     /**
214      * reposition the cloud layer at the specified origin and
215      * orientation.
216      * @param p position vector
217      * @param up the local up vector
218      * @param lon specifies a rotation about the Z axis
219      * @param lat specifies a rotation about the new Y axis
220      * @param spin specifies a rotation about the new Z axis
221      *        (and orients the sunrise/set effects)
222      * @param dt the time elapsed since the last call
223      */
224     bool reposition( const SGVec3f& p, const SGVec3f& up,
225                      double lon, double lat, double alt,
226                      double dt = 0.0 );
227
228     osg::Switch* getNode() { return cloud_root.get(); }
229
230     /** return the 3D layer cloud associated with this 2D layer */
231     SGCloudField *get_layer3D(void) { return layer3D; }
232
233 protected:
234     void setTextureOffset(const osg::Vec2& offset);
235 private:
236
237     osg::ref_ptr<osg::Switch> cloud_root;
238     osg::ref_ptr<osg::Switch> layer_root;
239     osg::ref_ptr<osg::Group> group_top, group_bottom;
240     osg::ref_ptr<osg::MatrixTransform> layer_transform;
241     osg::ref_ptr<osg::Geode> layer[4];
242
243     float cloud_alpha;          // 1.0 = drawn fully, 0.0 faded out completely
244
245     osg::ref_ptr<osg::Vec4Array> cl[4];
246     osg::ref_ptr<osg::Vec3Array> vl[4];
247     osg::ref_ptr<osg::Vec2Array> tl[4];
248     osg::ref_ptr<osg::Vec3Array> tl2[4];
249
250     // height above sea level (meters)
251     SGPath texture_path;
252     float layer_span;
253     float layer_asl;
254     float layer_thickness;
255     float layer_transition;
256     float layer_visibility;
257     Coverage layer_coverage;
258     float scale;
259     float speed;
260     float direction;
261
262     // for handling texture coordinates to simulate cloud movement
263     // from winds, and to simulate the clouds being tied to ground
264     // position, not view position
265     // double xoff, yoff;
266     SGGeod last_pos;
267     double last_course;
268     double max_alpha;
269
270     osg::Vec2 base;
271
272     SGCloudField *layer3D;
273
274 };
275
276 #endif // _SG_CLOUD_HXX_