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