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