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