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 if (cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR){
123 cloud_layers[i]->repaint( fog_color );
135 // reposition the sky at the specified origin and orientation
137 // lon specifies a rotation about the Z axis
138 // lat specifies a rotation about the new Y axis
139 // spin specifies a rotation about the new Z axis (this allows
140 // additional orientation for the sunrise/set effects and is used by
141 // the skydome and perhaps clouds.
142 bool SGSky::reposition( sgVec3 view_pos, sgVec3 zero_elev, sgVec3 view_up,
143 double lon, double lat, double alt, double spin,
145 double sun_ra, double sun_dec, double sun_dist,
146 double moon_ra, double moon_dec, double moon_dist )
148 double angle = gst * 15; // degrees
149 dome->reposition( zero_elev, lon, lat, spin );
150 oursun->reposition( view_pos, angle, sun_ra, sun_dec, sun_dist );
151 moon->reposition( view_pos, angle, moon_ra, moon_dec, moon_dist );
152 planets->reposition( view_pos, angle );
153 stars->reposition( view_pos, angle );
155 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
156 if ( cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR ) {
157 cloud_layers[i]->reposition( zero_elev, view_up, lon, lat, alt );
165 // draw background portions of the sky ... do this before you draw the
166 // rest of your scene.
167 void SGSky::preDraw() {
168 ssgCullAndDraw( pre_root );
172 // draw translucent clouds ... do this after you've drawn all the
173 // oapaque elements of your scene.
174 void SGSky::postDraw( float alt ) {
175 float slop = 5.0; // if we are closer than this to a cloud layer,
178 int in_cloud = -1; // cloud we are in
182 // check where we are relative to the cloud layers
183 for ( i = 0; i < (int)cloud_layers.size(); ++i ) {
184 float asl = cloud_layers[i]->getElevation_m();
185 float thickness = cloud_layers[i]->getThickness_m();
187 if ( alt < asl - slop ) {
189 } else if ( alt < asl + thickness + slop ) {
192 // bail now and don't draw any clouds
199 // determine rendering order
201 while ( pos < (int)cloud_layers.size() &&
202 alt > cloud_layers[pos]->getElevation_m())
208 // we are below all the cloud layers, draw top to bottom
209 for ( i = cloud_layers.size() - 1; i >= 0; --i ) {
210 if ( i != in_cloud ) {
211 cloud_layers[i]->draw();
214 } else if ( pos >= (int)cloud_layers.size() ) {
215 // we are above all the cloud layers, draw bottom to top
216 for ( i = 0; i < (int)cloud_layers.size(); ++i ) {
217 if ( i != in_cloud ) {
218 cloud_layers[i]->draw();
222 // we are between cloud layers, draw lower layers bottom to
223 // top and upper layers top to bottom
224 for ( i = 0; i < pos; ++i ) {
225 if ( i != in_cloud ) {
226 cloud_layers[i]->draw();
229 for ( i = cloud_layers.size() - 1; i >= pos; --i ) {
230 if ( i != in_cloud ) {
231 cloud_layers[i]->draw();
238 SGSky::add_cloud_layer( SGCloudLayer * layer )
240 cloud_layers.push_back(layer);
244 SGSky::get_cloud_layer (int i) const
246 return cloud_layers[i];
250 SGSky::get_cloud_layer (int i)
252 return cloud_layers[i];
256 SGSky::get_cloud_layer_count () const
258 return cloud_layers.size();
261 // modify the current visibility based on cloud layers, thickness,
262 // transition range, and simulated "puffs".
263 void SGSky::modify_vis( float alt, float time_factor ) {
264 float effvis = visibility;
266 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
267 float asl = cloud_layers[i]->getElevation_m();
268 float thickness = cloud_layers[i]->getThickness_m();
269 float transition = cloud_layers[i]->getTransition_m();
273 if ( alt < asl - transition ) {
276 } else if ( alt < asl ) {
277 // in lower transition
278 ratio = (asl - alt) / transition;
279 } else if ( alt < asl + thickness ) {
282 } else if ( alt < asl + thickness + transition ) {
283 // in upper transition
284 ratio = (alt - (asl + thickness)) / transition;
290 // accumulate effects from multiple cloud layers
295 // calc chance of entering cloud puff
296 double rnd = sg_random();
297 double chance = rnd * rnd * rnd;
298 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
300 puff_length = sg_random() * 2.0; // up to 2 seconds
301 puff_progression = 0.0;
306 // modify actual_visibility based on puff envelope
308 if ( puff_progression <= ramp_up ) {
309 double x = 0.5 * SGD_PI * puff_progression / ramp_up;
310 double factor = 1.0 - sin( x );
311 // cout << "ramp up = " << puff_progression
312 // << " factor = " << factor << endl;
313 effvis = effvis * factor;
314 } else if ( puff_progression >= ramp_up + puff_length ) {
315 double x = 0.5 * SGD_PI *
316 (puff_progression - (ramp_up + puff_length)) /
318 double factor = sin( x );
319 // cout << "ramp down = "
320 // << puff_progression - (ramp_up + puff_length)
321 // << " factor = " << factor << endl;
322 effvis = effvis * factor;
327 /* cout << "len = " << puff_length
329 << " factor = " << factor
330 << " actual_visibility = " << actual_visibility
333 // time_factor = ( global_multi_loop *
334 // current_options.get_speed_up() ) /
335 // (double)current_options.get_model_hz();
337 puff_progression += time_factor;
338 // cout << "time factor = " << time_factor << endl;
340 /* cout << "gml = " << global_multi_loop
341 << " speed up = " << current_options.get_speed_up()
342 << " hz = " << current_options.get_model_hz() << endl;
345 if ( puff_progression > puff_length + ramp_up + ramp_down) {
350 // never let visibility drop below 25 meters
351 if ( effvis <= 25.0 ) {
357 effective_visibility = effvis;