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
53 SGSky::~SGSky( void ) {
57 // initialize the sky and connect the components to the scene graph at
58 // the provided branch
59 void SGSky::build( double sun_size, double moon_size,
60 int nplanets, sgdVec3 *planet_data,
62 int nstars, sgdVec3 *star_data, double star_dist )
64 pre_root = new ssgRoot;
65 post_root = new ssgRoot;
67 pre_selector = new ssgSelector;
68 post_selector = new ssgSelector;
70 pre_transform = new ssgTransform;
71 post_transform = new ssgTransform;
74 pre_transform -> addKid( dome->build() );
76 planets = new SGStars;
77 pre_transform -> addKid( planets->build(nplanets, planet_data,
82 pre_transform -> addKid( stars->build(nstars, star_data, star_dist) );
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 );
99 // add the cloud ssgStates to the material lib
102 cloud_path.set( tex_path.str() );
103 cloud_path.append( "overcast.rgb" );
104 cloud_mats[SG_CLOUD_OVERCAST] = SGCloudMakeState( cloud_path.str() );
106 cloud_path.set( tex_path.str() );
107 cloud_path.append( "mostlycloudy.rgba" );
108 cloud_mats[SG_CLOUD_MOSTLY_CLOUDY] = SGCloudMakeState( cloud_path.str() );
110 cloud_path.set( tex_path.str() );
111 cloud_path.append( "mostlysunny.rgba" );
112 cloud_mats[SG_CLOUD_MOSTLY_SUNNY] = SGCloudMakeState( cloud_path.str() );
114 cloud_path.set( tex_path.str() );
115 cloud_path.append( "cirrus.rgba" );
116 cloud_mats[SG_CLOUD_CIRRUS] = SGCloudMakeState( cloud_path.str() );
120 // repaint the sky components based on current value of sun_angle,
121 // sky, and fog colors.
123 // sun angle in degrees relative to verticle
124 // 0 degrees = high noon
125 // 90 degrees = sun rise/set
126 // 180 degrees = darkest midnight
127 bool SGSky::repaint( sgVec4 sky_color, sgVec4 fog_color,
128 double sun_angle, double moon_angle,
129 int nplanets, sgdVec3 *planet_data,
130 int nstars, sgdVec3 *star_data )
132 if ( effective_visibility > 1000.0 ) {
134 dome->repaint( sky_color, fog_color, sun_angle, effective_visibility );
135 oursun->repaint( sun_angle );
136 moon->repaint( moon_angle );
137 planets->repaint( sun_angle, nplanets, planet_data );
138 stars->repaint( sun_angle, nstars, star_data );
140 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
141 cloud_layers[i]->repaint( fog_color );
152 // reposition the sky at the specified origin and orientation
154 // lon specifies a rotation about the Z axis
155 // lat specifies a rotation about the new Y axis
156 // spin specifies a rotation about the new Z axis (this allows
157 // additional orientation for the sunrise/set effects and is used by
158 // the skydome and perhaps clouds.
159 bool SGSky::reposition( sgVec3 view_pos, sgVec3 zero_elev, sgVec3 view_up,
160 double lon, double lat, double alt, double spin,
162 double sun_ra, double sun_dec, double sun_dist,
163 double moon_ra, double moon_dec, double moon_dist )
165 double angle = gst * 15; // degrees
166 dome->reposition( zero_elev, lon, lat, spin );
167 oursun->reposition( view_pos, angle, sun_ra, sun_dec, sun_dist );
168 moon->reposition( view_pos, angle, moon_ra, moon_dec, moon_dist );
169 planets->reposition( view_pos, angle );
170 stars->reposition( view_pos, angle );
172 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
173 cloud_layers[i]->reposition( zero_elev, view_up, lon, lat, alt );
180 // draw background portions of the sky ... do this before you draw the
181 // rest of your scene.
182 void SGSky::preDraw() {
183 ssgCullAndDraw( pre_root );
187 // draw translucent clouds ... do this after you've drawn all the
188 // oapaque elements of your scene.
189 void SGSky::postDraw( float alt ) {
190 float slop = 5.0; // if we are closer than this to a cloud layer,
193 int in_cloud = -1; // cloud we are in
195 // check where we are relative to the cloud layers
196 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
197 float asl = cloud_layers[i]->get_asl();
198 float thickness = cloud_layers[i]->get_thickness();
200 if ( alt < asl - slop ) {
202 } else if ( alt < asl + thickness + slop ) {
205 // bail now and don't draw any clouds
212 // determine rendering order
214 while ( pos < (int)cloud_layers.size() &&
215 alt > cloud_layers[pos]->get_asl())
221 // we are below all the cloud layers, draw top to bottom
222 for ( int i = cloud_layers.size() - 1; i >= 0; --i ) {
223 if ( i != in_cloud ) {
224 cloud_layers[i]->draw();
227 } else if ( pos >= (int)cloud_layers.size() ) {
228 // we are above all the cloud layers, draw bottom to top
229 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
230 if ( i != in_cloud ) {
231 cloud_layers[i]->draw();
235 // we are between cloud layers, draw lower layers bottom to
236 // top and upper layers top to bottom
237 for ( int i = 0; i < pos; ++i ) {
238 if ( i != in_cloud ) {
239 cloud_layers[i]->draw();
242 for ( int i = cloud_layers.size() - 1; i >= pos; --i ) {
243 if ( i != in_cloud ) {
244 cloud_layers[i]->draw();
251 void SGSky::add_cloud_layer( double span, double asl,
252 double thickness, double transition,
253 ssgSimpleState *state ) {
254 SGCloudLayer *layer = new SGCloudLayer;
255 layer->build( span, asl, thickness, transition, state );
257 layer_list_iterator current = cloud_layers.begin();
258 layer_list_iterator last = cloud_layers.end();
259 while ( current != last && (*current)->get_asl() < asl ) {
263 if ( current != last ) {
264 cloud_layers.insert( current, layer );
266 cloud_layers.push_back( layer );
269 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
270 cout << "layer " << i << " = " << cloud_layers[i]->get_asl() << endl;
276 void SGSky::add_cloud_layer( double span, double asl,
277 double thickness, double transition,
278 const string &tex_path ) {
279 ssgSimpleState *state = SGCloudMakeState( tex_path );
280 add_cloud_layer( span, asl, thickness, transition, state );
284 void SGSky::add_cloud_layer( double span, double asl,
285 double thickness, double transition,
287 if ( type > 0 && type < SG_MAX_CLOUD_TYPES ) {
288 add_cloud_layer( span, asl, thickness, transition, cloud_mats[type] );
293 // modify the current visibility based on cloud layers, thickness,
294 // transition range, and simulated "puffs".
295 void SGSky::modify_vis( float alt, float time_factor ) {
296 float effvis = visibility;
298 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
299 float asl = cloud_layers[i]->get_asl();
300 float thickness = cloud_layers[i]->get_thickness();
301 float transition = cloud_layers[i]->get_transition();
305 if ( alt < asl - transition ) {
308 } else if ( alt < asl ) {
309 // in lower transition
310 ratio = (asl - alt) / transition;
311 } else if ( alt < asl + thickness ) {
314 } else if ( alt < asl + thickness + transition ) {
315 // in upper transition
316 ratio = (alt - (asl + thickness)) / transition;
322 // accumulate effects from multiple cloud layers
327 // calc chance of entering cloud puff
328 double rnd = fg_random();
329 double chance = rnd * rnd * rnd;
330 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
332 puff_length = fg_random() * 2.0; // up to 2 seconds
333 puff_progression = 0.0;
338 // modify actual_visibility based on puff envelope
340 if ( puff_progression <= ramp_up ) {
341 double x = 0.5 * SGD_PI * puff_progression / ramp_up;
342 double factor = 1.0 - sin( x );
343 // cout << "ramp up = " << puff_progression
344 // << " factor = " << factor << endl;
345 effvis = effvis * factor;
346 } else if ( puff_progression >= ramp_up + puff_length ) {
347 double x = 0.5 * SGD_PI *
348 (puff_progression - (ramp_up + puff_length)) /
350 double factor = sin( x );
351 // cout << "ramp down = "
352 // << puff_progression - (ramp_up + puff_length)
353 // << " factor = " << factor << endl;
354 effvis = effvis * factor;
359 /* cout << "len = " << puff_length
361 << " factor = " << factor
362 << " actual_visibility = " << actual_visibility
365 // time_factor = ( global_multi_loop *
366 // current_options.get_speed_up() ) /
367 // (double)current_options.get_model_hz();
369 puff_progression += time_factor;
370 // cout << "time factor = " << time_factor << endl;
372 /* cout << "gml = " << global_multi_loop
373 << " speed up = " << current_options.get_speed_up()
374 << " hz = " << current_options.get_model_hz() << endl;
377 if ( puff_progression > puff_length + ramp_up + ramp_down) {
382 // never let visibility drop below 25 meters
383 if ( effvis <= 25.0 ) {
389 effective_visibility = effvis;