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