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>
34 #include <Objects/matlib.hxx>
40 SGSky::SGSky( void ) {
41 effective_visibility = visibility = 10000.0;
43 // 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
101 ssgSimpleState *cloud_state;
103 cloud_path.set( tex_path.str() );
104 cloud_path.append( "cirrus.rgba" );
105 cloud_state = SGCloudMakeState( cloud_path.str() );
106 material_lib.add_item( "CloudCirrus", cloud_state );
108 cloud_path.set( tex_path.str() );
109 cloud_path.append( "mostlycloudy.rgba" );
110 cloud_state = SGCloudMakeState( cloud_path.str() );
111 material_lib.add_item( "CloudMostlyCloudy", cloud_state );
113 cloud_path.set( tex_path.str() );
114 cloud_path.append( "mostlysunny.rgba" );
115 cloud_state = SGCloudMakeState( cloud_path.str() );
116 material_lib.add_item( "CloudMostlySunny", cloud_state );
118 cloud_path.set( tex_path.str() );
119 cloud_path.append( "overcast.rgb" );
120 cloud_state = SGCloudMakeState( cloud_path.str() );
121 material_lib.add_item( "CloudOvercast", cloud_state );
125 // repaint the sky components based on current value of sun_angle,
126 // sky, and fog colors.
128 // sun angle in degrees relative to verticle
129 // 0 degrees = high noon
130 // 90 degrees = sun rise/set
131 // 180 degrees = darkest midnight
132 bool SGSky::repaint( sgVec4 sky_color, sgVec4 fog_color,
133 double sun_angle, double moon_angle,
134 int nplanets, sgdVec3 *planet_data,
135 int nstars, sgdVec3 *star_data )
137 if ( effective_visibility > 1000.0 ) {
139 dome->repaint( sky_color, fog_color, sun_angle );
140 oursun->repaint( sun_angle );
141 moon->repaint( moon_angle );
142 planets->repaint( sun_angle, nplanets, planet_data );
143 stars->repaint( sun_angle, nstars, star_data );
145 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
146 cloud_layers[i]->repaint( fog_color );
157 // reposition the sky at the specified origin and orientation
159 // lon specifies a rotation about the Z axis
160 // lat specifies a rotation about the new Y axis
161 // spin specifies a rotation about the new Z axis (this allows
162 // additional orientation for the sunrise/set effects and is used by
163 // the skydome and perhaps clouds.
164 bool SGSky::reposition( sgVec3 view_pos, sgVec3 zero_elev, sgVec3 view_up,
165 double lon, double lat, double alt, double spin,
167 double sun_ra, double sun_dec, double sun_dist,
168 double moon_ra, double moon_dec, double moon_dist )
170 double angle = gst * 15; // degrees
171 dome->reposition( zero_elev, lon, lat, spin );
172 oursun->reposition( view_pos, angle, sun_ra, sun_dec, sun_dist );
173 moon->reposition( view_pos, angle, moon_ra, moon_dec, moon_dist );
174 planets->reposition( view_pos, angle );
175 stars->reposition( view_pos, angle );
177 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
178 cloud_layers[i]->reposition( zero_elev, view_up, lon, lat, alt );
185 // draw background portions of the sky
186 void SGSky::draw_background() {
187 ssgCullAndDraw( pre_root );
191 // draw scenery elements of the sky
192 void SGSky::draw_scene( float alt ) {
194 if ( effective_visibility < 4000.0 ) {
195 // bail and don't draw clouds
199 // determine rendering order
201 while ( pos < (int)cloud_layers.size() &&
202 alt > cloud_layers[pos]->get_asl())
208 // we are below all the cloud layers, draw top to bottom
209 for ( int i = cloud_layers.size() - 1; i >= 0; --i ) {
210 cloud_layers[i]->draw();
212 } else if ( pos >= (int)cloud_layers.size() ) {
213 // we are above all the cloud layers, draw bottom to top
214 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
215 cloud_layers[i]->draw();
218 // we are between cloud layers, draw lower layers bottom to
219 // top and upper layers top to bottom
220 for ( int i = 0; i < pos; ++i ) {
221 cloud_layers[i]->draw();
223 for ( int i = cloud_layers.size() - 1; i >= pos; --i ) {
224 cloud_layers[i]->draw();
230 void SGSky::add_cloud_layer( double asl, double thickness, double transition,
232 SGCloudLayer *layer = new SGCloudLayer;
233 layer->build(tex_path, 40000.0f, asl, thickness, transition, type );
235 layer_list_iterator current = cloud_layers.begin();
236 layer_list_iterator last = cloud_layers.end();
237 while ( current != last && (*current)->get_asl() < asl ) {
241 if ( current != last ) {
242 cloud_layers.insert( current, layer );
244 cloud_layers.push_back( layer );
247 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
248 cout << "layer " << i << " = " << cloud_layers[i]->get_asl() << endl;
254 // modify the current visibility based on cloud layers, thickness,
255 // transition range, and simulated "puffs".
256 void SGSky::modify_vis( float alt, float time_factor ) {
257 float effvis = visibility;
259 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
260 float asl = cloud_layers[i]->get_asl();
261 float thickness = cloud_layers[i]->get_thickness();
262 float transition = cloud_layers[i]->get_transition();
266 if ( alt < asl - transition ) {
269 } else if ( alt < asl ) {
270 // in lower transition
271 ratio = (asl - alt) / transition;
272 } else if ( alt < asl + thickness ) {
275 } else if ( alt < asl + thickness + transition ) {
276 // in upper transition
277 ratio = (alt - (asl + thickness)) / transition;
283 // accumulate effects from multiple cloud layers
288 // calc chance of entering cloud puff
289 double rnd = fg_random();
290 double chance = rnd * rnd * rnd;
291 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
294 puff_length = fg_random() * 2.0; // up to 2 seconds
295 } while ( puff_length <= 0.0 );
296 puff_progression = 0.0;
301 // modify actual_visibility based on puff envelope
303 if ( puff_progression <= ramp_up ) {
304 double x = FG_PI_2 * puff_progression / ramp_up;
305 double factor = 1.0 - sin( x );
306 effvis = effvis * factor;
307 } else if ( puff_progression >= ramp_up + puff_length ) {
309 (puff_progression - (ramp_up + puff_length)) /
311 double factor = sin( x );
312 effvis = effvis * factor;
317 /* cout << "len = " << puff_length
319 << " factor = " << factor
320 << " actual_visibility = " << actual_visibility
323 // time_factor = ( global_multi_loop *
324 // current_options.get_speed_up() ) /
325 // (double)current_options.get_model_hz();
327 puff_progression += time_factor;
329 /* cout << "gml = " << global_multi_loop
330 << " speed up = " << current_options.get_speed_up()
331 << " hz = " << current_options.get_model_hz() << endl;
334 if ( puff_progression > puff_length + ramp_up + ramp_down) {
339 // never let visibility drop below zero
346 effective_visibility = effvis;