]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/sky.cxx
059bdf469705890aa09fb5662435ddedd8383d73
[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         oursun->repaint( sc.sun_angle, effective_visibility );
116
117         moon->repaint( sc.moon_angle );
118
119         planets->repaint( sc.sun_angle, sc.nplanets, sc.planet_data );
120
121         stars->repaint( sc.sun_angle, sc.nstars, sc.star_data );
122
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 );
126             }
127         }
128     } else {
129         // turn off sky
130         disable();
131     }
132
133     return true;
134 }
135
136
137 // reposition the sky at the specified origin and orientation
138 //
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 )
145 {
146
147     double angle = st.gst * 15; // degrees
148
149     dome->reposition( st.zero_elev, st.lon, st.lat, st.spin );
150
151     oursun->reposition( st.view_pos, angle,
152                         st.sun_ra, st.sun_dec, st.sun_dist );
153
154     moon->reposition( st.view_pos, angle,
155                       st.moon_ra, st.moon_dec, st.moon_dist );
156
157     planets->reposition( st.view_pos, angle );
158
159     stars->reposition( st.view_pos, angle );
160
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 );
165         }
166     }
167
168     return true;
169 }
170
171
172 // draw background portions of the sky ... do this before you draw the
173 // rest of your scene.
174 void SGSky::preDraw( float alt, float fog_exp2_density ) {
175     ssgCullAndDraw( pre_root );
176
177     // FIXME: This should not be needed, but at this time (08/15/2003)
178     //        certain NVidia drivers don't seem to implement
179     //        fgPushAttrib(FG_FOG_BIT) properly. The result is that
180     //        there is not fog when looking at the sun.
181     glFogf ( GL_FOG_DENSITY, fog_exp2_density );
182
183         // if we are closer than this to a cloud layer, don't draw clouds
184     static const float slop = 5.0;
185     int i;
186
187     // check where we are relative to the cloud layers
188     in_cloud = -1;
189     for ( i = 0; i < (int)cloud_layers.size(); ++i ) {
190        float asl = cloud_layers[i]->getElevation_m();
191        float thickness = cloud_layers[i]->getThickness_m();
192
193        if ( alt < asl - slop ) {
194            // below cloud layer
195        } else if ( alt < asl + thickness + slop ) {
196            // in cloud layer
197
198            // bail now and don't draw any clouds
199            in_cloud = i;
200        } else {
201            // above cloud layer
202        }
203     }
204
205     // determine rendering order
206     cur_layer_pos = 0;
207     while ( cur_layer_pos < (int)cloud_layers.size() &&
208            alt > cloud_layers[cur_layer_pos]->getElevation_m())
209     {
210        ++cur_layer_pos;
211     }
212
213     // draw the cloud layers that are above us, top to bottom
214     for ( i = (int)cloud_layers.size() - 1; i >= cur_layer_pos; --i ) {
215         if ( i != in_cloud ) {
216             cloud_layers[i]->draw();
217         }
218     }
219 }
220
221
222 // draw translucent clouds ... do this after you've drawn all the
223 // oapaque elements of your scene.
224 void SGSky::postDraw( float alt ) {
225
226     // draw the cloud layers that are below us, bottom to top
227
228     for ( int i = 0; i < cur_layer_pos; ++i ) {
229         if ( i != in_cloud ) {
230             cloud_layers[i]->draw();
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 ( alt < asl - transition ) {
272             // below cloud layer
273             ratio = 1.0;
274         } else if ( alt < asl ) {
275             // in lower transition
276             ratio = (asl - alt) / transition;
277         } else if ( alt < asl + thickness ) {
278             // in cloud layer
279             ratio = 0.0;
280         } else if ( alt < asl + thickness + transition ) {
281             // in upper transition
282             ratio = (alt - (asl + thickness)) / transition;
283         } else {
284             // above cloud layer
285             ratio = 1.0;
286         }
287
288         // accumulate effects from multiple cloud layers
289         effvis *= ratio;
290
291         if ( ratio < 1.0 ) {
292             if ( ! in_puff ) {
293                 // calc chance of entering cloud puff
294                 double rnd = sg_random();
295                 double chance = rnd * rnd * rnd;
296                 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
297                     in_puff = true;
298                     puff_length = sg_random() * 2.0; // up to 2 seconds
299                     puff_progression = 0.0;
300                 }
301             }
302
303             if ( in_puff ) {
304                 // modify actual_visibility based on puff envelope
305
306                 if ( puff_progression <= ramp_up ) {
307                     double x = 0.5 * SGD_PI * puff_progression / ramp_up;
308                     double factor = 1.0 - sin( x );
309                     // cout << "ramp up = " << puff_progression
310                     //      << "  factor = " << factor << endl;
311                     effvis = effvis * factor;
312                 } else if ( puff_progression >= ramp_up + puff_length ) {
313                     double x = 0.5 * SGD_PI * 
314                         (puff_progression - (ramp_up + puff_length)) /
315                         ramp_down;
316                     double factor = sin( x );
317                     // cout << "ramp down = " 
318                     //      << puff_progression - (ramp_up + puff_length) 
319                     //      << "  factor = " << factor << endl;
320                     effvis = effvis * factor;
321                 } else {
322                     effvis = 0.0;
323                 }
324
325                 /* cout << "len = " << puff_length
326                    << "  x = " << x 
327                    << "  factor = " << factor
328                    << "  actual_visibility = " << actual_visibility 
329                    << endl; */
330
331                 // time_factor = ( global_multi_loop * 
332                 //                 current_options.get_speed_up() ) /
333                 //                (double)current_options.get_model_hz();
334
335                 puff_progression += time_factor;
336                 // cout << "time factor = " << time_factor << endl;
337
338                 /* cout << "gml = " << global_multi_loop 
339                    << "  speed up = " << current_options.get_speed_up()
340                    << "  hz = " << current_options.get_model_hz() << endl;
341                    */ 
342
343                 if ( puff_progression > puff_length + ramp_up + ramp_down) {
344                     in_puff = false; 
345                 }
346             }
347
348             // never let visibility drop below 25 meters
349             if ( effvis <= 25.0 ) {
350                 effvis = 25.0;
351             }
352         }
353     } // for
354
355     effective_visibility = effvis;
356 }