]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/sky.cxx
3D clouds from Stuart Buchanan. Need a recent driver update, --enable-clouds3d option...
[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 void SGSky::set_3dClouds(bool enable)
191 {
192     for ( unsigned i = 0; i < cloud_layers.size(); ++i ) {
193         cloud_layers[i]->set_enable3dClouds(enable);
194     }
195
196     clouds_3d_enabled = enable;
197 }
198
199 bool SGSky::get_3dClouds() const {
200     return clouds_3d_enabled;
201 }
202
203 double SGSky::get_3dCloudDensity() const {
204     return SGCloudField::get_density();
205 }
206
207 void SGSky::set_3dCloudDensity(double density)
208 {
209     SGCloudField::set_density(density);
210
211     for ( unsigned i = 0; i < cloud_layers.size(); ++i ) {
212         cloud_layers[i]->applyDensity();
213     }
214 }
215
216 void SGSky::texture_path( const string& path ) {
217         tex_path = SGPath( path );
218 }
219
220 // modify the current visibility based on cloud layers, thickness,
221 // transition range, and simulated "puffs".
222 void SGSky::modify_vis( float alt, float time_factor ) {
223     float effvis = visibility;
224
225     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
226         float asl = cloud_layers[i]->getElevation_m();
227         float thickness = cloud_layers[i]->getThickness_m();
228         float transition = cloud_layers[i]->getTransition_m();
229
230         double ratio = 1.0;
231
232         if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ) {
233             // less than 50% coverage -- assume we're in the clear for now
234             ratio = 1.0;
235         } else if ( alt < asl - transition ) {
236             // below cloud layer
237             ratio = 1.0;
238         } else if ( alt < asl ) {
239             // in lower transition
240             ratio = (asl - alt) / transition;
241         } else if ( alt < asl + thickness ) {
242             // in cloud layer
243             ratio = 0.0;
244         } else if ( alt < asl + thickness + transition ) {
245             // in upper transition
246             ratio = (alt - (asl + thickness)) / transition;
247         } else {
248             // above cloud layer
249             ratio = 1.0;
250         }
251
252         if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ||
253              get_3dClouds()) {
254             // do nothing, clear layers aren't drawn, don't affect
255             // visibility andn dont' need to be faded in or out.
256         } else if ( (cloud_layers[i]->getCoverage() == 
257                      SGCloudLayer::SG_CLOUD_FEW)
258                     || (cloud_layers[i]->getCoverage() ==
259                         SGCloudLayer::SG_CLOUD_SCATTERED) )
260         {
261             // set the alpha fade value for the cloud layer.  For less
262             // dense cloud layers we fade the layer to nothing as we
263             // approach it because we stay clear visibility-wise as we
264             // pass through it.
265             float temp = ratio * 2.0;
266             if ( temp > 1.0 ) { temp = 1.0; }
267             cloud_layers[i]->setAlpha( temp );
268
269             // don't touch visibility
270         } else {
271             // maintain full alpha for denser cloud layer types.
272             // Let's set the value explicitly in case someone changed
273             // the layer type.
274             cloud_layers[i]->setAlpha( 1.0 );
275
276             // lower visibility as we approach the cloud layer.
277             // accumulate effects from multiple cloud layers
278             effvis *= ratio;
279         }
280
281 #if 0
282         if ( ratio < 1.0 ) {
283             if ( ! in_puff ) {
284                 // calc chance of entering cloud puff
285                 double rnd = sg_random();
286                 double chance = rnd * rnd * rnd;
287                 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
288                     in_puff = true;
289                     puff_length = sg_random() * 2.0; // up to 2 seconds
290                     puff_progression = 0.0;
291                 }
292             }
293
294             if ( in_puff ) {
295                 // modify actual_visibility based on puff envelope
296
297                 if ( puff_progression <= ramp_up ) {
298                     double x = SGD_PI_2 * puff_progression / ramp_up;
299                     double factor = 1.0 - sin( x );
300                     // cout << "ramp up = " << puff_progression
301                     //      << "  factor = " << factor << endl;
302                     effvis = effvis * factor;
303                 } else if ( puff_progression >= ramp_up + puff_length ) {
304                     double x = SGD_PI_2 * 
305                         (puff_progression - (ramp_up + puff_length)) /
306                         ramp_down;
307                     double factor = sin( x );
308                     // cout << "ramp down = " 
309                     //      << puff_progression - (ramp_up + puff_length) 
310                     //      << "  factor = " << factor << endl;
311                     effvis = effvis * factor;
312                 } else {
313                     effvis = 0.0;
314                 }
315
316                 /* cout << "len = " << puff_length
317                    << "  x = " << x 
318                    << "  factor = " << factor
319                    << "  actual_visibility = " << actual_visibility 
320                    << endl; */
321
322                 // time_factor = ( global_multi_loop * 
323                 //                 current_options.get_speed_up() ) /
324                 //                (double)current_options.get_model_hz();
325
326                 puff_progression += time_factor;
327                 // cout << "time factor = " << time_factor << endl;
328
329                 /* cout << "gml = " << global_multi_loop 
330                    << "  speed up = " << current_options.get_speed_up()
331                    << "  hz = " << current_options.get_model_hz() << endl;
332                    */ 
333
334                 if ( puff_progression > puff_length + ramp_up + ramp_down) {
335                     in_puff = false; 
336                 }
337             }
338         }
339 #endif
340
341         // never let visibility drop below 25 meters
342         if ( effvis <= 25.0 ) {
343             effvis = 25.0;
344         }
345
346     } // for
347
348     effective_visibility = effvis;
349 }
350
351