]> git.mxchange.org Git - simgear.git/blob - simgear/sky/sky.cxx
Updates to remove unneeded dependencies on FlightGear and SimGear.
[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
179 void SGSky::draw_background() {
180     ssgCullAndDraw( pre_root );
181 }
182
183
184 // draw scenery elements of the sky
185 void SGSky::draw_scene( float alt ) {
186
187     if ( effective_visibility < 4000.0 ) {
188         // bail and don't draw clouds
189         return;
190     }
191
192     // determine rendering order
193     int pos = 0;
194     while ( pos < (int)cloud_layers.size() && 
195             alt > cloud_layers[pos]->get_asl())
196     {
197         ++pos;
198     }
199
200     if ( pos == 0 ) {
201         // we are below all the cloud layers, draw top to bottom
202         for ( int i = cloud_layers.size() - 1; i >= 0; --i ) {
203             cloud_layers[i]->draw();
204         }
205     } else if ( pos >= (int)cloud_layers.size() ) {
206         // we are above all the cloud layers, draw bottom to top
207         for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
208             cloud_layers[i]->draw();
209         }
210     } else {
211         // we are between cloud layers, draw lower layers bottom to
212         // top and upper layers top to bottom
213         for ( int i = 0; i < pos; ++i ) {
214             cloud_layers[i]->draw();
215         }
216         for ( int i = cloud_layers.size() - 1; i >= pos; --i ) {
217             cloud_layers[i]->draw();
218         }
219     }
220 }
221
222  
223 void SGSky::add_cloud_layer( double asl, double thickness, double transition,
224                              ssgSimpleState *state ) {
225     SGCloudLayer *layer = new SGCloudLayer;
226     layer->build( 40000.0f, asl, thickness, transition, state );
227
228     layer_list_iterator current = cloud_layers.begin();
229     layer_list_iterator last = cloud_layers.end();
230     while ( current != last && (*current)->get_asl() < asl ) {
231         ++current;
232     }
233
234     if ( current != last ) {
235         cloud_layers.insert( current, layer );
236     } else {
237         cloud_layers.push_back( layer );
238     }
239
240     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
241         cout << "layer " << i << " = " << cloud_layers[i]->get_asl() << endl;
242     }
243     cout << endl;
244 }
245
246
247 void SGSky::add_cloud_layer( double asl, double thickness, double transition,
248                              const string &tex_path ) {
249     ssgSimpleState *state = SGCloudMakeState( tex_path );
250     add_cloud_layer( asl, thickness, transition, state );
251 }
252
253
254 void SGSky::add_cloud_layer( double asl, double thickness, double transition,
255                              SGCloudType type ) {
256     if ( type > 0 && type < SG_MAX_CLOUD_TYPES ) {
257         add_cloud_layer( asl, thickness, transition, cloud_mats[type] );
258     }
259 }
260
261
262 // modify the current visibility based on cloud layers, thickness,
263 // transition range, and simulated "puffs".
264 void SGSky::modify_vis( float alt, float time_factor ) {
265     float effvis = visibility;
266
267     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
268         float asl = cloud_layers[i]->get_asl();
269         float thickness = cloud_layers[i]->get_thickness();
270         float transition = cloud_layers[i]->get_transition();
271
272         double ratio = 1.0;
273
274         if ( alt < asl - transition ) {
275             // below cloud layer
276             ratio = 1.0;
277         } else if ( alt < asl ) {
278             // in lower transition
279             ratio = (asl - alt) / transition;
280         } else if ( alt < asl + thickness ) {
281             // in cloud layer
282             ratio = 0.0;
283         } else if ( alt < asl + thickness + transition ) {
284             // in upper transition
285             ratio = (alt - (asl + thickness)) / transition;
286         } else {
287             // above cloud layer
288             ratio = 1.0;
289         }
290
291         // accumulate effects from multiple cloud layers
292         effvis *= ratio;
293
294         if ( ratio < 1.0 ) {
295             if ( ! in_puff ) {
296                 // calc chance of entering cloud puff
297                 double rnd = fg_random();
298                 double chance = rnd * rnd * rnd;
299                 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
300                     in_puff = true;
301                     do {
302                         puff_length = fg_random() * 2.0; // up to 2 seconds
303                     } while ( puff_length <= 0.0 );
304                     puff_progression = 0.0;
305                 }
306             }
307
308             if ( in_puff ) {
309                 // modify actual_visibility based on puff envelope
310             
311                 if ( puff_progression <= ramp_up ) {
312                     double x = 2 * SGD_PI * puff_progression / ramp_up;
313                     double factor = 1.0 - sin( x );
314                     effvis = effvis * factor;
315                 } else if ( puff_progression >= ramp_up + puff_length ) {
316                     double x = 2 * SGD_PI * 
317                         (puff_progression - (ramp_up + puff_length)) /
318                         ramp_down;
319                     double factor = sin( x );
320                     effvis = effvis * factor;
321                 } else {
322                     effvis = 0.0;
323                 }
324
325                 /* cout << "len = " << puff_length
326                    << "  x = " << x 
327                    << "  factor = " << factor
328                    << "  actual_visibility = " << actual_visibility 
329                    << endl; */
330
331                 // time_factor = ( global_multi_loop * 
332                 //                 current_options.get_speed_up() ) /
333                 //                (double)current_options.get_model_hz();
334
335                 puff_progression += time_factor;
336
337                 /* cout << "gml = " << global_multi_loop 
338                    << "  speed up = " << current_options.get_speed_up()
339                    << "  hz = " << current_options.get_model_hz() << endl;
340                    */ 
341
342                 if ( puff_progression > puff_length + ramp_up + ramp_down) {
343                     in_puff = false; 
344                 }
345             }
346
347             // never let visibility drop below zero
348             if ( effvis <= 0 ) {
349                 effvis = 0.1;
350             }
351         }
352     } // for
353
354     effective_visibility = effvis;
355 }
356