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