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