]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/sky.cxx
Added some OSG headers for the correct evaluation of the OSG_VERSION_LESS_THAN macro.
[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/scene/util/RenderConstants.hxx>
33 #include <simgear/scene/util/OsgMath.hxx>
34 #include <simgear/sg_inlines.h>
35
36 #include <osg/StateSet>
37 #include <osg/Depth>
38
39 // Constructor
40 SGSky::SGSky( void ) {
41     effective_visibility = visibility = 10000.0;
42
43     // near cloud visibility state variables
44     in_puff = false;
45     puff_length = 0;
46     puff_progression = 0;
47     ramp_up = 0.15;
48     ramp_down = 0.15;
49
50     in_cloud  = -1;
51     
52     clouds_3d_enabled = false;
53     clouds_3d_density = 0.8;
54
55     pre_root = new osg::Group;
56     pre_root->setNodeMask(simgear::BACKGROUND_BIT);
57     osg::StateSet* preStateSet = new osg::StateSet;
58     preStateSet->setAttribute(new osg::Depth(osg::Depth::LESS, 0.0, 1.0,
59                                              false));
60     pre_root->setStateSet(preStateSet);
61     cloud_root = new osg::Switch;
62     cloud_root->setNodeMask(simgear::MODEL_BIT);
63     cloud_root->setName("SGSky-cloud-root");
64
65     pre_transform = new osg::Group;
66     _ephTransform = new osg::MatrixTransform;
67     
68     // Set up a RNG that is repeatable within 10 minutes to ensure that clouds
69     // are synced up in multi-process deployments.
70     mt_init_time_10(&seed);
71 }
72
73
74 // Destructor
75 SGSky::~SGSky( void )
76 {
77 }
78
79
80 // initialize the sky and connect the components to the scene graph at
81 // the provided branch
82 void SGSky::build( double h_radius_m,
83                    double v_radius_m,
84                    double sun_size,
85                    double moon_size,
86                    const SGEphemeris& eph,
87                    SGPropertyNode *property_tree_node,
88                    simgear::SGReaderWriterOptions* options )
89 {
90     dome = new SGSkyDome;
91     pre_transform->addChild( dome->build( h_radius_m, v_radius_m, options ) );
92
93     pre_transform->addChild(_ephTransform.get());
94     planets = new SGStars;
95     _ephTransform->addChild( planets->build(eph.getNumPlanets(), eph.getPlanets(), h_radius_m) );
96
97     stars = new SGStars;
98     _ephTransform->addChild( stars->build(eph.getNumStars(), eph.getStars(), h_radius_m) );
99     
100     moon = new SGMoon;
101     _ephTransform->addChild( moon->build(tex_path, moon_size) );
102
103     oursun = new SGSun;
104     _ephTransform->addChild( oursun->build(tex_path, sun_size, property_tree_node ) );
105
106     pre_root->addChild( pre_transform.get() );
107 }
108
109
110 // repaint the sky components based on current value of sun_angle,
111 // sky, and fog colors.
112 //
113 // sun angle in degrees relative to verticle
114 // 0 degrees = high noon
115 // 90 degrees = sun rise/set
116 // 180 degrees = darkest midnight
117 bool SGSky::repaint( const SGSkyColor &sc, const SGEphemeris& eph )
118 {
119         dome->repaint( sc.adj_sky_color, sc.sky_color, sc.fog_color,
120                        sc.sun_angle, effective_visibility );
121
122         stars->repaint( sc.sun_angle, eph.getNumStars(), eph.getStars() );
123         planets->repaint( sc.sun_angle, eph.getNumPlanets(), eph.getPlanets() );
124         oursun->repaint( sc.sun_angle, effective_visibility );
125         moon->repaint( sc.moon_angle );
126
127         for ( unsigned i = 0; i < cloud_layers.size(); ++i ) {
128             if (cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR){
129                 cloud_layers[i]->repaint( sc.cloud_color );
130             }
131         }
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]->get_layer3D()->isDefined3D() ) {
182             cloud_layers[i]->reposition( zero_elev, view_up, lon, lat, alt, dt);
183         } else {
184           cloud_layers[i]->getNode()->setAllChildrenOff();
185     }
186     }
187
188     return true;
189 }
190
191 void
192 SGSky::set_visibility( float v )
193 {
194     visibility = std::max(v, 25.0f);
195 }
196
197 void
198 SGSky::add_cloud_layer( SGCloudLayer * layer )
199 {
200     cloud_layers.push_back(layer);
201     cloud_root->addChild(layer->getNode(), true);
202
203     layer->set_enable3dClouds(clouds_3d_enabled);
204 }
205
206 const SGCloudLayer *
207 SGSky::get_cloud_layer (int i) const
208 {
209     return cloud_layers[i];
210 }
211
212 SGCloudLayer *
213 SGSky::get_cloud_layer (int i)
214 {
215     return cloud_layers[i];
216 }
217
218 int
219 SGSky::get_cloud_layer_count () const
220 {
221     return cloud_layers.size();
222 }
223
224 double SGSky::get_3dCloudDensity() const {
225     return SGNewCloud::getDensity();
226 }
227
228 void SGSky::set_3dCloudDensity(double density)
229 {
230     SGNewCloud::setDensity(density);
231 }
232
233 float SGSky::get_3dCloudVisRange() const {
234     return SGCloudField::getVisRange();
235 }
236
237 void SGSky::set_3dCloudVisRange(float vis)
238 {
239     SGCloudField::setVisRange(vis);
240     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
241         cloud_layers[i]->get_layer3D()->applyVisAndLoDRange();
242     }
243 }
244
245 float SGSky::get_3dCloudImpostorDistance() const {
246     return SGCloudField::getImpostorDistance();
247 }
248
249 void SGSky::set_3dCloudImpostorDistance(float vis)
250 {
251     SGCloudField::setImpostorDistance(vis);
252     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
253         cloud_layers[i]->get_layer3D()->applyVisAndLoDRange();
254     }
255 }
256
257 float SGSky::get_3dCloudLoD1Range() const {
258     return SGCloudField::getLoD1Range();
259 }
260
261 void SGSky::set_3dCloudLoD1Range(float vis)
262 {
263     SGCloudField::setLoD1Range(vis);
264     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
265         cloud_layers[i]->get_layer3D()->applyVisAndLoDRange();
266     }
267 }
268
269 float SGSky::get_3dCloudLoD2Range() const {
270     return SGCloudField::getLoD2Range();
271 }
272
273 void SGSky::set_3dCloudLoD2Range(float vis)
274 {
275     SGCloudField::setLoD2Range(vis);
276     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
277         cloud_layers[i]->get_layer3D()->applyVisAndLoDRange();
278     }
279 }
280
281 bool SGSky::get_3dCloudWrap() const {
282     return SGCloudField::getWrap();
283 }
284
285 void SGSky::set_3dCloudWrap(bool wrap)
286 {
287     SGCloudField::setWrap(wrap);
288 }
289
290 bool SGSky::get_3dCloudUseImpostors() const {
291     return SGCloudField::getUseImpostors();
292 }
293
294 void SGSky::set_3dCloudUseImpostors(bool imp)
295 {
296     SGCloudField::setUseImpostors(imp);
297 }
298
299 void SGSky::texture_path( const std::string& path ) {
300         tex_path = SGPath( path );
301 }
302
303 // modify the current visibility based on cloud layers, thickness,
304 // transition range, and simulated "puffs".
305 void SGSky::modify_vis( float alt, float time_factor ) {
306     float effvis = visibility;
307
308     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
309         float asl = cloud_layers[i]->getElevation_m();
310         float thickness = cloud_layers[i]->getThickness_m();
311         float transition = cloud_layers[i]->getTransition_m();
312
313         double ratio = 1.0;
314
315         if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ) {
316             // less than 50% coverage -- assume we're in the clear for now
317             ratio = 1.0;
318         } else if ( alt < asl - transition ) {
319             // below cloud layer
320             ratio = 1.0;
321         } else if ( alt < asl ) {
322             // in lower transition
323             ratio = (asl - alt) / transition;
324         } else if ( alt < asl + thickness ) {
325             // in cloud layer
326             ratio = 0.0;
327         } else if ( alt < asl + thickness + transition ) {
328             // in upper transition
329             ratio = (alt - (asl + thickness)) / transition;
330         } else {
331             // above cloud layer
332             ratio = 1.0;
333         }
334
335         if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ||
336              cloud_layers[i]->get_layer3D()->isDefined3D()) {
337             // do nothing, clear layers aren't drawn, don't affect
338             // visibility andn dont' need to be faded in or out.
339         } else if ( (cloud_layers[i]->getCoverage() == 
340                      SGCloudLayer::SG_CLOUD_FEW)
341                     || (cloud_layers[i]->getCoverage() ==
342                         SGCloudLayer::SG_CLOUD_SCATTERED) )
343         {
344             // set the alpha fade value for the cloud layer.  For less
345             // dense cloud layers we fade the layer to nothing as we
346             // approach it because we stay clear visibility-wise as we
347             // pass through it.
348             float temp = ratio * 2.0;
349             if ( temp > 1.0 ) { temp = 1.0; }
350             cloud_layers[i]->setAlpha( temp );
351
352             // don't touch visibility
353         } else {
354             // maintain full alpha for denser cloud layer types.
355             // Let's set the value explicitly in case someone changed
356             // the layer type.
357             cloud_layers[i]->setAlpha( 1.0 );
358
359             // lower visibility as we approach the cloud layer.
360             // accumulate effects from multiple cloud layers
361             effvis *= ratio;
362         }
363
364 #if 0
365         if ( ratio < 1.0 ) {
366             if ( ! in_puff ) {
367                 // calc chance of entering cloud puff
368                 double rnd = mt_rand(&seed);
369                 double chance = rnd * rnd * rnd;
370                 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
371                     in_puff = true;
372                     puff_length = mt_rand(&seed) * 2.0; // up to 2 seconds
373                     puff_progression = 0.0;
374                 }
375             }
376
377             if ( in_puff ) {
378                 // modify actual_visibility based on puff envelope
379
380                 if ( puff_progression <= ramp_up ) {
381                     double x = SGD_PI_2 * puff_progression / ramp_up;
382                     double factor = 1.0 - sin( x );
383                     // cout << "ramp up = " << puff_progression
384                     //      << "  factor = " << factor << endl;
385                     effvis = effvis * factor;
386                 } else if ( puff_progression >= ramp_up + puff_length ) {
387                     double x = SGD_PI_2 * 
388                         (puff_progression - (ramp_up + puff_length)) /
389                         ramp_down;
390                     double factor = sin( x );
391                     // cout << "ramp down = " 
392                     //      << puff_progression - (ramp_up + puff_length) 
393                     //      << "  factor = " << factor << endl;
394                     effvis = effvis * factor;
395                 } else {
396                     effvis = 0.0;
397                 }
398
399                 /* cout << "len = " << puff_length
400                    << "  x = " << x 
401                    << "  factor = " << factor
402                    << "  actual_visibility = " << actual_visibility 
403                    << endl; */
404
405                 // time_factor = ( global_multi_loop * 
406                 //                 current_options.get_speed_up() ) /
407                 //                (double)current_options.get_model_hz();
408
409                 puff_progression += time_factor;
410                 // cout << "time factor = " << time_factor << endl;
411
412                 /* cout << "gml = " << global_multi_loop 
413                    << "  speed up = " << current_options.get_speed_up()
414                    << "  hz = " << current_options.get_model_hz() << endl;
415                    */ 
416
417                 if ( puff_progression > puff_length + ramp_up + ramp_down) {
418                     in_puff = false; 
419                 }
420             }
421         }
422 #endif
423
424         // never let visibility drop below the layer's configured visibility
425        effvis = SG_MAX2<float>(cloud_layers[i]->getVisibility_m(), effvis );
426
427     } // for
428
429     effective_visibility = effvis;
430 }
431
432 void SGSky::set_clouds_enabled(bool enabled)
433 {
434     if (enabled) {
435         cloud_root->setAllChildrenOn();
436     } else {
437         cloud_root->setAllChildrenOff();
438     }
439 }