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.
26 # include <simgear_config.h>
32 #include <simgear/math/sg_random.h>
35 #include "cloudfield.hxx"
38 SGSky::SGSky( void ) {
39 effective_visibility = visibility = 10000.0;
41 // near cloud visibility state variables
57 for (unsigned int i = 0; i < cloud_layers.size(); i++)
58 delete cloud_layers[i];
62 // initialize the sky and connect the components to the scene graph at
63 // the provided branch
64 void SGSky::build( double h_radius_m, double v_radius_m,
65 double sun_size, double moon_size,
66 int nplanets, sgdVec3 *planet_data,
67 int nstars, sgdVec3 *star_data )
69 pre_root = new ssgRoot;
70 post_root = new ssgRoot;
72 pre_selector = new ssgSelector;
73 post_selector = new ssgSelector;
75 pre_transform = new ssgTransform;
76 post_transform = new ssgTransform;
79 pre_transform -> addKid( dome->build( h_radius_m, v_radius_m ) );
81 planets = new SGStars;
82 pre_transform -> addKid(planets->build(nplanets, planet_data, h_radius_m));
85 pre_transform -> addKid( stars->build(nstars, star_data, h_radius_m) );
88 pre_transform -> addKid( moon->build(tex_path, moon_size) );
91 pre_transform -> addKid( oursun->build(tex_path, sun_size) );
93 pre_selector->addKid( pre_transform );
94 pre_selector->clrTraversalMaskBits( SSGTRAV_HOT );
96 post_selector->addKid( post_transform );
97 post_selector->clrTraversalMaskBits( SSGTRAV_HOT );
99 pre_root->addKid( pre_selector );
100 post_root->addKid( post_selector );
104 // repaint the sky components based on current value of sun_angle,
105 // sky, and fog colors.
107 // sun angle in degrees relative to verticle
108 // 0 degrees = high noon
109 // 90 degrees = sun rise/set
110 // 180 degrees = darkest midnight
111 bool SGSky::repaint( const SGSkyColor &sc )
113 if ( effective_visibility > 1000.0 ) {
115 dome->repaint( sc.sky_color, sc.fog_color, sc.sun_angle,
116 effective_visibility );
118 stars->repaint( sc.sun_angle, sc.nstars, sc.star_data );
119 planets->repaint( sc.sun_angle, sc.nplanets, sc.planet_data );
121 oursun->repaint( sc.sun_angle, effective_visibility );
122 moon->repaint( sc.moon_angle );
124 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
125 if (cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR){
126 cloud_layers[i]->repaint( sc.cloud_color );
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( SGSkyState &st, double dt )
148 double angle = st.gst * 15; // degrees
150 dome->reposition( st.zero_elev, st.lon, st.lat, st.spin );
152 stars->reposition( st.view_pos, angle );
153 planets->reposition( st.view_pos, angle );
155 oursun->reposition( st.view_pos, angle,
156 st.sun_ra, st.sun_dec, st.sun_dist );
158 moon->reposition( st.view_pos, angle,
159 st.moon_ra, st.moon_dec, st.moon_dist );
161 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
162 if ( cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR ) {
163 cloud_layers[i]->reposition( st.zero_elev, st.view_up,
164 st.lon, st.lat, st.alt, dt );
172 // draw background portions of the sky ... do this before you draw the
173 // rest of your scene.
174 void SGSky::preDraw( float alt, float fog_exp2_density ) {
175 ssgCullAndDraw( pre_root );
177 // if we are closer than this to a cloud layer, don't draw clouds
178 static const float slop = 5.0;
181 // check where we are relative to the cloud layers
183 for ( i = 0; i < (int)cloud_layers.size(); ++i ) {
184 float asl = cloud_layers[i]->getElevation_m();
185 float thickness = cloud_layers[i]->getThickness_m();
187 if ( alt < asl - slop ) {
189 } else if ( alt < asl + thickness + slop ) {
192 // bail now and don't draw any clouds
193 if( cloud_layers[i]->get_layer3D()->is3D() && SGCloudField::enable3D )
201 // determine rendering order
203 while ( cur_layer_pos < (int)cloud_layers.size() &&
204 alt > cloud_layers[cur_layer_pos]->getElevation_m() )
209 // FIXME: This should not be needed, but at this time (08/15/2003)
210 // certain NVidia drivers don't seem to implement
211 // glPushAttrib(FG_FOG_BIT) properly. The result is that
212 // there is not fog when looking at the sun.
213 glFogf ( GL_FOG_DENSITY, fog_exp2_density );
216 void SGSky::drawUpperClouds( ) {
217 // draw the cloud layers that are above us, top to bottom
218 for ( int i = (int)cloud_layers.size() - 1; i >= cur_layer_pos; --i ) {
219 if ( i != in_cloud ) {
220 cloud_layers[i]->draw( false );
226 // draw translucent clouds ... do this after you've drawn all the
227 // oapaque elements of your scene.
228 void SGSky::drawLowerClouds() {
230 // draw the cloud layers that are below us, bottom to top
231 for ( int i = 0; i < cur_layer_pos; ++i ) {
232 if ( i != in_cloud ) {
233 cloud_layers[i]->draw( true );
239 SGSky::add_cloud_layer( SGCloudLayer * layer )
241 cloud_layers.push_back(layer);
245 SGSky::get_cloud_layer (int i) const
247 return cloud_layers[i];
251 SGSky::get_cloud_layer (int i)
253 return cloud_layers[i];
257 SGSky::get_cloud_layer_count () const
259 return cloud_layers.size();
262 // modify the current visibility based on cloud layers, thickness,
263 // transition range, and simulated "puffs".
264 void SGSky::modify_vis( float alt, float time_factor ) {
265 float effvis = visibility;
267 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
268 float asl = cloud_layers[i]->getElevation_m();
269 float thickness = cloud_layers[i]->getThickness_m();
270 float transition = cloud_layers[i]->getTransition_m();
274 if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ) {
275 // less than 50% coverage -- assume we're in the clear for now
277 } else if ( alt < asl - transition ) {
280 } else if ( alt < asl ) {
281 // in lower transition
282 ratio = (asl - alt) / transition;
283 } else if ( alt < asl + thickness ) {
286 } else if ( alt < asl + thickness + transition ) {
287 // in upper transition
288 ratio = (alt - (asl + thickness)) / transition;
294 if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ||
295 cloud_layers[i]->get_layer3D()->is3D() && SGCloudField::enable3D) {
296 // do nothing, clear layers aren't drawn, don't affect
297 // visibility andn dont' need to be faded in or out.
298 } else if ( (cloud_layers[i]->getCoverage() ==
299 SGCloudLayer::SG_CLOUD_FEW)
300 || (cloud_layers[i]->getCoverage() ==
301 SGCloudLayer::SG_CLOUD_SCATTERED) )
303 // set the alpha fade value for the cloud layer. For less
304 // dense cloud layers we fade the layer to nothing as we
305 // approach it because we stay clear visibility-wise as we
307 float temp = ratio * 2.0;
308 if ( temp > 1.0 ) { temp = 1.0; }
309 cloud_layers[i]->setAlpha( temp );
311 // don't touch visibility
313 // maintain full alpha for denser cloud layer types.
314 // Let's set the value explicitly in case someone changed
316 cloud_layers[i]->setAlpha( 1.0 );
318 // lower visibility as we approach the cloud layer.
319 // accumulate effects from multiple cloud layers
326 // calc chance of entering cloud puff
327 double rnd = sg_random();
328 double chance = rnd * rnd * rnd;
329 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
331 puff_length = sg_random() * 2.0; // up to 2 seconds
332 puff_progression = 0.0;
337 // modify actual_visibility based on puff envelope
339 if ( puff_progression <= ramp_up ) {
340 double x = SGD_PI_2 * puff_progression / ramp_up;
341 double factor = 1.0 - sin( x );
342 // cout << "ramp up = " << puff_progression
343 // << " factor = " << factor << endl;
344 effvis = effvis * factor;
345 } else if ( puff_progression >= ramp_up + puff_length ) {
346 double x = SGD_PI_2 *
347 (puff_progression - (ramp_up + puff_length)) /
349 double factor = sin( x );
350 // cout << "ramp down = "
351 // << puff_progression - (ramp_up + puff_length)
352 // << " factor = " << factor << endl;
353 effvis = effvis * factor;
358 /* cout << "len = " << puff_length
360 << " factor = " << factor
361 << " actual_visibility = " << actual_visibility
364 // time_factor = ( global_multi_loop *
365 // current_options.get_speed_up() ) /
366 // (double)current_options.get_model_hz();
368 puff_progression += time_factor;
369 // cout << "time factor = " << time_factor << endl;
371 /* cout << "gml = " << global_multi_loop
372 << " speed up = " << current_options.get_speed_up()
373 << " hz = " << current_options.get_model_hz() << endl;
376 if ( puff_progression > puff_length + ramp_up + ramp_down) {
383 // never let visibility drop below 25 meters
384 if ( effvis <= 25.0 ) {
390 effective_visibility = effvis;