]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/sky.cxx
- Tweaks to doxygen main page.
[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
48
49 // Destructor
50 SGSky::~SGSky( void )
51 {
52     for (unsigned int i = 0; i < cloud_layers.size(); i++)
53         delete cloud_layers[i];
54 }
55
56
57 // initialize the sky and connect the components to the scene graph at
58 // the provided branch
59 void SGSky::build( double h_radius_m, double v_radius_m,
60                    double sun_size, double moon_size,
61                    int nplanets, sgdVec3 *planet_data,
62                    int nstars, sgdVec3 *star_data )
63 {
64     pre_root = new ssgRoot;
65     post_root = new ssgRoot;
66
67     pre_selector = new ssgSelector;
68     post_selector = new ssgSelector;
69
70     pre_transform = new ssgTransform;
71     post_transform = new ssgTransform;
72
73     dome = new SGSkyDome;
74     pre_transform -> addKid( dome->build( h_radius_m, v_radius_m ) );
75
76     planets = new SGStars;
77     pre_transform -> addKid(planets->build(nplanets, planet_data, h_radius_m));
78
79     stars = new SGStars;
80     pre_transform -> addKid( stars->build(nstars, star_data, h_radius_m) );
81     
82     moon = new SGMoon;
83     pre_transform -> addKid( moon->build(tex_path, moon_size) );
84
85     oursun = new SGSun;
86     pre_transform -> addKid( oursun->build(tex_path, sun_size) );
87
88     pre_selector->addKid( pre_transform );
89     pre_selector->clrTraversalMaskBits( SSGTRAV_HOT );
90
91     post_selector->addKid( post_transform );
92     post_selector->clrTraversalMaskBits( SSGTRAV_HOT );
93
94     pre_root->addKid( pre_selector );
95     post_root->addKid( post_selector );
96 }
97
98
99 // repaint the sky components based on current value of sun_angle,
100 // sky, and fog colors.
101 //
102 // sun angle in degrees relative to verticle
103 // 0 degrees = high noon
104 // 90 degrees = sun rise/set
105 // 180 degrees = darkest midnight
106 bool SGSky::repaint( sgVec4 sky_color, sgVec4 fog_color, sgVec4 cloud_color,
107                      double sun_angle, double moon_angle,
108                      int nplanets, sgdVec3 *planet_data,
109                      int nstars, sgdVec3 *star_data )
110 {
111     if ( effective_visibility > 1000.0 ) {
112         enable();
113         dome->repaint( sky_color, fog_color, sun_angle, effective_visibility );
114         oursun->repaint( sun_angle );
115         moon->repaint( moon_angle );
116         planets->repaint( sun_angle, nplanets, planet_data );
117         stars->repaint( sun_angle, nstars, star_data );
118
119         for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
120             if (cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR){
121                 cloud_layers[i]->repaint( cloud_color );
122             }
123         }
124     } else {
125         // turn off sky
126         disable();
127     }
128
129     return true;
130 }
131
132
133 // reposition the sky at the specified origin and orientation
134 //
135 // lon specifies a rotation about the Z axis
136 // lat specifies a rotation about the new Y axis
137 // spin specifies a rotation about the new Z axis (this allows
138 // additional orientation for the sunrise/set effects and is used by
139 // the skydome and perhaps clouds.
140 bool SGSky::reposition( sgVec3 view_pos, sgVec3 zero_elev, sgVec3 view_up,
141                         double lon, double lat, double alt, double spin,
142                         double gst, 
143                         double sun_ra, double sun_dec, double sun_dist,
144                         double moon_ra, double moon_dec, double moon_dist )
145 {
146     double angle = gst * 15;    // degrees
147     dome->reposition( zero_elev, lon, lat, spin );
148     oursun->reposition( view_pos, angle, sun_ra, sun_dec, sun_dist );
149     moon->reposition( view_pos, angle, moon_ra, moon_dec, moon_dist );
150     planets->reposition( view_pos, angle );
151     stars->reposition( view_pos, angle );
152
153     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
154         if ( cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR ) {
155             cloud_layers[i]->reposition( zero_elev, view_up, lon, lat, alt );
156         }
157     }
158
159     return true;
160 }
161
162
163 // draw background portions of the sky ... do this before you draw the
164 // rest of your scene.
165 void SGSky::preDraw() {
166     ssgCullAndDraw( pre_root );
167 }
168
169
170 // draw translucent clouds ... do this after you've drawn all the
171 // oapaque elements of your scene.
172 void SGSky::postDraw( float alt ) {
173     float slop = 5.0;           // if we are closer than this to a cloud layer,
174                                 // don't draw clouds
175
176     int in_cloud = -1;          // cloud we are in
177
178     int i;
179
180     // check where we are relative to the cloud layers
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             in_cloud = i;
192         } else {
193             // above cloud layer
194         }
195     }
196
197     // determine rendering order
198     int pos = 0;
199     while ( pos < (int)cloud_layers.size() && 
200             alt > cloud_layers[pos]->getElevation_m())
201     {
202         ++pos;
203     }
204
205     if ( pos == 0 ) {
206         // we are below all the cloud layers, draw top to bottom
207         for ( i = cloud_layers.size() - 1; i >= 0; --i ) {
208             if ( i != in_cloud ) {
209                 cloud_layers[i]->draw();
210             }
211         }
212     } else if ( pos >= (int)cloud_layers.size() ) {
213         // we are above all the cloud layers, draw bottom to top
214         for ( i = 0; i < (int)cloud_layers.size(); ++i ) {
215             if ( i != in_cloud ) {
216                 cloud_layers[i]->draw();
217             }
218         }
219     } else {
220         // we are between cloud layers, draw lower layers bottom to
221         // top and upper layers top to bottom
222         for ( i = 0; i < pos; ++i ) {
223             if ( i != in_cloud ) {
224                 cloud_layers[i]->draw();
225             }
226         }
227         for ( i = cloud_layers.size() - 1; i >= pos; --i ) {
228             if ( i != in_cloud ) {
229                 cloud_layers[i]->draw();
230             }
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 }