]> git.mxchange.org Git - simgear.git/blob - simgear/sky/sky.cxx
Just a few more visibility tweaks and clean ups.
[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, double transition,
252                              ssgSimpleState *state ) {
253     SGCloudLayer *layer = new SGCloudLayer;
254     layer->build( 40000.0f, asl, thickness, transition, state );
255
256     layer_list_iterator current = cloud_layers.begin();
257     layer_list_iterator last = cloud_layers.end();
258     while ( current != last && (*current)->get_asl() < asl ) {
259         ++current;
260     }
261
262     if ( current != last ) {
263         cloud_layers.insert( current, layer );
264     } else {
265         cloud_layers.push_back( layer );
266     }
267
268     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
269         cout << "layer " << i << " = " << cloud_layers[i]->get_asl() << endl;
270     }
271     cout << endl;
272 }
273
274
275 void SGSky::add_cloud_layer( double asl, double thickness, double transition,
276                              const string &tex_path ) {
277     ssgSimpleState *state = SGCloudMakeState( tex_path );
278     add_cloud_layer( asl, thickness, transition, state );
279 }
280
281
282 void SGSky::add_cloud_layer( double asl, double thickness, double transition,
283                              SGCloudType type ) {
284     if ( type > 0 && type < SG_MAX_CLOUD_TYPES ) {
285         add_cloud_layer( asl, thickness, transition, cloud_mats[type] );
286     }
287 }
288
289
290 // modify the current visibility based on cloud layers, thickness,
291 // transition range, and simulated "puffs".
292 void SGSky::modify_vis( float alt, float time_factor ) {
293     float effvis = visibility;
294
295     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
296         float asl = cloud_layers[i]->get_asl();
297         float thickness = cloud_layers[i]->get_thickness();
298         float transition = cloud_layers[i]->get_transition();
299
300         double ratio = 1.0;
301
302         if ( alt < asl - transition ) {
303             // below cloud layer
304             ratio = 1.0;
305         } else if ( alt < asl ) {
306             // in lower transition
307             ratio = (asl - alt) / transition;
308         } else if ( alt < asl + thickness ) {
309             // in cloud layer
310             ratio = 0.0;
311         } else if ( alt < asl + thickness + transition ) {
312             // in upper transition
313             ratio = (alt - (asl + thickness)) / transition;
314         } else {
315             // above cloud layer
316             ratio = 1.0;
317         }
318
319         // accumulate effects from multiple cloud layers
320         effvis *= ratio;
321
322         if ( ratio < 1.0 ) {
323             if ( ! in_puff ) {
324                 // calc chance of entering cloud puff
325                 double rnd = fg_random();
326                 double chance = rnd * rnd * rnd;
327                 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
328                     in_puff = true;
329                     puff_length = fg_random() * 2.0; // up to 2 seconds
330                     puff_progression = 0.0;
331                 }
332             }
333
334             if ( in_puff ) {
335                 // modify actual_visibility based on puff envelope
336
337                 if ( puff_progression <= ramp_up ) {
338                     double x = 0.5 * SGD_PI * puff_progression / ramp_up;
339                     double factor = 1.0 - sin( x );
340                     // cout << "ramp up = " << puff_progression
341                     //      << "  factor = " << factor << endl;
342                     effvis = effvis * factor;
343                 } else if ( puff_progression >= ramp_up + puff_length ) {
344                     double x = 0.5 * SGD_PI * 
345                         (puff_progression - (ramp_up + puff_length)) /
346                         ramp_down;
347                     double factor = sin( x );
348                     // cout << "ramp down = " 
349                     //      << puff_progression - (ramp_up + puff_length) 
350                     //      << "  factor = " << factor << endl;
351                     effvis = effvis * factor;
352                 } else {
353                     effvis = 0.0;
354                 }
355
356                 /* cout << "len = " << puff_length
357                    << "  x = " << x 
358                    << "  factor = " << factor
359                    << "  actual_visibility = " << actual_visibility 
360                    << endl; */
361
362                 // time_factor = ( global_multi_loop * 
363                 //                 current_options.get_speed_up() ) /
364                 //                (double)current_options.get_model_hz();
365
366                 puff_progression += time_factor;
367                 // cout << "time factor = " << time_factor << endl;
368
369                 /* cout << "gml = " << global_multi_loop 
370                    << "  speed up = " << current_options.get_speed_up()
371                    << "  hz = " << current_options.get_model_hz() << endl;
372                    */ 
373
374                 if ( puff_progression > puff_length + ramp_up + ramp_down) {
375                     in_puff = false; 
376                 }
377             }
378
379             // never let visibility drop below 25 meters
380             if ( effvis <= 25.0 ) {
381                 effvis = 25.0;
382             }
383         }
384     } // for
385
386     effective_visibility = effvis;
387 }
388