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
54 for (unsigned int i = 0; i < cloud_layers.size(); i++)
55 delete cloud_layers[i];
59 // initialize the sky and connect the components to the scene graph at
60 // the provided branch
61 void SGSky::build( double h_radius_m, double v_radius_m,
62 double sun_size, double moon_size,
63 int nplanets, sgdVec3 *planet_data,
64 int nstars, sgdVec3 *star_data )
66 pre_root = new ssgRoot;
67 post_root = new ssgRoot;
69 pre_selector = new ssgSelector;
70 post_selector = new ssgSelector;
72 pre_transform = new ssgTransform;
73 post_transform = new ssgTransform;
76 pre_transform -> addKid( dome->build( h_radius_m, v_radius_m ) );
78 planets = new SGStars;
79 pre_transform -> addKid(planets->build(nplanets, planet_data, h_radius_m));
82 pre_transform -> addKid( stars->build(nstars, star_data, h_radius_m) );
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( const SGSkyColor &sc )
110 if ( effective_visibility > 1000.0 ) {
112 dome->repaint( sc.sky_color, sc.fog_color, sc.sun_angle,
113 effective_visibility );
115 oursun->repaint( sc.sun_angle, effective_visibility );
117 moon->repaint( sc.moon_angle );
119 planets->repaint( sc.sun_angle, sc.nplanets, sc.planet_data );
121 stars->repaint( sc.sun_angle, sc.nstars, sc.star_data );
123 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
124 if (cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR){
125 cloud_layers[i]->repaint( sc.cloud_color );
137 // reposition the sky at the specified origin and orientation
139 // lon specifies a rotation about the Z axis
140 // lat specifies a rotation about the new Y axis
141 // spin specifies a rotation about the new Z axis (this allows
142 // additional orientation for the sunrise/set effects and is used by
143 // the skydome and perhaps clouds.
144 bool SGSky::reposition( SGSkyState &st, double dt )
147 double angle = st.gst * 15; // degrees
149 dome->reposition( st.zero_elev, st.lon, st.lat, st.spin );
151 oursun->reposition( st.view_pos, angle,
152 st.sun_ra, st.sun_dec, st.sun_dist );
154 moon->reposition( st.view_pos, angle,
155 st.moon_ra, st.moon_dec, st.moon_dist );
157 planets->reposition( st.view_pos, angle );
159 stars->reposition( st.view_pos, angle );
161 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
162 if ( cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR ) {
163 cloud_layers[i]->reposition( st.zero_elev, st.view_up,
164 st.lon, st.lat, st.alt, dt );
172 // draw background portions of the sky ... do this before you draw the
173 // rest of your scene.
174 void SGSky::preDraw( float alt ) {
175 ssgCullAndDraw( pre_root );
177 // if we are closer than this to a cloud layer, don't draw clouds
178 static const float slop = 5.0;
181 // check where we are relative to the cloud layers
182 for ( i = 0; i < (int)cloud_layers.size(); ++i ) {
183 float asl = cloud_layers[i]->getElevation_m();
184 float thickness = cloud_layers[i]->getThickness_m();
186 if ( alt < asl - slop ) {
188 } else if ( alt < asl + thickness + slop ) {
191 // bail now and don't draw any clouds
198 // determine rendering order
200 while ( pos < (int)cloud_layers.size() &&
201 alt > cloud_layers[pos]->getElevation_m())
206 // draw the cloud layers that are above us, top to bottom
208 glDepthMask( GL_FALSE );
209 for ( i = (int)cloud_layers.size() - 1; i >= 0; --i ) {
210 if ( i != in_cloud ) {
211 cloud_layers[i]->draw();
214 glDepthMask( GL_TRUE );
218 // draw translucent clouds ... do this after you've drawn all the
219 // oapaque elements of your scene.
220 void SGSky::postDraw( float alt ) {
222 // if we are closer than this to a cloud layer, don't draw clouds
223 static const float slop = 5.0;
227 // determine rendering order
229 while ( pos < (int)cloud_layers.size() &&
230 alt > cloud_layers[pos]->getElevation_m())
235 // draw the cloud layers that are below us, bottom to top
237 glDepthMask( GL_FALSE );
238 for ( i = 0; i < (int)cloud_layers.size(); ++i ) {
239 if ( i != in_cloud ) {
240 cloud_layers[i]->draw();
243 glDepthMask( GL_TRUE );
249 SGSky::add_cloud_layer( SGCloudLayer * layer )
251 cloud_layers.push_back(layer);
255 SGSky::get_cloud_layer (int i) const
257 return cloud_layers[i];
261 SGSky::get_cloud_layer (int i)
263 return cloud_layers[i];
267 SGSky::get_cloud_layer_count () const
269 return cloud_layers.size();
272 // modify the current visibility based on cloud layers, thickness,
273 // transition range, and simulated "puffs".
274 void SGSky::modify_vis( float alt, float time_factor ) {
275 float effvis = visibility;
277 for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
278 float asl = cloud_layers[i]->getElevation_m();
279 float thickness = cloud_layers[i]->getThickness_m();
280 float transition = cloud_layers[i]->getTransition_m();
284 if ( alt < asl - transition ) {
287 } else if ( alt < asl ) {
288 // in lower transition
289 ratio = (asl - alt) / transition;
290 } else if ( alt < asl + thickness ) {
293 } else if ( alt < asl + thickness + transition ) {
294 // in upper transition
295 ratio = (alt - (asl + thickness)) / transition;
301 // accumulate effects from multiple cloud layers
306 // calc chance of entering cloud puff
307 double rnd = sg_random();
308 double chance = rnd * rnd * rnd;
309 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
311 puff_length = sg_random() * 2.0; // up to 2 seconds
312 puff_progression = 0.0;
317 // modify actual_visibility based on puff envelope
319 if ( puff_progression <= ramp_up ) {
320 double x = 0.5 * SGD_PI * puff_progression / ramp_up;
321 double factor = 1.0 - sin( x );
322 // cout << "ramp up = " << puff_progression
323 // << " factor = " << factor << endl;
324 effvis = effvis * factor;
325 } else if ( puff_progression >= ramp_up + puff_length ) {
326 double x = 0.5 * SGD_PI *
327 (puff_progression - (ramp_up + puff_length)) /
329 double factor = sin( x );
330 // cout << "ramp down = "
331 // << puff_progression - (ramp_up + puff_length)
332 // << " factor = " << factor << endl;
333 effvis = effvis * factor;
338 /* cout << "len = " << puff_length
340 << " factor = " << factor
341 << " actual_visibility = " << actual_visibility
344 // time_factor = ( global_multi_loop *
345 // current_options.get_speed_up() ) /
346 // (double)current_options.get_model_hz();
348 puff_progression += time_factor;
349 // cout << "time factor = " << time_factor << endl;
351 /* cout << "gml = " << global_multi_loop
352 << " speed up = " << current_options.get_speed_up()
353 << " hz = " << current_options.get_model_hz() << endl;
356 if ( puff_progression > puff_length + ramp_up + ramp_down) {
361 // never let visibility drop below 25 meters
362 if ( effvis <= 25.0 ) {
368 effective_visibility = effvis;