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