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