]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/sky/sky.cxx
Replace OpenThreads with SGThread to avoid useless OSG dependency.
[simgear.git] / simgear / scene / sky / sky.cxx
index ca2c0004b4865c38dc28eba284d3f28bedc10d8a..f22a9a6a5a906389c286304e46d566b079c4f9a0 100644 (file)
@@ -31,6 +31,7 @@
 
 #include <simgear/math/sg_random.h>
 #include <simgear/scene/util/RenderConstants.hxx>
+#include <simgear/sg_inlines.h>
 
 #include <osg/StateSet>
 #include <osg/Depth>
@@ -62,7 +63,9 @@ SGSky::SGSky( void ) {
 
     pre_selector = new osg::Switch;
 
-    pre_transform = new osg::MatrixTransform;
+    pre_transform = new osg::Group;
+
+    _ephTransform = new osg::MatrixTransform;
 }
 
 
@@ -76,23 +79,23 @@ SGSky::~SGSky( void )
 // the provided branch
 void SGSky::build( double h_radius_m, double v_radius_m,
                    double sun_size, double moon_size,
-                  int nplanets, SGVec3d planet_data[7],
-                  int nstars, SGVec3d star_data[], SGPropertyNode *property_tree_node )
+                   const SGEphemeris& eph, SGPropertyNode *property_tree_node )
 {
     dome = new SGSkyDome;
     pre_transform->addChild( dome->build( h_radius_m, v_radius_m ) );
 
+    pre_transform->addChild(_ephTransform.get());
     planets = new SGStars;
-    pre_transform->addChild(planets->build(nplanets, planet_data, h_radius_m));
+    _ephTransform->addChild( planets->build(eph.getNumPlanets(), eph.getPlanets(), h_radius_m) );
 
     stars = new SGStars;
-    pre_transform->addChild( stars->build(nstars, star_data, h_radius_m) );
+    _ephTransform->addChild( stars->build(eph.getNumStars(), eph.getStars(), h_radius_m) );
     
     moon = new SGMoon;
-    pre_transform->addChild( moon->build(tex_path, moon_size) );
+    _ephTransform->addChild( moon->build(tex_path, moon_size) );
 
     oursun = new SGSun;
-    pre_transform->addChild( oursun->build(tex_path, sun_size, property_tree_node ) );
+    _ephTransform->addChild( oursun->build(tex_path, sun_size, property_tree_node ) );
 
     pre_selector->addChild( pre_transform.get() );
 
@@ -107,15 +110,15 @@ void SGSky::build( double h_radius_m, double v_radius_m,
 // 0 degrees = high noon
 // 90 degrees = sun rise/set
 // 180 degrees = darkest midnight
-bool SGSky::repaint( const SGSkyColor &sc )
+bool SGSky::repaint( const SGSkyColor &sc, const SGEphemeris& eph )
 {
     if ( effective_visibility > 1000.0 ) {
        enable();
-       dome->repaint( sc.sky_color, sc.fog_color, sc.sun_angle,
-                       effective_visibility );
+       dome->repaint( sc.adj_sky_color, sc.sky_color, sc.fog_color,
+                       sc.sun_angle, effective_visibility );
 
-        stars->repaint( sc.sun_angle, sc.nstars, sc.star_data );
-        planets->repaint( sc.sun_angle, sc.nplanets, sc.planet_data );
+        stars->repaint( sc.sun_angle, eph.getNumStars(), eph.getStars() );
+        planets->repaint( sc.sun_angle, eph.getNumPlanets(), eph.getPlanets() );
        oursun->repaint( sc.sun_angle, effective_visibility );
        moon->repaint( sc.moon_angle );
 
@@ -129,11 +132,10 @@ bool SGSky::repaint( const SGSkyColor &sc )
        disable();
     }
     SGCloudField::updateFog((double)effective_visibility,
-                            osg::Vec4f(sc.fog_color.osg(), 1.0f));
+                            osg::Vec4f(toOsg(sc.fog_color), 1.0f));
     return true;
 }
 
-
 // reposition the sky at the specified origin and orientation
 //
 // lon specifies a rotation about the Z axis
@@ -141,29 +143,48 @@ bool SGSky::repaint( const SGSkyColor &sc )
 // spin specifies a rotation about the new Z axis (this allows
 // additional orientation for the sunrise/set effects and is used by
 // the skydome and perhaps clouds.
-bool SGSky::reposition( SGSkyState &st, double dt )
+bool SGSky::reposition( const SGSkyState &st, const SGEphemeris& eph, double dt )
 {
-
     double angle = st.gst * 15;        // degrees
+    double angleRad = SGMiscd::deg2rad(angle);
+
+    SGVec3f zero_elev, view_up;
+    double lon, lat, alt;
 
-    dome->reposition( st.zero_elev, st.alt, st.lon, st.lat, st.spin );
+    SGGeod geodZeroViewPos = SGGeod::fromGeodM(st.pos_geod, 0);
+    zero_elev = toVec3f( SGVec3d::fromGeod(geodZeroViewPos) );
 
-    stars->reposition( st.view_pos, angle );
-    planets->reposition( st.view_pos, angle );
+    // calculate the scenery up vector
+    SGQuatd hlOr = SGQuatd::fromLonLat(st.pos_geod);
+    view_up = toVec3f(hlOr.backTransform(-SGVec3d::e3()));
 
-    oursun->reposition( st.view_pos, angle,
-                        st.sun_ra, st.sun_dec, st.sun_dist, st.lat, st.alt, st.sun_angle );
+    // viewer location
+    lon = st.pos_geod.getLongitudeRad();
+    lat = st.pos_geod.getLatitudeRad();
+    alt = st.pos_geod.getElevationM();
 
-    moon->reposition( st.view_pos, angle,
-                      st.moon_ra, st.moon_dec, st.moon_dist );
+    dome->reposition( zero_elev, alt, lon, lat, st.spin );
+
+    osg::Matrix m = osg::Matrix::rotate(angleRad, osg::Vec3(0, 0, -1));
+    m.postMultTranslate(toOsg(st.pos));
+    _ephTransform->setMatrix(m);
+
+    double sun_ra = eph.getSunRightAscension();
+    double sun_dec = eph.getSunDeclination();
+    oursun->reposition( sun_ra, sun_dec, st.sun_dist, lat, alt, st.sun_angle );
+
+    double moon_ra = eph.getMoonRightAscension();
+    double moon_dec = eph.getMoonDeclination();
+    moon->reposition( moon_ra, moon_dec, st.moon_dist );
 
     for ( unsigned i = 0; i < cloud_layers.size(); ++i ) {
-        if ( cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR ) {
-            cloud_layers[i]->reposition( st.zero_elev, st.view_up,
-                                         st.lon, st.lat, st.alt, dt );
-        } else
+        if ( cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR ||
+               cloud_layers[i]->get_layer3D()->isDefined3D() ) {
+            cloud_layers[i]->reposition( zero_elev, view_up, lon, lat, alt, dt);
+        } else {
           cloud_layers[i]->getNode()->setAllChildrenOff();
     }
+    }
 
     return true;
 }
@@ -216,6 +237,16 @@ void SGSky::set_3dCloudVisRange(float vis)
     }
 }
 
+bool SGSky::get_3dCloudWrap() const {
+    return SGCloudField::getWrap();
+}
+
+void SGSky::set_3dCloudWrap(bool wrap)
+{
+    SGCloudField::setWrap(wrap);
+}
+
+
 void SGSky::texture_path( const string& path ) {
        tex_path = SGPath( path );
 }
@@ -253,7 +284,7 @@ void SGSky::modify_vis( float alt, float time_factor ) {
        }
 
         if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ||
-             cloud_layers[i]->get_layer3D()->defined3D) {
+             cloud_layers[i]->get_layer3D()->isDefined3D()) {
             // do nothing, clear layers aren't drawn, don't affect
             // visibility andn dont' need to be faded in or out.
         } else if ( (cloud_layers[i]->getCoverage() == 
@@ -341,10 +372,8 @@ void SGSky::modify_vis( float alt, float time_factor ) {
        }
 #endif
 
-        // never let visibility drop below 25 meters
-        if ( effvis <= 25.0 ) {
-            effvis = 25.0;
-        }
+        // never let visibility drop below the layer's configured visibility
+       effvis = SG_MAX2<float>(cloud_layers[i]->getVisibility_m(), effvis );
 
     } // for