1 // sky.cxx -- ssg based sky model
3 // Written by Curtis Olson, started December 1997.
4 // SSG-ified by Curtis Olson, February 2000.
6 // Copyright (C) 1997-2000 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
25 # include <simgear_config.h>
29 #include "cloudfield.hxx"
30 #include "newcloud.hxx"
32 #include <simgear/scene/util/RenderConstants.hxx>
33 #include <simgear/scene/util/OsgMath.hxx>
34 #include <simgear/sg_inlines.h>
36 #include <osg/StateSet>
40 SGSky::SGSky( void ) {
41 effective_visibility = visibility = 10000.0;
42 minimum_sky_visibility = 1000;
44 // near cloud visibility state variables
53 clouds_3d_enabled = false;
54 clouds_3d_density = 0.8;
56 pre_root = new osg::Group;
57 pre_root->setNodeMask(simgear::BACKGROUND_BIT);
58 osg::StateSet* preStateSet = new osg::StateSet;
59 preStateSet->setAttribute(new osg::Depth(osg::Depth::LESS, 0.0, 1.0,
61 pre_root->setStateSet(preStateSet);
62 cloud_root = new osg::Group;
63 cloud_root->setNodeMask(simgear::MODEL_BIT);
64 cloud_root->setName("SGSky-cloud-root");
65 pre_selector = new osg::Switch;
67 pre_transform = new osg::Group;
69 _ephTransform = new osg::MatrixTransform;
71 // Set up a RNG that is repeatable within 10 minutes to ensure that clouds
72 // are synced up in multi-process deployments.
73 mt_init_time_10(&seed);
83 // initialize the sky and connect the components to the scene graph at
84 // the provided branch
85 void SGSky::build( double h_radius_m, double v_radius_m,
86 double sun_size, double moon_size,
87 const SGEphemeris& eph, SGPropertyNode *property_tree_node,
88 simgear::SGReaderWriterOptions* options )
91 pre_transform->addChild( dome->build( h_radius_m, v_radius_m, options ) );
93 pre_transform->addChild(_ephTransform.get());
94 planets = new SGStars;
95 _ephTransform->addChild( planets->build(eph.getNumPlanets(), eph.getPlanets(), h_radius_m) );
98 _ephTransform->addChild( stars->build(eph.getNumStars(), eph.getStars(), h_radius_m) );
101 _ephTransform->addChild( moon->build(tex_path, moon_size) );
104 _ephTransform->addChild( oursun->build(tex_path, sun_size, property_tree_node ) );
106 pre_selector->addChild( pre_transform.get() );
108 pre_root->addChild( pre_selector.get() );
112 // repaint the sky components based on current value of sun_angle,
113 // sky, and fog colors.
115 // sun angle in degrees relative to verticle
116 // 0 degrees = high noon
117 // 90 degrees = sun rise/set
118 // 180 degrees = darkest midnight
119 bool SGSky::repaint( const SGSkyColor &sc, const SGEphemeris& eph )
121 if ( effective_visibility > minimum_sky_visibility ) {
123 dome->repaint( sc.adj_sky_color, sc.sky_color, sc.fog_color,
124 sc.sun_angle, effective_visibility );
126 stars->repaint( sc.sun_angle, eph.getNumStars(), eph.getStars() );
127 planets->repaint( sc.sun_angle, eph.getNumPlanets(), eph.getPlanets() );
128 oursun->repaint( sc.sun_angle, effective_visibility );
129 moon->repaint( sc.moon_angle );
131 for ( unsigned i = 0; i < cloud_layers.size(); ++i ) {
132 if (cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR){
133 cloud_layers[i]->repaint( sc.cloud_color );
140 SGCloudField::updateFog((double)effective_visibility,
141 osg::Vec4f(toOsg(sc.fog_color), 1.0f));
145 // reposition the sky at the specified origin and orientation
147 // lon specifies a rotation about the Z axis
148 // lat specifies a rotation about the new Y axis
149 // spin specifies a rotation about the new Z axis (this allows
150 // additional orientation for the sunrise/set effects and is used by
151 // the skydome and perhaps clouds.
152 bool SGSky::reposition( const SGSkyState &st, const SGEphemeris& eph, double dt )
154 double angle = st.gst * 15; // degrees
155 double angleRad = SGMiscd::deg2rad(angle);
157 SGVec3f zero_elev, view_up;
158 double lon, lat, alt;
160 SGGeod geodZeroViewPos = SGGeod::fromGeodM(st.pos_geod, 0);
161 zero_elev = toVec3f( SGVec3d::fromGeod(geodZeroViewPos) );
163 // calculate the scenery up vector
164 SGQuatd hlOr = SGQuatd::fromLonLat(st.pos_geod);
165 view_up = toVec3f(hlOr.backTransform(-SGVec3d::e3()));
168 lon = st.pos_geod.getLongitudeRad();
169 lat = st.pos_geod.getLatitudeRad();
170 alt = st.pos_geod.getElevationM();
172 dome->reposition( zero_elev, alt, lon, lat, st.spin );
174 osg::Matrix m = osg::Matrix::rotate(angleRad, osg::Vec3(0, 0, -1));
175 m.postMultTranslate(toOsg(st.pos));
176 _ephTransform->setMatrix(m);
178 double sun_ra = eph.getSunRightAscension();
179 double sun_dec = eph.getSunDeclination();
180 oursun->reposition( sun_ra, sun_dec, st.sun_dist, lat, alt, st.sun_angle );
182 double moon_ra = eph.getMoonRightAscension();
183 double moon_dec = eph.getMoonDeclination();
184 moon->reposition( moon_ra, moon_dec, st.moon_dist );
186 for ( unsigned i = 0; i < cloud_layers.size(); ++i ) {
187 if ( cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR ||
188 cloud_layers[i]->get_layer3D()->isDefined3D() ) {
189 cloud_layers[i]->reposition( zero_elev, view_up, lon, lat, alt, dt);
191 cloud_layers[i]->getNode()->setAllChildrenOff();
199 SGSky::add_cloud_layer( SGCloudLayer * layer )
201 cloud_layers.push_back(layer);
202 cloud_root->addChild(layer->getNode());
204 layer->set_enable3dClouds(clouds_3d_enabled);
208 SGSky::get_cloud_layer (int i) const
210 return cloud_layers[i];
214 SGSky::get_cloud_layer (int i)
216 return cloud_layers[i];
220 SGSky::get_cloud_layer_count () const
222 return cloud_layers.size();
225 double SGSky::get_3dCloudDensity() const {
226 return SGNewCloud::getDensity();
229 void SGSky::set_3dCloudDensity(double density)
231 SGNewCloud::setDensity(density);
234 float SGSky::get_3dCloudVisRange() const {
235 return SGCloudField::getVisRange();
238 void SGSky::set_3dCloudVisRange(float vis)
240 SGCloudField::setVisRange(vis);
241 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
242 cloud_layers[i]->get_layer3D()->applyVisAndLoDRange();
246 float SGSky::get_3dCloudImpostorDistance() const {
247 return SGCloudField::getImpostorDistance();
250 void SGSky::set_3dCloudImpostorDistance(float vis)
252 SGCloudField::setImpostorDistance(vis);
253 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
254 cloud_layers[i]->get_layer3D()->applyVisAndLoDRange();
258 float SGSky::get_3dCloudLoD1Range() const {
259 return SGCloudField::getLoD1Range();
262 void SGSky::set_3dCloudLoD1Range(float vis)
264 SGCloudField::setLoD1Range(vis);
265 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
266 cloud_layers[i]->get_layer3D()->applyVisAndLoDRange();
270 float SGSky::get_3dCloudLoD2Range() const {
271 return SGCloudField::getLoD2Range();
274 void SGSky::set_3dCloudLoD2Range(float vis)
276 SGCloudField::setLoD2Range(vis);
277 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
278 cloud_layers[i]->get_layer3D()->applyVisAndLoDRange();
282 bool SGSky::get_3dCloudWrap() const {
283 return SGCloudField::getWrap();
286 void SGSky::set_3dCloudWrap(bool wrap)
288 SGCloudField::setWrap(wrap);
291 bool SGSky::get_3dCloudUseImpostors() const {
292 return SGCloudField::getUseImpostors();
295 void SGSky::set_3dCloudUseImpostors(bool imp)
297 SGCloudField::setUseImpostors(imp);
300 float SGSky::get_minimum_sky_visibility() const
302 return minimum_sky_visibility;
305 void SGSky::set_minimum_sky_visibility( float value )
307 minimum_sky_visibility = value;
310 void SGSky::texture_path( const string& path ) {
311 tex_path = SGPath( path );
314 // modify the current visibility based on cloud layers, thickness,
315 // transition range, and simulated "puffs".
316 void SGSky::modify_vis( float alt, float time_factor ) {
317 float effvis = visibility;
319 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
320 float asl = cloud_layers[i]->getElevation_m();
321 float thickness = cloud_layers[i]->getThickness_m();
322 float transition = cloud_layers[i]->getTransition_m();
326 if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ) {
327 // less than 50% coverage -- assume we're in the clear for now
329 } else if ( alt < asl - transition ) {
332 } else if ( alt < asl ) {
333 // in lower transition
334 ratio = (asl - alt) / transition;
335 } else if ( alt < asl + thickness ) {
338 } else if ( alt < asl + thickness + transition ) {
339 // in upper transition
340 ratio = (alt - (asl + thickness)) / transition;
346 if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ||
347 cloud_layers[i]->get_layer3D()->isDefined3D()) {
348 // do nothing, clear layers aren't drawn, don't affect
349 // visibility andn dont' need to be faded in or out.
350 } else if ( (cloud_layers[i]->getCoverage() ==
351 SGCloudLayer::SG_CLOUD_FEW)
352 || (cloud_layers[i]->getCoverage() ==
353 SGCloudLayer::SG_CLOUD_SCATTERED) )
355 // set the alpha fade value for the cloud layer. For less
356 // dense cloud layers we fade the layer to nothing as we
357 // approach it because we stay clear visibility-wise as we
359 float temp = ratio * 2.0;
360 if ( temp > 1.0 ) { temp = 1.0; }
361 cloud_layers[i]->setAlpha( temp );
363 // don't touch visibility
365 // maintain full alpha for denser cloud layer types.
366 // Let's set the value explicitly in case someone changed
368 cloud_layers[i]->setAlpha( 1.0 );
370 // lower visibility as we approach the cloud layer.
371 // accumulate effects from multiple cloud layers
378 // calc chance of entering cloud puff
379 double rnd = mt_rand(&seed);
380 double chance = rnd * rnd * rnd;
381 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
383 puff_length = mt_rand(&seed) * 2.0; // up to 2 seconds
384 puff_progression = 0.0;
389 // modify actual_visibility based on puff envelope
391 if ( puff_progression <= ramp_up ) {
392 double x = SGD_PI_2 * puff_progression / ramp_up;
393 double factor = 1.0 - sin( x );
394 // cout << "ramp up = " << puff_progression
395 // << " factor = " << factor << endl;
396 effvis = effvis * factor;
397 } else if ( puff_progression >= ramp_up + puff_length ) {
398 double x = SGD_PI_2 *
399 (puff_progression - (ramp_up + puff_length)) /
401 double factor = sin( x );
402 // cout << "ramp down = "
403 // << puff_progression - (ramp_up + puff_length)
404 // << " factor = " << factor << endl;
405 effvis = effvis * factor;
410 /* cout << "len = " << puff_length
412 << " factor = " << factor
413 << " actual_visibility = " << actual_visibility
416 // time_factor = ( global_multi_loop *
417 // current_options.get_speed_up() ) /
418 // (double)current_options.get_model_hz();
420 puff_progression += time_factor;
421 // cout << "time factor = " << time_factor << endl;
423 /* cout << "gml = " << global_multi_loop
424 << " speed up = " << current_options.get_speed_up()
425 << " hz = " << current_options.get_model_hz() << endl;
428 if ( puff_progression > puff_length + ramp_up + ramp_down) {
435 // never let visibility drop below the layer's configured visibility
436 effvis = SG_MAX2<float>(cloud_layers[i]->getVisibility_m(), effvis );
440 effective_visibility = effvis;