@@ -83,6 +83,7 @@ Sky *Sky::s_instance = nullptr;
8383Sky::Sky ()
8484 : coVRPlugin(COVER_PLUGIN_NAME )
8585 , ui::Owner(" Sky" , cover->ui)
86+ , m_mode(DISABLED )
8687{
8788 assert (s_instance == nullptr );
8889 s_instance = this ;
@@ -144,6 +145,7 @@ bool Sky::init()
144145 {
145146 if (selection >= skyListNameStart)
146147 {
148+ m_mode = TEXTURE ;
147149 setSkyEntry (m_skies[selection - skyListNameStart]);
148150 }
149151 else if (selection == 0 )
@@ -387,7 +389,8 @@ void Sky::setSkyEphemeris()
387389void Sky::setSkyAuto ()
388390{
389391 m_mode = AUTO ;
390- // TODO
392+ m_currentAutoSky = nullptr ;
393+ updateAutoSky ();
391394}
392395
393396void Sky::setSkyTexture (std::string_view nameOrFile)
@@ -474,6 +477,43 @@ void Sky::setTrueNorth(float trueNorth)
474477 if (skyNorthSlider)
475478 skyNorthSlider->setValue (northAngle);
476479}
480+
481+ void Sky::updateAutoSky ()
482+ {
483+ auto globalPosition = GeoData::instance ()->getGlobalPosition ();
484+ SkyEntry *closestSky = findClosestSky (globalPosition);
485+ if (!closestSky)
486+ return ;
487+
488+ if (closestSky == m_currentAutoSky)
489+ return ;
490+
491+ m_currentAutoSky = closestSky;
492+ setSkyEntry (*closestSky);
493+ }
494+
495+ SkyEntry *Sky::findClosestSky (const osg::Vec3 &globalPosition)
496+ {
497+ SkyEntry *closestSky = nullptr ;
498+ double closestDistance = std::numeric_limits<double >::max ();
499+
500+ double longitude = globalPosition.x ();
501+ double latitude = globalPosition.y ();
502+
503+ for (auto &sky : m_skies)
504+ {
505+ double dlon = sky.longitude - longitude;
506+ double dlat = sky.latitude - latitude;
507+ double distance = std::sqrt (dlon * dlon + dlat * dlat);
508+ if (distance < closestDistance)
509+ {
510+ closestDistance = distance;
511+ closestSky = &sky;
512+ }
513+ }
514+ return closestSky;
515+ }
516+
477517bool Sky::update ()
478518{
479519 float nearValue = coVRConfig::instance ()->nearClip ();
@@ -488,7 +528,7 @@ bool Sky::update()
488528
489529 if (m_mode == AUTO )
490530 {
491- // TODO: automatically select appropriate sky
531+ updateAutoSky ();
492532 }
493533 else if (m_mode == EPHEMERIS )
494534 {
0 commit comments