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.
33 #include <simgear/math/sg_random.h>
39 SGSky::SGSky( void ) {
40 effective_visibility = visibility = 10000.0;
42 // near cloud visibility state variables
54 SGSky::~SGSky( void ) {
58 // initialize the sky and connect the components to the scene graph at
59 // the provided branch
60 void SGSky::build( double sun_size, double moon_size,
61 int nplanets, sgdVec3 *planet_data,
63 int nstars, sgdVec3 *star_data, double star_dist )
65 pre_root = new ssgRoot;
66 post_root = new ssgRoot;
68 pre_selector = new ssgSelector;
69 post_selector = new ssgSelector;
71 pre_transform = new ssgTransform;
72 post_transform = new ssgTransform;
75 pre_transform -> addKid( dome->build() );
77 planets = new SGStars;
78 pre_transform -> addKid( planets->build(nplanets, planet_data,
83 pre_transform -> addKid( stars->build(nstars, star_data, star_dist) );
86 pre_transform -> addKid( moon->build(tex_path, moon_size) );
89 pre_transform -> addKid( oursun->build(tex_path, sun_size) );
91 pre_selector->addKid( pre_transform );
92 pre_selector->clrTraversalMaskBits( SSGTRAV_HOT );
94 post_selector->addKid( post_transform );
95 post_selector->clrTraversalMaskBits( SSGTRAV_HOT );
97 pre_root->addKid( pre_selector );
98 post_root->addKid( post_selector );
100 // add the cloud ssgStates to the material lib
103 cloud_path.set( tex_path.str() );
104 cloud_path.append( "overcast.rgb" );
105 cloud_mats[SG_CLOUD_OVERCAST] = SGCloudMakeState( cloud_path.str() );
107 cloud_path.set( tex_path.str() );
108 cloud_path.append( "mostlycloudy.rgba" );
109 cloud_mats[SG_CLOUD_MOSTLY_CLOUDY] = SGCloudMakeState( cloud_path.str() );
111 cloud_path.set( tex_path.str() );
112 cloud_path.append( "mostlysunny.rgba" );
113 cloud_mats[SG_CLOUD_MOSTLY_SUNNY] = SGCloudMakeState( cloud_path.str() );
115 cloud_path.set( tex_path.str() );
116 cloud_path.append( "cirrus.rgba" );
117 cloud_mats[SG_CLOUD_CIRRUS] = SGCloudMakeState( cloud_path.str() );
121 // repaint the sky components based on current value of sun_angle,
122 // sky, and fog colors.
124 // sun angle in degrees relative to verticle
125 // 0 degrees = high noon
126 // 90 degrees = sun rise/set
127 // 180 degrees = darkest midnight
128 bool SGSky::repaint( sgVec4 sky_color, sgVec4 fog_color,
129 double sun_angle, double moon_angle,
130 int nplanets, sgdVec3 *planet_data,
131 int nstars, sgdVec3 *star_data )
133 if ( effective_visibility > 1000.0 ) {
135 dome->repaint( sky_color, fog_color, sun_angle, effective_visibility );
136 oursun->repaint( sun_angle );
137 moon->repaint( moon_angle );
138 planets->repaint( sun_angle, nplanets, planet_data );
139 stars->repaint( sun_angle, nstars, star_data );
141 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
142 cloud_layers[i]->repaint( fog_color );
153 // reposition the sky at the specified origin and orientation
155 // lon specifies a rotation about the Z axis
156 // lat specifies a rotation about the new Y axis
157 // spin specifies a rotation about the new Z axis (this allows
158 // additional orientation for the sunrise/set effects and is used by
159 // the skydome and perhaps clouds.
160 bool SGSky::reposition( sgVec3 view_pos, sgVec3 zero_elev, sgVec3 view_up,
161 double lon, double lat, double alt, double spin,
163 double sun_ra, double sun_dec, double sun_dist,
164 double moon_ra, double moon_dec, double moon_dist )
166 double angle = gst * 15; // degrees
167 dome->reposition( zero_elev, lon, lat, spin );
168 oursun->reposition( view_pos, angle, sun_ra, sun_dec, sun_dist );
169 moon->reposition( view_pos, angle, moon_ra, moon_dec, moon_dist );
170 planets->reposition( view_pos, angle );
171 stars->reposition( view_pos, angle );
173 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
174 cloud_layers[i]->reposition( zero_elev, view_up, lon, lat, alt );
181 // draw background portions of the sky ... do this before you draw the
182 // rest of your scene.
183 void SGSky::preDraw() {
184 ssgCullAndDraw( pre_root );
188 // draw translucent clouds ... do this after you've drawn all the
189 // oapaque elements of your scene.
190 void SGSky::postDraw( float alt ) {
191 float slop = 5.0; // if we are closer than this to a cloud layer,
194 int in_cloud = -1; // cloud we are in
198 // check where we are relative to the cloud layers
199 for ( i = 0; i < (int)cloud_layers.size(); ++i ) {
200 float asl = cloud_layers[i]->get_asl();
201 float thickness = cloud_layers[i]->get_thickness();
203 if ( alt < asl - slop ) {
205 } else if ( alt < asl + thickness + slop ) {
208 // bail now and don't draw any clouds
215 // determine rendering order
217 while ( pos < (int)cloud_layers.size() &&
218 alt > cloud_layers[pos]->get_asl())
224 // we are below all the cloud layers, draw top to bottom
225 for ( i = cloud_layers.size() - 1; i >= 0; --i ) {
226 if ( i != in_cloud ) {
227 cloud_layers[i]->draw();
230 } else if ( pos >= (int)cloud_layers.size() ) {
231 // we are above all the cloud layers, draw bottom to top
232 for ( i = 0; i < (int)cloud_layers.size(); ++i ) {
233 if ( i != in_cloud ) {
234 cloud_layers[i]->draw();
238 // we are between cloud layers, draw lower layers bottom to
239 // top and upper layers top to bottom
240 for ( i = 0; i < pos; ++i ) {
241 if ( i != in_cloud ) {
242 cloud_layers[i]->draw();
245 for ( i = cloud_layers.size() - 1; i >= pos; --i ) {
246 if ( i != in_cloud ) {
247 cloud_layers[i]->draw();
254 void SGSky::add_cloud_layer( double asl, double thickness,
255 double transition, double span,
256 ssgSimpleState *state ) {
257 SGCloudLayer *layer = new SGCloudLayer;
258 layer->build( span, asl, thickness, transition, state );
260 layer_list_iterator current = cloud_layers.begin();
261 layer_list_iterator last = cloud_layers.end();
262 while ( current != last && (*current)->get_asl() < asl ) {
266 if ( current != last ) {
267 cloud_layers.insert( current, layer );
269 cloud_layers.push_back( layer );
272 // for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
273 // cout << "layer " << i << " = " << cloud_layers[i]->get_asl() << endl;
279 void SGSky::add_cloud_layer( double asl, double thickness,
280 double transition, double span,
281 const string &tex_path ) {
282 ssgSimpleState *state = SGCloudMakeState( tex_path );
283 add_cloud_layer( asl, thickness, transition, span, state );
287 void SGSky::add_cloud_layer( double asl, double thickness,
288 double transition, double span,
290 if ( type > 0 && type < SG_MAX_CLOUD_TYPES ) {
291 add_cloud_layer( asl, thickness, transition, span, cloud_mats[type] );
296 // modify the current visibility based on cloud layers, thickness,
297 // transition range, and simulated "puffs".
298 void SGSky::modify_vis( float alt, float time_factor ) {
299 float effvis = visibility;
301 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
302 float asl = cloud_layers[i]->get_asl();
303 float thickness = cloud_layers[i]->get_thickness();
304 float transition = cloud_layers[i]->get_transition();
308 if ( alt < asl - transition ) {
311 } else if ( alt < asl ) {
312 // in lower transition
313 ratio = (asl - alt) / transition;
314 } else if ( alt < asl + thickness ) {
317 } else if ( alt < asl + thickness + transition ) {
318 // in upper transition
319 ratio = (alt - (asl + thickness)) / transition;
325 // accumulate effects from multiple cloud layers
330 // calc chance of entering cloud puff
331 double rnd = sg_random();
332 double chance = rnd * rnd * rnd;
333 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
335 puff_length = sg_random() * 2.0; // up to 2 seconds
336 puff_progression = 0.0;
341 // modify actual_visibility based on puff envelope
343 if ( puff_progression <= ramp_up ) {
344 double x = 0.5 * SGD_PI * puff_progression / ramp_up;
345 double factor = 1.0 - sin( x );
346 // cout << "ramp up = " << puff_progression
347 // << " factor = " << factor << endl;
348 effvis = effvis * factor;
349 } else if ( puff_progression >= ramp_up + puff_length ) {
350 double x = 0.5 * SGD_PI *
351 (puff_progression - (ramp_up + puff_length)) /
353 double factor = sin( x );
354 // cout << "ramp down = "
355 // << puff_progression - (ramp_up + puff_length)
356 // << " factor = " << factor << endl;
357 effvis = effvis * factor;
362 /* cout << "len = " << puff_length
364 << " factor = " << factor
365 << " actual_visibility = " << actual_visibility
368 // time_factor = ( global_multi_loop *
369 // current_options.get_speed_up() ) /
370 // (double)current_options.get_model_hz();
372 puff_progression += time_factor;
373 // cout << "time factor = " << time_factor << endl;
375 /* cout << "gml = " << global_multi_loop
376 << " speed up = " << current_options.get_speed_up()
377 << " hz = " << current_options.get_model_hz() << endl;
380 if ( puff_progression > puff_length + ramp_up + ramp_down) {
385 // never let visibility drop below 25 meters
386 if ( effvis <= 25.0 ) {
392 effective_visibility = effvis;