]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/sky.cxx
scenery: remove default zero reader writer options argument.
[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 #include <simgear/scene/util/OsgMath.hxx>
35 #include <simgear/sg_inlines.h>
36
37 #include <osg/StateSet>
38 #include <osg/Depth>
39
40 // Constructor
41 SGSky::SGSky( void ) {
42     effective_visibility = visibility = 10000.0;
43     minimum_sky_visibility = 1000;
44
45     // near cloud visibility state variables
46     in_puff = false;
47     puff_length = 0;
48     puff_progression = 0;
49     ramp_up = 0.15;
50     ramp_down = 0.15;
51
52     in_cloud  = -1;
53     
54     clouds_3d_enabled = false;
55     clouds_3d_density = 0.8;
56
57     pre_root = new osg::Group;
58     pre_root->setNodeMask(simgear::BACKGROUND_BIT);
59     osg::StateSet* preStateSet = new osg::StateSet;
60     preStateSet->setAttribute(new osg::Depth(osg::Depth::LESS, 0.0, 1.0,
61                                              false));
62     pre_root->setStateSet(preStateSet);
63     cloud_root = new osg::Group;
64     cloud_root->setNodeMask(simgear::MODEL_BIT);
65
66     pre_selector = new osg::Switch;
67
68     pre_transform = new osg::Group;
69
70     _ephTransform = new osg::MatrixTransform;
71 }
72
73
74 // Destructor
75 SGSky::~SGSky( void )
76 {
77 }
78
79
80 // initialize the sky and connect the components to the scene graph at
81 // the provided branch
82 void SGSky::build( double h_radius_m, double v_radius_m,
83                    double sun_size, double moon_size,
84                    const SGEphemeris& eph, SGPropertyNode *property_tree_node,
85                    simgear::SGReaderWriterOptions* options )
86 {
87     dome = new SGSkyDome;
88     pre_transform->addChild( dome->build( h_radius_m, v_radius_m, options ) );
89
90     pre_transform->addChild(_ephTransform.get());
91     planets = new SGStars;
92     _ephTransform->addChild( planets->build(eph.getNumPlanets(), eph.getPlanets(), h_radius_m) );
93
94     stars = new SGStars;
95     _ephTransform->addChild( stars->build(eph.getNumStars(), eph.getStars(), h_radius_m) );
96     
97     moon = new SGMoon;
98     _ephTransform->addChild( moon->build(tex_path, moon_size) );
99
100     oursun = new SGSun;
101     _ephTransform->addChild( oursun->build(tex_path, sun_size, property_tree_node ) );
102
103     pre_selector->addChild( pre_transform.get() );
104
105     pre_root->addChild( pre_selector.get() );    
106 }
107
108
109 // repaint the sky components based on current value of sun_angle,
110 // sky, and fog colors.
111 //
112 // sun angle in degrees relative to verticle
113 // 0 degrees = high noon
114 // 90 degrees = sun rise/set
115 // 180 degrees = darkest midnight
116 bool SGSky::repaint( const SGSkyColor &sc, const SGEphemeris& eph )
117 {
118     if ( effective_visibility > minimum_sky_visibility ) {
119         enable();
120         dome->repaint( sc.adj_sky_color, sc.sky_color, sc.fog_color,
121                        sc.sun_angle, effective_visibility );
122
123         stars->repaint( sc.sun_angle, eph.getNumStars(), eph.getStars() );
124         planets->repaint( sc.sun_angle, eph.getNumPlanets(), eph.getPlanets() );
125         oursun->repaint( sc.sun_angle, effective_visibility );
126         moon->repaint( sc.moon_angle );
127
128         for ( unsigned i = 0; i < cloud_layers.size(); ++i ) {
129             if (cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR){
130                 cloud_layers[i]->repaint( sc.cloud_color );
131             }
132         }
133     } else {
134         // turn off sky
135         disable();
136     }
137     SGCloudField::updateFog((double)effective_visibility,
138                             osg::Vec4f(toOsg(sc.fog_color), 1.0f));
139     return true;
140 }
141
142 // reposition the sky at the specified origin and orientation
143 //
144 // lon specifies a rotation about the Z axis
145 // lat specifies a rotation about the new Y axis
146 // spin specifies a rotation about the new Z axis (this allows
147 // additional orientation for the sunrise/set effects and is used by
148 // the skydome and perhaps clouds.
149 bool SGSky::reposition( const SGSkyState &st, const SGEphemeris& eph, double dt )
150 {
151     double angle = st.gst * 15; // degrees
152     double angleRad = SGMiscd::deg2rad(angle);
153
154     SGVec3f zero_elev, view_up;
155     double lon, lat, alt;
156
157     SGGeod geodZeroViewPos = SGGeod::fromGeodM(st.pos_geod, 0);
158     zero_elev = toVec3f( SGVec3d::fromGeod(geodZeroViewPos) );
159
160     // calculate the scenery up vector
161     SGQuatd hlOr = SGQuatd::fromLonLat(st.pos_geod);
162     view_up = toVec3f(hlOr.backTransform(-SGVec3d::e3()));
163
164     // viewer location
165     lon = st.pos_geod.getLongitudeRad();
166     lat = st.pos_geod.getLatitudeRad();
167     alt = st.pos_geod.getElevationM();
168
169     dome->reposition( zero_elev, alt, lon, lat, st.spin );
170
171     osg::Matrix m = osg::Matrix::rotate(angleRad, osg::Vec3(0, 0, -1));
172     m.postMultTranslate(toOsg(st.pos));
173     _ephTransform->setMatrix(m);
174
175     double sun_ra = eph.getSunRightAscension();
176     double sun_dec = eph.getSunDeclination();
177     oursun->reposition( sun_ra, sun_dec, st.sun_dist, lat, alt, st.sun_angle );
178
179     double moon_ra = eph.getMoonRightAscension();
180     double moon_dec = eph.getMoonDeclination();
181     moon->reposition( moon_ra, moon_dec, st.moon_dist );
182
183     for ( unsigned i = 0; i < cloud_layers.size(); ++i ) {
184         if ( cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR ||
185                cloud_layers[i]->get_layer3D()->isDefined3D() ) {
186             cloud_layers[i]->reposition( zero_elev, view_up, lon, lat, alt, dt);
187         } else {
188           cloud_layers[i]->getNode()->setAllChildrenOff();
189     }
190     }
191
192     return true;
193 }
194
195 void
196 SGSky::add_cloud_layer( SGCloudLayer * layer )
197 {
198     cloud_layers.push_back(layer);
199     cloud_root->addChild(layer->getNode());
200
201     layer->set_enable3dClouds(clouds_3d_enabled);
202 }
203
204 const SGCloudLayer *
205 SGSky::get_cloud_layer (int i) const
206 {
207     return cloud_layers[i];
208 }
209
210 SGCloudLayer *
211 SGSky::get_cloud_layer (int i)
212 {
213     return cloud_layers[i];
214 }
215
216 int
217 SGSky::get_cloud_layer_count () const
218 {
219     return cloud_layers.size();
220 }
221
222 double SGSky::get_3dCloudDensity() const {
223     return SGNewCloud::getDensity();
224 }
225
226 void SGSky::set_3dCloudDensity(double density)
227 {
228     SGNewCloud::setDensity(density);
229 }
230
231 float SGSky::get_3dCloudVisRange() const {
232     return SGCloudField::getVisRange();
233 }
234
235 void SGSky::set_3dCloudVisRange(float vis)
236 {
237     SGCloudField::setVisRange(vis);
238     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
239         cloud_layers[i]->get_layer3D()->applyVisAndLoDRange();
240     }
241 }
242
243 float SGSky::get_3dCloudImpostorDistance() const {
244     return SGCloudField::getImpostorDistance();
245 }
246
247 void SGSky::set_3dCloudImpostorDistance(float vis)
248 {
249     SGCloudField::setImpostorDistance(vis);
250     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
251         cloud_layers[i]->get_layer3D()->applyVisAndLoDRange();
252     }
253 }
254
255 float SGSky::get_3dCloudLoD1Range() const {
256     return SGCloudField::getLoD1Range();
257 }
258
259 void SGSky::set_3dCloudLoD1Range(float vis)
260 {
261     SGCloudField::setLoD1Range(vis);
262     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
263         cloud_layers[i]->get_layer3D()->applyVisAndLoDRange();
264     }
265 }
266
267 float SGSky::get_3dCloudLoD2Range() const {
268     return SGCloudField::getLoD2Range();
269 }
270
271 void SGSky::set_3dCloudLoD2Range(float vis)
272 {
273     SGCloudField::setLoD2Range(vis);
274     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
275         cloud_layers[i]->get_layer3D()->applyVisAndLoDRange();
276     }
277 }
278
279 bool SGSky::get_3dCloudWrap() const {
280     return SGCloudField::getWrap();
281 }
282
283 void SGSky::set_3dCloudWrap(bool wrap)
284 {
285     SGCloudField::setWrap(wrap);
286 }
287
288 bool SGSky::get_3dCloudUseImpostors() const {
289     return SGCloudField::getUseImpostors();
290 }
291
292 void SGSky::set_3dCloudUseImpostors(bool imp)
293 {
294     SGCloudField::setUseImpostors(imp);
295 }
296
297 float SGSky::get_minimum_sky_visibility() const 
298 {
299     return minimum_sky_visibility;
300 }
301
302 void SGSky::set_minimum_sky_visibility( float value )
303 {
304     minimum_sky_visibility = value;
305 }
306
307 void SGSky::texture_path( const string& path ) {
308         tex_path = SGPath( path );
309 }
310
311 // modify the current visibility based on cloud layers, thickness,
312 // transition range, and simulated "puffs".
313 void SGSky::modify_vis( float alt, float time_factor ) {
314     float effvis = visibility;
315
316     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
317         float asl = cloud_layers[i]->getElevation_m();
318         float thickness = cloud_layers[i]->getThickness_m();
319         float transition = cloud_layers[i]->getTransition_m();
320
321         double ratio = 1.0;
322
323         if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ) {
324             // less than 50% coverage -- assume we're in the clear for now
325             ratio = 1.0;
326         } else if ( alt < asl - transition ) {
327             // below cloud layer
328             ratio = 1.0;
329         } else if ( alt < asl ) {
330             // in lower transition
331             ratio = (asl - alt) / transition;
332         } else if ( alt < asl + thickness ) {
333             // in cloud layer
334             ratio = 0.0;
335         } else if ( alt < asl + thickness + transition ) {
336             // in upper transition
337             ratio = (alt - (asl + thickness)) / transition;
338         } else {
339             // above cloud layer
340             ratio = 1.0;
341         }
342
343         if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ||
344              cloud_layers[i]->get_layer3D()->isDefined3D()) {
345             // do nothing, clear layers aren't drawn, don't affect
346             // visibility andn dont' need to be faded in or out.
347         } else if ( (cloud_layers[i]->getCoverage() == 
348                      SGCloudLayer::SG_CLOUD_FEW)
349                     || (cloud_layers[i]->getCoverage() ==
350                         SGCloudLayer::SG_CLOUD_SCATTERED) )
351         {
352             // set the alpha fade value for the cloud layer.  For less
353             // dense cloud layers we fade the layer to nothing as we
354             // approach it because we stay clear visibility-wise as we
355             // pass through it.
356             float temp = ratio * 2.0;
357             if ( temp > 1.0 ) { temp = 1.0; }
358             cloud_layers[i]->setAlpha( temp );
359
360             // don't touch visibility
361         } else {
362             // maintain full alpha for denser cloud layer types.
363             // Let's set the value explicitly in case someone changed
364             // the layer type.
365             cloud_layers[i]->setAlpha( 1.0 );
366
367             // lower visibility as we approach the cloud layer.
368             // accumulate effects from multiple cloud layers
369             effvis *= ratio;
370         }
371
372 #if 0
373         if ( ratio < 1.0 ) {
374             if ( ! in_puff ) {
375                 // calc chance of entering cloud puff
376                 double rnd = sg_random();
377                 double chance = rnd * rnd * rnd;
378                 if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
379                     in_puff = true;
380                     puff_length = sg_random() * 2.0; // up to 2 seconds
381                     puff_progression = 0.0;
382                 }
383             }
384
385             if ( in_puff ) {
386                 // modify actual_visibility based on puff envelope
387
388                 if ( puff_progression <= ramp_up ) {
389                     double x = SGD_PI_2 * puff_progression / ramp_up;
390                     double factor = 1.0 - sin( x );
391                     // cout << "ramp up = " << puff_progression
392                     //      << "  factor = " << factor << endl;
393                     effvis = effvis * factor;
394                 } else if ( puff_progression >= ramp_up + puff_length ) {
395                     double x = SGD_PI_2 * 
396                         (puff_progression - (ramp_up + puff_length)) /
397                         ramp_down;
398                     double factor = sin( x );
399                     // cout << "ramp down = " 
400                     //      << puff_progression - (ramp_up + puff_length) 
401                     //      << "  factor = " << factor << endl;
402                     effvis = effvis * factor;
403                 } else {
404                     effvis = 0.0;
405                 }
406
407                 /* cout << "len = " << puff_length
408                    << "  x = " << x 
409                    << "  factor = " << factor
410                    << "  actual_visibility = " << actual_visibility 
411                    << endl; */
412
413                 // time_factor = ( global_multi_loop * 
414                 //                 current_options.get_speed_up() ) /
415                 //                (double)current_options.get_model_hz();
416
417                 puff_progression += time_factor;
418                 // cout << "time factor = " << time_factor << endl;
419
420                 /* cout << "gml = " << global_multi_loop 
421                    << "  speed up = " << current_options.get_speed_up()
422                    << "  hz = " << current_options.get_model_hz() << endl;
423                    */ 
424
425                 if ( puff_progression > puff_length + ramp_up + ramp_down) {
426                     in_puff = false; 
427                 }
428             }
429         }
430 #endif
431
432         // never let visibility drop below the layer's configured visibility
433        effvis = SG_MAX2<float>(cloud_layers[i]->getVisibility_m(), effvis );
434
435     } // for
436
437     effective_visibility = effvis;
438 }
439
440