]> git.mxchange.org Git - simgear.git/blob - simgear/sky/sky.cxx
5cde898fb29e360e460a0c0f12b50326c597dd53
[simgear.git] / simgear / sky / sky.cxx
1 // sky.cxx -- ssg based sky model
2 //
3 // Written by Curtis Olson, started December 1997.
4 // SSG-ified by Curtis Olson, February 2000.
5 //
6 // Copyright (C) 1997-2000  Curtis L. Olson  - curt@flightgear.org
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 //
22 // $Id$
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include <plib/sg.h>
30 #include <plib/ssg.h>
31
32 #include <simgear/math/fg_random.h>
33
34 #include "sky.hxx"
35
36
37 // Constructor
38 SGSky::SGSky( void ) {
39     effective_visibility = visibility = 10000.0;
40
41     // near cloud visibility state variables
42     in_puff = false;
43     puff_length = 0;
44     puff_progression = 0;
45     ramp_up = 0.15;
46     ramp_down = 0.15;
47     // ramp_up = 4.0;
48     // ramp_down = 4.0;
49 }
50
51
52 // Destructor
53 SGSky::~SGSky( void ) {
54 }
55
56
57 // initialize the sky and connect the components to the scene graph at
58 // the provided branch
59 void SGSky::build(  double sun_size, double moon_size,
60                     int nplanets, sgdVec3 *planet_data,
61                     double planet_dist,
62                     int nstars, sgdVec3 *star_data, double star_dist )
63 {
64     pre_root = new ssgRoot;
65     post_root = new ssgRoot;
66
67     pre_selector = new ssgSelector;
68     post_selector = new ssgSelector;
69
70     pre_transform = new ssgTransform;
71     post_transform = new ssgTransform;
72
73     dome = new SGSkyDome;
74     pre_transform -> addKid( dome->build() );
75
76     planets = new SGStars;
77     pre_transform -> addKid( planets->build(nplanets, planet_data,
78                                             planet_dist)
79                              );
80
81     stars = new SGStars;
82     pre_transform -> addKid( stars->build(nstars, star_data, star_dist) );
83     
84     moon = new SGMoon;
85     pre_transform -> addKid( moon->build(tex_path, moon_size) );
86
87     oursun = new SGSun;
88     pre_transform -> addKid( oursun->build(tex_path, sun_size) );
89
90     pre_selector->addKid( pre_transform );
91     pre_selector->clrTraversalMaskBits( SSGTRAV_HOT );
92
93     post_selector->addKid( post_transform );
94     post_selector->clrTraversalMaskBits( SSGTRAV_HOT );
95
96     pre_root->addKid( pre_selector );
97     post_root->addKid( post_selector );
98
99     // add the cloud ssgStates to the material lib
100     FGPath cloud_path;
101
102     cloud_path.set( tex_path.str() );
103     cloud_path.append( "overcast.rgb" );
104     cloud_mats[SG_CLOUD_OVERCAST] = SGCloudMakeState( cloud_path.str() );
105
106     cloud_path.set( tex_path.str() );
107     cloud_path.append( "mostlycloudy.rgba" );
108     cloud_mats[SG_CLOUD_MOSTLY_CLOUDY] = SGCloudMakeState( cloud_path.str() );
109
110     cloud_path.set( tex_path.str() );
111     cloud_path.append( "mostlysunny.rgba" );
112     cloud_mats[SG_CLOUD_MOSTLY_SUNNY] = SGCloudMakeState( cloud_path.str() );
113
114     cloud_path.set( tex_path.str() );
115     cloud_path.append( "cirrus.rgba" );
116     cloud_mats[SG_CLOUD_CIRRUS] = SGCloudMakeState( cloud_path.str() );
117 }
118
119
120 // repaint the sky components based on current value of sun_angle,
121 // sky, and fog colors.
122 //
123 // sun angle in degrees relative to verticle
124 // 0 degrees = high noon
125 // 90 degrees = sun rise/set
126 // 180 degrees = darkest midnight
127 bool SGSky::repaint( sgVec4 sky_color, sgVec4 fog_color,
128                      double sun_angle, double moon_angle,
129                      int nplanets, sgdVec3 *planet_data,
130                      int nstars, sgdVec3 *star_data )
131 {
132     if ( effective_visibility > 1000.0 ) {
133         enable();
134         dome->repaint( sky_color, fog_color, sun_angle, effective_visibility );
135         oursun->repaint( sun_angle );
136         moon->repaint( moon_angle );
137         planets->repaint( sun_angle, nplanets, planet_data );
138         stars->repaint( sun_angle, nstars, star_data );
139
140         for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
141             cloud_layers[i]->repaint( fog_color );
142         }
143     } else {
144         // turn off sky
145         disable();
146     }
147
148     return true;
149 }
150
151
152 // reposition the sky at the specified origin and orientation
153 //
154 // lon specifies a rotation about the Z axis
155 // lat specifies a rotation about the new Y axis
156 // spin specifies a rotation about the new Z axis (this allows
157 // additional orientation for the sunrise/set effects and is used by
158 // the skydome and perhaps clouds.
159 bool SGSky::reposition( sgVec3 view_pos, sgVec3 zero_elev, sgVec3 view_up,
160                         double lon, double lat, double alt, double spin,
161                         double gst, 
162                         double sun_ra, double sun_dec, double sun_dist,
163                         double moon_ra, double moon_dec, double moon_dist )
164 {
165     double angle = gst * 15;    // degrees
166     dome->reposition( zero_elev, lon, lat, spin );
167     oursun->reposition( view_pos, angle, sun_ra, sun_dec, sun_dist );
168     moon->reposition( view_pos, angle, moon_ra, moon_dec, moon_dist );
169     planets->reposition( view_pos, angle );
170     stars->reposition( view_pos, angle );
171
172     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
173         cloud_layers[i]->reposition( zero_elev, view_up, lon, lat, alt );
174     }
175
176     return true;
177 }
178
179
180 // draw background portions of the sky ... do this before you draw the
181 // rest of your scene.
182 void SGSky::preDraw() {
183     ssgCullAndDraw( pre_root );
184 }
185
186
187 // draw translucent clouds ... do this after you've drawn all the
188 // oapaque elements of your scene.
189 void SGSky::postDraw( float alt ) {
190     float slop = 5.0;           // if we are closer than this to a cloud layer,
191                                 // don't draw clouds
192
193     int in_cloud = -1;          // cloud we are in
194
195     // check where we are relative to the cloud layers
196     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
197         float asl = cloud_layers[i]->get_asl();
198         float thickness = cloud_layers[i]->get_thickness();
199
200         if ( alt < asl - slop ) {
201             // below cloud layer
202         } else if ( alt < asl + thickness + slop ) {
203             // in cloud layer
204
205             // bail now and don't draw any clouds
206             in_cloud = i;
207         } else {
208             // above cloud layer
209         }
210     }
211
212     // determine rendering order
213     int pos = 0;
214     while ( pos < (int)cloud_layers.size() && 
215             alt > cloud_layers[pos]->get_asl())
216     {
217         ++pos;
218     }
219
220     if ( pos == 0 ) {
221         // we are below all the cloud layers, draw top to bottom
222         for ( int i = cloud_layers.size() - 1; i >= 0; --i ) {
223             if ( i != in_cloud ) {
224                 cloud_layers[i]->draw();
225             }
226         }
227     } else if ( pos >= (int)cloud_layers.size() ) {
228         // we are above all the cloud layers, draw bottom to top
229         for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
230             if ( i != in_cloud ) {
231                 cloud_layers[i]->draw();
232             }
233         }
234     } else {
235         // we are between cloud layers, draw lower layers bottom to
236         // top and upper layers top to bottom
237         for ( int i = 0; i < pos; ++i ) {
238             if ( i != in_cloud ) {
239                 cloud_layers[i]->draw();
240             }
241         }
242         for ( int i = cloud_layers.size() - 1; i >= pos; --i ) {
243             if ( i != in_cloud ) {
244                 cloud_layers[i]->draw();
245             }
246         }
247     }
248 }
249
250  
251 void SGSky::add_cloud_layer( double asl, double thickness,
252                              double transition, double span,
253                              ssgSimpleState *state ) {
254     SGCloudLayer *layer = new SGCloudLayer;
255     layer->build( span, asl, thickness, transition, state );
256
257     layer_list_iterator current = cloud_layers.begin();
258     layer_list_iterator last = cloud_layers.end();
259     while ( current != last && (*current)->get_asl() < asl ) {
260         ++current;
261     }
262
263     if ( current != last ) {
264         cloud_layers.insert( current, layer );
265     } else {
266         cloud_layers.push_back( layer );
267     }
268
269     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
270         cout << "layer " << i << " = " << cloud_layers[i]->get_asl() << endl;
271     }
272     cout << endl;
273 }
274
275
276 void SGSky::add_cloud_layer( double asl, double thickness,
277                              double transition, double span,
278                              const string &tex_path ) {
279     ssgSimpleState *state = SGCloudMakeState( tex_path );
280     add_cloud_layer( asl, thickness, transition, span, state );
281 }
282
283
284 void SGSky::add_cloud_layer( double asl, double thickness,
285                              double transition, double span,
286                              SGCloudType type ) {
287     if ( type > 0 && type < SG_MAX_CLOUD_TYPES ) {
288         add_cloud_layer( asl, thickness, transition, span, cloud_mats[type] );
289     }
290 }
291
292
293 // modify the current visibility based on cloud layers, thickness,
294 // transition range, and simulated "puffs".
295 void SGSky::modify_vis( float alt, float time_factor ) {
296     float effvis = visibility;
297
298     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
299         float asl = cloud_layers[i]->get_asl();
300         float thickness = cloud_layers[i]->get_thickness();
301         float transition = cloud_layers[i]->get_transition();
302
303         double ratio = 1.0;
304
305         if ( alt < asl - transition ) {
306             // below cloud layer
307             ratio = 1.0;
308         } else if ( alt < asl ) {
309             // in lower transition
310             ratio = (asl - alt) / transition;
311         } else if ( alt < asl + thickness ) {
312             // in cloud layer
313             ratio = 0.0;
314         } else if ( alt < asl + thickness + transition ) {
315             // in upper transition
316             ratio = (alt - (asl + thickness)) / transition;
317         } else {
318             // above cloud layer
319             ratio = 1.0;
320         }
321
322         // accumulate effects from multiple cloud layers
323         effvis *= ratio;
324
325         if ( ratio < 1.0 ) {
326             if ( ! in_puff ) {
327                 // calc chance of entering cloud puff
328                 double rnd = fg_random();
329                 double chance = rnd * rnd * rnd;
330                 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
331                     in_puff = true;
332                     puff_length = fg_random() * 2.0; // up to 2 seconds
333                     puff_progression = 0.0;
334                 }
335             }
336
337             if ( in_puff ) {
338                 // modify actual_visibility based on puff envelope
339
340                 if ( puff_progression <= ramp_up ) {
341                     double x = 0.5 * SGD_PI * puff_progression / ramp_up;
342                     double factor = 1.0 - sin( x );
343                     // cout << "ramp up = " << puff_progression
344                     //      << "  factor = " << factor << endl;
345                     effvis = effvis * factor;
346                 } else if ( puff_progression >= ramp_up + puff_length ) {
347                     double x = 0.5 * SGD_PI * 
348                         (puff_progression - (ramp_up + puff_length)) /
349                         ramp_down;
350                     double factor = sin( x );
351                     // cout << "ramp down = " 
352                     //      << puff_progression - (ramp_up + puff_length) 
353                     //      << "  factor = " << factor << endl;
354                     effvis = effvis * factor;
355                 } else {
356                     effvis = 0.0;
357                 }
358
359                 /* cout << "len = " << puff_length
360                    << "  x = " << x 
361                    << "  factor = " << factor
362                    << "  actual_visibility = " << actual_visibility 
363                    << endl; */
364
365                 // time_factor = ( global_multi_loop * 
366                 //                 current_options.get_speed_up() ) /
367                 //                (double)current_options.get_model_hz();
368
369                 puff_progression += time_factor;
370                 // cout << "time factor = " << time_factor << endl;
371
372                 /* cout << "gml = " << global_multi_loop 
373                    << "  speed up = " << current_options.get_speed_up()
374                    << "  hz = " << current_options.get_model_hz() << endl;
375                    */ 
376
377                 if ( puff_progression > puff_length + ramp_up + ramp_down) {
378                     in_puff = false; 
379                 }
380             }
381
382             // never let visibility drop below 25 meters
383             if ( effvis <= 25.0 ) {
384                 effvis = 25.0;
385             }
386         }
387     } // for
388
389     effective_visibility = effvis;
390 }
391