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