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::MatrixTransform;
75 // initialize the sky and connect the components to the scene graph at
76 // the provided branch
77 void SGSky::build( double h_radius_m, double v_radius_m,
78 double sun_size, double moon_size,
79 int nplanets, SGVec3d planet_data[7],
80 int nstars, SGVec3d star_data[], SGPropertyNode *property_tree_node )
83 pre_transform->addChild( dome->build( h_radius_m, v_radius_m ) );
85 planets = new SGStars;
86 pre_transform->addChild(planets->build(nplanets, planet_data, h_radius_m));
89 pre_transform->addChild( stars->build(nstars, star_data, h_radius_m) );
92 pre_transform->addChild( moon->build(tex_path, moon_size) );
95 pre_transform->addChild( oursun->build(tex_path, sun_size, property_tree_node ) );
97 pre_selector->addChild( pre_transform.get() );
99 pre_root->addChild( pre_selector.get() );
103 // repaint the sky components based on current value of sun_angle,
104 // sky, and fog colors.
106 // sun angle in degrees relative to verticle
107 // 0 degrees = high noon
108 // 90 degrees = sun rise/set
109 // 180 degrees = darkest midnight
110 bool SGSky::repaint( const SGSkyColor &sc )
112 if ( effective_visibility > 1000.0 ) {
114 dome->repaint( sc.sky_color, sc.fog_color, sc.sun_angle,
115 effective_visibility );
117 stars->repaint( sc.sun_angle, sc.nstars, sc.star_data );
118 planets->repaint( sc.sun_angle, sc.nplanets, sc.planet_data );
119 oursun->repaint( sc.sun_angle, effective_visibility );
120 moon->repaint( sc.moon_angle );
122 for ( unsigned i = 0; i < cloud_layers.size(); ++i ) {
123 if (cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR){
124 cloud_layers[i]->repaint( sc.cloud_color );
131 SGCloudField::updateFog((double)effective_visibility,
132 osg::Vec4f(sc.fog_color.osg(), 1.0f));
137 // reposition the sky at the specified origin and orientation
139 // lon specifies a rotation about the Z axis
140 // lat specifies a rotation about the new Y axis
141 // spin specifies a rotation about the new Z axis (this allows
142 // additional orientation for the sunrise/set effects and is used by
143 // the skydome and perhaps clouds.
144 bool SGSky::reposition( SGSkyState &st, double dt )
147 double angle = st.gst * 15; // degrees
149 dome->reposition( st.zero_elev, st.alt, st.lon, st.lat, st.spin );
151 stars->reposition( st.view_pos, angle );
152 planets->reposition( st.view_pos, angle );
154 oursun->reposition( st.view_pos, angle,
155 st.sun_ra, st.sun_dec, st.sun_dist, st.lat, st.alt, st.sun_angle );
157 moon->reposition( st.view_pos, angle,
158 st.moon_ra, st.moon_dec, st.moon_dist );
160 for ( unsigned i = 0; i < cloud_layers.size(); ++i ) {
161 if ( cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR ) {
162 cloud_layers[i]->reposition( st.zero_elev, st.view_up,
163 st.lon, st.lat, st.alt, dt );
165 cloud_layers[i]->getNode()->setAllChildrenOff();
172 SGSky::add_cloud_layer( SGCloudLayer * layer )
174 cloud_layers.push_back(layer);
175 cloud_root->addChild(layer->getNode());
177 layer->set_enable3dClouds(clouds_3d_enabled);
181 SGSky::get_cloud_layer (int i) const
183 return cloud_layers[i];
187 SGSky::get_cloud_layer (int i)
189 return cloud_layers[i];
193 SGSky::get_cloud_layer_count () const
195 return cloud_layers.size();
198 double SGSky::get_3dCloudDensity() const {
199 return SGNewCloud::getDensity();
202 void SGSky::set_3dCloudDensity(double density)
204 SGNewCloud::setDensity(density);
207 float SGSky::get_3dCloudVisRange() const {
208 return SGCloudField::getVisRange();
211 void SGSky::set_3dCloudVisRange(float vis)
213 SGCloudField::setVisRange(vis);
214 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
215 cloud_layers[i]->get_layer3D()->applyVisRange();
219 float SGSky::get_3dCloudNumFlavours() const {
220 return (float) SGNewCloud::getNumFlavours();
223 void SGSky::set_3dCloudNumFlavours(float n)
225 SGNewCloud::setNumFlavours((int) n);
228 void SGSky::texture_path( const string& path ) {
229 tex_path = SGPath( path );
232 // modify the current visibility based on cloud layers, thickness,
233 // transition range, and simulated "puffs".
234 void SGSky::modify_vis( float alt, float time_factor ) {
235 float effvis = visibility;
237 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
238 float asl = cloud_layers[i]->getElevation_m();
239 float thickness = cloud_layers[i]->getThickness_m();
240 float transition = cloud_layers[i]->getTransition_m();
244 if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ) {
245 // less than 50% coverage -- assume we're in the clear for now
247 } else if ( alt < asl - transition ) {
250 } else if ( alt < asl ) {
251 // in lower transition
252 ratio = (asl - alt) / transition;
253 } else if ( alt < asl + thickness ) {
256 } else if ( alt < asl + thickness + transition ) {
257 // in upper transition
258 ratio = (alt - (asl + thickness)) / transition;
264 if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ||
265 cloud_layers[i]->get_layer3D()->defined3D) {
266 // do nothing, clear layers aren't drawn, don't affect
267 // visibility andn dont' need to be faded in or out.
268 } else if ( (cloud_layers[i]->getCoverage() ==
269 SGCloudLayer::SG_CLOUD_FEW)
270 || (cloud_layers[i]->getCoverage() ==
271 SGCloudLayer::SG_CLOUD_SCATTERED) )
273 // set the alpha fade value for the cloud layer. For less
274 // dense cloud layers we fade the layer to nothing as we
275 // approach it because we stay clear visibility-wise as we
277 float temp = ratio * 2.0;
278 if ( temp > 1.0 ) { temp = 1.0; }
279 cloud_layers[i]->setAlpha( temp );
281 // don't touch visibility
283 // maintain full alpha for denser cloud layer types.
284 // Let's set the value explicitly in case someone changed
286 cloud_layers[i]->setAlpha( 1.0 );
288 // lower visibility as we approach the cloud layer.
289 // accumulate effects from multiple cloud layers
296 // calc chance of entering cloud puff
297 double rnd = sg_random();
298 double chance = rnd * rnd * rnd;
299 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
301 puff_length = sg_random() * 2.0; // up to 2 seconds
302 puff_progression = 0.0;
307 // modify actual_visibility based on puff envelope
309 if ( puff_progression <= ramp_up ) {
310 double x = SGD_PI_2 * puff_progression / ramp_up;
311 double factor = 1.0 - sin( x );
312 // cout << "ramp up = " << puff_progression
313 // << " factor = " << factor << endl;
314 effvis = effvis * factor;
315 } else if ( puff_progression >= ramp_up + puff_length ) {
316 double x = SGD_PI_2 *
317 (puff_progression - (ramp_up + puff_length)) /
319 double factor = sin( x );
320 // cout << "ramp down = "
321 // << puff_progression - (ramp_up + puff_length)
322 // << " factor = " << factor << endl;
323 effvis = effvis * factor;
328 /* cout << "len = " << puff_length
330 << " factor = " << factor
331 << " actual_visibility = " << actual_visibility
334 // time_factor = ( global_multi_loop *
335 // current_options.get_speed_up() ) /
336 // (double)current_options.get_model_hz();
338 puff_progression += time_factor;
339 // cout << "time factor = " << time_factor << endl;
341 /* cout << "gml = " << global_multi_loop
342 << " speed up = " << current_options.get_speed_up()
343 << " hz = " << current_options.get_model_hz() << endl;
346 if ( puff_progression > puff_length + ramp_up + ramp_down) {
353 // never let visibility drop below 25 meters
354 if ( effvis <= 25.0 ) {
360 effective_visibility = effvis;