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