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