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 ... do this before you draw the
179 // rest of your scene.
180 void SGSky::preDraw() {
181 ssgCullAndDraw( pre_root );
185 // draw translucent clouds ... do this after you've drawn all the
186 // oapaque elements of your scene.
187 void SGSky::postDraw( float alt ) {
188 float slop = 4.0; // if we are closer than this to a cloud layer,
191 int in_cloud = -1; // cloud we are in
193 // check where we are relative to the cloud layers
194 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
195 float asl = cloud_layers[i]->get_asl();
196 float thickness = cloud_layers[i]->get_thickness();
198 if ( alt < asl - slop ) {
200 } else if ( alt < asl + thickness + slop ) {
203 // bail now and don't draw any clouds
210 // determine rendering order
212 while ( pos < (int)cloud_layers.size() &&
213 alt > cloud_layers[pos]->get_asl())
219 // we are below all the cloud layers, draw top to bottom
220 for ( int i = cloud_layers.size() - 1; i >= 0; --i ) {
221 if ( i != in_cloud ) {
222 cloud_layers[i]->draw();
225 } else if ( pos >= (int)cloud_layers.size() ) {
226 // we are above all the cloud layers, draw bottom to top
227 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
228 if ( i != in_cloud ) {
229 cloud_layers[i]->draw();
233 // we are between cloud layers, draw lower layers bottom to
234 // top and upper layers top to bottom
235 for ( int i = 0; i < pos; ++i ) {
236 if ( i != in_cloud ) {
237 cloud_layers[i]->draw();
240 for ( int i = cloud_layers.size() - 1; i >= pos; --i ) {
241 if ( i != in_cloud ) {
242 cloud_layers[i]->draw();
249 void SGSky::add_cloud_layer( double asl, double thickness, double transition,
250 ssgSimpleState *state ) {
251 SGCloudLayer *layer = new SGCloudLayer;
252 layer->build( 40000.0f, asl, thickness, transition, state );
254 layer_list_iterator current = cloud_layers.begin();
255 layer_list_iterator last = cloud_layers.end();
256 while ( current != last && (*current)->get_asl() < asl ) {
260 if ( current != last ) {
261 cloud_layers.insert( current, layer );
263 cloud_layers.push_back( layer );
266 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
267 cout << "layer " << i << " = " << cloud_layers[i]->get_asl() << endl;
273 void SGSky::add_cloud_layer( double asl, double thickness, double transition,
274 const string &tex_path ) {
275 ssgSimpleState *state = SGCloudMakeState( tex_path );
276 add_cloud_layer( asl, thickness, transition, state );
280 void SGSky::add_cloud_layer( double asl, double thickness, double transition,
282 if ( type > 0 && type < SG_MAX_CLOUD_TYPES ) {
283 add_cloud_layer( asl, thickness, transition, cloud_mats[type] );
288 // modify the current visibility based on cloud layers, thickness,
289 // transition range, and simulated "puffs".
290 void SGSky::modify_vis( float alt, float time_factor ) {
291 float effvis = visibility;
293 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
294 float asl = cloud_layers[i]->get_asl();
295 float thickness = cloud_layers[i]->get_thickness();
296 float transition = cloud_layers[i]->get_transition();
300 if ( alt < asl - transition ) {
303 } else if ( alt < asl ) {
304 // in lower transition
305 ratio = (asl - alt) / transition;
306 } else if ( alt < asl + thickness ) {
309 } else if ( alt < asl + thickness + transition ) {
310 // in upper transition
311 ratio = (alt - (asl + thickness)) / transition;
317 // accumulate effects from multiple cloud layers
322 // calc chance of entering cloud puff
323 double rnd = fg_random();
324 double chance = rnd * rnd * rnd;
325 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
327 puff_length = fg_random() * 2.0; // up to 2 seconds
328 puff_progression = 0.0;
333 // modify actual_visibility based on puff envelope
335 if ( puff_progression <= ramp_up ) {
336 double x = 0.5 * SGD_PI * puff_progression / ramp_up;
337 double factor = 1.0 - sin( x );
338 // cout << "ramp up = " << puff_progression
339 // << " factor = " << factor << endl;
340 effvis = effvis * factor;
341 } else if ( puff_progression >= ramp_up + puff_length ) {
342 double x = 0.5 * SGD_PI *
343 (puff_progression - (ramp_up + puff_length)) /
345 double factor = sin( x );
346 // cout << "ramp down = "
347 // << puff_progression - (ramp_up + puff_length)
348 // << " factor = " << factor << endl;
349 effvis = effvis * factor;
354 /* cout << "len = " << puff_length
356 << " factor = " << factor
357 << " actual_visibility = " << actual_visibility
360 // time_factor = ( global_multi_loop *
361 // current_options.get_speed_up() ) /
362 // (double)current_options.get_model_hz();
364 puff_progression += time_factor;
365 // cout << "time factor = " << time_factor << endl;
367 /* cout << "gml = " << global_multi_loop
368 << " speed up = " << current_options.get_speed_up()
369 << " hz = " << current_options.get_model_hz() << endl;
372 if ( puff_progression > puff_length + ramp_up + ramp_down) {
377 // never let visibility drop below 25 meters
378 if ( effvis <= 25.0 ) {
384 effective_visibility = effvis;