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