]> git.mxchange.org Git - simgear.git/blob - simgear/sky/sky.cxx
dc0627f18722036bf7098ef54c9fd5271c68a363
[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/ssg.h>           // plib include
30
31 #include <simgear/constants.h>
32 #include <simgear/math/fg_random.h>
33
34 #include <Objects/matlib.hxx>
35
36 #include "sky.hxx"
37
38
39 // Constructor
40 SGSky::SGSky( void ) {
41     effective_visibility = visibility = 10000.0;
42
43     // near cloud visibility state variables
44     in_puff = false;
45     puff_length = 0;
46     puff_progression = 0;
47     ramp_up = 0.15;
48     ramp_down = 0.15;
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     ssgSimpleState *cloud_state;
102
103     cloud_path.set( tex_path.str() );
104     cloud_path.append( "cirrus.rgba" );
105     cloud_state = SGCloudMakeState( cloud_path.str() );
106     material_lib.add_item( "CloudCirrus", cloud_state );
107
108     cloud_path.set( tex_path.str() );
109     cloud_path.append( "mostlycloudy.rgba" );
110     cloud_state = SGCloudMakeState( cloud_path.str() );
111     material_lib.add_item( "CloudMostlyCloudy", cloud_state );
112
113     cloud_path.set( tex_path.str() );
114     cloud_path.append( "mostlysunny.rgba" );
115     cloud_state = SGCloudMakeState( cloud_path.str() );
116     material_lib.add_item( "CloudMostlySunny", cloud_state );
117
118     cloud_path.set( tex_path.str() );
119     cloud_path.append( "overcast.rgb" );
120     cloud_state = SGCloudMakeState( cloud_path.str() );
121     material_lib.add_item( "CloudOvercast", cloud_state );
122 }
123
124
125 // repaint the sky components based on current value of sun_angle,
126 // sky, and fog colors.
127 //
128 // sun angle in degrees relative to verticle
129 // 0 degrees = high noon
130 // 90 degrees = sun rise/set
131 // 180 degrees = darkest midnight
132 bool SGSky::repaint( sgVec4 sky_color, sgVec4 fog_color,
133                      double sun_angle, double moon_angle,
134                      int nplanets, sgdVec3 *planet_data,
135                      int nstars, sgdVec3 *star_data )
136 {
137     if ( effective_visibility > 1000.0 ) {
138         enable();
139         dome->repaint( sky_color, fog_color, sun_angle );
140         oursun->repaint( sun_angle );
141         moon->repaint( moon_angle );
142         planets->repaint( sun_angle, nplanets, planet_data );
143         stars->repaint( sun_angle, nstars, star_data );
144
145         for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
146             cloud_layers[i]->repaint( fog_color );
147         }
148     } else {
149         // turn off sky
150         disable();
151     }
152
153     return true;
154 }
155
156
157 // reposition the sky at the specified origin and orientation
158 //
159 // lon specifies a rotation about the Z axis
160 // lat specifies a rotation about the new Y axis
161 // spin specifies a rotation about the new Z axis (this allows
162 // additional orientation for the sunrise/set effects and is used by
163 // the skydome and perhaps clouds.
164 bool SGSky::reposition( sgVec3 view_pos, sgVec3 zero_elev, sgVec3 view_up,
165                         double lon, double lat, double alt, double spin,
166                         double gst, 
167                         double sun_ra, double sun_dec, double sun_dist,
168                         double moon_ra, double moon_dec, double moon_dist )
169 {
170     double angle = gst * 15;    // degrees
171     dome->reposition( zero_elev, lon, lat, spin );
172     oursun->reposition( view_pos, angle, sun_ra, sun_dec, sun_dist );
173     moon->reposition( view_pos, angle, moon_ra, moon_dec, moon_dist );
174     planets->reposition( view_pos, angle );
175     stars->reposition( view_pos, angle );
176
177     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
178         cloud_layers[i]->reposition( zero_elev, view_up, lon, lat, alt );
179     }
180
181     return true;
182 }
183
184
185 // draw background portions of the sky
186 void SGSky::draw_background() {
187     ssgCullAndDraw( pre_root );
188 }
189
190
191 // draw scenery elements of the sky
192 void SGSky::draw_scene( float alt ) {
193
194     if ( effective_visibility < 4000.0 ) {
195         // bail and don't draw clouds
196         return;
197     }
198
199     // determine rendering order
200     int pos = 0;
201     while ( pos < (int)cloud_layers.size() && 
202             alt > cloud_layers[pos]->get_asl())
203     {
204         ++pos;
205     }
206
207     if ( pos == 0 ) {
208         // we are below all the cloud layers, draw top to bottom
209         for ( int i = cloud_layers.size() - 1; i >= 0; --i ) {
210             cloud_layers[i]->draw();
211         }
212     } else if ( pos >= (int)cloud_layers.size() ) {
213         // we are above all the cloud layers, draw bottom to top
214         for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
215             cloud_layers[i]->draw();
216         }
217     } else {
218         // we are between cloud layers, draw lower layers bottom to
219         // top and upper layers top to bottom
220         for ( int i = 0; i < pos; ++i ) {
221             cloud_layers[i]->draw();
222         }
223         for ( int i = cloud_layers.size() - 1; i >= pos; --i ) {
224             cloud_layers[i]->draw();
225         }
226     }
227 }
228
229  
230 void SGSky::add_cloud_layer( double asl, double thickness, double transition,
231                              SGCloudType type ) {
232     SGCloudLayer *layer = new SGCloudLayer;
233     layer->build(tex_path, 40000.0f, asl, thickness, transition, type );
234
235     layer_list_iterator current = cloud_layers.begin();
236     layer_list_iterator last = cloud_layers.end();
237     while ( current != last && (*current)->get_asl() < asl ) {
238         ++current;
239     }
240
241     if ( current != last ) {
242         cloud_layers.insert( current, layer );
243     } else {
244         cloud_layers.push_back( layer );
245     }
246
247     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
248         cout << "layer " << i << " = " << cloud_layers[i]->get_asl() << endl;
249     }
250     cout << endl;
251 }
252
253
254 // modify the current visibility based on cloud layers, thickness,
255 // transition range, and simulated "puffs".
256 void SGSky::modify_vis( float alt, float time_factor ) {
257     float effvis = visibility;
258
259     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
260         float asl = cloud_layers[i]->get_asl();
261         float thickness = cloud_layers[i]->get_thickness();
262         float transition = cloud_layers[i]->get_transition();
263
264         double ratio = 1.0;
265
266         if ( alt < asl - transition ) {
267             // below cloud layer
268             ratio = 1.0;
269         } else if ( alt < asl ) {
270             // in lower transition
271             ratio = (asl - alt) / transition;
272         } else if ( alt < asl + thickness ) {
273             // in cloud layer
274             ratio = 0.0;
275         } else if ( alt < asl + thickness + transition ) {
276             // in upper transition
277             ratio = (alt - (asl + thickness)) / transition;
278         } else {
279             // above cloud layer
280             ratio = 1.0;
281         }
282
283         // accumulate effects from multiple cloud layers
284         effvis *= ratio;
285
286         if ( ratio < 1.0 ) {
287             if ( ! in_puff ) {
288                 // calc chance of entering cloud puff
289                 double rnd = fg_random();
290                 double chance = rnd * rnd * rnd;
291                 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
292                     in_puff = true;
293                     do {
294                         puff_length = fg_random() * 2.0; // up to 2 seconds
295                     } while ( puff_length <= 0.0 );
296                     puff_progression = 0.0;
297                 }
298             }
299
300             if ( in_puff ) {
301                 // modify actual_visibility based on puff envelope
302             
303                 if ( puff_progression <= ramp_up ) {
304                     double x = FG_PI_2 * puff_progression / ramp_up;
305                     double factor = 1.0 - sin( x );
306                     effvis = effvis * factor;
307                 } else if ( puff_progression >= ramp_up + puff_length ) {
308                     double x = FG_PI_2 * 
309                         (puff_progression - (ramp_up + puff_length)) /
310                         ramp_down;
311                     double factor = sin( x );
312                     effvis = effvis * factor;
313                 } else {
314                     effvis = 0.0;
315                 }
316
317                 /* cout << "len = " << puff_length
318                    << "  x = " << x 
319                    << "  factor = " << factor
320                    << "  actual_visibility = " << actual_visibility 
321                    << endl; */
322
323                 // time_factor = ( global_multi_loop * 
324                 //                 current_options.get_speed_up() ) /
325                 //                (double)current_options.get_model_hz();
326
327                 puff_progression += time_factor;
328
329                 /* cout << "gml = " << global_multi_loop 
330                    << "  speed up = " << current_options.get_speed_up()
331                    << "  hz = " << current_options.get_model_hz() << endl;
332                    */ 
333
334                 if ( puff_progression > puff_length + ramp_up + ramp_down) {
335                     in_puff = false; 
336                 }
337             }
338
339             // never let visibility drop below zero
340             if ( effvis <= 0 ) {
341                 effvis = 0.1;
342             }
343         }
344     } // for
345
346     effective_visibility = effvis;
347 }
348