]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/sky.cxx
Modified Files:
[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  - http://www.flightgear.org/~curt
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 General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 // $Id$
23
24 #ifdef HAVE_CONFIG_H
25 #  include <simgear_config.h>
26 #endif
27
28 #include <simgear/math/sg_random.h>
29
30 #include "sky.hxx"
31 #include "cloudfield.hxx"
32
33 // Constructor
34 SGSky::SGSky( void ) {
35     effective_visibility = visibility = 10000.0;
36
37     // near cloud visibility state variables
38     in_puff = false;
39     puff_length = 0;
40     puff_progression = 0;
41     ramp_up = 0.15;
42     ramp_down = 0.15;
43
44     in_cloud  = -1;
45
46     pre_root = new osg::Group;
47     post_root = new osg::Group;
48     cloud_root = new osg::Group;
49
50     pre_selector = new osg::Switch;
51     post_selector = new osg::Switch;
52
53     pre_transform = new osg::MatrixTransform;
54     post_transform = new osg::MatrixTransform;
55 }
56
57
58 // Destructor
59 SGSky::~SGSky( void )
60 {
61 }
62
63
64 // initialize the sky and connect the components to the scene graph at
65 // the provided branch
66 void SGSky::build( double h_radius_m, double v_radius_m,
67                    double sun_size, double moon_size,
68                    int nplanets, SGVec3d planet_data[7],
69                    int nstars, SGVec3d star_data[], SGPropertyNode *property_tree_node )
70 {
71     dome = new SGSkyDome;
72     pre_transform->addChild( dome->build( h_radius_m, v_radius_m ) );
73
74     planets = new SGStars;
75     pre_transform->addChild(planets->build(nplanets, planet_data, h_radius_m));
76
77     stars = new SGStars;
78     pre_transform->addChild( stars->build(nstars, star_data, h_radius_m) );
79     
80     moon = new SGMoon;
81     pre_transform->addChild( moon->build(tex_path, moon_size) );
82
83     oursun = new SGSun;
84     pre_transform->addChild( oursun->build(tex_path, sun_size, property_tree_node ) );
85
86     pre_selector->addChild( pre_transform.get() );
87
88     post_selector->addChild( post_transform.get() );
89
90     pre_root->addChild( pre_selector.get() );
91     post_root->addChild( post_selector.get() );
92 }
93
94
95 // repaint the sky components based on current value of sun_angle,
96 // sky, and fog colors.
97 //
98 // sun angle in degrees relative to verticle
99 // 0 degrees = high noon
100 // 90 degrees = sun rise/set
101 // 180 degrees = darkest midnight
102 bool SGSky::repaint( const SGSkyColor &sc )
103 {
104     if ( effective_visibility > 1000.0 ) {
105         enable();
106         dome->repaint( sc.sky_color, sc.fog_color, sc.sun_angle,
107                        effective_visibility );
108
109         stars->repaint( sc.sun_angle, sc.nstars, sc.star_data );
110         planets->repaint( sc.sun_angle, sc.nplanets, sc.planet_data );
111         oursun->repaint( sc.sun_angle, effective_visibility );
112         moon->repaint( sc.moon_angle );
113
114         for ( unsigned i = 0; i < cloud_layers.size(); ++i ) {
115             if (cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR){
116                 cloud_layers[i]->repaint( sc.cloud_color );
117             }
118         }
119     } else {
120         // turn off sky
121         disable();
122     }
123
124     return true;
125 }
126
127
128 // reposition the sky at the specified origin and orientation
129 //
130 // lon specifies a rotation about the Z axis
131 // lat specifies a rotation about the new Y axis
132 // spin specifies a rotation about the new Z axis (this allows
133 // additional orientation for the sunrise/set effects and is used by
134 // the skydome and perhaps clouds.
135 bool SGSky::reposition( SGSkyState &st, double dt )
136 {
137
138     double angle = st.gst * 15; // degrees
139
140     dome->reposition( st.zero_elev, st.alt, st.lon, st.lat, st.spin );
141
142     stars->reposition( st.view_pos, angle );
143     planets->reposition( st.view_pos, angle );
144
145     oursun->reposition( st.view_pos, angle,
146                         st.sun_ra, st.sun_dec, st.sun_dist, st.lat, st.alt, st.sun_angle );
147
148     moon->reposition( st.view_pos, angle,
149                       st.moon_ra, st.moon_dec, st.moon_dist );
150
151     for ( unsigned i = 0; i < cloud_layers.size(); ++i ) {
152         if ( cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR ) {
153             cloud_layers[i]->reposition( st.zero_elev, st.view_up,
154                                          st.lon, st.lat, st.alt, dt );
155         } else
156           cloud_layers[i]->getNode()->setAllChildrenOff();
157     }
158
159     return true;
160 }
161
162 void
163 SGSky::add_cloud_layer( SGCloudLayer * layer )
164 {
165     cloud_layers.push_back(layer);
166     cloud_root->addChild(layer->getNode());
167 }
168
169 const SGCloudLayer *
170 SGSky::get_cloud_layer (int i) const
171 {
172     return cloud_layers[i];
173 }
174
175 SGCloudLayer *
176 SGSky::get_cloud_layer (int i)
177 {
178     return cloud_layers[i];
179 }
180
181 int
182 SGSky::get_cloud_layer_count () const
183 {
184     return cloud_layers.size();
185 }
186
187 // modify the current visibility based on cloud layers, thickness,
188 // transition range, and simulated "puffs".
189 void SGSky::modify_vis( float alt, float time_factor ) {
190     float effvis = visibility;
191
192     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
193         float asl = cloud_layers[i]->getElevation_m();
194         float thickness = cloud_layers[i]->getThickness_m();
195         float transition = cloud_layers[i]->getTransition_m();
196
197         double ratio = 1.0;
198
199         if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ) {
200             // less than 50% coverage -- assume we're in the clear for now
201             ratio = 1.0;
202         } else if ( alt < asl - transition ) {
203             // below cloud layer
204             ratio = 1.0;
205         } else if ( alt < asl ) {
206             // in lower transition
207             ratio = (asl - alt) / transition;
208         } else if ( alt < asl + thickness ) {
209             // in cloud layer
210             ratio = 0.0;
211         } else if ( alt < asl + thickness + transition ) {
212             // in upper transition
213             ratio = (alt - (asl + thickness)) / transition;
214         } else {
215             // above cloud layer
216             ratio = 1.0;
217         }
218
219         if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ||
220                         cloud_layers[i]->get_layer3D()->is3D() && SGCloudField::enable3D) {
221             // do nothing, clear layers aren't drawn, don't affect
222             // visibility andn dont' need to be faded in or out.
223         } else if ( (cloud_layers[i]->getCoverage() == 
224                      SGCloudLayer::SG_CLOUD_FEW)
225                     || (cloud_layers[i]->getCoverage() ==
226                         SGCloudLayer::SG_CLOUD_SCATTERED) )
227         {
228             // set the alpha fade value for the cloud layer.  For less
229             // dense cloud layers we fade the layer to nothing as we
230             // approach it because we stay clear visibility-wise as we
231             // pass through it.
232             float temp = ratio * 2.0;
233             if ( temp > 1.0 ) { temp = 1.0; }
234             cloud_layers[i]->setAlpha( temp );
235
236             // don't touch visibility
237         } else {
238             // maintain full alpha for denser cloud layer types.
239             // Let's set the value explicitly in case someone changed
240             // the layer type.
241             cloud_layers[i]->setAlpha( 1.0 );
242
243             // lower visibility as we approach the cloud layer.
244             // accumulate effects from multiple cloud layers
245             effvis *= ratio;
246         }
247
248 #if 0
249         if ( ratio < 1.0 ) {
250             if ( ! in_puff ) {
251                 // calc chance of entering cloud puff
252                 double rnd = sg_random();
253                 double chance = rnd * rnd * rnd;
254                 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
255                     in_puff = true;
256                     puff_length = sg_random() * 2.0; // up to 2 seconds
257                     puff_progression = 0.0;
258                 }
259             }
260
261             if ( in_puff ) {
262                 // modify actual_visibility based on puff envelope
263
264                 if ( puff_progression <= ramp_up ) {
265                     double x = SGD_PI_2 * puff_progression / ramp_up;
266                     double factor = 1.0 - sin( x );
267                     // cout << "ramp up = " << puff_progression
268                     //      << "  factor = " << factor << endl;
269                     effvis = effvis * factor;
270                 } else if ( puff_progression >= ramp_up + puff_length ) {
271                     double x = SGD_PI_2 * 
272                         (puff_progression - (ramp_up + puff_length)) /
273                         ramp_down;
274                     double factor = sin( x );
275                     // cout << "ramp down = " 
276                     //      << puff_progression - (ramp_up + puff_length) 
277                     //      << "  factor = " << factor << endl;
278                     effvis = effvis * factor;
279                 } else {
280                     effvis = 0.0;
281                 }
282
283                 /* cout << "len = " << puff_length
284                    << "  x = " << x 
285                    << "  factor = " << factor
286                    << "  actual_visibility = " << actual_visibility 
287                    << endl; */
288
289                 // time_factor = ( global_multi_loop * 
290                 //                 current_options.get_speed_up() ) /
291                 //                (double)current_options.get_model_hz();
292
293                 puff_progression += time_factor;
294                 // cout << "time factor = " << time_factor << endl;
295
296                 /* cout << "gml = " << global_multi_loop 
297                    << "  speed up = " << current_options.get_speed_up()
298                    << "  hz = " << current_options.get_model_hz() << endl;
299                    */ 
300
301                 if ( puff_progression > puff_length + ramp_up + ramp_down) {
302                     in_puff = false; 
303                 }
304             }
305         }
306 #endif
307
308         // never let visibility drop below 25 meters
309         if ( effvis <= 25.0 ) {
310             effvis = 25.0;
311         }
312
313     } // for
314
315     effective_visibility = effvis;
316 }