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