diff --git a/include/openspace/interaction/interactionhandler.h b/include/openspace/interaction/interactionhandler.h index a27b27986d..1dcbe52580 100644 --- a/include/openspace/interaction/interactionhandler.h +++ b/include/openspace/interaction/interactionhandler.h @@ -58,6 +58,7 @@ public: void mouseScrollWheelCallback(int pos); private: + glm::vec3 mapToTrackball(glm::vec2 mousePos); Camera *camera_; bool enabled_; @@ -75,4 +76,4 @@ private: } // namespace openspace -#endif \ No newline at end of file +#endif diff --git a/src/interaction/interactionhandler.cpp b/src/interaction/interactionhandler.cpp index 9290b56389..316fabda9b 100644 --- a/src/interaction/interactionhandler.cpp +++ b/src/interaction/interactionhandler.cpp @@ -1,5 +1,6 @@ // open space includes +#include #include #include #include @@ -10,6 +11,8 @@ // std includes #include +std::string _loggerCat = "InteractionHandler"; + namespace openspace { InteractionHandler::InteractionHandler() { @@ -225,6 +228,18 @@ double InteractionHandler::getDt() { return dt_; } +// Implementation of Holroyd's Trackball +glm::vec3 InteractionHandler::mapToTrackball(glm::vec2 mousePos) { + glm::vec3 out = glm::vec3(mousePos.x, mousePos.y, 0); + const float RADIUS = 0.5; // Sphere radius + if (out.x*out.x + out.y*out.y <= RADIUS*RADIUS/2.0) + out.z = sqrt(RADIUS*RADIUS - (out.x*out.x + out.y*out.y)); + else + out.z = (RADIUS*RADIUS/2.0)/sqrt(out.x*out.x + out.y*out.y); + + return out; +} + void InteractionHandler::keyboardCallback(int key, int action) { // TODO package in script const double speed = 2.75; @@ -312,6 +327,11 @@ void InteractionHandler::mouseButtonCallback(int key, int action) { } void InteractionHandler::mousePositionCallback(int x, int y) { + float width = sgct::Engine::instance()->getActiveXResolution(); + float height = sgct::Engine::instance()->getActiveYResolution(); + glm::vec2 mousePos = glm::vec2(((float)x/width)-0.5, ((float)y/height)-0.5); + glm::vec3 trackballPos = mapToTrackball(mousePos); + LDEBUG(mousePos.x << ", " << mousePos.y << " = " << trackballPos.x << ", " << trackballPos.y << ", " << trackballPos.z); //if(mouseControl_ != nullptr) { // mouseControl_->mousePosCallback(x,y); //} @@ -324,4 +344,4 @@ void InteractionHandler::mouseScrollWheelCallback(int pos) { } -} // namespace openspace \ No newline at end of file +} // namespace openspace