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>
28 #include <simgear/math/sg_random.h>
29 #include <simgear/scene/util/RenderConstants.hxx>
32 #include "cloudfield.hxx"
33 #include "newcloud.hxx"
36 SGSky::SGSky( void ) {
37 effective_visibility = visibility = 10000.0;
39 // near cloud visibility state variables
48 clouds_3d_enabled = false;
49 clouds_3d_density = 0.8;
51 pre_root = new osg::Group;
52 pre_root->setNodeMask(simgear::BACKGROUND_BIT);
53 cloud_root = new osg::Group;
54 cloud_root->setNodeMask(simgear::BACKGROUND_BIT | simgear::MODEL_BIT);
56 pre_selector = new osg::Switch;
58 pre_transform = new osg::MatrixTransform;
68 // initialize the sky and connect the components to the scene graph at
69 // the provided branch
70 void SGSky::build( double h_radius_m, double v_radius_m,
71 double sun_size, double moon_size,
72 int nplanets, SGVec3d planet_data[7],
73 int nstars, SGVec3d star_data[], SGPropertyNode *property_tree_node )
76 pre_transform->addChild( dome->build( h_radius_m, v_radius_m ) );
78 planets = new SGStars;
79 pre_transform->addChild(planets->build(nplanets, planet_data, h_radius_m));
82 pre_transform->addChild( stars->build(nstars, star_data, h_radius_m) );
85 pre_transform->addChild( moon->build(tex_path, moon_size) );
88 pre_transform->addChild( oursun->build(tex_path, sun_size, property_tree_node ) );
90 pre_selector->addChild( pre_transform.get() );
92 pre_root->addChild( pre_selector.get() );
96 // repaint the sky components based on current value of sun_angle,
97 // sky, and fog colors.
99 // sun angle in degrees relative to verticle
100 // 0 degrees = high noon
101 // 90 degrees = sun rise/set
102 // 180 degrees = darkest midnight
103 bool SGSky::repaint( const SGSkyColor &sc )
105 if ( effective_visibility > 1000.0 ) {
107 dome->repaint( sc.sky_color, sc.fog_color, sc.sun_angle,
108 effective_visibility );
110 stars->repaint( sc.sun_angle, sc.nstars, sc.star_data );
111 planets->repaint( sc.sun_angle, sc.nplanets, sc.planet_data );
112 oursun->repaint( sc.sun_angle, effective_visibility );
113 moon->repaint( sc.moon_angle );
115 for ( unsigned i = 0; i < cloud_layers.size(); ++i ) {
116 if (cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR){
117 cloud_layers[i]->repaint( sc.cloud_color );
129 // reposition the sky at the specified origin and orientation
131 // lon specifies a rotation about the Z axis
132 // lat specifies a rotation about the new Y axis
133 // spin specifies a rotation about the new Z axis (this allows
134 // additional orientation for the sunrise/set effects and is used by
135 // the skydome and perhaps clouds.
136 bool SGSky::reposition( SGSkyState &st, double dt )
139 double angle = st.gst * 15; // degrees
141 dome->reposition( st.zero_elev, st.alt, st.lon, st.lat, st.spin );
143 stars->reposition( st.view_pos, angle );
144 planets->reposition( st.view_pos, angle );
146 oursun->reposition( st.view_pos, angle,
147 st.sun_ra, st.sun_dec, st.sun_dist, st.lat, st.alt, st.sun_angle );
149 moon->reposition( st.view_pos, angle,
150 st.moon_ra, st.moon_dec, st.moon_dist );
152 for ( unsigned i = 0; i < cloud_layers.size(); ++i ) {
153 if ( cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR ) {
154 cloud_layers[i]->reposition( st.zero_elev, st.view_up,
155 st.lon, st.lat, st.alt, dt );
157 cloud_layers[i]->getNode()->setAllChildrenOff();
164 SGSky::add_cloud_layer( SGCloudLayer * layer )
166 cloud_layers.push_back(layer);
167 cloud_root->addChild(layer->getNode());
169 layer->set_enable3dClouds(clouds_3d_enabled);
173 SGSky::get_cloud_layer (int i) const
175 return cloud_layers[i];
179 SGSky::get_cloud_layer (int i)
181 return cloud_layers[i];
185 SGSky::get_cloud_layer_count () const
187 return cloud_layers.size();
190 double SGSky::get_3dCloudDensity() const {
191 return SGNewCloud::getDensity();
194 void SGSky::set_3dCloudDensity(double density)
196 SGNewCloud::setDensity(density);
199 float SGSky::get_3dCloudVisRange() const {
200 return SGCloudField::getVisRange();
203 void SGSky::set_3dCloudVisRange(float vis)
205 SGCloudField::setVisRange(vis);
206 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
207 cloud_layers[i]->get_layer3D()->applyVisRange();
211 float SGSky::get_3dCloudNumFlavours() const {
212 return (float) SGNewCloud::getNumFlavours();
215 void SGSky::set_3dCloudNumFlavours(float n)
217 SGNewCloud::setNumFlavours((int) n);
220 void SGSky::texture_path( const string& path ) {
221 tex_path = SGPath( path );
224 // modify the current visibility based on cloud layers, thickness,
225 // transition range, and simulated "puffs".
226 void SGSky::modify_vis( float alt, float time_factor ) {
227 float effvis = visibility;
229 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
230 float asl = cloud_layers[i]->getElevation_m();
231 float thickness = cloud_layers[i]->getThickness_m();
232 float transition = cloud_layers[i]->getTransition_m();
236 if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ) {
237 // less than 50% coverage -- assume we're in the clear for now
239 } else if ( alt < asl - transition ) {
242 } else if ( alt < asl ) {
243 // in lower transition
244 ratio = (asl - alt) / transition;
245 } else if ( alt < asl + thickness ) {
248 } else if ( alt < asl + thickness + transition ) {
249 // in upper transition
250 ratio = (alt - (asl + thickness)) / transition;
256 if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ||
257 cloud_layers[i]->get_layer3D()->defined3D) {
258 // do nothing, clear layers aren't drawn, don't affect
259 // visibility andn dont' need to be faded in or out.
260 } else if ( (cloud_layers[i]->getCoverage() ==
261 SGCloudLayer::SG_CLOUD_FEW)
262 || (cloud_layers[i]->getCoverage() ==
263 SGCloudLayer::SG_CLOUD_SCATTERED) )
265 // set the alpha fade value for the cloud layer. For less
266 // dense cloud layers we fade the layer to nothing as we
267 // approach it because we stay clear visibility-wise as we
269 float temp = ratio * 2.0;
270 if ( temp > 1.0 ) { temp = 1.0; }
271 cloud_layers[i]->setAlpha( temp );
273 // don't touch visibility
275 // maintain full alpha for denser cloud layer types.
276 // Let's set the value explicitly in case someone changed
278 cloud_layers[i]->setAlpha( 1.0 );
280 // lower visibility as we approach the cloud layer.
281 // accumulate effects from multiple cloud layers
288 // calc chance of entering cloud puff
289 double rnd = sg_random();
290 double chance = rnd * rnd * rnd;
291 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
293 puff_length = sg_random() * 2.0; // up to 2 seconds
294 puff_progression = 0.0;
299 // modify actual_visibility based on puff envelope
301 if ( puff_progression <= ramp_up ) {
302 double x = SGD_PI_2 * puff_progression / ramp_up;
303 double factor = 1.0 - sin( x );
304 // cout << "ramp up = " << puff_progression
305 // << " factor = " << factor << endl;
306 effvis = effvis * factor;
307 } else if ( puff_progression >= ramp_up + puff_length ) {
308 double x = SGD_PI_2 *
309 (puff_progression - (ramp_up + puff_length)) /
311 double factor = sin( x );
312 // cout << "ramp down = "
313 // << puff_progression - (ramp_up + puff_length)
314 // << " factor = " << factor << endl;
315 effvis = effvis * factor;
320 /* cout << "len = " << puff_length
322 << " factor = " << factor
323 << " actual_visibility = " << actual_visibility
326 // time_factor = ( global_multi_loop *
327 // current_options.get_speed_up() ) /
328 // (double)current_options.get_model_hz();
330 puff_progression += time_factor;
331 // cout << "time factor = " << time_factor << endl;
333 /* cout << "gml = " << global_multi_loop
334 << " speed up = " << current_options.get_speed_up()
335 << " hz = " << current_options.get_model_hz() << endl;
338 if ( puff_progression > puff_length + ramp_up + ramp_down) {
345 // never let visibility drop below 25 meters
346 if ( effvis <= 25.0 ) {
352 effective_visibility = effvis;