diff --git a/include/openspace/interaction/interactionhandler.h b/include/openspace/interaction/interactionhandler.h index 821bdf6003..c9fbde65f5 100644 --- a/include/openspace/interaction/interactionhandler.h +++ b/include/openspace/interaction/interactionhandler.h @@ -39,75 +39,28 @@ namespace interaction { class InteractionHandler { public: - InteractionHandler() - : _camera(nullptr) - , _focusNode(nullptr) - , _keyboardController(nullptr) - , _mouseController(nullptr) - { - } + InteractionHandler(); - ~InteractionHandler() { - delete _keyboardController; - delete _mouseController; - for (size_t i = 0; i < _controllers.size(); ++i) - delete _controllers[i]; + ~InteractionHandler(); - } + void setKeyboardController(KeyboardController* controller); + void setMouseController(MouseController* controller); + void addController(Controller* controller); - void setKeyboardController(KeyboardController* controller) { - delete _keyboardController; - _keyboardController = controller; - _keyboardController->setHandler(this); - } + void lockControls(); + void unlockControls(); - void setMouseController(MouseController* controller) { - delete _mouseController; - _mouseController = controller; - _mouseController->setHandler(this); - } + void update(double deltaTime); - void addController(Controller* controller) { - _controllers.push_back(controller); - controller->setHandler(this); - } + void setFocusNode(SceneGraphNode* node); + const SceneGraphNode* const focusNode() const; + void setCamera(Camera* camera); + const Camera* const camera() const; - void lockControls() { - _mutex.lock(); - } - - void unlockControls() { - _mutex.unlock(); - } - - void update(double deltaTime) { - _deltaTime = deltaTime; - } - - void setFocusNode(SceneGraphNode* node) { - _focusNode = node; - } - - void keyboardCallback(int key, int action) { - if (_keyboardController) - _keyboardController->keyPressed(KeyAction(action), Keys(key)); - } - - void mouseButtonCallback(int button, int action) { - if (_mouseController) - _mouseController->button(MouseAction(action), MouseButton(button)); - } - - void mousePositionCallback(int x, int y) { - if (_mouseController) - // TODO Remap screen coordinates to [0,1] - _mouseController->move(float(x), float(y)); - } - - void mouseScrollWheelCallback(int pos) { - if (_mouseController) - _mouseController->scrollWheel(float(pos)); - } + void keyboardCallback(int key, int action); + void mouseButtonCallback(int button, int action); + void mousePositionCallback(int x, int y); + void mouseScrollWheelCallback(int pos); private: friend class Controller; diff --git a/include/openspace/interaction/keyboardcontroller.h b/include/openspace/interaction/keyboardcontroller.h index e6096478b2..3b8d1b979f 100644 --- a/include/openspace/interaction/keyboardcontroller.h +++ b/include/openspace/interaction/keyboardcontroller.h @@ -39,16 +39,12 @@ public: class KeyboardControllerFixed : public KeyboardController { public: - void keyPressed(KeyAction action, Keys key) { - - } + void keyPressed(KeyAction action, Keys key); }; class KeyboardControllerLua : public KeyboardController { public: - void keyPressed(KeyAction action, Keys key) { - - } + void keyPressed(KeyAction action, Keys key); }; } // namespace interaction diff --git a/include/openspace/interaction/keys.h b/include/openspace/interaction/keys.h index 72f86ddd99..19e722577e 100644 --- a/include/openspace/interaction/keys.h +++ b/include/openspace/interaction/keys.h @@ -162,7 +162,7 @@ enum class Keys { Control = LeftControl | RightControl, Alt = LeftAlt | RightAlt, Super = LeftSuper | RightSuper -} +}; } // namespace interaction } // namespace openspace diff --git a/include/openspace/interaction/mouse.h b/include/openspace/interaction/mouse.h index 6c2d40cc14..2a4cca19d6 100644 --- a/include/openspace/interaction/mouse.h +++ b/include/openspace/interaction/mouse.h @@ -49,7 +49,7 @@ enum class MouseButton { Button7 = SGCT_MOUSE_BUTTON_7, Button8 = SGCT_MOUSE_BUTTON_8, ButtonLast = SGCT_MOUSE_BUTTON_LAST, -} +}; } // namespace interaction } // namespace openspace diff --git a/include/openspace/interaction/mousecontroller.h b/include/openspace/interaction/mousecontroller.h index 29527e600e..64f085d442 100644 --- a/include/openspace/interaction/mousecontroller.h +++ b/include/openspace/interaction/mousecontroller.h @@ -36,10 +36,7 @@ namespace interaction { class MouseController : public Controller { public: - MouseController() - : _lastTrackballPos(0.f) - , _isMouseBeingPressedAndHeld(false) - {} + MouseController(); virtual void button(MouseAction action, MouseButton button) = 0; virtual void move(float x, float y) = 0; @@ -49,107 +46,22 @@ protected: glm::vec3 _lastTrackballPos; bool _isMouseBeingPressedAndHeld; - glm::vec3 mapToTrackball(glm::vec2 mousePos) { - const float RADIUS = 0.5; // Sphere radius - glm::vec3 out = glm::vec3(mousePos.x-0.5, -1.0*(mousePos.y-0.5), 0); + glm::vec3 mapToTrackball(glm::vec2 mousePos); - // Mapping according to Holroyds trackball - // Piece-wise sphere + hyperbolic sheet - if (out.x*out.x + out.y*out.y <= RADIUS*RADIUS/2.0) { - //Spherical Region - out.z = RADIUS*RADIUS - (out.x*out.x + out.y*out.y); - out.z = out.z > 0.0 ? sqrtf(out.z) : 0.0; - } else { //Hyperbolic Region - for smooth z values - out.z = (RADIUS*RADIUS)/(2.0*sqrt(out.x*out.x + out.y*out.y)); - } + glm::vec3 mapToCamera(glm::vec3 trackballPos); - return glm::normalize(out); - } - - glm::vec3 mapToCamera(glm::vec3 trackballPos) { - // return glm::vec3((sgct::Engine::instance()->getActiveViewMatrix() * glm::vec4(trackballPos,0))); - - //Get x,y,z axis vectors of current camera view - glm::vec3 currentViewYaxis = glm::normalize(camera()->lookUpVector()); - psc viewDir = camera()->position() - focusNode()->worldPosition(); - glm::vec3 currentViewZaxis = glm::normalize(viewDir.vec3()); - glm::vec3 currentViewXaxis = glm::normalize(glm::cross(currentViewYaxis, currentViewZaxis)); - - //mapping to camera co-ordinate - currentViewXaxis*=trackballPos.x; - currentViewYaxis*=trackballPos.y; - currentViewZaxis*=trackballPos.z; - return (currentViewXaxis + currentViewYaxis + currentViewZaxis); - } - - void trackballRotate(int x, int y) { - // Normalize mouse coordinates to [0,1] - float width = sgct::Engine::instance()->getActiveXResolution(); - float height = sgct::Engine::instance()->getActiveYResolution(); - glm::vec2 mousePos = glm::vec2((float)x/width, (float)y/height); - - mousePos = glm::clamp(mousePos, -0.5, 1.5); // Ugly fix #1: Camera position becomes NaN on mouse values outside [-0.5, 1.5] - //mousePos[1] = 0.5; // Ugly fix #2: Tempoarily only allow rotation around y - - glm::vec3 curTrackballPos = mapToTrackball(mousePos); - // LDEBUG(mousePos.x << ", " << mousePos.y << " = " << curTrackballPos.x << ", " << curTrackballPos.y << ", " << curTrackballPos.z); - - // Disable movement on the first click for extra smoothness - if (!_isMouseBeingPressedAndHeld) { - _lastTrackballPos = curTrackballPos; - _isMouseBeingPressedAndHeld = true; - } - - if (curTrackballPos != _lastTrackballPos) { - // calculate rotation angle (in radians) - float rotationAngle = glm::angle(curTrackballPos, _lastTrackballPos); - rotationAngle *= deltaTime() * 100.0f; - - // Map trackballpos to camera - // glm::vec3 trackballMappedToCamera = mapToCamera(_lastTrackballPos - curTrackballPos); - // psc currentCamPos = camera_->getPosition(); - // glm::vec3 nextCamPos = currentCamPos.getVec3f() + trackballMappedToCamera; - // glm::vec3 rotationAxis = glm::cross(currentCamPos.getVec3f(), nextCamPos); - - glm::vec3 rotationAxis = glm::cross(_lastTrackballPos, curTrackballPos); - rotationAxis = glm::normalize(rotationAxis); - glm::quat quaternion = glm::angleAxis(rotationAngle, rotationAxis); - - // Apply quaternion to camera - orbitDelta(quaternion); - - _lastTrackballPos = curTrackballPos; - } - } + void trackballRotate(int x, int y); }; class TrackballMouseController : public MouseController { public: - void button(MouseAction action, MouseButton button) { - if (button == MouseButton::Left && action == MouseAction::Press) - _leftMouseButtonDown = true; - else if (button == MouseButton::Left && action == MouseAction::Release) { - _leftMouseButtonDown = false; - _isMouseBeingPressedAndHeld = false; - } - } + TrackballMouseController(); - void move(float x, float y) { - if (_leftMouseButtonDown) - trackballRotate(x,y); - } + void button(MouseAction action, MouseButton button); - void scrollWheel(int pos) { - const double speed = 4.75; - const double dt = deltaTime(); - if (pos < 0) { - PowerScaledScalar dist(speed * dt, 0.0); - distanceDelta(dist); - } else if(pos > 0) { - PowerScaledScalar dist(-speed * dt, 0.0); - distanceDelta(dist); - } - } + void move(float x, float y); + + void scrollWheel(int pos); protected: bool _leftMouseButtonDown; diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index 3106b4d87d..f528652f9c 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -75,7 +75,7 @@ OpenSpaceEngine::OpenSpaceEngine(std::string programName) OpenSpaceEngine::~OpenSpaceEngine() { SpiceManager::deinitialize(); Time::deinitialize(); - DeviceIdentifier::deinit(); + //DeviceIdentifier::deinit(); FileSystem::deinitialize(); LogManager::deinitialize(); } @@ -306,8 +306,8 @@ bool OpenSpaceEngine::initialize() { sceneGraph->scheduleLoadSceneFile(sceneDescriptionPath); // Initialize OpenSpace input devices - DeviceIdentifier::init(); - DeviceIdentifier::ref().scanDevices(); + //DeviceIdentifier::init(); + //DeviceIdentifier::ref().scanDevices(); _interactionHandler.setKeyboardController(new interaction::KeyboardControllerFixed); _interactionHandler.setMouseController(new interaction::TrackballMouseController); diff --git a/src/interaction/externalcontrol/externalcontrol.cpp b/src/interaction/externalcontrol/externalcontrol.cpp index ff37ffd055..9060259c7f 100644 --- a/src/interaction/externalcontrol/externalcontrol.cpp +++ b/src/interaction/externalcontrol/externalcontrol.cpp @@ -17,15 +17,15 @@ void ExternalControl::update() { } void ExternalControl::rotate(const glm::quat &rotation) { - OsEng.interactionHandler().rotate(rotation); + //OsEng.interactionHandler().rotate(rotation); } void ExternalControl::orbit(const glm::quat &rotation) { - OsEng.interactionHandler().orbit(rotation); + //OsEng.interactionHandler().orbit(rotation); } void ExternalControl::distance(const PowerScaledScalar &distance) { - OsEng.interactionHandler().distance(distance); + //OsEng.interactionHandler().distance(distance); } diff --git a/src/interaction/interactionhandler.cpp b/src/interaction/interactionhandler.cpp index d4024dd9d4..bceb05d861 100644 --- a/src/interaction/interactionhandler.cpp +++ b/src/interaction/interactionhandler.cpp @@ -1,26 +1,119 @@ -///***************************************************************************************** -// * * -// * OpenSpace * -// * * -// * Copyright (c) 2014 * -// * * -// * Permission is hereby granted, free of charge, to any person obtaining a copy of this * -// * software and associated documentation files (the "Software"), to deal in the Software * -// * without restriction, including without limitation the rights to use, copy, modify, * -// * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * -// * permit persons to whom the Software is furnished to do so, subject to the following * -// * conditions: * -// * * -// * The above copyright notice and this permission notice shall be included in all copies * -// * or substantial portions of the Software. * -// * * -// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * -// * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * -// * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * -// * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * -// * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * -// * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * -// ****************************************************************************************/ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014 * + * * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this * + * software and associated documentation files (the "Software"), to deal in the Software * + * without restriction, including without limitation the rights to use, copy, modify, * + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * + * permit persons to whom the Software is furnished to do so, subject to the following * + * conditions: * + * * + * The above copyright notice and this permission notice shall be included in all copies * + * or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * + ****************************************************************************************/ + +#include + +namespace openspace { +namespace interaction { + +InteractionHandler::InteractionHandler() + : _camera(nullptr) + , _focusNode(nullptr) + , _keyboardController(nullptr) + , _mouseController(nullptr) +{ +} + +InteractionHandler::~InteractionHandler() { + delete _keyboardController; + delete _mouseController; + for (size_t i = 0; i < _controllers.size(); ++i) + delete _controllers[i]; +} + +void InteractionHandler::setKeyboardController(KeyboardController* controller) { + assert(controller); + delete _keyboardController; + _keyboardController = controller; + _keyboardController->setHandler(this); +} + +void InteractionHandler::setMouseController(MouseController* controller) { + assert(controller); + delete _mouseController; + _mouseController = controller; + _mouseController->setHandler(this); +} + +void InteractionHandler::addController(Controller* controller) { + assert(controller); + _controllers.push_back(controller); + controller->setHandler(this); +} + +void InteractionHandler::lockControls() { + _mutex.lock(); +} + +void InteractionHandler::unlockControls() { + _mutex.unlock(); +} + +void InteractionHandler::update(double deltaTime) { + _deltaTime = deltaTime; +} + +void InteractionHandler::setFocusNode(SceneGraphNode* node) { + _focusNode = node; +} + +const SceneGraphNode* const InteractionHandler::focusNode() const { + return _focusNode; +} + +void InteractionHandler::setCamera(Camera* camera) { + assert(camera); + _camera = camera; +} +const Camera* const InteractionHandler::camera() const { + return _camera; +} + +void InteractionHandler::keyboardCallback(int key, int action) { + if (_keyboardController) + _keyboardController->keyPressed(KeyAction(action), Keys(key)); +} + +void InteractionHandler::mouseButtonCallback(int button, int action) { + if (_mouseController) + _mouseController->button(MouseAction(action), MouseButton(button)); +} + +void InteractionHandler::mousePositionCallback(int x, int y) { + if (_mouseController) + // TODO Remap screen coordinates to [0,1] + _mouseController->move(float(x), float(y)); +} + +void InteractionHandler::mouseScrollWheelCallback(int pos) { + if (_mouseController) + _mouseController->scrollWheel(float(pos)); +} + +} // namespace interaction +} // namespace openspace + // //// open space includes //#include diff --git a/src/interaction/keyboardcontroller.cpp b/src/interaction/keyboardcontroller.cpp index e69de29bb2..6aeb91d383 100644 --- a/src/interaction/keyboardcontroller.cpp +++ b/src/interaction/keyboardcontroller.cpp @@ -0,0 +1,152 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014 * + * * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this * + * software and associated documentation files (the "Software"), to deal in the Software * + * without restriction, including without limitation the rights to use, copy, modify, * + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * + * permit persons to whom the Software is furnished to do so, subject to the following * + * conditions: * + * * + * The above copyright notice and this permission notice shall be included in all copies * + * or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * + ****************************************************************************************/ + +#include + +#include +#include + +namespace openspace { +namespace interaction { + +void KeyboardControllerFixed::keyPressed(KeyAction action, Keys key) { + // TODO package in script + const double speed = 2.75; + const double dt = deltaTime(); + if(action == KeyAction::Press|| action == KeyAction::Repeat) { + if (key == Keys::S) { + glm::vec3 euler(speed * dt, 0.0, 0.0); + glm::quat rot = glm::quat(euler); + orbitDelta(rot); + } + if (key == Keys::W) { + glm::vec3 euler(-speed * dt, 0.0, 0.0); + glm::quat rot = glm::quat(euler); + orbitDelta(rot); + } + if (key == Keys::A) { + glm::vec3 euler(0.0, -speed * dt, 0.0); + glm::quat rot = glm::quat(euler); + orbitDelta(rot); + } + if (key == Keys::D) { + glm::vec3 euler(0.0, speed * dt, 0.0); + glm::quat rot = glm::quat(euler); + orbitDelta(rot); + } + if (key == Keys::Q) { + Time::ref().advanceTime(dt); + } + if (key == Keys::Right) { + glm::vec3 euler(0.0, speed * dt, 0.0); + glm::quat rot = glm::quat(euler); + rotateDelta(rot); + } + if (key == Keys::Left) { + glm::vec3 euler(0.0, -speed * dt, 0.0); + glm::quat rot = glm::quat(euler); + rotateDelta(rot); + } + if (key == Keys::Down) { + glm::vec3 euler(speed * dt, 0.0, 0.0); + glm::quat rot = glm::quat(euler); + rotateDelta(rot); + } + if (key == Keys::Up) { + glm::vec3 euler(-speed * dt, 0.0, 0.0); + glm::quat rot = glm::quat(euler); + rotateDelta(rot); + } + if (key == Keys::R) { + PowerScaledScalar dist(-speed * dt, 0.0); + distanceDelta(dist); + } + if (key == Keys::F) { + PowerScaledScalar dist(speed * dt, 0.0); + distanceDelta(dist); + } + if (key == Keys::T) { + PowerScaledScalar dist(-speed * pow(10, 11) * dt, 0.0); + distanceDelta(dist); + } + //if (key == Keys::G) { + // acc += 0.001; + // PowerScaledScalar dist(speed * pow(10, 8 * acc) * dt, 0.0); + // distanceDelta(dist); + //} + if (key == Keys::Y) { + PowerScaledScalar dist(-speed * 100.0 * dt, 6.0); + distanceDelta(dist); + } + if (key == Keys::H) { + PowerScaledScalar dist(speed * 100.0 * dt, 6.0); + distanceDelta(dist); + } + + if (key == Keys::KeypadSubtract) { + glm::vec2 s = OsEng.renderEngine().camera()->scaling(); + s[1] -= 0.5; + OsEng.renderEngine().camera()->setScaling(s); + } + if (key == Keys::KeypadAdd) { + glm::vec2 s = OsEng.renderEngine().camera()->scaling(); + s[1] += 0.5; + OsEng.renderEngine().camera()->setScaling(s); + } + } + /* + if (key == '1') { + SceneGraphNode* node = getSceneGraphNode("sun"); + + setFocusNode(node); + getCamera()->setPosition(node->getWorldPosition() + psc(0.0, 0.0, 0.5, 10.0)); + getCamera()->setCameraDirection(glm::vec3(0.0, 0.0, -1.0)); + } + + if (key == '2') { + SceneGraphNode* node = getSceneGraphNode("earth"); + + setFocusNode(node); + getCamera()->setPosition(node->getWorldPosition() + psc(0.0, 0.0, 1.0, 8.0)); + getCamera()->setCameraDirection(glm::vec3(0.0, 0.0, -1.0)); + } + + + if (key == '3') { + SceneGraphNode* node = getSceneGraphNode("moon"); + + setFocusNode(node); + getCamera()->setPosition(node->getWorldPosition() + psc(0.0, 0.0, 0.5, 8.0)); + getCamera()->setCameraDirection(glm::vec3(0.0, 0.0, -1.0)); + } + */ +} + +void KeyboardControllerLua::keyPressed(KeyAction action, Keys key) +{ + +} + +} // namespace interaction +} // namespace openspace \ No newline at end of file diff --git a/src/interaction/mousecontroller.cpp b/src/interaction/mousecontroller.cpp index 4a918dcff7..35a58c93f7 100644 --- a/src/interaction/mousecontroller.cpp +++ b/src/interaction/mousecontroller.cpp @@ -22,3 +22,122 @@ * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ +#include + +namespace openspace { +namespace interaction { + +MouseController::MouseController() + : _lastTrackballPos(0.f) + , _isMouseBeingPressedAndHeld(false) +{} + +glm::vec3 MouseController::mapToTrackball(glm::vec2 mousePos) { + const float RADIUS = 0.5; // Sphere radius + glm::vec3 out = glm::vec3(mousePos.x - 0.5, -1.0*(mousePos.y - 0.5), 0); + + // Mapping according to Holroyds trackball + // Piece-wise sphere + hyperbolic sheet + if (out.x*out.x + out.y*out.y <= RADIUS*RADIUS / 2.0) { + //Spherical Region + out.z = RADIUS*RADIUS - (out.x*out.x + out.y*out.y); + out.z = out.z > 0.0 ? sqrtf(out.z) : 0.0; + } + else { //Hyperbolic Region - for smooth z values + out.z = (RADIUS*RADIUS) / (2.0*sqrt(out.x*out.x + out.y*out.y)); + } + + return glm::normalize(out); +} + +glm::vec3 MouseController::mapToCamera(glm::vec3 trackballPos) { + // return glm::vec3((sgct::Engine::instance()->getActiveViewMatrix() * glm::vec4(trackballPos,0))); + + //Get x,y,z axis vectors of current camera view + glm::vec3 currentViewYaxis = glm::normalize(camera()->lookUpVector()); + psc viewDir = camera()->position() - focusNode()->worldPosition(); + glm::vec3 currentViewZaxis = glm::normalize(viewDir.vec3()); + glm::vec3 currentViewXaxis = glm::normalize(glm::cross(currentViewYaxis, currentViewZaxis)); + + //mapping to camera co-ordinate + currentViewXaxis *= trackballPos.x; + currentViewYaxis *= trackballPos.y; + currentViewZaxis *= trackballPos.z; + return (currentViewXaxis + currentViewYaxis + currentViewZaxis); +} + +void MouseController::trackballRotate(int x, int y) { + // Normalize mouse coordinates to [0,1] + float width = sgct::Engine::instance()->getActiveXResolution(); + float height = sgct::Engine::instance()->getActiveYResolution(); + glm::vec2 mousePos = glm::vec2((float)x / width, (float)y / height); + + mousePos = glm::clamp(mousePos, -0.5, 1.5); // Ugly fix #1: Camera position becomes NaN on mouse values outside [-0.5, 1.5] + //mousePos[1] = 0.5; // Ugly fix #2: Tempoarily only allow rotation around y + + glm::vec3 curTrackballPos = mapToTrackball(mousePos); + // LDEBUG(mousePos.x << ", " << mousePos.y << " = " << curTrackballPos.x << ", " << curTrackballPos.y << ", " << curTrackballPos.z); + + // Disable movement on the first click for extra smoothness + if (!_isMouseBeingPressedAndHeld) { + _lastTrackballPos = curTrackballPos; + _isMouseBeingPressedAndHeld = true; + } + + if (curTrackballPos != _lastTrackballPos) { + // calculate rotation angle (in radians) + float rotationAngle = glm::angle(curTrackballPos, _lastTrackballPos); + rotationAngle *= deltaTime() * 100.0f; + + // Map trackballpos to camera + // glm::vec3 trackballMappedToCamera = mapToCamera(_lastTrackballPos - curTrackballPos); + // psc currentCamPos = camera_->getPosition(); + // glm::vec3 nextCamPos = currentCamPos.getVec3f() + trackballMappedToCamera; + // glm::vec3 rotationAxis = glm::cross(currentCamPos.getVec3f(), nextCamPos); + + glm::vec3 rotationAxis = glm::cross(_lastTrackballPos, curTrackballPos); + rotationAxis = glm::normalize(rotationAxis); + glm::quat quaternion = glm::angleAxis(rotationAngle, rotationAxis); + + // Apply quaternion to camera + orbitDelta(quaternion); + + _lastTrackballPos = curTrackballPos; + } +} + + +TrackballMouseController::TrackballMouseController() + : MouseController() + , _leftMouseButtonDown(false) +{} + +void TrackballMouseController::button(MouseAction action, MouseButton button) { + if (button == MouseButton::Left && action == MouseAction::Press) + _leftMouseButtonDown = true; + else if (button == MouseButton::Left && action == MouseAction::Release) { + _leftMouseButtonDown = false; + _isMouseBeingPressedAndHeld = false; + } +} + +void TrackballMouseController::move(float x, float y) { + if (_leftMouseButtonDown) + trackballRotate(x, y); +} + +void TrackballMouseController::scrollWheel(int pos) { + const double speed = 4.75; + const double dt = deltaTime(); + if (pos < 0) { + PowerScaledScalar dist(speed * dt, 0.0); + distanceDelta(dist); + } + else if (pos > 0) { + PowerScaledScalar dist(-speed * dt, 0.0); + distanceDelta(dist); + } +} + +} // namespace interaction +} // namespace openspace diff --git a/src/rendering/renderengine.cpp b/src/rendering/renderengine.cpp index 04d3617fa8..a30493002e 100644 --- a/src/rendering/renderengine.cpp +++ b/src/rendering/renderengine.cpp @@ -257,7 +257,7 @@ void RenderEngine::render() const glm::vec2 scaling = _mainCamera->scaling(); const glm::vec3 viewdirection = _mainCamera->viewDirection(); const psc position = _mainCamera->position(); - const psc origin = OsEng.interactionHandler().getOrigin(); + const psc origin = OsEng.interactionHandler().focusNode()->worldPosition(); const PowerScaledScalar pssl = (position - origin).length(); /* GUI PRINT */