]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/sky.cxx
Mark's dynamic sun color changes.
[simgear.git] / simgear / scene / sky / sky.cxx
1 // sky.cxx -- ssg based sky model
2 //
3 // Written by Curtis Olson, started December 1997.
4 // SSG-ified by Curtis Olson, February 2000.
5 //
6 // Copyright (C) 1997-2000  Curtis L. Olson  - http://www.flightgear.org/~curt
7 //
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.
12 //
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.
17 //
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 // $Id$
23
24 #ifdef HAVE_CONFIG_H
25 #  include <simgear_config.h>
26 #endif
27
28 #include <plib/sg.h>
29 #include <plib/ssg.h>
30
31 #include <simgear/math/sg_random.h>
32
33 #include "sky.hxx"
34 #include "cloudfield.hxx"
35
36 // Constructor
37 SGSky::SGSky( void ) {
38     effective_visibility = visibility = 10000.0;
39
40     // near cloud visibility state variables
41     in_puff = false;
42     puff_length = 0;
43     puff_progression = 0;
44     ramp_up = 0.15;
45     ramp_down = 0.15;
46     // ramp_up = 4.0;
47     // ramp_down = 4.0;
48
49     in_cloud  = -1;
50 }
51
52
53 // Destructor
54 SGSky::~SGSky( void )
55 {
56     for (unsigned int i = 0; i < cloud_layers.size(); i++)
57         delete cloud_layers[i];
58 }
59
60
61 // initialize the sky and connect the components to the scene graph at
62 // the provided branch
63 void SGSky::build( double h_radius_m, double v_radius_m,
64                    double sun_size, double moon_size,
65                    int nplanets, sgdVec3 *planet_data,
66                    int nstars, sgdVec3 *star_data, SGPropertyNode *property_tree_node )
67 {
68     pre_root = new ssgRoot;
69     post_root = new ssgRoot;
70
71     pre_selector = new ssgSelector;
72     post_selector = new ssgSelector;
73
74     pre_transform = new ssgTransform;
75     post_transform = new ssgTransform;
76
77     dome = new SGSkyDome;
78     pre_transform -> addKid( dome->build( h_radius_m, v_radius_m ) );
79
80     planets = new SGStars;
81     pre_transform -> addKid(planets->build(nplanets, planet_data, h_radius_m));
82
83     stars = new SGStars;
84     pre_transform -> addKid( stars->build(nstars, star_data, h_radius_m) );
85     
86     moon = new SGMoon;
87     pre_transform -> addKid( moon->build(tex_path, moon_size) );
88
89     oursun = new SGSun;
90     pre_transform -> addKid( oursun->build(tex_path, sun_size, property_tree_node ) );
91
92     pre_selector->addKid( pre_transform );
93     pre_selector->clrTraversalMaskBits( SSGTRAV_HOT );
94
95     post_selector->addKid( post_transform );
96     post_selector->clrTraversalMaskBits( SSGTRAV_HOT );
97
98     pre_root->addKid( pre_selector );
99     post_root->addKid( post_selector );
100 }
101
102
103 // repaint the sky components based on current value of sun_angle,
104 // sky, and fog colors.
105 //
106 // sun angle in degrees relative to verticle
107 // 0 degrees = high noon
108 // 90 degrees = sun rise/set
109 // 180 degrees = darkest midnight
110 bool SGSky::repaint( const SGSkyColor &sc )
111 {
112     if ( effective_visibility > 1000.0 ) {
113         enable();
114         dome->repaint( sc.sky_color, sc.fog_color, sc.sun_angle,
115                        effective_visibility );
116
117         stars->repaint( sc.sun_angle, sc.nstars, sc.star_data );
118         planets->repaint( sc.sun_angle, sc.nplanets, sc.planet_data );
119         oursun->repaint( sc.sun_angle, effective_visibility );
120         moon->repaint( sc.moon_angle );
121
122         for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
123             if (cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR){
124                 cloud_layers[i]->repaint( sc.cloud_color );
125             }
126         }
127     } else {
128         // turn off sky
129         disable();
130     }
131
132     return true;
133 }
134
135
136 // reposition the sky at the specified origin and orientation
137 //
138 // lon specifies a rotation about the Z axis
139 // lat specifies a rotation about the new Y axis
140 // spin specifies a rotation about the new Z axis (this allows
141 // additional orientation for the sunrise/set effects and is used by
142 // the skydome and perhaps clouds.
143 bool SGSky::reposition( SGSkyState &st, double dt )
144 {
145
146     double angle = st.gst * 15; // degrees
147
148     dome->reposition( st.zero_elev, st.lon, st.lat, st.spin );
149
150     stars->reposition( st.view_pos, angle );
151     planets->reposition( st.view_pos, angle );
152
153     oursun->reposition( st.view_pos, angle,
154                         st.sun_ra, st.sun_dec, st.sun_dist, st.lat, st.alt, st.sun_angle );
155
156     moon->reposition( st.view_pos, angle,
157                       st.moon_ra, st.moon_dec, st.moon_dist );
158
159     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
160         if ( cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR ) {
161             cloud_layers[i]->reposition( st.zero_elev, st.view_up,
162                                          st.lon, st.lat, st.alt, dt );
163         }
164     }
165
166     return true;
167 }
168
169
170 // draw background portions of the sky ... do this before you draw the
171 // rest of your scene.
172 void SGSky::preDraw( float alt, float fog_exp2_density ) {
173     ssgCullAndDraw( pre_root );
174
175     // if we are closer than this to a cloud layer, don't draw clouds
176     static const float slop = 5.0;
177     int i;
178
179     // check where we are relative to the cloud layers
180     in_cloud = -1;
181     for ( i = 0; i < (int)cloud_layers.size(); ++i ) {
182         float asl = cloud_layers[i]->getElevation_m();
183         float thickness = cloud_layers[i]->getThickness_m();
184
185         if ( alt < asl - slop ) {
186             // below cloud layer
187         } else if ( alt < asl + thickness + slop ) {
188             // in cloud layer
189
190             // bail now and don't draw any clouds
191                         if( cloud_layers[i]->get_layer3D()->is3D() && SGCloudField::enable3D )
192                                 continue;
193             in_cloud = i;
194         } else {
195             // above cloud layer
196         }
197     }
198
199     // determine rendering order
200     cur_layer_pos = 0;
201     while ( cur_layer_pos < (int)cloud_layers.size() &&
202             alt > cloud_layers[cur_layer_pos]->getElevation_m() )
203     {
204         ++cur_layer_pos;
205     }
206
207     // FIXME: This should not be needed, but at this time (08/15/2003)
208     //        certain NVidia drivers don't seem to implement
209     //        glPushAttrib(FG_FOG_BIT) properly. The result is that
210     //        there is not fog when looking at the sun.
211     glFogf ( GL_FOG_DENSITY, fog_exp2_density );
212 }
213
214 void SGSky::drawUpperClouds( ) {
215     // draw the cloud layers that are above us, top to bottom
216     for ( int i = (int)cloud_layers.size() - 1; i >= cur_layer_pos; --i ) {
217         if ( i != in_cloud ) {
218             cloud_layers[i]->draw( false );
219         }
220     }
221 }
222
223
224 // draw translucent clouds ... do this after you've drawn all the
225 // oapaque elements of your scene.
226 void SGSky::drawLowerClouds() {
227
228     // draw the cloud layers that are below us, bottom to top
229     for ( int i = 0; i < cur_layer_pos; ++i ) {
230         if ( i != in_cloud ) {
231             cloud_layers[i]->draw( true );
232         }
233     }
234 }
235
236 void
237 SGSky::add_cloud_layer( SGCloudLayer * layer )
238 {
239     cloud_layers.push_back(layer);
240 }
241
242 const SGCloudLayer *
243 SGSky::get_cloud_layer (int i) const
244 {
245     return cloud_layers[i];
246 }
247
248 SGCloudLayer *
249 SGSky::get_cloud_layer (int i)
250 {
251     return cloud_layers[i];
252 }
253
254 int
255 SGSky::get_cloud_layer_count () const
256 {
257     return cloud_layers.size();
258 }
259
260 // modify the current visibility based on cloud layers, thickness,
261 // transition range, and simulated "puffs".
262 void SGSky::modify_vis( float alt, float time_factor ) {
263     float effvis = visibility;
264
265     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
266         float asl = cloud_layers[i]->getElevation_m();
267         float thickness = cloud_layers[i]->getThickness_m();
268         float transition = cloud_layers[i]->getTransition_m();
269
270         double ratio = 1.0;
271
272         if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ) {
273             // less than 50% coverage -- assume we're in the clear for now
274             ratio = 1.0;
275         } else if ( alt < asl - transition ) {
276             // below cloud layer
277             ratio = 1.0;
278         } else if ( alt < asl ) {
279             // in lower transition
280             ratio = (asl - alt) / transition;
281         } else if ( alt < asl + thickness ) {
282             // in cloud layer
283             ratio = 0.0;
284         } else if ( alt < asl + thickness + transition ) {
285             // in upper transition
286             ratio = (alt - (asl + thickness)) / transition;
287         } else {
288             // above cloud layer
289             ratio = 1.0;
290         }
291
292         if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ||
293                         cloud_layers[i]->get_layer3D()->is3D() && SGCloudField::enable3D) {
294             // do nothing, clear layers aren't drawn, don't affect
295             // visibility andn dont' need to be faded in or out.
296         } else if ( (cloud_layers[i]->getCoverage() == 
297                      SGCloudLayer::SG_CLOUD_FEW)
298                     || (cloud_layers[i]->getCoverage() ==
299                         SGCloudLayer::SG_CLOUD_SCATTERED) )
300         {
301             // set the alpha fade value for the cloud layer.  For less
302             // dense cloud layers we fade the layer to nothing as we
303             // approach it because we stay clear visibility-wise as we
304             // pass through it.
305             float temp = ratio * 2.0;
306             if ( temp > 1.0 ) { temp = 1.0; }
307             cloud_layers[i]->setAlpha( temp );
308
309             // don't touch visibility
310         } else {
311             // maintain full alpha for denser cloud layer types.
312             // Let's set the value explicitly in case someone changed
313             // the layer type.
314             cloud_layers[i]->setAlpha( 1.0 );
315
316             // lower visibility as we approach the cloud layer.
317             // accumulate effects from multiple cloud layers
318             effvis *= ratio;
319         }
320
321 #if 0
322         if ( ratio < 1.0 ) {
323             if ( ! in_puff ) {
324                 // calc chance of entering cloud puff
325                 double rnd = sg_random();
326                 double chance = rnd * rnd * rnd;
327                 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
328                     in_puff = true;
329                     puff_length = sg_random() * 2.0; // up to 2 seconds
330                     puff_progression = 0.0;
331                 }
332             }
333
334             if ( in_puff ) {
335                 // modify actual_visibility based on puff envelope
336
337                 if ( puff_progression <= ramp_up ) {
338                     double x = SGD_PI_2 * puff_progression / ramp_up;
339                     double factor = 1.0 - sin( x );
340                     // cout << "ramp up = " << puff_progression
341                     //      << "  factor = " << factor << endl;
342                     effvis = effvis * factor;
343                 } else if ( puff_progression >= ramp_up + puff_length ) {
344                     double x = SGD_PI_2 * 
345                         (puff_progression - (ramp_up + puff_length)) /
346                         ramp_down;
347                     double factor = sin( x );
348                     // cout << "ramp down = " 
349                     //      << puff_progression - (ramp_up + puff_length) 
350                     //      << "  factor = " << factor << endl;
351                     effvis = effvis * factor;
352                 } else {
353                     effvis = 0.0;
354                 }
355
356                 /* cout << "len = " << puff_length
357                    << "  x = " << x 
358                    << "  factor = " << factor
359                    << "  actual_visibility = " << actual_visibility 
360                    << endl; */
361
362                 // time_factor = ( global_multi_loop * 
363                 //                 current_options.get_speed_up() ) /
364                 //                (double)current_options.get_model_hz();
365
366                 puff_progression += time_factor;
367                 // cout << "time factor = " << time_factor << endl;
368
369                 /* cout << "gml = " << global_multi_loop 
370                    << "  speed up = " << current_options.get_speed_up()
371                    << "  hz = " << current_options.get_model_hz() << endl;
372                    */ 
373
374                 if ( puff_progression > puff_length + ramp_up + ramp_down) {
375                     in_puff = false; 
376                 }
377             }
378         }
379 #endif
380
381         // never let visibility drop below 25 meters
382         if ( effvis <= 25.0 ) {
383             effvis = 25.0;
384         }
385
386     } // for
387
388     effective_visibility = effvis;
389 }