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