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 Library General Public
19 // License along with this library; if not, write to the
20 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 // Boston, MA 02111-1307, USA.
29 #include <simgear/math/sg_random.h>
32 #include "cloudfield.hxx"
35 SGSky::SGSky( void ) {
36 effective_visibility = visibility = 10000.0;
38 // near cloud visibility state variables
54 for (unsigned int i = 0; i < cloud_layers.size(); i++)
55 delete cloud_layers[i];
59 // initialize the sky and connect the components to the scene graph at
60 // the provided branch
61 void SGSky::build( double h_radius_m, double v_radius_m,
62 double sun_size, double moon_size,
63 int nplanets, sgdVec3 *planet_data,
64 int nstars, sgdVec3 *star_data )
66 pre_root = new ssgRoot;
67 post_root = new ssgRoot;
69 pre_selector = new ssgSelector;
70 post_selector = new ssgSelector;
72 pre_transform = new ssgTransform;
73 post_transform = new ssgTransform;
76 pre_transform -> addKid( dome->build( h_radius_m, v_radius_m ) );
78 planets = new SGStars;
79 pre_transform -> addKid(planets->build(nplanets, planet_data, h_radius_m));
82 pre_transform -> addKid( stars->build(nstars, star_data, h_radius_m) );
85 pre_transform -> addKid( moon->build(tex_path, moon_size) );
88 pre_transform -> addKid( oursun->build(tex_path, sun_size) );
90 pre_selector->addKid( pre_transform );
91 pre_selector->clrTraversalMaskBits( SSGTRAV_HOT );
93 post_selector->addKid( post_transform );
94 post_selector->clrTraversalMaskBits( SSGTRAV_HOT );
96 pre_root->addKid( pre_selector );
97 post_root->addKid( post_selector );
101 // repaint the sky components based on current value of sun_angle,
102 // sky, and fog colors.
104 // sun angle in degrees relative to verticle
105 // 0 degrees = high noon
106 // 90 degrees = sun rise/set
107 // 180 degrees = darkest midnight
108 bool SGSky::repaint( const SGSkyColor &sc )
110 if ( effective_visibility > 1000.0 ) {
112 dome->repaint( sc.sky_color, sc.fog_color, sc.sun_angle,
113 effective_visibility );
115 stars->repaint( sc.sun_angle, sc.nstars, sc.star_data );
116 planets->repaint( sc.sun_angle, sc.nplanets, sc.planet_data );
118 oursun->repaint( sc.sun_angle, effective_visibility );
119 moon->repaint( sc.moon_angle );
121 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
122 if (cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR){
123 cloud_layers[i]->repaint( sc.cloud_color );
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( SGSkyState &st, double dt )
145 double angle = st.gst * 15; // degrees
147 dome->reposition( st.zero_elev, st.lon, st.lat, st.spin );
149 stars->reposition( st.view_pos, angle );
150 planets->reposition( st.view_pos, angle );
152 oursun->reposition( st.view_pos, angle,
153 st.sun_ra, st.sun_dec, st.sun_dist );
155 moon->reposition( st.view_pos, angle,
156 st.moon_ra, st.moon_dec, st.moon_dist );
158 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
159 if ( cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR ) {
160 cloud_layers[i]->reposition( st.zero_elev, st.view_up,
161 st.lon, st.lat, st.alt, dt );
169 // draw background portions of the sky ... do this before you draw the
170 // rest of your scene.
171 void SGSky::preDraw( float alt, float fog_exp2_density ) {
172 ssgCullAndDraw( pre_root );
174 // if we are closer than this to a cloud layer, don't draw clouds
175 static const float slop = 5.0;
178 // check where we are relative to the cloud layers
180 for ( i = 0; i < (int)cloud_layers.size(); ++i ) {
181 float asl = cloud_layers[i]->getElevation_m();
182 float thickness = cloud_layers[i]->getThickness_m();
184 if ( alt < asl - slop ) {
186 } else if ( alt < asl + thickness + slop ) {
189 // bail now and don't draw any clouds
190 if( cloud_layers[i]->get_layer3D()->is3D() && SGCloudField::enable3D )
198 // determine rendering order
200 while ( cur_layer_pos < (int)cloud_layers.size() &&
201 alt > cloud_layers[cur_layer_pos]->getElevation_m() )
206 // FIXME: This should not be needed, but at this time (08/15/2003)
207 // certain NVidia drivers don't seem to implement
208 // glPushAttrib(FG_FOG_BIT) properly. The result is that
209 // there is not fog when looking at the sun.
210 glFogf ( GL_FOG_DENSITY, fog_exp2_density );
213 void SGSky::drawUpperClouds( ) {
214 // draw the cloud layers that are above us, top to bottom
215 for ( int i = (int)cloud_layers.size() - 1; i >= cur_layer_pos; --i ) {
216 if ( i != in_cloud ) {
217 cloud_layers[i]->draw( false );
223 // draw translucent clouds ... do this after you've drawn all the
224 // oapaque elements of your scene.
225 void SGSky::drawLowerClouds() {
227 // draw the cloud layers that are below us, bottom to top
228 for ( int i = 0; i < cur_layer_pos; ++i ) {
229 if ( i != in_cloud ) {
230 cloud_layers[i]->draw( true );
236 SGSky::add_cloud_layer( SGCloudLayer * layer )
238 cloud_layers.push_back(layer);
242 SGSky::get_cloud_layer (int i) const
244 return cloud_layers[i];
248 SGSky::get_cloud_layer (int i)
250 return cloud_layers[i];
254 SGSky::get_cloud_layer_count () const
256 return cloud_layers.size();
259 // modify the current visibility based on cloud layers, thickness,
260 // transition range, and simulated "puffs".
261 void SGSky::modify_vis( float alt, float time_factor ) {
262 float effvis = visibility;
264 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
265 float asl = cloud_layers[i]->getElevation_m();
266 float thickness = cloud_layers[i]->getThickness_m();
267 float transition = cloud_layers[i]->getTransition_m();
271 if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ) {
272 // less than 50% coverage -- assume we're in the clear for now
274 } else if ( alt < asl - transition ) {
277 } else if ( alt < asl ) {
278 // in lower transition
279 ratio = (asl - alt) / transition;
280 } else if ( alt < asl + thickness ) {
283 } else if ( alt < asl + thickness + transition ) {
284 // in upper transition
285 ratio = (alt - (asl + thickness)) / transition;
291 if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ||
292 cloud_layers[i]->get_layer3D()->is3D() && SGCloudField::enable3D) {
293 // do nothing, clear layers aren't drawn, don't affect
294 // visibility andn dont' need to be faded in or out.
295 } else if ( (cloud_layers[i]->getCoverage() ==
296 SGCloudLayer::SG_CLOUD_FEW)
297 || (cloud_layers[i]->getCoverage() ==
298 SGCloudLayer::SG_CLOUD_SCATTERED) )
300 // set the alpha fade value for the cloud layer. For less
301 // dense cloud layers we fade the layer to nothing as we
302 // approach it because we stay clear visibility-wise as we
304 float temp = ratio * 2.0;
305 if ( temp > 1.0 ) { temp = 1.0; }
306 cloud_layers[i]->setAlpha( temp );
308 // don't touch visibility
310 // maintain full alpha for denser cloud layer types.
311 // Let's set the value explicitly in case someone changed
313 cloud_layers[i]->setAlpha( 1.0 );
315 // lower visibility as we approach the cloud layer.
316 // accumulate effects from multiple cloud layers
323 // calc chance of entering cloud puff
324 double rnd = sg_random();
325 double chance = rnd * rnd * rnd;
326 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
328 puff_length = sg_random() * 2.0; // up to 2 seconds
329 puff_progression = 0.0;
334 // modify actual_visibility based on puff envelope
336 if ( puff_progression <= ramp_up ) {
337 double x = 0.5 * SGD_PI * puff_progression / ramp_up;
338 double factor = 1.0 - sin( x );
339 // cout << "ramp up = " << puff_progression
340 // << " factor = " << factor << endl;
341 effvis = effvis * factor;
342 } else if ( puff_progression >= ramp_up + puff_length ) {
343 double x = 0.5 * SGD_PI *
344 (puff_progression - (ramp_up + puff_length)) /
346 double factor = sin( x );
347 // cout << "ramp down = "
348 // << puff_progression - (ramp_up + puff_length)
349 // << " factor = " << factor << endl;
350 effvis = effvis * factor;
355 /* cout << "len = " << puff_length
357 << " factor = " << factor
358 << " actual_visibility = " << actual_visibility
361 // time_factor = ( global_multi_loop *
362 // current_options.get_speed_up() ) /
363 // (double)current_options.get_model_hz();
365 puff_progression += time_factor;
366 // cout << "time factor = " << time_factor << endl;
368 /* cout << "gml = " << global_multi_loop
369 << " speed up = " << current_options.get_speed_up()
370 << " hz = " << current_options.get_model_hz() << endl;
373 if ( puff_progression > puff_length + ramp_up + ramp_down) {
380 // never let visibility drop below 25 meters
381 if ( effvis <= 25.0 ) {
387 effective_visibility = effvis;