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