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/math/sg_random.h>
33 #include <simgear/scene/util/RenderConstants.hxx>
35 #include <osg/StateSet>
39 SGSky::SGSky( void ) {
40 effective_visibility = visibility = 10000.0;
42 // near cloud visibility state variables
51 clouds_3d_enabled = false;
52 clouds_3d_density = 0.8;
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,
59 pre_root->setStateSet(preStateSet);
60 cloud_root = new osg::Group;
61 cloud_root->setNodeMask(simgear::MODEL_BIT);
63 pre_selector = new osg::Switch;
65 pre_transform = new osg::Group;
67 _ephTransform = new osg::MatrixTransform;
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 )
84 pre_transform->addChild( dome->build( h_radius_m, v_radius_m ) );
86 pre_transform->addChild(_ephTransform.get());
87 planets = new SGStars;
88 _ephTransform->addChild( planets->build(eph.getNumPlanets(), eph.getPlanets(), h_radius_m) );
91 _ephTransform->addChild( stars->build(eph.getNumStars(), eph.getStars(), h_radius_m) );
94 _ephTransform->addChild( moon->build(tex_path, moon_size) );
97 _ephTransform->addChild( oursun->build(tex_path, sun_size, property_tree_node ) );
99 pre_selector->addChild( pre_transform.get() );
101 pre_root->addChild( pre_selector.get() );
105 // repaint the sky components based on current value of sun_angle,
106 // sky, and fog colors.
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 )
114 if ( effective_visibility > 1000.0 ) {
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 );
133 SGCloudField::updateFog((double)effective_visibility,
134 osg::Vec4f(sc.fog_color.osg(), 1.0f));
138 // reposition the sky at the specified origin and orientation
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 )
147 double angle = st.gst * 15; // degrees
148 double angleRad = SGMiscd::deg2rad(angle);
150 dome->reposition( st.zero_elev, st.alt, st.lon, st.lat, st.spin );
152 osg::Matrix m = osg::Matrix::rotate(angleRad, osg::Vec3(0, 0, -1));
153 m.postMultTranslate(st.view_pos.osg());
154 _ephTransform->setMatrix(m);
156 double sun_ra = eph.getSunRightAscension();
157 double sun_dec = eph.getSunDeclination();
158 oursun->reposition( sun_ra, sun_dec, st.sun_dist, st.lat, st.alt, st.sun_angle );
160 double moon_ra = eph.getMoonRightAscension();
161 double moon_dec = eph.getMoonDeclination();
162 moon->reposition( moon_ra, moon_dec, st.moon_dist );
164 for ( unsigned i = 0; i < cloud_layers.size(); ++i ) {
165 if ( cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR ) {
166 cloud_layers[i]->reposition( st.zero_elev, st.view_up,
167 st.lon, st.lat, st.alt, dt );
169 cloud_layers[i]->getNode()->setAllChildrenOff();
176 SGSky::add_cloud_layer( SGCloudLayer * layer )
178 cloud_layers.push_back(layer);
179 cloud_root->addChild(layer->getNode());
181 layer->set_enable3dClouds(clouds_3d_enabled);
185 SGSky::get_cloud_layer (int i) const
187 return cloud_layers[i];
191 SGSky::get_cloud_layer (int i)
193 return cloud_layers[i];
197 SGSky::get_cloud_layer_count () const
199 return cloud_layers.size();
202 double SGSky::get_3dCloudDensity() const {
203 return SGNewCloud::getDensity();
206 void SGSky::set_3dCloudDensity(double density)
208 SGNewCloud::setDensity(density);
211 float SGSky::get_3dCloudVisRange() const {
212 return SGCloudField::getVisRange();
215 void SGSky::set_3dCloudVisRange(float vis)
217 SGCloudField::setVisRange(vis);
218 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
219 cloud_layers[i]->get_layer3D()->applyVisRange();
223 void SGSky::texture_path( const string& path ) {
224 tex_path = SGPath( path );
227 // modify the current visibility based on cloud layers, thickness,
228 // transition range, and simulated "puffs".
229 void SGSky::modify_vis( float alt, float time_factor ) {
230 float effvis = visibility;
232 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
233 float asl = cloud_layers[i]->getElevation_m();
234 float thickness = cloud_layers[i]->getThickness_m();
235 float transition = cloud_layers[i]->getTransition_m();
239 if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ) {
240 // less than 50% coverage -- assume we're in the clear for now
242 } else if ( alt < asl - transition ) {
245 } else if ( alt < asl ) {
246 // in lower transition
247 ratio = (asl - alt) / transition;
248 } else if ( alt < asl + thickness ) {
251 } else if ( alt < asl + thickness + transition ) {
252 // in upper transition
253 ratio = (alt - (asl + thickness)) / transition;
259 if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ||
260 cloud_layers[i]->get_layer3D()->defined3D) {
261 // do nothing, clear layers aren't drawn, don't affect
262 // visibility andn dont' need to be faded in or out.
263 } else if ( (cloud_layers[i]->getCoverage() ==
264 SGCloudLayer::SG_CLOUD_FEW)
265 || (cloud_layers[i]->getCoverage() ==
266 SGCloudLayer::SG_CLOUD_SCATTERED) )
268 // set the alpha fade value for the cloud layer. For less
269 // dense cloud layers we fade the layer to nothing as we
270 // approach it because we stay clear visibility-wise as we
272 float temp = ratio * 2.0;
273 if ( temp > 1.0 ) { temp = 1.0; }
274 cloud_layers[i]->setAlpha( temp );
276 // don't touch visibility
278 // maintain full alpha for denser cloud layer types.
279 // Let's set the value explicitly in case someone changed
281 cloud_layers[i]->setAlpha( 1.0 );
283 // lower visibility as we approach the cloud layer.
284 // accumulate effects from multiple cloud layers
291 // calc chance of entering cloud puff
292 double rnd = sg_random();
293 double chance = rnd * rnd * rnd;
294 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
296 puff_length = sg_random() * 2.0; // up to 2 seconds
297 puff_progression = 0.0;
302 // modify actual_visibility based on puff envelope
304 if ( puff_progression <= ramp_up ) {
305 double x = SGD_PI_2 * puff_progression / ramp_up;
306 double factor = 1.0 - sin( x );
307 // cout << "ramp up = " << puff_progression
308 // << " factor = " << factor << endl;
309 effvis = effvis * factor;
310 } else if ( puff_progression >= ramp_up + puff_length ) {
311 double x = SGD_PI_2 *
312 (puff_progression - (ramp_up + puff_length)) /
314 double factor = sin( x );
315 // cout << "ramp down = "
316 // << puff_progression - (ramp_up + puff_length)
317 // << " factor = " << factor << endl;
318 effvis = effvis * factor;
323 /* cout << "len = " << puff_length
325 << " factor = " << factor
326 << " actual_visibility = " << actual_visibility
329 // time_factor = ( global_multi_loop *
330 // current_options.get_speed_up() ) /
331 // (double)current_options.get_model_hz();
333 puff_progression += time_factor;
334 // cout << "time factor = " << time_factor << endl;
336 /* cout << "gml = " << global_multi_loop
337 << " speed up = " << current_options.get_speed_up()
338 << " hz = " << current_options.get_model_hz() << endl;
341 if ( puff_progression > puff_length + ramp_up + ramp_down) {
348 // never let visibility drop below 25 meters
349 if ( effvis <= 25.0 ) {
355 effective_visibility = effvis;