]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/sky.cxx
make headers include headers they depend on, don't rely on the c(xx)
[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 )
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) );
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
120         oursun->repaint( sc.sun_angle, effective_visibility );
121         moon->repaint( sc.moon_angle );
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     stars->reposition( st.view_pos, angle );
152     planets->reposition( st.view_pos, angle );
153
154     oursun->reposition( st.view_pos, angle,
155                         st.sun_ra, st.sun_dec, st.sun_dist );
156
157     moon->reposition( st.view_pos, angle,
158                       st.moon_ra, st.moon_dec, st.moon_dist );
159
160     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
161         if ( cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR ) {
162             cloud_layers[i]->reposition( st.zero_elev, st.view_up,
163                                          st.lon, st.lat, st.alt, dt );
164         }
165     }
166
167     return true;
168 }
169
170
171 // draw background portions of the sky ... do this before you draw the
172 // rest of your scene.
173 void SGSky::preDraw( float alt, float fog_exp2_density ) {
174     ssgCullAndDraw( pre_root );
175
176     // if we are closer than this to a cloud layer, don't draw clouds
177     static const float slop = 5.0;
178     int i;
179
180     // check where we are relative to the cloud layers
181     in_cloud = -1;
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();
185
186         if ( alt < asl - slop ) {
187             // below cloud layer
188         } else if ( alt < asl + thickness + slop ) {
189             // in cloud layer
190
191             // bail now and don't draw any clouds
192                         if( cloud_layers[i]->get_layer3D()->is3D() && SGCloudField::enable3D )
193                                 continue;
194             in_cloud = i;
195         } else {
196             // above cloud layer
197         }
198     }
199
200     // determine rendering order
201     cur_layer_pos = 0;
202     while ( cur_layer_pos < (int)cloud_layers.size() &&
203             alt > cloud_layers[cur_layer_pos]->getElevation_m() )
204     {
205         ++cur_layer_pos;
206     }
207
208     // FIXME: This should not be needed, but at this time (08/15/2003)
209     //        certain NVidia drivers don't seem to implement
210     //        glPushAttrib(FG_FOG_BIT) properly. The result is that
211     //        there is not fog when looking at the sun.
212     glFogf ( GL_FOG_DENSITY, fog_exp2_density );
213 }
214
215 void SGSky::drawUpperClouds( ) {
216     // draw the cloud layers that are above us, top to bottom
217     for ( int i = (int)cloud_layers.size() - 1; i >= cur_layer_pos; --i ) {
218         if ( i != in_cloud ) {
219             cloud_layers[i]->draw( false );
220         }
221     }
222 }
223
224
225 // draw translucent clouds ... do this after you've drawn all the
226 // oapaque elements of your scene.
227 void SGSky::drawLowerClouds() {
228
229     // draw the cloud layers that are below us, bottom to top
230     for ( int i = 0; i < cur_layer_pos; ++i ) {
231         if ( i != in_cloud ) {
232             cloud_layers[i]->draw( true );
233         }
234     }
235 }
236
237 void
238 SGSky::add_cloud_layer( SGCloudLayer * layer )
239 {
240     cloud_layers.push_back(layer);
241 }
242
243 const SGCloudLayer *
244 SGSky::get_cloud_layer (int i) const
245 {
246     return cloud_layers[i];
247 }
248
249 SGCloudLayer *
250 SGSky::get_cloud_layer (int i)
251 {
252     return cloud_layers[i];
253 }
254
255 int
256 SGSky::get_cloud_layer_count () const
257 {
258     return cloud_layers.size();
259 }
260
261 // modify the current visibility based on cloud layers, thickness,
262 // transition range, and simulated "puffs".
263 void SGSky::modify_vis( float alt, float time_factor ) {
264     float effvis = visibility;
265
266     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
267         float asl = cloud_layers[i]->getElevation_m();
268         float thickness = cloud_layers[i]->getThickness_m();
269         float transition = cloud_layers[i]->getTransition_m();
270
271         double ratio = 1.0;
272
273         if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ) {
274             // less than 50% coverage -- assume we're in the clear for now
275             ratio = 1.0;
276         } else if ( alt < asl - transition ) {
277             // below cloud layer
278             ratio = 1.0;
279         } else if ( alt < asl ) {
280             // in lower transition
281             ratio = (asl - alt) / transition;
282         } else if ( alt < asl + thickness ) {
283             // in cloud layer
284             ratio = 0.0;
285         } else if ( alt < asl + thickness + transition ) {
286             // in upper transition
287             ratio = (alt - (asl + thickness)) / transition;
288         } else {
289             // above cloud layer
290             ratio = 1.0;
291         }
292
293         if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ||
294                         cloud_layers[i]->get_layer3D()->is3D() && SGCloudField::enable3D) {
295             // do nothing, clear layers aren't drawn, don't affect
296             // visibility andn dont' need to be faded in or out.
297         } else if ( (cloud_layers[i]->getCoverage() == 
298                      SGCloudLayer::SG_CLOUD_FEW)
299                     || (cloud_layers[i]->getCoverage() ==
300                         SGCloudLayer::SG_CLOUD_SCATTERED) )
301         {
302             // set the alpha fade value for the cloud layer.  For less
303             // dense cloud layers we fade the layer to nothing as we
304             // approach it because we stay clear visibility-wise as we
305             // pass through it.
306             float temp = ratio * 2.0;
307             if ( temp > 1.0 ) { temp = 1.0; }
308             cloud_layers[i]->setAlpha( temp );
309
310             // don't touch visibility
311         } else {
312             // maintain full alpha for denser cloud layer types.
313             // Let's set the value explicitly in case someone changed
314             // the layer type.
315             cloud_layers[i]->setAlpha( 1.0 );
316
317             // lower visibility as we approach the cloud layer.
318             // accumulate effects from multiple cloud layers
319             effvis *= ratio;
320         }
321
322 #if 0
323         if ( ratio < 1.0 ) {
324             if ( ! in_puff ) {
325                 // calc chance of entering cloud puff
326                 double rnd = sg_random();
327                 double chance = rnd * rnd * rnd;
328                 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
329                     in_puff = true;
330                     puff_length = sg_random() * 2.0; // up to 2 seconds
331                     puff_progression = 0.0;
332                 }
333             }
334
335             if ( in_puff ) {
336                 // modify actual_visibility based on puff envelope
337
338                 if ( puff_progression <= ramp_up ) {
339                     double x = SGD_PI_2 * puff_progression / ramp_up;
340                     double factor = 1.0 - sin( x );
341                     // cout << "ramp up = " << puff_progression
342                     //      << "  factor = " << factor << endl;
343                     effvis = effvis * factor;
344                 } else if ( puff_progression >= ramp_up + puff_length ) {
345                     double x = SGD_PI_2 * 
346                         (puff_progression - (ramp_up + puff_length)) /
347                         ramp_down;
348                     double factor = sin( x );
349                     // cout << "ramp down = " 
350                     //      << puff_progression - (ramp_up + puff_length) 
351                     //      << "  factor = " << factor << endl;
352                     effvis = effvis * factor;
353                 } else {
354                     effvis = 0.0;
355                 }
356
357                 /* cout << "len = " << puff_length
358                    << "  x = " << x 
359                    << "  factor = " << factor
360                    << "  actual_visibility = " << actual_visibility 
361                    << endl; */
362
363                 // time_factor = ( global_multi_loop * 
364                 //                 current_options.get_speed_up() ) /
365                 //                (double)current_options.get_model_hz();
366
367                 puff_progression += time_factor;
368                 // cout << "time factor = " << time_factor << endl;
369
370                 /* cout << "gml = " << global_multi_loop 
371                    << "  speed up = " << current_options.get_speed_up()
372                    << "  hz = " << current_options.get_model_hz() << endl;
373                    */ 
374
375                 if ( puff_progression > puff_length + ramp_up + ramp_down) {
376                     in_puff = false; 
377                 }
378             }
379         }
380 #endif
381
382         // never let visibility drop below 25 meters
383         if ( effvis <= 25.0 ) {
384             effvis = 25.0;
385         }
386
387     } // for
388
389     effective_visibility = effvis;
390 }