]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/sky.cxx
Render sky with depth test off.
[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 "sky.hxx"
29 #include "cloudfield.hxx"
30 #include "newcloud.hxx"
31
32 #include <simgear/math/sg_random.h>
33 #include <simgear/scene/util/RenderConstants.hxx>
34
35 #include <osg/StateSet>
36 #include <osg/Depth>
37
38 // Constructor
39 SGSky::SGSky( void ) {
40     effective_visibility = visibility = 10000.0;
41
42     // near cloud visibility state variables
43     in_puff = false;
44     puff_length = 0;
45     puff_progression = 0;
46     ramp_up = 0.15;
47     ramp_down = 0.15;
48
49     in_cloud  = -1;
50     
51     clouds_3d_enabled = false;
52     clouds_3d_density = 0.8;
53
54     pre_root = new osg::Group;
55     pre_root->setNodeMask(simgear::BACKGROUND_BIT);
56     osg::StateSet* preStateSet = new osg::StateSet;
57     preStateSet->setAttribute(new osg::Depth(osg::Depth::LESS, 0.0, 1.0,
58                                              false));
59     pre_root->setStateSet(preStateSet);
60     cloud_root = new osg::Group;
61     cloud_root->setNodeMask(simgear::MODEL_BIT);
62
63     pre_selector = new osg::Switch;
64
65     pre_transform = new osg::MatrixTransform;
66 }
67
68
69 // Destructor
70 SGSky::~SGSky( void )
71 {
72 }
73
74
75 // initialize the sky and connect the components to the scene graph at
76 // the provided branch
77 void SGSky::build( double h_radius_m, double v_radius_m,
78                    double sun_size, double moon_size,
79                    int nplanets, SGVec3d planet_data[7],
80                    int nstars, SGVec3d star_data[], SGPropertyNode *property_tree_node )
81 {
82     dome = new SGSkyDome;
83     pre_transform->addChild( dome->build( h_radius_m, v_radius_m ) );
84
85     planets = new SGStars;
86     pre_transform->addChild(planets->build(nplanets, planet_data, h_radius_m));
87
88     stars = new SGStars;
89     pre_transform->addChild( stars->build(nstars, star_data, h_radius_m) );
90     
91     moon = new SGMoon;
92     pre_transform->addChild( moon->build(tex_path, moon_size) );
93
94     oursun = new SGSun;
95     pre_transform->addChild( oursun->build(tex_path, sun_size, property_tree_node ) );
96
97     pre_selector->addChild( pre_transform.get() );
98
99     pre_root->addChild( pre_selector.get() );    
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         oursun->repaint( sc.sun_angle, effective_visibility );
120         moon->repaint( sc.moon_angle );
121
122         for ( unsigned i = 0; i < cloud_layers.size(); ++i ) {
123             if (cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR){
124                 cloud_layers[i]->repaint( sc.cloud_color );
125             }
126         }
127     } else {
128         // turn off sky
129         disable();
130     }
131
132     return true;
133 }
134
135
136 // reposition the sky at the specified origin and orientation
137 //
138 // lon specifies a rotation about the Z axis
139 // lat specifies a rotation about the new Y axis
140 // spin specifies a rotation about the new Z axis (this allows
141 // additional orientation for the sunrise/set effects and is used by
142 // the skydome and perhaps clouds.
143 bool SGSky::reposition( SGSkyState &st, double dt )
144 {
145
146     double angle = st.gst * 15; // degrees
147
148     dome->reposition( st.zero_elev, st.alt, st.lon, st.lat, st.spin );
149
150     stars->reposition( st.view_pos, angle );
151     planets->reposition( st.view_pos, angle );
152
153     oursun->reposition( st.view_pos, angle,
154                         st.sun_ra, st.sun_dec, st.sun_dist, st.lat, st.alt, st.sun_angle );
155
156     moon->reposition( st.view_pos, angle,
157                       st.moon_ra, st.moon_dec, st.moon_dist );
158
159     for ( unsigned i = 0; i < cloud_layers.size(); ++i ) {
160         if ( cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR ) {
161             cloud_layers[i]->reposition( st.zero_elev, st.view_up,
162                                          st.lon, st.lat, st.alt, dt );
163         } else
164           cloud_layers[i]->getNode()->setAllChildrenOff();
165     }
166
167     return true;
168 }
169
170 void
171 SGSky::add_cloud_layer( SGCloudLayer * layer )
172 {
173     cloud_layers.push_back(layer);
174     cloud_root->addChild(layer->getNode());
175
176     layer->set_enable3dClouds(clouds_3d_enabled);
177 }
178
179 const SGCloudLayer *
180 SGSky::get_cloud_layer (int i) const
181 {
182     return cloud_layers[i];
183 }
184
185 SGCloudLayer *
186 SGSky::get_cloud_layer (int i)
187 {
188     return cloud_layers[i];
189 }
190
191 int
192 SGSky::get_cloud_layer_count () const
193 {
194     return cloud_layers.size();
195 }
196
197 double SGSky::get_3dCloudDensity() const {
198     return SGNewCloud::getDensity();
199 }
200
201 void SGSky::set_3dCloudDensity(double density)
202 {
203     SGNewCloud::setDensity(density);
204 }
205
206 float SGSky::get_3dCloudVisRange() const {
207     return SGCloudField::getVisRange();
208 }
209
210 void SGSky::set_3dCloudVisRange(float vis)
211 {
212     SGCloudField::setVisRange(vis);
213     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
214         cloud_layers[i]->get_layer3D()->applyVisRange();
215     }
216 }
217
218 float SGSky::get_3dCloudNumFlavours() const {
219     return (float) SGNewCloud::getNumFlavours();
220 }
221
222 void SGSky::set_3dCloudNumFlavours(float n)
223 {
224     SGNewCloud::setNumFlavours((int) n);
225 }
226
227 void SGSky::texture_path( const string& path ) {
228         tex_path = SGPath( path );
229 }
230
231 // modify the current visibility based on cloud layers, thickness,
232 // transition range, and simulated "puffs".
233 void SGSky::modify_vis( float alt, float time_factor ) {
234     float effvis = visibility;
235
236     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
237         float asl = cloud_layers[i]->getElevation_m();
238         float thickness = cloud_layers[i]->getThickness_m();
239         float transition = cloud_layers[i]->getTransition_m();
240
241         double ratio = 1.0;
242
243         if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ) {
244             // less than 50% coverage -- assume we're in the clear for now
245             ratio = 1.0;
246         } else if ( alt < asl - transition ) {
247             // below cloud layer
248             ratio = 1.0;
249         } else if ( alt < asl ) {
250             // in lower transition
251             ratio = (asl - alt) / transition;
252         } else if ( alt < asl + thickness ) {
253             // in cloud layer
254             ratio = 0.0;
255         } else if ( alt < asl + thickness + transition ) {
256             // in upper transition
257             ratio = (alt - (asl + thickness)) / transition;
258         } else {
259             // above cloud layer
260             ratio = 1.0;
261         }
262
263         if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ||
264              cloud_layers[i]->get_layer3D()->defined3D) {
265             // do nothing, clear layers aren't drawn, don't affect
266             // visibility andn dont' need to be faded in or out.
267         } else if ( (cloud_layers[i]->getCoverage() == 
268                      SGCloudLayer::SG_CLOUD_FEW)
269                     || (cloud_layers[i]->getCoverage() ==
270                         SGCloudLayer::SG_CLOUD_SCATTERED) )
271         {
272             // set the alpha fade value for the cloud layer.  For less
273             // dense cloud layers we fade the layer to nothing as we
274             // approach it because we stay clear visibility-wise as we
275             // pass through it.
276             float temp = ratio * 2.0;
277             if ( temp > 1.0 ) { temp = 1.0; }
278             cloud_layers[i]->setAlpha( temp );
279
280             // don't touch visibility
281         } else {
282             // maintain full alpha for denser cloud layer types.
283             // Let's set the value explicitly in case someone changed
284             // the layer type.
285             cloud_layers[i]->setAlpha( 1.0 );
286
287             // lower visibility as we approach the cloud layer.
288             // accumulate effects from multiple cloud layers
289             effvis *= ratio;
290         }
291
292 #if 0
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 = SGD_PI_2 * 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 = SGD_PI_2 * 
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 #endif
351
352         // never let visibility drop below 25 meters
353         if ( effvis <= 25.0 ) {
354             effvis = 25.0;
355         }
356
357     } // for
358
359     effective_visibility = effvis;
360 }
361
362