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;
43 // near cloud visibility state variables
52 clouds_3d_enabled = false;
53 clouds_3d_density = 0.8;
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,
60 pre_root->setStateSet(preStateSet);
61 cloud_root = new osg::Group;
62 cloud_root->setNodeMask(simgear::MODEL_BIT);
63 cloud_root->setName("SGSky-cloud-root");
65 pre_transform = new osg::Group;
66 _ephTransform = new osg::MatrixTransform;
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);
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, double v_radius_m,
83 double sun_size, double moon_size,
84 const SGEphemeris& eph, SGPropertyNode *property_tree_node,
85 simgear::SGReaderWriterOptions* options )
88 pre_transform->addChild( dome->build( h_radius_m, v_radius_m, options ) );
90 pre_transform->addChild(_ephTransform.get());
91 planets = new SGStars;
92 _ephTransform->addChild( planets->build(eph.getNumPlanets(), eph.getPlanets(), h_radius_m) );
95 _ephTransform->addChild( stars->build(eph.getNumStars(), eph.getStars(), h_radius_m) );
98 _ephTransform->addChild( moon->build(tex_path, moon_size) );
101 _ephTransform->addChild( oursun->build(tex_path, sun_size, property_tree_node ) );
103 pre_root->addChild( pre_transform.get() );
107 // repaint the sky components based on current value of sun_angle,
108 // sky, and fog colors.
110 // sun angle in degrees relative to verticle
111 // 0 degrees = high noon
112 // 90 degrees = sun rise/set
113 // 180 degrees = darkest midnight
114 bool SGSky::repaint( const SGSkyColor &sc, const SGEphemeris& eph )
116 dome->repaint( sc.adj_sky_color, sc.sky_color, sc.fog_color,
117 sc.sun_angle, effective_visibility );
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 );
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 );
130 SGCloudField::updateFog((double)effective_visibility,
131 osg::Vec4f(toOsg(sc.fog_color), 1.0f));
135 // reposition the sky at the specified origin and orientation
137 // lon specifies a rotation about the Z axis
138 // lat specifies a rotation about the new Y axis
139 // spin specifies a rotation about the new Z axis (this allows
140 // additional orientation for the sunrise/set effects and is used by
141 // the skydome and perhaps clouds.
142 bool SGSky::reposition( const SGSkyState &st, const SGEphemeris& eph, double dt )
144 double angle = st.gst * 15; // degrees
145 double angleRad = SGMiscd::deg2rad(angle);
147 SGVec3f zero_elev, view_up;
148 double lon, lat, alt;
150 SGGeod geodZeroViewPos = SGGeod::fromGeodM(st.pos_geod, 0);
151 zero_elev = toVec3f( SGVec3d::fromGeod(geodZeroViewPos) );
153 // calculate the scenery up vector
154 SGQuatd hlOr = SGQuatd::fromLonLat(st.pos_geod);
155 view_up = toVec3f(hlOr.backTransform(-SGVec3d::e3()));
158 lon = st.pos_geod.getLongitudeRad();
159 lat = st.pos_geod.getLatitudeRad();
160 alt = st.pos_geod.getElevationM();
162 dome->reposition( zero_elev, alt, lon, lat, st.spin );
164 osg::Matrix m = osg::Matrix::rotate(angleRad, osg::Vec3(0, 0, -1));
165 m.postMultTranslate(toOsg(st.pos));
166 _ephTransform->setMatrix(m);
168 double sun_ra = eph.getSunRightAscension();
169 double sun_dec = eph.getSunDeclination();
170 oursun->reposition( sun_ra, sun_dec, st.sun_dist, lat, alt, st.sun_angle );
172 double moon_ra = eph.getMoonRightAscension();
173 double moon_dec = eph.getMoonDeclination();
174 moon->reposition( moon_ra, moon_dec, st.moon_dist );
176 for ( unsigned i = 0; i < cloud_layers.size(); ++i ) {
177 if ( cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR ||
178 cloud_layers[i]->get_layer3D()->isDefined3D() ) {
179 cloud_layers[i]->reposition( zero_elev, view_up, lon, lat, alt, dt);
181 cloud_layers[i]->getNode()->setAllChildrenOff();
189 SGSky::set_visibility( float v )
191 visibility = std::max(v, 25.0f);
195 SGSky::add_cloud_layer( SGCloudLayer * layer )
197 cloud_layers.push_back(layer);
198 cloud_root->addChild(layer->getNode());
200 layer->set_enable3dClouds(clouds_3d_enabled);
204 SGSky::get_cloud_layer (int i) const
206 return cloud_layers[i];
210 SGSky::get_cloud_layer (int i)
212 return cloud_layers[i];
216 SGSky::get_cloud_layer_count () const
218 return cloud_layers.size();
221 double SGSky::get_3dCloudDensity() const {
222 return SGNewCloud::getDensity();
225 void SGSky::set_3dCloudDensity(double density)
227 SGNewCloud::setDensity(density);
230 float SGSky::get_3dCloudVisRange() const {
231 return SGCloudField::getVisRange();
234 void SGSky::set_3dCloudVisRange(float vis)
236 SGCloudField::setVisRange(vis);
237 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
238 cloud_layers[i]->get_layer3D()->applyVisAndLoDRange();
242 float SGSky::get_3dCloudImpostorDistance() const {
243 return SGCloudField::getImpostorDistance();
246 void SGSky::set_3dCloudImpostorDistance(float vis)
248 SGCloudField::setImpostorDistance(vis);
249 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
250 cloud_layers[i]->get_layer3D()->applyVisAndLoDRange();
254 float SGSky::get_3dCloudLoD1Range() const {
255 return SGCloudField::getLoD1Range();
258 void SGSky::set_3dCloudLoD1Range(float vis)
260 SGCloudField::setLoD1Range(vis);
261 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
262 cloud_layers[i]->get_layer3D()->applyVisAndLoDRange();
266 float SGSky::get_3dCloudLoD2Range() const {
267 return SGCloudField::getLoD2Range();
270 void SGSky::set_3dCloudLoD2Range(float vis)
272 SGCloudField::setLoD2Range(vis);
273 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
274 cloud_layers[i]->get_layer3D()->applyVisAndLoDRange();
278 bool SGSky::get_3dCloudWrap() const {
279 return SGCloudField::getWrap();
282 void SGSky::set_3dCloudWrap(bool wrap)
284 SGCloudField::setWrap(wrap);
287 bool SGSky::get_3dCloudUseImpostors() const {
288 return SGCloudField::getUseImpostors();
291 void SGSky::set_3dCloudUseImpostors(bool imp)
293 SGCloudField::setUseImpostors(imp);
296 void SGSky::texture_path( const string& path ) {
297 tex_path = SGPath( path );
300 // modify the current visibility based on cloud layers, thickness,
301 // transition range, and simulated "puffs".
302 void SGSky::modify_vis( float alt, float time_factor ) {
303 float effvis = visibility;
305 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
306 float asl = cloud_layers[i]->getElevation_m();
307 float thickness = cloud_layers[i]->getThickness_m();
308 float transition = cloud_layers[i]->getTransition_m();
312 if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ) {
313 // less than 50% coverage -- assume we're in the clear for now
315 } else if ( alt < asl - transition ) {
318 } else if ( alt < asl ) {
319 // in lower transition
320 ratio = (asl - alt) / transition;
321 } else if ( alt < asl + thickness ) {
324 } else if ( alt < asl + thickness + transition ) {
325 // in upper transition
326 ratio = (alt - (asl + thickness)) / transition;
332 if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ||
333 cloud_layers[i]->get_layer3D()->isDefined3D()) {
334 // do nothing, clear layers aren't drawn, don't affect
335 // visibility andn dont' need to be faded in or out.
336 } else if ( (cloud_layers[i]->getCoverage() ==
337 SGCloudLayer::SG_CLOUD_FEW)
338 || (cloud_layers[i]->getCoverage() ==
339 SGCloudLayer::SG_CLOUD_SCATTERED) )
341 // set the alpha fade value for the cloud layer. For less
342 // dense cloud layers we fade the layer to nothing as we
343 // approach it because we stay clear visibility-wise as we
345 float temp = ratio * 2.0;
346 if ( temp > 1.0 ) { temp = 1.0; }
347 cloud_layers[i]->setAlpha( temp );
349 // don't touch visibility
351 // maintain full alpha for denser cloud layer types.
352 // Let's set the value explicitly in case someone changed
354 cloud_layers[i]->setAlpha( 1.0 );
356 // lower visibility as we approach the cloud layer.
357 // accumulate effects from multiple cloud layers
364 // calc chance of entering cloud puff
365 double rnd = mt_rand(&seed);
366 double chance = rnd * rnd * rnd;
367 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
369 puff_length = mt_rand(&seed) * 2.0; // up to 2 seconds
370 puff_progression = 0.0;
375 // modify actual_visibility based on puff envelope
377 if ( puff_progression <= ramp_up ) {
378 double x = SGD_PI_2 * puff_progression / ramp_up;
379 double factor = 1.0 - sin( x );
380 // cout << "ramp up = " << puff_progression
381 // << " factor = " << factor << endl;
382 effvis = effvis * factor;
383 } else if ( puff_progression >= ramp_up + puff_length ) {
384 double x = SGD_PI_2 *
385 (puff_progression - (ramp_up + puff_length)) /
387 double factor = sin( x );
388 // cout << "ramp down = "
389 // << puff_progression - (ramp_up + puff_length)
390 // << " factor = " << factor << endl;
391 effvis = effvis * factor;
396 /* cout << "len = " << puff_length
398 << " factor = " << factor
399 << " actual_visibility = " << actual_visibility
402 // time_factor = ( global_multi_loop *
403 // current_options.get_speed_up() ) /
404 // (double)current_options.get_model_hz();
406 puff_progression += time_factor;
407 // cout << "time factor = " << time_factor << endl;
409 /* cout << "gml = " << global_multi_loop
410 << " speed up = " << current_options.get_speed_up()
411 << " hz = " << current_options.get_model_hz() << endl;
414 if ( puff_progression > puff_length + ramp_up + ramp_down) {
421 // never let visibility drop below the layer's configured visibility
422 effvis = SG_MAX2<float>(cloud_layers[i]->getVisibility_m(), effvis );
426 effective_visibility = effvis;