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 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>
35 SGSky::SGSky( void ) {
36 effective_visibility = visibility = 10000.0;
38 // near cloud visibility state variables
50 SGSky::~SGSky( void ) {
54 // initialize the sky and connect the components to the scene graph at
55 // the provided branch
56 void SGSky::build( double sun_size, double moon_size,
57 int nplanets, sgdVec3 *planet_data,
59 int nstars, sgdVec3 *star_data, double star_dist )
61 pre_root = new ssgRoot;
62 post_root = new ssgRoot;
64 pre_selector = new ssgSelector;
65 post_selector = new ssgSelector;
67 pre_transform = new ssgTransform;
68 post_transform = new ssgTransform;
71 pre_transform -> addKid( dome->build() );
73 planets = new SGStars;
74 pre_transform -> addKid( planets->build(nplanets, planet_data,
79 pre_transform -> addKid( stars->build(nstars, star_data, star_dist) );
82 pre_transform -> addKid( moon->build(tex_path, moon_size) );
85 pre_transform -> addKid( oursun->build(tex_path, sun_size) );
87 pre_selector->addKid( pre_transform );
88 pre_selector->clrTraversalMaskBits( SSGTRAV_HOT );
90 post_selector->addKid( post_transform );
91 post_selector->clrTraversalMaskBits( SSGTRAV_HOT );
93 pre_root->addKid( pre_selector );
94 post_root->addKid( post_selector );
96 // add the cloud ssgStates to the material lib
99 cloud_path.set( tex_path.str() );
100 cloud_path.append( "overcast.rgb" );
101 cloud_mats[SG_CLOUD_OVERCAST] = SGCloudMakeState( cloud_path.str() );
103 cloud_path.set( tex_path.str() );
104 cloud_path.append( "mostlycloudy.rgba" );
105 cloud_mats[SG_CLOUD_MOSTLY_CLOUDY] = SGCloudMakeState( cloud_path.str() );
107 cloud_path.set( tex_path.str() );
108 cloud_path.append( "mostlysunny.rgba" );
109 cloud_mats[SG_CLOUD_MOSTLY_SUNNY] = SGCloudMakeState( cloud_path.str() );
111 cloud_path.set( tex_path.str() );
112 cloud_path.append( "cirrus.rgba" );
113 cloud_mats[SG_CLOUD_CIRRUS] = SGCloudMakeState( cloud_path.str() );
117 // repaint the sky components based on current value of sun_angle,
118 // sky, and fog colors.
120 // sun angle in degrees relative to verticle
121 // 0 degrees = high noon
122 // 90 degrees = sun rise/set
123 // 180 degrees = darkest midnight
124 bool SGSky::repaint( sgVec4 sky_color, sgVec4 fog_color,
125 double sun_angle, double moon_angle,
126 int nplanets, sgdVec3 *planet_data,
127 int nstars, sgdVec3 *star_data )
129 if ( effective_visibility > 1000.0 ) {
131 dome->repaint( sky_color, fog_color, sun_angle, effective_visibility );
132 oursun->repaint( sun_angle );
133 moon->repaint( moon_angle );
134 planets->repaint( sun_angle, nplanets, planet_data );
135 stars->repaint( sun_angle, nstars, star_data );
137 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
138 cloud_layers[i]->repaint( fog_color );
149 // reposition the sky at the specified origin and orientation
151 // lon specifies a rotation about the Z axis
152 // lat specifies a rotation about the new Y axis
153 // spin specifies a rotation about the new Z axis (this allows
154 // additional orientation for the sunrise/set effects and is used by
155 // the skydome and perhaps clouds.
156 bool SGSky::reposition( sgVec3 view_pos, sgVec3 zero_elev, sgVec3 view_up,
157 double lon, double lat, double alt, double spin,
159 double sun_ra, double sun_dec, double sun_dist,
160 double moon_ra, double moon_dec, double moon_dist )
162 double angle = gst * 15; // degrees
163 dome->reposition( zero_elev, lon, lat, spin );
164 oursun->reposition( view_pos, angle, sun_ra, sun_dec, sun_dist );
165 moon->reposition( view_pos, angle, moon_ra, moon_dec, moon_dist );
166 planets->reposition( view_pos, angle );
167 stars->reposition( view_pos, angle );
169 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
170 cloud_layers[i]->reposition( zero_elev, view_up, lon, lat, alt );
177 // draw background portions of the sky ... do this before you draw the
178 // rest of your scene.
179 void SGSky::preDraw() {
180 ssgCullAndDraw( pre_root );
184 // draw translucent clouds ... do this after you've drawn all the
185 // oapaque elements of your scene.
186 void SGSky::postDraw( float alt ) {
187 float slop = 5.0; // if we are closer than this to a cloud layer,
190 int in_cloud = -1; // cloud we are in
194 // check where we are relative to the cloud layers
195 for ( i = 0; i < (int)cloud_layers.size(); ++i ) {
196 float asl = cloud_layers[i]->get_asl();
197 float thickness = cloud_layers[i]->get_thickness();
199 if ( alt < asl - slop ) {
201 } else if ( alt < asl + thickness + slop ) {
204 // bail now and don't draw any clouds
211 // determine rendering order
213 while ( pos < (int)cloud_layers.size() &&
214 alt > cloud_layers[pos]->get_asl())
220 // we are below all the cloud layers, draw top to bottom
221 for ( i = cloud_layers.size() - 1; i >= 0; --i ) {
222 if ( i != in_cloud ) {
223 cloud_layers[i]->draw();
226 } else if ( pos >= (int)cloud_layers.size() ) {
227 // we are above all the cloud layers, draw bottom to top
228 for ( i = 0; i < (int)cloud_layers.size(); ++i ) {
229 if ( i != in_cloud ) {
230 cloud_layers[i]->draw();
234 // we are between cloud layers, draw lower layers bottom to
235 // top and upper layers top to bottom
236 for ( i = 0; i < pos; ++i ) {
237 if ( i != in_cloud ) {
238 cloud_layers[i]->draw();
241 for ( i = cloud_layers.size() - 1; i >= pos; --i ) {
242 if ( i != in_cloud ) {
243 cloud_layers[i]->draw();
250 void SGSky::add_cloud_layer( double asl, double thickness,
251 double transition, double span,
252 ssgSimpleState *state ) {
253 SGCloudLayer *layer = new SGCloudLayer;
254 layer->build( span, asl, thickness, transition, state );
256 layer_list_iterator current = cloud_layers.begin();
257 layer_list_iterator last = cloud_layers.end();
258 while ( current != last && (*current)->get_asl() < asl ) {
262 if ( current != last ) {
263 cloud_layers.insert( current, layer );
265 cloud_layers.push_back( layer );
268 // for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
269 // cout << "layer " << i << " = " << cloud_layers[i]->get_asl() << endl;
275 void SGSky::add_cloud_layer( double asl, double thickness,
276 double transition, double span,
277 const string &tex_path ) {
278 ssgSimpleState *state = SGCloudMakeState( tex_path );
279 add_cloud_layer( asl, thickness, transition, span, state );
283 void SGSky::add_cloud_layer( double asl, double thickness,
284 double transition, double span,
286 if ( type > 0 && type < SG_MAX_CLOUD_TYPES ) {
287 add_cloud_layer( asl, thickness, transition, span, cloud_mats[type] );
292 // modify the current visibility based on cloud layers, thickness,
293 // transition range, and simulated "puffs".
294 void SGSky::modify_vis( float alt, float time_factor ) {
295 float effvis = visibility;
297 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
298 float asl = cloud_layers[i]->get_asl();
299 float thickness = cloud_layers[i]->get_thickness();
300 float transition = cloud_layers[i]->get_transition();
304 if ( alt < asl - transition ) {
307 } else if ( alt < asl ) {
308 // in lower transition
309 ratio = (asl - alt) / transition;
310 } else if ( alt < asl + thickness ) {
313 } else if ( alt < asl + thickness + transition ) {
314 // in upper transition
315 ratio = (alt - (asl + thickness)) / transition;
321 // 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 = 0.5 * SGD_PI * 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 = 0.5 * SGD_PI *
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) {
381 // never let visibility drop below 25 meters
382 if ( effvis <= 25.0 ) {
388 effective_visibility = effvis;