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