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