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.
29 #include <plib/ssg.h> // plib include
31 #include <simgear/constants.h>
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 );
99 // repaint the sky components based on current value of sun_angle,
100 // sky, and fog colors.
102 // sun angle in degrees relative to verticle
103 // 0 degrees = high noon
104 // 90 degrees = sun rise/set
105 // 180 degrees = darkest midnight
106 bool SGSky::repaint( sgVec4 sky_color, sgVec4 fog_color,
107 double sun_angle, double moon_angle,
108 int nplanets, sgdVec3 *planet_data,
109 int nstars, sgdVec3 *star_data )
111 if ( effective_visibility > 1000.0 ) {
113 dome->repaint( sky_color, fog_color, sun_angle );
114 oursun->repaint( sun_angle );
115 moon->repaint( moon_angle );
116 planets->repaint( sun_angle, nplanets, planet_data );
117 stars->repaint( sun_angle, nstars, star_data );
119 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
120 cloud_layers[i]->repaint( fog_color );
131 // reposition the sky at the specified origin and orientation
133 // lon specifies a rotation about the Z axis
134 // lat specifies a rotation about the new Y axis
135 // spin specifies a rotation about the new Z axis (this allows
136 // additional orientation for the sunrise/set effects and is used by
137 // the skydome and perhaps clouds.
138 bool SGSky::reposition( sgVec3 view_pos, sgVec3 zero_elev, sgVec3 view_up,
139 double lon, double lat, double alt, double spin,
141 double sun_ra, double sun_dec, double sun_dist,
142 double moon_ra, double moon_dec, double moon_dist )
144 double angle = gst * 15; // degrees
145 dome->reposition( zero_elev, lon, lat, spin );
146 oursun->reposition( view_pos, angle, sun_ra, sun_dec, sun_dist );
147 moon->reposition( view_pos, angle, moon_ra, moon_dec, moon_dist );
148 planets->reposition( view_pos, angle );
149 stars->reposition( view_pos, angle );
151 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
152 cloud_layers[i]->reposition( zero_elev, view_up, lon, lat, alt );
159 // draw background portions of the sky
160 void SGSky::draw_background() {
161 ssgCullAndDraw( pre_root );
165 // draw scenery elements of the sky
166 void SGSky::draw_scene( float alt ) {
168 if ( effective_visibility < 4000.0 ) {
169 // bail and don't draw clouds
173 // determine rendering order
175 while ( pos < (int)cloud_layers.size() &&
176 alt > cloud_layers[pos]->get_asl())
182 // we are below all the cloud layers, draw top to bottom
183 for ( int i = cloud_layers.size() - 1; i >= 0; --i ) {
184 cloud_layers[i]->draw();
186 } else if ( pos >= (int)cloud_layers.size() ) {
187 // we are above all the cloud layers, draw bottom to top
188 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
189 cloud_layers[i]->draw();
192 // we are between cloud layers, draw lower layers bottom to
193 // top and upper layers top to bottom
194 for ( int i = 0; i < pos; ++i ) {
195 cloud_layers[i]->draw();
197 for ( int i = cloud_layers.size() - 1; i >= pos; --i ) {
198 cloud_layers[i]->draw();
204 void SGSky::add_cloud_layer( double asl, double thickness, double transition ) {
205 SGCloudLayer *layer = new SGCloudLayer;
206 layer->build(tex_path, 40000.0f, asl, thickness, transition);
208 layer_list_iterator current = cloud_layers.begin();
209 layer_list_iterator last = cloud_layers.end();
210 while ( current != last && (*current)->get_asl() < asl ) {
214 if ( current != last ) {
215 cloud_layers.insert( current, layer );
217 cloud_layers.push_back( layer );
220 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
221 cout << "layer " << i << " = " << cloud_layers[i]->get_asl() << endl;
227 // modify the current visibility based on cloud layers, thickness,
228 // transition range, and simulated "puffs".
229 void SGSky::modify_vis( float alt, float time_factor ) {
230 float effvis = visibility;
232 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
233 float asl = cloud_layers[i]->get_asl();
234 float thickness = cloud_layers[i]->get_thickness();
235 float transition = cloud_layers[i]->get_transition();
239 if ( alt < asl - transition ) {
242 } else if ( alt < asl ) {
243 // in lower transition
244 ratio = (asl - alt) / transition;
245 } else if ( alt < asl + thickness ) {
248 } else if ( alt < asl + thickness + transition ) {
249 // in upper transition
250 ratio = (alt - (asl + thickness)) / transition;
256 // accumulate effects from multiple cloud layers
261 // calc chance of entering cloud puff
262 double rnd = fg_random();
263 double chance = rnd * rnd * rnd;
264 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
267 puff_length = fg_random() * 2.0; // up to 2 seconds
268 } while ( puff_length <= 0.0 );
269 puff_progression = 0.0;
274 // modify actual_visibility based on puff envelope
276 if ( puff_progression <= ramp_up ) {
277 double x = FG_PI_2 * puff_progression / ramp_up;
278 double factor = 1.0 - sin( x );
279 effvis = effvis * factor;
280 } else if ( puff_progression >= ramp_up + puff_length ) {
282 (puff_progression - (ramp_up + puff_length)) /
284 double factor = sin( x );
285 effvis = effvis * factor;
290 /* cout << "len = " << puff_length
292 << " factor = " << factor
293 << " actual_visibility = " << actual_visibility
296 // time_factor = ( global_multi_loop *
297 // current_options.get_speed_up() ) /
298 // (double)current_options.get_model_hz();
300 puff_progression += time_factor;
302 /* cout << "gml = " << global_multi_loop
303 << " speed up = " << current_options.get_speed_up()
304 << " hz = " << current_options.get_model_hz() << endl;
307 if ( puff_progression > puff_length + ramp_up + ramp_down) {
312 // never let visibility drop below zero
319 effective_visibility = effvis;