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
52 for (unsigned int i = 0; i < cloud_layers.size(); i++)
53 delete cloud_layers[i];
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 );
101 // repaint the sky components based on current value of sun_angle,
102 // sky, and fog colors.
104 // sun angle in degrees relative to verticle
105 // 0 degrees = high noon
106 // 90 degrees = sun rise/set
107 // 180 degrees = darkest midnight
108 bool SGSky::repaint( sgVec4 sky_color, sgVec4 fog_color,
109 double sun_angle, double moon_angle,
110 int nplanets, sgdVec3 *planet_data,
111 int nstars, sgdVec3 *star_data )
113 if ( effective_visibility > 1000.0 ) {
115 dome->repaint( sky_color, fog_color, sun_angle, effective_visibility );
116 oursun->repaint( sun_angle );
117 moon->repaint( moon_angle );
118 planets->repaint( sun_angle, nplanets, planet_data );
119 stars->repaint( sun_angle, nstars, star_data );
121 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
122 cloud_layers[i]->repaint( fog_color );
133 // reposition the sky at the specified origin and orientation
135 // lon specifies a rotation about the Z axis
136 // lat specifies a rotation about the new Y axis
137 // spin specifies a rotation about the new Z axis (this allows
138 // additional orientation for the sunrise/set effects and is used by
139 // the skydome and perhaps clouds.
140 bool SGSky::reposition( sgVec3 view_pos, sgVec3 zero_elev, sgVec3 view_up,
141 double lon, double lat, double alt, double spin,
143 double sun_ra, double sun_dec, double sun_dist,
144 double moon_ra, double moon_dec, double moon_dist )
146 double angle = gst * 15; // degrees
147 dome->reposition( zero_elev, lon, lat, spin );
148 oursun->reposition( view_pos, angle, sun_ra, sun_dec, sun_dist );
149 moon->reposition( view_pos, angle, moon_ra, moon_dec, moon_dist );
150 planets->reposition( view_pos, angle );
151 stars->reposition( view_pos, angle );
153 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
154 cloud_layers[i]->reposition( zero_elev, view_up, lon, lat, alt );
161 // draw background portions of the sky ... do this before you draw the
162 // rest of your scene.
163 void SGSky::preDraw() {
164 ssgCullAndDraw( pre_root );
168 // draw translucent clouds ... do this after you've drawn all the
169 // oapaque elements of your scene.
170 void SGSky::postDraw( float alt ) {
171 float slop = 5.0; // if we are closer than this to a cloud layer,
174 int in_cloud = -1; // cloud we are in
178 // check where we are relative to the cloud layers
179 for ( i = 0; i < (int)cloud_layers.size(); ++i ) {
180 float asl = cloud_layers[i]->getElevation_m();
181 float thickness = cloud_layers[i]->getThickness_m();
183 if ( alt < asl - slop ) {
185 } else if ( alt < asl + thickness + slop ) {
188 // bail now and don't draw any clouds
195 // determine rendering order
197 while ( pos < (int)cloud_layers.size() &&
198 alt > cloud_layers[pos]->getElevation_m())
204 // we are below all the cloud layers, draw top to bottom
205 for ( i = cloud_layers.size() - 1; i >= 0; --i ) {
206 if ( i != in_cloud ) {
207 cloud_layers[i]->draw();
210 } else if ( pos >= (int)cloud_layers.size() ) {
211 // we are above all the cloud layers, draw bottom to top
212 for ( i = 0; i < (int)cloud_layers.size(); ++i ) {
213 if ( i != in_cloud ) {
214 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 ( i = 0; i < pos; ++i ) {
221 if ( i != in_cloud ) {
222 cloud_layers[i]->draw();
225 for ( i = cloud_layers.size() - 1; i >= pos; --i ) {
226 if ( i != in_cloud ) {
227 cloud_layers[i]->draw();
234 SGSky::add_cloud_layer( SGCloudLayer * layer )
236 cloud_layers.push_back(layer);
240 SGSky::get_cloud_layer (int i) const
242 return cloud_layers[i];
246 SGSky::get_cloud_layer (int i)
248 return cloud_layers[i];
252 SGSky::get_cloud_layer_count () const
254 return cloud_layers.size();
257 // modify the current visibility based on cloud layers, thickness,
258 // transition range, and simulated "puffs".
259 void SGSky::modify_vis( float alt, float time_factor ) {
260 float effvis = visibility;
262 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
263 float asl = cloud_layers[i]->getElevation_m();
264 float thickness = cloud_layers[i]->getThickness_m();
265 float transition = cloud_layers[i]->getTransition_m();
269 if ( alt < asl - transition ) {
272 } else if ( alt < asl ) {
273 // in lower transition
274 ratio = (asl - alt) / transition;
275 } else if ( alt < asl + thickness ) {
278 } else if ( alt < asl + thickness + transition ) {
279 // in upper transition
280 ratio = (alt - (asl + thickness)) / transition;
286 // accumulate effects from multiple cloud layers
291 // calc chance of entering cloud puff
292 double rnd = sg_random();
293 double chance = rnd * rnd * rnd;
294 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
296 puff_length = sg_random() * 2.0; // up to 2 seconds
297 puff_progression = 0.0;
302 // modify actual_visibility based on puff envelope
304 if ( puff_progression <= ramp_up ) {
305 double x = 0.5 * SGD_PI * puff_progression / ramp_up;
306 double factor = 1.0 - sin( x );
307 // cout << "ramp up = " << puff_progression
308 // << " factor = " << factor << endl;
309 effvis = effvis * factor;
310 } else if ( puff_progression >= ramp_up + puff_length ) {
311 double x = 0.5 * SGD_PI *
312 (puff_progression - (ramp_up + puff_length)) /
314 double factor = sin( x );
315 // cout << "ramp down = "
316 // << puff_progression - (ramp_up + puff_length)
317 // << " factor = " << factor << endl;
318 effvis = effvis * factor;
323 /* cout << "len = " << puff_length
325 << " factor = " << factor
326 << " actual_visibility = " << actual_visibility
329 // time_factor = ( global_multi_loop *
330 // current_options.get_speed_up() ) /
331 // (double)current_options.get_model_hz();
333 puff_progression += time_factor;
334 // cout << "time factor = " << time_factor << endl;
336 /* cout << "gml = " << global_multi_loop
337 << " speed up = " << current_options.get_speed_up()
338 << " hz = " << current_options.get_model_hz() << endl;
341 if ( puff_progression > puff_length + ramp_up + ramp_down) {
346 // never let visibility drop below 25 meters
347 if ( effvis <= 25.0 ) {
353 effective_visibility = effvis;