]> git.mxchange.org Git - simgear.git/blob - simgear/sky/sky.cxx
Tweaks to follow flightgear STL standard coding procedure.
[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/sg_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     int i;
197
198     // check where we are relative to the cloud layers
199     for ( i = 0; i < (int)cloud_layers.size(); ++i ) {
200         float asl = cloud_layers[i]->get_asl();
201         float thickness = cloud_layers[i]->get_thickness();
202
203         if ( alt < asl - slop ) {
204             // below cloud layer
205         } else if ( alt < asl + thickness + slop ) {
206             // in cloud layer
207
208             // bail now and don't draw any clouds
209             in_cloud = i;
210         } else {
211             // above cloud layer
212         }
213     }
214
215     // determine rendering order
216     int pos = 0;
217     while ( pos < (int)cloud_layers.size() && 
218             alt > cloud_layers[pos]->get_asl())
219     {
220         ++pos;
221     }
222
223     if ( pos == 0 ) {
224         // we are below all the cloud layers, draw top to bottom
225         for ( i = cloud_layers.size() - 1; i >= 0; --i ) {
226             if ( i != in_cloud ) {
227                 cloud_layers[i]->draw();
228             }
229         }
230     } else if ( pos >= (int)cloud_layers.size() ) {
231         // we are above all the cloud layers, draw bottom to top
232         for ( i = 0; i < (int)cloud_layers.size(); ++i ) {
233             if ( i != in_cloud ) {
234                 cloud_layers[i]->draw();
235             }
236         }
237     } else {
238         // we are between cloud layers, draw lower layers bottom to
239         // top and upper layers top to bottom
240         for ( i = 0; i < pos; ++i ) {
241             if ( i != in_cloud ) {
242                 cloud_layers[i]->draw();
243             }
244         }
245         for ( i = cloud_layers.size() - 1; i >= pos; --i ) {
246             if ( i != in_cloud ) {
247                 cloud_layers[i]->draw();
248             }
249         }
250     }
251 }
252
253  
254 void SGSky::add_cloud_layer( double asl, double thickness,
255                              double transition, double span,
256                              ssgSimpleState *state ) {
257     SGCloudLayer *layer = new SGCloudLayer;
258     layer->build( span, asl, thickness, transition, state );
259
260     layer_list_iterator current = cloud_layers.begin();
261     layer_list_iterator last = cloud_layers.end();
262     while ( current != last && (*current)->get_asl() < asl ) {
263         ++current;
264     }
265
266     if ( current != last ) {
267         cloud_layers.insert( current, layer );
268     } else {
269         cloud_layers.push_back( layer );
270     }
271
272     // for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
273     //   cout << "layer " << i << " = " << cloud_layers[i]->get_asl() << endl;
274     // }
275     // cout << endl;
276 }
277
278
279 void SGSky::add_cloud_layer( double asl, double thickness,
280                              double transition, double span,
281                              const string &tex_path ) {
282     ssgSimpleState *state = SGCloudMakeState( tex_path );
283     add_cloud_layer( asl, thickness, transition, span, state );
284 }
285
286
287 void SGSky::add_cloud_layer( double asl, double thickness,
288                              double transition, double span,
289                              SGCloudType type ) {
290     if ( type > 0 && type < SG_MAX_CLOUD_TYPES ) {
291         add_cloud_layer( asl, thickness, transition, span, cloud_mats[type] );
292     }
293 }
294
295
296 // modify the current visibility based on cloud layers, thickness,
297 // transition range, and simulated "puffs".
298 void SGSky::modify_vis( float alt, float time_factor ) {
299     float effvis = visibility;
300
301     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
302         float asl = cloud_layers[i]->get_asl();
303         float thickness = cloud_layers[i]->get_thickness();
304         float transition = cloud_layers[i]->get_transition();
305
306         double ratio = 1.0;
307
308         if ( alt < asl - transition ) {
309             // below cloud layer
310             ratio = 1.0;
311         } else if ( alt < asl ) {
312             // in lower transition
313             ratio = (asl - alt) / transition;
314         } else if ( alt < asl + thickness ) {
315             // in cloud layer
316             ratio = 0.0;
317         } else if ( alt < asl + thickness + transition ) {
318             // in upper transition
319             ratio = (alt - (asl + thickness)) / transition;
320         } else {
321             // above cloud layer
322             ratio = 1.0;
323         }
324
325         // accumulate effects from multiple cloud layers
326         effvis *= ratio;
327
328         if ( ratio < 1.0 ) {
329             if ( ! in_puff ) {
330                 // calc chance of entering cloud puff
331                 double rnd = sg_random();
332                 double chance = rnd * rnd * rnd;
333                 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
334                     in_puff = true;
335                     puff_length = sg_random() * 2.0; // up to 2 seconds
336                     puff_progression = 0.0;
337                 }
338             }
339
340             if ( in_puff ) {
341                 // modify actual_visibility based on puff envelope
342
343                 if ( puff_progression <= ramp_up ) {
344                     double x = 0.5 * SGD_PI * puff_progression / ramp_up;
345                     double factor = 1.0 - sin( x );
346                     // cout << "ramp up = " << puff_progression
347                     //      << "  factor = " << factor << endl;
348                     effvis = effvis * factor;
349                 } else if ( puff_progression >= ramp_up + puff_length ) {
350                     double x = 0.5 * SGD_PI * 
351                         (puff_progression - (ramp_up + puff_length)) /
352                         ramp_down;
353                     double factor = sin( x );
354                     // cout << "ramp down = " 
355                     //      << puff_progression - (ramp_up + puff_length) 
356                     //      << "  factor = " << factor << endl;
357                     effvis = effvis * factor;
358                 } else {
359                     effvis = 0.0;
360                 }
361
362                 /* cout << "len = " << puff_length
363                    << "  x = " << x 
364                    << "  factor = " << factor
365                    << "  actual_visibility = " << actual_visibility 
366                    << endl; */
367
368                 // time_factor = ( global_multi_loop * 
369                 //                 current_options.get_speed_up() ) /
370                 //                (double)current_options.get_model_hz();
371
372                 puff_progression += time_factor;
373                 // cout << "time factor = " << time_factor << endl;
374
375                 /* cout << "gml = " << global_multi_loop 
376                    << "  speed up = " << current_options.get_speed_up()
377                    << "  hz = " << current_options.get_model_hz() << endl;
378                    */ 
379
380                 if ( puff_progression > puff_length + ramp_up + ramp_down) {
381                     in_puff = false; 
382                 }
383             }
384
385             // never let visibility drop below 25 meters
386             if ( effvis <= 25.0 ) {
387                 effvis = 25.0;
388             }
389         }
390     } // for
391
392     effective_visibility = effvis;
393 }
394