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