Skip to content

Commit 5541356

Browse files
committed
Sky: implement AUTO mode
1 parent e3ff197 commit 5541356

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

src/OpenCOVER/plugins/general/Sky/Sky.cpp

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Sky *Sky::s_instance = nullptr;
8383
Sky::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()
387389
void Sky::setSkyAuto()
388390
{
389391
m_mode = AUTO;
390-
// TODO
392+
m_currentAutoSky = nullptr;
393+
updateAutoSky();
391394
}
392395

393396
void 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+
477517
bool 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
{

src/OpenCOVER/plugins/general/Sky/Sky.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,15 @@ class Sky : public opencover::coVRPlugin, public opencover::ui::Owner
8484
void setSkyEntry(SkyEntry &sky);
8585
void setSkyEphemeris();
8686
void setSkyAuto();
87+
void updateAutoSky();
88+
SkyEntry *findClosestSky(const osg::Vec3 &globalPosition);
8789
void removeExistingSky();
8890

8991
static Sky *s_instance;
9092
std::vector<SkyEntry> m_skies;
9193

94+
SkyEntry *m_currentAutoSky = nullptr;
95+
9296
osg::ref_ptr<osg::MatrixTransform> skyRootNode;
9397
osg::ref_ptr<osg::Geode> texturedSphere;
9498

0 commit comments

Comments
 (0)