]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/sky.cxx
this simple patch will prevent to have fog in Clear cload layers.
[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  - curt@flightgear.org
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
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            in_cloud = i;
191        } else {
192            // above cloud layer
193        }
194     }
195
196     // determine rendering order
197     cur_layer_pos = 0;
198     while ( cur_layer_pos < (int)cloud_layers.size() &&
199            alt > cloud_layers[cur_layer_pos]->getElevation_m())
200     {
201        ++cur_layer_pos;
202     }
203
204     // FIXME: This should not be needed, but at this time (08/15/2003)
205     //        certain NVidia drivers don't seem to implement
206     //        glPushAttrib(FG_FOG_BIT) properly. The result is that
207     //        there is not fog when looking at the sun.
208     glFogf ( GL_FOG_DENSITY, fog_exp2_density );
209 }
210
211 void SGSky::drawUpperClouds( ) {
212     // draw the cloud layers that are above us, top to bottom
213     for ( int i = (int)cloud_layers.size() - 1; i >= cur_layer_pos; --i ) {
214         if ( i != in_cloud ) {
215             cloud_layers[i]->draw( false );
216         }
217     }
218 }
219
220
221 // draw translucent clouds ... do this after you've drawn all the
222 // oapaque elements of your scene.
223 void SGSky::drawLowerClouds() {
224
225     // draw the cloud layers that are below us, bottom to top
226     for ( int i = 0; i < cur_layer_pos; ++i ) {
227         if ( i != in_cloud ) {
228             cloud_layers[i]->draw( true );
229         }
230     }
231 }
232
233 void
234 SGSky::add_cloud_layer( SGCloudLayer * layer )
235 {
236     cloud_layers.push_back(layer);
237 }
238
239 const SGCloudLayer *
240 SGSky::get_cloud_layer (int i) const
241 {
242     return cloud_layers[i];
243 }
244
245 SGCloudLayer *
246 SGSky::get_cloud_layer (int i)
247 {
248     return cloud_layers[i];
249 }
250
251 int
252 SGSky::get_cloud_layer_count () const
253 {
254     return cloud_layers.size();
255 }
256
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;
261
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();
266
267         double ratio = 1.0;
268
269         if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ) {
270             // clear layer
271             ratio = 1.0;
272         } else if ( alt < asl - transition ) {
273             // below cloud layer
274             ratio = 1.0;
275         } else if ( alt < asl ) {
276             // in lower transition
277             ratio = (asl - alt) / transition;
278         } else if ( alt < asl + thickness ) {
279             // in cloud layer
280             ratio = 0.0;
281         } else if ( alt < asl + thickness + transition ) {
282             // in upper transition
283             ratio = (alt - (asl + thickness)) / transition;
284         } else {
285             // above cloud layer
286             ratio = 1.0;
287         }
288
289         // accumulate effects from multiple cloud layers
290         effvis *= ratio;
291
292         if ( ratio < 1.0 ) {
293             if ( ! in_puff ) {
294                 // calc chance of entering cloud puff
295                 double rnd = sg_random();
296                 double chance = rnd * rnd * rnd;
297                 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
298                     in_puff = true;
299                     puff_length = sg_random() * 2.0; // up to 2 seconds
300                     puff_progression = 0.0;
301                 }
302             }
303
304             if ( in_puff ) {
305                 // modify actual_visibility based on puff envelope
306
307                 if ( puff_progression <= ramp_up ) {
308                     double x = 0.5 * SGD_PI * puff_progression / ramp_up;
309                     double factor = 1.0 - sin( x );
310                     // cout << "ramp up = " << puff_progression
311                     //      << "  factor = " << factor << endl;
312                     effvis = effvis * factor;
313                 } else if ( puff_progression >= ramp_up + puff_length ) {
314                     double x = 0.5 * SGD_PI * 
315                         (puff_progression - (ramp_up + puff_length)) /
316                         ramp_down;
317                     double factor = sin( x );
318                     // cout << "ramp down = " 
319                     //      << puff_progression - (ramp_up + puff_length) 
320                     //      << "  factor = " << factor << endl;
321                     effvis = effvis * factor;
322                 } else {
323                     effvis = 0.0;
324                 }
325
326                 /* cout << "len = " << puff_length
327                    << "  x = " << x 
328                    << "  factor = " << factor
329                    << "  actual_visibility = " << actual_visibility 
330                    << endl; */
331
332                 // time_factor = ( global_multi_loop * 
333                 //                 current_options.get_speed_up() ) /
334                 //                (double)current_options.get_model_hz();
335
336                 puff_progression += time_factor;
337                 // cout << "time factor = " << time_factor << endl;
338
339                 /* cout << "gml = " << global_multi_loop 
340                    << "  speed up = " << current_options.get_speed_up()
341                    << "  hz = " << current_options.get_model_hz() << endl;
342                    */ 
343
344                 if ( puff_progression > puff_length + ramp_up + ramp_down) {
345                     in_puff = false; 
346                 }
347             }
348
349             // never let visibility drop below 25 meters
350             if ( effvis <= 25.0 ) {
351                 effvis = 25.0;
352             }
353         }
354     } // for
355
356     effective_visibility = effvis;
357 }