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>
31 #include <simgear/math/sg_random.h>
34 #include "cloudfield.hxx"
37 SGSky::SGSky( void ) {
38 effective_visibility = visibility = 10000.0;
40 // near cloud visibility state variables
56 for (unsigned int i = 0; i < cloud_layers.size(); i++)
57 delete cloud_layers[i];
61 // initialize the sky and connect the components to the scene graph at
62 // the provided branch
63 void SGSky::build( double h_radius_m, double v_radius_m,
64 double sun_size, double moon_size,
65 int nplanets, sgdVec3 *planet_data,
66 int nstars, sgdVec3 *star_data )
68 pre_root = new ssgRoot;
69 post_root = new ssgRoot;
71 pre_selector = new ssgSelector;
72 post_selector = new ssgSelector;
74 pre_transform = new ssgTransform;
75 post_transform = new ssgTransform;
78 pre_transform -> addKid( dome->build( h_radius_m, v_radius_m ) );
80 planets = new SGStars;
81 pre_transform -> addKid(planets->build(nplanets, planet_data, h_radius_m));
84 pre_transform -> addKid( stars->build(nstars, star_data, h_radius_m) );
87 pre_transform -> addKid( moon->build(tex_path, moon_size) );
90 pre_transform -> addKid( oursun->build(tex_path, sun_size) );
92 pre_selector->addKid( pre_transform );
93 pre_selector->clrTraversalMaskBits( SSGTRAV_HOT );
95 post_selector->addKid( post_transform );
96 post_selector->clrTraversalMaskBits( SSGTRAV_HOT );
98 pre_root->addKid( pre_selector );
99 post_root->addKid( post_selector );
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 );
120 oursun->repaint( sc.sun_angle, effective_visibility );
121 moon->repaint( sc.moon_angle );
123 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
124 if (cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR){
125 cloud_layers[i]->repaint( sc.cloud_color );
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.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 );
157 moon->reposition( st.view_pos, angle,
158 st.moon_ra, st.moon_dec, st.moon_dist );
160 for ( int i = 0; i < (int)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 );
171 // draw background portions of the sky ... do this before you draw the
172 // rest of your scene.
173 void SGSky::preDraw( float alt, float fog_exp2_density ) {
174 ssgCullAndDraw( pre_root );
176 // if we are closer than this to a cloud layer, don't draw clouds
177 static const float slop = 5.0;
180 // check where we are relative to the cloud layers
182 for ( i = 0; i < (int)cloud_layers.size(); ++i ) {
183 float asl = cloud_layers[i]->getElevation_m();
184 float thickness = cloud_layers[i]->getThickness_m();
186 if ( alt < asl - slop ) {
188 } else if ( alt < asl + thickness + slop ) {
191 // bail now and don't draw any clouds
192 if( cloud_layers[i]->get_layer3D()->is3D() && SGCloudField::enable3D )
200 // determine rendering order
202 while ( cur_layer_pos < (int)cloud_layers.size() &&
203 alt > cloud_layers[cur_layer_pos]->getElevation_m() )
208 // FIXME: This should not be needed, but at this time (08/15/2003)
209 // certain NVidia drivers don't seem to implement
210 // glPushAttrib(FG_FOG_BIT) properly. The result is that
211 // there is not fog when looking at the sun.
212 glFogf ( GL_FOG_DENSITY, fog_exp2_density );
215 void SGSky::drawUpperClouds( ) {
216 // draw the cloud layers that are above us, top to bottom
217 for ( int i = (int)cloud_layers.size() - 1; i >= cur_layer_pos; --i ) {
218 if ( i != in_cloud ) {
219 cloud_layers[i]->draw( false );
225 // draw translucent clouds ... do this after you've drawn all the
226 // oapaque elements of your scene.
227 void SGSky::drawLowerClouds() {
229 // draw the cloud layers that are below us, bottom to top
230 for ( int i = 0; i < cur_layer_pos; ++i ) {
231 if ( i != in_cloud ) {
232 cloud_layers[i]->draw( true );
238 SGSky::add_cloud_layer( SGCloudLayer * layer )
240 cloud_layers.push_back(layer);
244 SGSky::get_cloud_layer (int i) const
246 return cloud_layers[i];
250 SGSky::get_cloud_layer (int i)
252 return cloud_layers[i];
256 SGSky::get_cloud_layer_count () const
258 return cloud_layers.size();
261 // modify the current visibility based on cloud layers, thickness,
262 // transition range, and simulated "puffs".
263 void SGSky::modify_vis( float alt, float time_factor ) {
264 float effvis = visibility;
266 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
267 float asl = cloud_layers[i]->getElevation_m();
268 float thickness = cloud_layers[i]->getThickness_m();
269 float transition = cloud_layers[i]->getTransition_m();
273 if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ) {
274 // less than 50% coverage -- assume we're in the clear for now
276 } else if ( alt < asl - transition ) {
279 } else if ( alt < asl ) {
280 // in lower transition
281 ratio = (asl - alt) / transition;
282 } else if ( alt < asl + thickness ) {
285 } else if ( alt < asl + thickness + transition ) {
286 // in upper transition
287 ratio = (alt - (asl + thickness)) / transition;
293 if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ||
294 cloud_layers[i]->get_layer3D()->is3D() && SGCloudField::enable3D) {
295 // do nothing, clear layers aren't drawn, don't affect
296 // visibility andn dont' need to be faded in or out.
297 } else if ( (cloud_layers[i]->getCoverage() ==
298 SGCloudLayer::SG_CLOUD_FEW)
299 || (cloud_layers[i]->getCoverage() ==
300 SGCloudLayer::SG_CLOUD_SCATTERED) )
302 // set the alpha fade value for the cloud layer. For less
303 // dense cloud layers we fade the layer to nothing as we
304 // approach it because we stay clear visibility-wise as we
306 float temp = ratio * 2.0;
307 if ( temp > 1.0 ) { temp = 1.0; }
308 cloud_layers[i]->setAlpha( temp );
310 // don't touch visibility
312 // maintain full alpha for denser cloud layer types.
313 // Let's set the value explicitly in case someone changed
315 cloud_layers[i]->setAlpha( 1.0 );
317 // lower visibility as we approach the cloud layer.
318 // accumulate effects from multiple cloud layers
325 // calc chance of entering cloud puff
326 double rnd = sg_random();
327 double chance = rnd * rnd * rnd;
328 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
330 puff_length = sg_random() * 2.0; // up to 2 seconds
331 puff_progression = 0.0;
336 // modify actual_visibility based on puff envelope
338 if ( puff_progression <= ramp_up ) {
339 double x = SGD_PI_2 * puff_progression / ramp_up;
340 double factor = 1.0 - sin( x );
341 // cout << "ramp up = " << puff_progression
342 // << " factor = " << factor << endl;
343 effvis = effvis * factor;
344 } else if ( puff_progression >= ramp_up + puff_length ) {
345 double x = SGD_PI_2 *
346 (puff_progression - (ramp_up + puff_length)) /
348 double factor = sin( x );
349 // cout << "ramp down = "
350 // << puff_progression - (ramp_up + puff_length)
351 // << " factor = " << factor << endl;
352 effvis = effvis * factor;
357 /* cout << "len = " << puff_length
359 << " factor = " << factor
360 << " actual_visibility = " << actual_visibility
363 // time_factor = ( global_multi_loop *
364 // current_options.get_speed_up() ) /
365 // (double)current_options.get_model_hz();
367 puff_progression += time_factor;
368 // cout << "time factor = " << time_factor << endl;
370 /* cout << "gml = " << global_multi_loop
371 << " speed up = " << current_options.get_speed_up()
372 << " hz = " << current_options.get_model_hz() << endl;
375 if ( puff_progression > puff_length + ramp_up + ramp_down) {
382 // never let visibility drop below 25 meters
383 if ( effvis <= 25.0 ) {
389 effective_visibility = effvis;