]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/sky.hxx
Merge branch 'maint' into next
[simgear.git] / simgear / scene / sky / sky.hxx
1 /**
2  * \file sky.hxx
3  * Provides a class to model a realistic (time/date/position) based sky.
4  */
5
6 // Written by Curtis Olson, started December 1997.
7 // SSG-ified by Curtis Olson, February 2000.
8 //
9 // Copyright (C) 1997-2000  Curtis L. Olson  - http://www.flightgear.org/~curt
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Library General Public
13 // License as published by the Free Software Foundation; either
14 // version 2 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 // Library General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
24 //
25 // $Id$
26
27
28 #ifndef _SG_SKY_HXX
29 #define _SG_SKY_HXX
30
31
32 #ifndef __cplusplus
33 # error This library requires C++
34 #endif
35
36 #include <simgear/compiler.h>
37 #include <simgear/misc/sg_path.hxx>
38 #include <simgear/props/props.hxx>
39
40 #include <vector>
41
42 #include <osg/ref_ptr>
43 #include <osg/MatrixTransform>
44 #include <osg/Node>
45 #include <osg/Switch>
46
47 #include <simgear/ephemeris/ephemeris.hxx>
48
49 #include <simgear/scene/sky/cloud.hxx>
50 #include <simgear/scene/sky/dome.hxx>
51 #include <simgear/scene/sky/moon.hxx>
52 #include <simgear/scene/sky/oursun.hxx>
53 #include <simgear/scene/sky/stars.hxx>
54
55 using std::vector;
56
57
58 typedef struct {
59         SGVec3f view_pos, zero_elev, view_up;
60         double lon, lat, alt, spin;
61         double gst;
62         double sun_dist;
63         double moon_dist;
64         double sun_angle;
65 } SGSkyState;
66
67 typedef struct {
68         SGVec3f sky_color, fog_color;
69         SGVec3f cloud_color;
70         double sun_angle, moon_angle;
71 } SGSkyColor;
72
73 /**
74  * A class to model a realistic (time/date/position) based sky.
75  *
76  * Introduction 
77  *
78  * The SGSky class models a blended sky dome, a haloed sun, a textured
79  * moon with phase that properly matches the date, stars and planets,
80  * and cloud layers. SGSky is designed to be dropped into existing
81  * plib based applications and depends heavily on plib's scene graph
82  * library, ssg. The sky implements various time of day lighting
83  * effects, it plays well with fog and visibility effects, and
84  * implements scudded cloud fly-through effects. Additionally, you can
85  * wire in the output of the SGEphemeris class to accurately position
86  * all the objects in the sky.
87  *
88  * Building the sky 
89  *
90
91  * Once you have created an instance of SGSky you must call the
92  * build() method.  Building the sky requires several textures. So,
93  * you must specify the path/directory where these textures reside
94  * before building the sky.  You do this first by calling the
95  * texture_path() method.
96
97  * The arguments you pass to the build() method allow you to specify
98  * the horizontal and vertical radiuses of the sky dome, the size of
99  * your sun sphere and moon sphere, a number of planets, and a
100  * multitude of stars.  For the planets and stars you pass in an array
101  * of right ascensions, declinations, and magnitudes.
102
103  * Cloud Layers 
104
105  * Cloud layers can be added, changed, or removed individually. To add
106  * a cloud layer use the add_cloud_layer() method.  The arguments
107  * allow you to specify base height above sea level, layer thickness,
108  * a transition zone for entering/leaving the cloud layer, the size of
109  * the cloud object, and the type of cloud texture. All distances are
110  * in meters. There are additional forms of this method that allow you
111  * to specify your own ssgSimpleState or texture name for drawing the
112  * cloud layer.
113
114  * Repainting the Sky 
115
116  * As the sun circles the globe, you can call the repaint() method to
117  * recolor the sky objects to simulate sunrise and sunset effects,
118  * visibility, and other lighting changes.  The arguments allow you to
119  * specify a base sky color (for the top of the dome), a fog color
120  * (for the horizon), the sun angle with the horizon (for
121  * sunrise/sunset effects), the moon angle (so we can make it more
122  * yellow at the horizon), and new star and planet data so that we can
123  * optionally change the magnitude of these (for day / night
124  * transitions.)
125
126  * Positioning Sky Objects 
127
128  * As time progresses and as you move across the surface of the earth,
129  * the apparent position of the objects and the various lighting
130  * effects can change. the reposition() method allows you to specify
131  * the positions of all the sky objects as well as your view position.
132  * The arguments allow you to specify your view position in world
133  * Cartesian coordinates, the zero elevation position in world
134  * Cartesian coordinates (your longitude, your latitude, sea level),
135  * the ``up'' vector in world Cartesian coordinates, current
136  * longitude, latitude, and altitude. A ``spin'' angle can be
137  * specified for orienting the sky with the sun position so sunset and
138  * sunrise effects look correct. You must specify GMT side real time,
139  * the sun right ascension, sun declination, and sun distance from
140  * view point (to keep it inside your view volume.) You also must
141  * specify moon right ascension, moon declination, and moon distance
142  * from view point.
143
144  * Rendering the Sky 
145
146  * The sky is designed to be rendered in three stages. The first stage
147  * renders the parts that form your back drop - the sky dome, the
148  * stars and planets, the sun, and the moon.  These should be rendered
149  * before the rest of your scene by calling the preDraw() method. The
150  * second stage renders the clouds that are above the viewer. This stage 
151  * is done before translucent objects in the main scene are drawn. It 
152  * is seperated from the preDraw routine to enable to implement a 
153  * multi passes technique and is located in the drawUpperClouds() method.
154  * The third stage renders the clouds that are below the viewer an which 
155  * are likely to be translucent (depending on type) and should be drawn 
156  * after your scene has been rendered.  Use the drawLowerClouds() method 
157  * to draw the second stage of the sky.
158
159  * A typical application might do the following: 
160
161  * <li> thesky->preDraw( my_altitude );
162  * <li> thesky->drawUpperClouds();
163  * <li> ssgCullAndDraw ( myscene ) ;
164  * <li> thesky->drawLowerClouds();
165
166  * The current altitude in meters is passed to the preDraw() method
167  * so the clouds layers can be rendered correction from most distant
168  * to closest.
169
170  * Visibility Effects 
171
172  * Visibility and fog is important for correctly rendering the
173  * sky. You can inform SGSky of the current visibility by calling the
174  * set_visibility() method.
175
176  * When transitioning through clouds, it is nice to pull in the fog as
177  * you get close to the cloud layer to hide the fact that the clouds
178  * are drawn as a flat polygon. As you get nearer to the cloud layer
179  * it is also nice to temporarily pull in the visibility to simulate
180  * the effects of flying in and out of the puffy edge of the
181  * cloud. These effects can all be accomplished by calling the
182  * modify_vis() method.  The arguments allow you to specify your
183  * current altitude (which is then compared to the altitudes of the
184  * various cloud layers.) You can also specify a time factor which
185  * should be the length in seconds since the last time you called
186  * modify_vis(). The time_factor value allows the puffy cloud effect
187  * to be calculated correctly.
188
189  * The modify_vis() method alters the SGSky's internal idea of
190  * visibility, so you should subsequently call get_visibility() to get
191  * the actual modified visibility. You should then make the
192  * appropriate glFog() calls to setup fog properly for your scene.
193
194  * Accessor Methods 
195
196  * Once an instance of SGSky has been successfully initialized, there
197  * are a couple accessor methods you can use such as get_num_layers()
198  * to return the number of cloud layers, get_cloud_layer(i) to return
199  * cloud layer number i, get_visibility() to return the actual
200  * visibility as modified by the sky/cloud model.
201
202  */
203
204 class SGSky {
205
206 private:
207     typedef std::vector<SGSharedPtr<SGCloudLayer> > layer_list_type;
208     typedef layer_list_type::iterator layer_list_iterator;
209     typedef layer_list_type::const_iterator layer_list_const_iterator;
210
211     // components of the sky
212     SGSharedPtr<SGSkyDome> dome;
213     SGSharedPtr<SGSun> oursun;
214     SGSharedPtr<SGMoon> moon;
215     SGSharedPtr<SGStars> planets;
216     SGSharedPtr<SGStars> stars;
217     layer_list_type cloud_layers;
218
219     osg::ref_ptr<osg::Group> pre_root, cloud_root;
220     osg::ref_ptr<osg::Switch> pre_selector;
221     osg::ref_ptr<osg::Group> pre_transform;
222
223     osg::ref_ptr<osg::MatrixTransform> _ephTransform;
224
225     SGPath tex_path;
226
227     // visibility
228     float visibility;
229     float effective_visibility;
230
231     int in_cloud;
232     int cur_layer_pos;
233
234     // near cloud visibility state variables
235     bool in_puff;
236     double puff_length;         // in seconds
237     double puff_progression;    // in seconds
238     double ramp_up;             // in seconds
239     double ramp_down;           // in seconds
240
241     // 3D clouds enabled
242     bool clouds_3d_enabled;
243
244     // 3D cloud density
245     double clouds_3d_density;
246
247 public:
248
249     /** Constructor */
250     SGSky( void );
251
252     /** Destructor */
253     ~SGSky( void );
254
255     /**
256      * Initialize the sky and connect the components to the scene
257      * graph at the provided branch.  See discussion in detailed class
258      * description.
259      * @param h_radius_m horizontal radius of sky dome
260      * @param v_radius_m vertical radius of sky dome
261      * @param sun_size size of sun
262      * @param moon_size size of moon
263      * @param nplanets number of planets
264      * @param planet_data an array of planet right ascensions, declinations,
265      *        and magnitudes
266      * @param nstars number of stars
267      * @param star_data an array of star right ascensions, declinations,
268      *        and magnitudes
269      */
270     void build( double h_radius_m, double v_radius_m,
271                 double sun_size, double moon_size,
272                 const SGEphemeris& eph, SGPropertyNode *property_tree_node );
273
274     /**
275      * Repaint the sky components based on current value of sun_angle,
276      * sky, and fog colors.  You can also specify new star and planet
277      * data so that we can optionally change the magnitude of these
278      * (for day/night transitions.)  See discussion in detailed
279      * class description.
280      *
281      * Sun and moon angles are specified in degrees relative to local up
282      * <li> 0 degrees = high noon
283      * <li> 90 degrees = sun rise/set
284      * <li> 180 degrees = darkest midnight
285      * @param sky_color the base sky color (for the top of the dome)
286      * @param fog_color the fog color (for the horizon)
287      * @param sun_angle the sun angle with the horizon (for sunrise/sunset
288      *        effects)
289      * @param moon_angle the moon angle (so we can make it more yellow
290      *        at the horizon)
291      * @param nplanets number of planets
292      * @param planet_data an array of planet right ascensions, declinations,
293      *        and magnitudes
294      * @param nstars number of stars
295      * @param star_data an array of star right ascensions, declinations,
296      *        and magnitudes
297      */
298     bool repaint( const SGSkyColor &sc, const SGEphemeris& eph );
299
300     /**
301      * Reposition the sky at the specified origin and orientation
302      *
303      * lon specifies a rotation about the Z axis
304      * lat specifies a rotation about the new Y axis
305      * spin specifies a rotation about the new Z axis (this allows
306      * additional orientation for the sunrise/set effects and is used
307      * by the skydome and perhaps clouds.  See discussion in detailed
308      * class description.
309      * @param view_pos specify your view position in world Cartesian
310      *        coordinates
311      * @param zero_elev the zero elevation position in world Cartesian
312      *        coordinates
313      * @param view_up the up vector in world Cartesian coordinates
314      * @param lon current longitude
315      * @param lat current latitude
316      * @param alt current altitude
317      * @param spin an offset angle for orienting the sky effects with the
318      *        sun position so sunset and sunrise effects look correct.
319      * @param gst GMT side real time
320      * @param sun_ra the sun's current right ascension
321      * @param sun_dec the sun's current declination
322      * @param sun_dist the sun's distance from the current view point
323      *        (to keep it inside your view volume.)
324      * @param moon_ra the moon's current right ascension
325      * @param moon_dec the moon's current declination
326      * @param moon_dist the moon's distance from the current view point. 
327      */
328     bool reposition( const SGSkyState &st, const SGEphemeris& eph, double dt = 0.0 );
329
330     /**
331      * Modify the given visibility based on cloud layers, thickness,
332      * transition range, and simulated "puffs".  See discussion in detailed
333      * class description.
334      * @param alt current altitude
335      * @param time_factor amount of time since modify_vis() last called so
336      *        we can scale effect rates properly despite variable frame rates.
337      */
338     void modify_vis( float alt, float time_factor );
339
340     osg::Node* getPreRoot() { return pre_root.get(); }
341     osg::Node* getCloudRoot() { return cloud_root.get(); }
342
343     /** 
344      * Specify the texture path (optional, defaults to current directory)
345      * @param path base path to texture locations
346      */
347     void texture_path( const string& path );
348
349     /** Enable drawing of the sky. */
350     inline void enable() {
351         pre_selector->setValue(0, 1);
352     }
353
354     /**
355      * Disable drawing of the sky in the scene graph.  The leaf node is still
356      * there, how ever it won't be traversed on by ssgCullandRender()
357      */
358     inline void disable() {
359         pre_selector->setValue(0, 0);
360     }
361
362     /**
363      * Get the current sun color
364      */
365     inline SGVec4f get_sun_color() { return oursun->get_color(); }
366
367     /**
368      * Add a cloud layer.
369      *
370      * Transfer pointer ownership to this object.
371      *
372      * @param layer The new cloud layer to add.
373      */
374     void add_cloud_layer (SGCloudLayer * layer);
375
376
377     /**
378      * Get a cloud layer (const).
379      *
380      * Pointer ownership remains with this object.
381      *
382      * @param i The index of the cloud layer, zero-based.
383      * @return A const pointer to the cloud layer.
384      */
385     const SGCloudLayer * get_cloud_layer (int i) const;
386
387
388     /**
389      * Get a cloud layer (non-const).
390      *
391      * Pointer ownership remains with this object.
392      *
393      * @param i The index of the cloud layer, zero-based.
394      * @return A non-const pointer to the cloud layer.
395      */
396     SGCloudLayer * get_cloud_layer (int i);
397
398
399     /**
400      * Return the number of cloud layers currently available.
401      *
402      * @return The cloud layer count.
403      */
404     int get_cloud_layer_count () const;
405
406
407     /** @return current effective visibility */
408     inline float get_visibility() const { return effective_visibility; }
409
410     /** Set desired clear air visibility.
411      * @param v visibility in meters
412      */
413     inline void set_visibility( float v ) {
414         effective_visibility = visibility = (v <= 25.0) ? 25.0 : v;
415     }
416
417     /** Get 3D cloud density */
418     virtual double get_3dCloudDensity() const;
419
420     /** Set 3D cloud density 
421      * @param density 3D cloud density
422      */
423     virtual void set_3dCloudDensity(double density);
424
425     /** Get 3D cloud visibility range*/
426     virtual float get_3dCloudVisRange() const;
427
428     /** Set 3D cloud visibility range
429      * @param density 3D cloud visibility range
430      */
431     virtual void set_3dCloudVisRange(float vis);
432
433 };
434
435
436 #endif // _SG_SKY_HXX