]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/sky.cxx
Cosmetic changes for new code moved into simgear to make the naming scheme
[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 sun_size, double moon_size,
60                     int nplanets, sgdVec3 *planet_data,
61                     double planet_dist,
62                     int nstars, sgdVec3 *star_data, double star_dist )
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() );
75
76     planets = new SGStars;
77     pre_transform -> addKid( planets->build(nplanets, planet_data,
78                                             planet_dist)
79                              );
80
81     stars = new SGStars;
82     pre_transform -> addKid( stars->build(nstars, star_data, star_dist) );
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( sgVec4 sky_color, sgVec4 fog_color,
109                      double sun_angle, double moon_angle,
110                      int nplanets, sgdVec3 *planet_data,
111                      int nstars, sgdVec3 *star_data )
112 {
113     if ( effective_visibility > 1000.0 ) {
114         enable();
115         dome->repaint( sky_color, fog_color, sun_angle, effective_visibility );
116         oursun->repaint( sun_angle );
117         moon->repaint( moon_angle );
118         planets->repaint( sun_angle, nplanets, planet_data );
119         stars->repaint( sun_angle, nstars, star_data );
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( fog_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( sgVec3 view_pos, sgVec3 zero_elev, sgVec3 view_up,
143                         double lon, double lat, double alt, double spin,
144                         double gst, 
145                         double sun_ra, double sun_dec, double sun_dist,
146                         double moon_ra, double moon_dec, double moon_dist )
147 {
148     double angle = gst * 15;    // degrees
149     dome->reposition( zero_elev, lon, lat, spin );
150     oursun->reposition( view_pos, angle, sun_ra, sun_dec, sun_dist );
151     moon->reposition( view_pos, angle, moon_ra, moon_dec, moon_dist );
152     planets->reposition( view_pos, angle );
153     stars->reposition( view_pos, angle );
154
155     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
156         if ( cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR ) {
157             cloud_layers[i]->reposition( zero_elev, view_up, lon, lat, alt );
158         }
159     }
160
161     return true;
162 }
163
164
165 // draw background portions of the sky ... do this before you draw the
166 // rest of your scene.
167 void SGSky::preDraw() {
168     ssgCullAndDraw( pre_root );
169 }
170
171
172 // draw translucent clouds ... do this after you've drawn all the
173 // oapaque elements of your scene.
174 void SGSky::postDraw( float alt ) {
175     float slop = 5.0;           // if we are closer than this to a cloud layer,
176                                 // don't draw clouds
177
178     int in_cloud = -1;          // cloud we are in
179
180     int i;
181
182     // check where we are relative to the cloud layers
183     for ( i = 0; i < (int)cloud_layers.size(); ++i ) {
184         float asl = cloud_layers[i]->getElevation_m();
185         float thickness = cloud_layers[i]->getThickness_m();
186
187         if ( alt < asl - slop ) {
188             // below cloud layer
189         } else if ( alt < asl + thickness + slop ) {
190             // in cloud layer
191
192             // bail now and don't draw any clouds
193             in_cloud = i;
194         } else {
195             // above cloud layer
196         }
197     }
198
199     // determine rendering order
200     int pos = 0;
201     while ( pos < (int)cloud_layers.size() && 
202             alt > cloud_layers[pos]->getElevation_m())
203     {
204         ++pos;
205     }
206
207     if ( pos == 0 ) {
208         // we are below all the cloud layers, draw top to bottom
209         for ( i = cloud_layers.size() - 1; i >= 0; --i ) {
210             if ( i != in_cloud ) {
211                 cloud_layers[i]->draw();
212             }
213         }
214     } else if ( pos >= (int)cloud_layers.size() ) {
215         // we are above all the cloud layers, draw bottom to top
216         for ( i = 0; i < (int)cloud_layers.size(); ++i ) {
217             if ( i != in_cloud ) {
218                 cloud_layers[i]->draw();
219             }
220         }
221     } else {
222         // we are between cloud layers, draw lower layers bottom to
223         // top and upper layers top to bottom
224         for ( i = 0; i < pos; ++i ) {
225             if ( i != in_cloud ) {
226                 cloud_layers[i]->draw();
227             }
228         }
229         for ( i = cloud_layers.size() - 1; i >= pos; --i ) {
230             if ( i != in_cloud ) {
231                 cloud_layers[i]->draw();
232             }
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 ( alt < asl - transition ) {
274             // below cloud layer
275             ratio = 1.0;
276         } else if ( alt < asl ) {
277             // in lower transition
278             ratio = (asl - alt) / transition;
279         } else if ( alt < asl + thickness ) {
280             // in cloud layer
281             ratio = 0.0;
282         } else if ( alt < asl + thickness + transition ) {
283             // in upper transition
284             ratio = (alt - (asl + thickness)) / transition;
285         } else {
286             // above cloud layer
287             ratio = 1.0;
288         }
289
290         // accumulate effects from multiple cloud layers
291         effvis *= ratio;
292
293         if ( ratio < 1.0 ) {
294             if ( ! in_puff ) {
295                 // calc chance of entering cloud puff
296                 double rnd = sg_random();
297                 double chance = rnd * rnd * rnd;
298                 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
299                     in_puff = true;
300                     puff_length = sg_random() * 2.0; // up to 2 seconds
301                     puff_progression = 0.0;
302                 }
303             }
304
305             if ( in_puff ) {
306                 // modify actual_visibility based on puff envelope
307
308                 if ( puff_progression <= ramp_up ) {
309                     double x = 0.5 * SGD_PI * puff_progression / ramp_up;
310                     double factor = 1.0 - sin( x );
311                     // cout << "ramp up = " << puff_progression
312                     //      << "  factor = " << factor << endl;
313                     effvis = effvis * factor;
314                 } else if ( puff_progression >= ramp_up + puff_length ) {
315                     double x = 0.5 * SGD_PI * 
316                         (puff_progression - (ramp_up + puff_length)) /
317                         ramp_down;
318                     double factor = sin( x );
319                     // cout << "ramp down = " 
320                     //      << puff_progression - (ramp_up + puff_length) 
321                     //      << "  factor = " << factor << endl;
322                     effvis = effvis * factor;
323                 } else {
324                     effvis = 0.0;
325                 }
326
327                 /* cout << "len = " << puff_length
328                    << "  x = " << x 
329                    << "  factor = " << factor
330                    << "  actual_visibility = " << actual_visibility 
331                    << endl; */
332
333                 // time_factor = ( global_multi_loop * 
334                 //                 current_options.get_speed_up() ) /
335                 //                (double)current_options.get_model_hz();
336
337                 puff_progression += time_factor;
338                 // cout << "time factor = " << time_factor << endl;
339
340                 /* cout << "gml = " << global_multi_loop 
341                    << "  speed up = " << current_options.get_speed_up()
342                    << "  hz = " << current_options.get_model_hz() << endl;
343                    */ 
344
345                 if ( puff_progression > puff_length + ramp_up + ramp_down) {
346                     in_puff = false; 
347                 }
348             }
349
350             // never let visibility drop below 25 meters
351             if ( effvis <= 25.0 ) {
352                 effvis = 25.0;
353             }
354         }
355     } // for
356
357     effective_visibility = effvis;
358 }