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