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 - curt@flightgear.org
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // 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., 675 Mass Ave, Cambridge, MA 02139, USA.
32 #include <simgear/math/fg_random.h>
38 SGSky::SGSky( void ) {
39 effective_visibility = visibility = 10000.0;
41 // near cloud visibility state variables
51 SGSky::~SGSky( void ) {
55 // initialize the sky and connect the components to the scene graph at
56 // the provided branch
57 void SGSky::build( double sun_size, double moon_size,
58 int nplanets, sgdVec3 *planet_data,
60 int nstars, sgdVec3 *star_data, double star_dist )
62 pre_root = new ssgRoot;
63 post_root = new ssgRoot;
65 pre_selector = new ssgSelector;
66 post_selector = new ssgSelector;
68 pre_transform = new ssgTransform;
69 post_transform = new ssgTransform;
72 pre_transform -> addKid( dome->build() );
74 planets = new SGStars;
75 pre_transform -> addKid( planets->build(nplanets, planet_data,
80 pre_transform -> addKid( stars->build(nstars, star_data, star_dist) );
83 pre_transform -> addKid( moon->build(tex_path, moon_size) );
86 pre_transform -> addKid( oursun->build(tex_path, sun_size) );
88 pre_selector->addKid( pre_transform );
89 pre_selector->clrTraversalMaskBits( SSGTRAV_HOT );
91 post_selector->addKid( post_transform );
92 post_selector->clrTraversalMaskBits( SSGTRAV_HOT );
94 pre_root->addKid( pre_selector );
95 post_root->addKid( post_selector );
97 // add the cloud ssgStates to the material lib
100 cloud_path.set( tex_path.str() );
101 cloud_path.append( "overcast.rgb" );
102 cloud_mats[SG_CLOUD_OVERCAST] = SGCloudMakeState( cloud_path.str() );
104 cloud_path.set( tex_path.str() );
105 cloud_path.append( "mostlycloudy.rgba" );
106 cloud_mats[SG_CLOUD_MOSTLY_CLOUDY] = SGCloudMakeState( cloud_path.str() );
108 cloud_path.set( tex_path.str() );
109 cloud_path.append( "mostlysunny.rgba" );
110 cloud_mats[SG_CLOUD_MOSTLY_SUNNY] = SGCloudMakeState( cloud_path.str() );
112 cloud_path.set( tex_path.str() );
113 cloud_path.append( "cirrus.rgba" );
114 cloud_mats[SG_CLOUD_CIRRUS] = SGCloudMakeState( cloud_path.str() );
118 // repaint the sky components based on current value of sun_angle,
119 // sky, and fog colors.
121 // sun angle in degrees relative to verticle
122 // 0 degrees = high noon
123 // 90 degrees = sun rise/set
124 // 180 degrees = darkest midnight
125 bool SGSky::repaint( sgVec4 sky_color, sgVec4 fog_color,
126 double sun_angle, double moon_angle,
127 int nplanets, sgdVec3 *planet_data,
128 int nstars, sgdVec3 *star_data )
130 if ( effective_visibility > 1000.0 ) {
132 dome->repaint( sky_color, fog_color, sun_angle );
133 oursun->repaint( sun_angle );
134 moon->repaint( moon_angle );
135 planets->repaint( sun_angle, nplanets, planet_data );
136 stars->repaint( sun_angle, nstars, star_data );
138 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
139 cloud_layers[i]->repaint( fog_color );
150 // reposition the sky at the specified origin and orientation
152 // lon specifies a rotation about the Z axis
153 // lat specifies a rotation about the new Y axis
154 // spin specifies a rotation about the new Z axis (this allows
155 // additional orientation for the sunrise/set effects and is used by
156 // the skydome and perhaps clouds.
157 bool SGSky::reposition( sgVec3 view_pos, sgVec3 zero_elev, sgVec3 view_up,
158 double lon, double lat, double alt, double spin,
160 double sun_ra, double sun_dec, double sun_dist,
161 double moon_ra, double moon_dec, double moon_dist )
163 double angle = gst * 15; // degrees
164 dome->reposition( zero_elev, lon, lat, spin );
165 oursun->reposition( view_pos, angle, sun_ra, sun_dec, sun_dist );
166 moon->reposition( view_pos, angle, moon_ra, moon_dec, moon_dist );
167 planets->reposition( view_pos, angle );
168 stars->reposition( view_pos, angle );
170 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
171 cloud_layers[i]->reposition( zero_elev, view_up, lon, lat, alt );
178 // draw background portions of the sky
179 void SGSky::draw_background() {
180 ssgCullAndDraw( pre_root );
184 // draw scenery elements of the sky
185 void SGSky::draw_scene( float alt ) {
187 if ( effective_visibility < 4000.0 ) {
188 // bail and don't draw clouds
192 // determine rendering order
194 while ( pos < (int)cloud_layers.size() &&
195 alt > cloud_layers[pos]->get_asl())
201 // we are below all the cloud layers, draw top to bottom
202 for ( int i = cloud_layers.size() - 1; i >= 0; --i ) {
203 cloud_layers[i]->draw();
205 } else if ( pos >= (int)cloud_layers.size() ) {
206 // we are above all the cloud layers, draw bottom to top
207 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
208 cloud_layers[i]->draw();
211 // we are between cloud layers, draw lower layers bottom to
212 // top and upper layers top to bottom
213 for ( int i = 0; i < pos; ++i ) {
214 cloud_layers[i]->draw();
216 for ( int i = cloud_layers.size() - 1; i >= pos; --i ) {
217 cloud_layers[i]->draw();
223 void SGSky::add_cloud_layer( double asl, double thickness, double transition,
224 ssgSimpleState *state ) {
225 SGCloudLayer *layer = new SGCloudLayer;
226 layer->build( 40000.0f, asl, thickness, transition, state );
228 layer_list_iterator current = cloud_layers.begin();
229 layer_list_iterator last = cloud_layers.end();
230 while ( current != last && (*current)->get_asl() < asl ) {
234 if ( current != last ) {
235 cloud_layers.insert( current, layer );
237 cloud_layers.push_back( layer );
240 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
241 cout << "layer " << i << " = " << cloud_layers[i]->get_asl() << endl;
247 void SGSky::add_cloud_layer( double asl, double thickness, double transition,
248 const string &tex_path ) {
249 ssgSimpleState *state = SGCloudMakeState( tex_path );
250 add_cloud_layer( asl, thickness, transition, state );
254 void SGSky::add_cloud_layer( double asl, double thickness, double transition,
256 if ( type > 0 && type < SG_MAX_CLOUD_TYPES ) {
257 add_cloud_layer( asl, thickness, transition, cloud_mats[type] );
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]->get_asl();
269 float thickness = cloud_layers[i]->get_thickness();
270 float transition = cloud_layers[i]->get_transition();
274 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 // accumulate effects from multiple cloud layers
296 // calc chance of entering cloud puff
297 double rnd = fg_random();
298 double chance = rnd * rnd * rnd;
299 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
302 puff_length = fg_random() * 2.0; // up to 2 seconds
303 } while ( puff_length <= 0.0 );
304 puff_progression = 0.0;
309 // modify actual_visibility based on puff envelope
311 if ( puff_progression <= ramp_up ) {
312 double x = 2 * SGD_PI * puff_progression / ramp_up;
313 double factor = 1.0 - sin( x );
314 effvis = effvis * factor;
315 } else if ( puff_progression >= ramp_up + puff_length ) {
316 double x = 2 * SGD_PI *
317 (puff_progression - (ramp_up + puff_length)) /
319 double factor = sin( x );
320 effvis = effvis * factor;
325 /* cout << "len = " << puff_length
327 << " factor = " << factor
328 << " actual_visibility = " << actual_visibility
331 // time_factor = ( global_multi_loop *
332 // current_options.get_speed_up() ) /
333 // (double)current_options.get_model_hz();
335 puff_progression += time_factor;
337 /* cout << "gml = " << global_multi_loop
338 << " speed up = " << current_options.get_speed_up()
339 << " hz = " << current_options.get_model_hz() << endl;
342 if ( puff_progression > puff_length + ramp_up + ramp_down) {
347 // never let visibility drop below zero
354 effective_visibility = effvis;