From d67c9ffe8d1f8436a15f826eab6fc45d58bd142d Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Sun, 12 Oct 2014 23:51:33 +0200 Subject: [PATCH] Started changes for InteractionManager --- include/openspace/engine/openspaceengine.h | 4 +- include/openspace/interaction/controller.h | 58 ++ .../interaction/interactionhandler.h | 176 ++-- .../interaction/keyboardcontroller.h | 57 ++ include/openspace/interaction/keys.h | 170 ++++ include/openspace/interaction/mouse.h | 57 ++ .../openspace/interaction/mousecontroller.h | 45 + src/engine/openspaceengine.cpp | 4 +- src/interaction/controller.cpp | 0 src/interaction/interactionhandler.cpp | 944 +++++++++--------- src/interaction/keyboardcontroller.cpp | 0 src/interaction/mousecontroller.cpp | 24 + src/rendering/renderengine.cpp | 3 - src/scenegraph/scenegraphnode.cpp | 10 +- 14 files changed, 1004 insertions(+), 548 deletions(-) create mode 100644 include/openspace/interaction/controller.h create mode 100644 include/openspace/interaction/keyboardcontroller.h create mode 100644 include/openspace/interaction/keys.h create mode 100644 include/openspace/interaction/mouse.h create mode 100644 include/openspace/interaction/mousecontroller.h create mode 100644 src/interaction/controller.cpp create mode 100644 src/interaction/keyboardcontroller.cpp create mode 100644 src/interaction/mousecontroller.cpp diff --git a/include/openspace/engine/openspaceengine.h b/include/openspace/engine/openspaceengine.h index 4973bb4606..e39319db15 100644 --- a/include/openspace/engine/openspaceengine.h +++ b/include/openspace/engine/openspaceengine.h @@ -72,7 +72,7 @@ public: ConfigurationManager& configurationManager(); ghoul::opencl::CLContext& clContext(); - InteractionHandler& interactionHandler(); + interaction::InteractionHandler& interactionHandler(); RenderEngine& renderEngine(); scripting::ScriptEngine& scriptEngine(); @@ -100,7 +100,7 @@ private: static OpenSpaceEngine* _engine; ConfigurationManager _configurationManager; - InteractionHandler _interactionHandler; + interaction::InteractionHandler _interactionHandler; RenderEngine _renderEngine; scripting::ScriptEngine _scriptEngine; ghoul::cmdparser::CommandlineParser _commandlineParser; diff --git a/include/openspace/interaction/controller.h b/include/openspace/interaction/controller.h new file mode 100644 index 0000000000..a98b9dd557 --- /dev/null +++ b/include/openspace/interaction/controller.h @@ -0,0 +1,58 @@ +/***************************************************************************************** + * * + * 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. * + ****************************************************************************************/ + +#ifndef __CONTROLLER_H__ +#define __CONTROLLER_H__ + +namespace openspace { +namespace interaction { + +class InteractionHandler; + +class Controller { +public: + void setHandler(InteractionHandler* handler) { + _handler = handler; + } + +protected: + void orbitDelta(const glm::quat& rotation) { + } + void rotateDelta(const glm::quat& rotation) { + } + void distanceDelta(const PowerScaledScalar& distance) { + } + void lookAt(const glm::quat& rotation) { + } + void setRotation(const glm::quat& rotation) { + } + +private: + InteractionHandler* _handler; +}; + +} // namespace interaction +} // namespace openspace + +#endif // __CONTROLLER_H__ \ No newline at end of file diff --git a/include/openspace/interaction/interactionhandler.h b/include/openspace/interaction/interactionhandler.h index 80b867dd91..26f15ed6c7 100644 --- a/include/openspace/interaction/interactionhandler.h +++ b/include/openspace/interaction/interactionhandler.h @@ -1,91 +1,137 @@ -#ifndef INTERACTIONHANDLER_H -#define INTERACTIONHANDLER_H +/***************************************************************************************** + * * + * 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. * + ****************************************************************************************/ -// open space includes -#include -#include -#include +#ifndef __INTERACTIONHANDLER_H__ +#define __INTERACTIONHANDLER_H__ + +#include +#include -// std includes -#include -#include #include -#include -#include -#include namespace openspace { +class Camera; +class SceneGraphNode; + +namespace interaction { + class InteractionHandler { public: - InteractionHandler(void); - InteractionHandler(const InteractionHandler& src); - InteractionHandler& operator=(const InteractionHandler& rhs); - virtual ~InteractionHandler(); + InteractionHandler() + : _camera(nullptr) + , _focusNode(nullptr) + , _keyboardController(nullptr) + , _mouseController(nullptr) + { + } - //static void init(); - //static void deinit(); - // static InteractionHandler& ref(); - //static bool isInitialized(); + ~InteractionHandler() { + delete _keyboardController; + delete _mouseController; + for (size_t i = 0; i < _controllers.size(); ++i) + delete _controllers[i]; - void enable(); - void disable(); - const bool isEnabled() const; + } - void connectDevices(); - void addExternalControl(ExternalControl* controller); + void setKeyboardController(KeyboardController* controller) { + delete _keyboardController; + _keyboardController = controller; + _keyboardController->setHandler(this); + } - void setCamera(Camera *camera = nullptr); - Camera * getCamera() const; - const psc getOrigin() const; - void lockControls(); - void unlockControls(); + void setMouseController(MouseController* controller) { + delete _mouseController; + _mouseController = controller; + _mouseController->setHandler(this); + } - void setFocusNode(SceneGraphNode *node); - - void orbit(const glm::quat &rotation); - void rotate(const glm::quat &rotation); - void distance(const PowerScaledScalar &distance); + void addController(Controller* controller) { + _controllers.push_back(controller); + controller->setHandler(this); + } - void lookAt(const glm::quat &rotation); - void setRotation(const glm::quat &rotation); + void lockControls() { + _mutex.lock(); + } - void update(const double dt); + void unlockControls() { + _mutex.unlock(); + } - double getDt(); + void update(double deltaTime) { + _deltaTime = deltaTime; + } - void keyboardCallback(int key, int action); - void mouseButtonCallback(int key, int action); - void mousePositionCallback(int x, int y); - void mouseScrollWheelCallback(int pos); + 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 addKeyCallback(int key, std::function f); - private: - glm::vec3 mapToTrackball(glm::vec2 mousePos); - glm::vec3 mapToCamera(glm::vec3 trackballPos); - void trackballRotate(int x, int y); + InteractionHandler(const InteractionHandler&) = delete; + InteractionHandler& operator=(const InteractionHandler&) = delete; + InteractionHandler(InteractionHandler&&) = delete; + InteractionHandler& operator=(InteractionHandler&&) = delete; - Camera* camera_; - bool enabled_; - SceneGraphNode *node_; - - double dt_; + Camera* _camera; + SceneGraphNode* _focusNode; + + double _deltaTime; + std::mutex _mutex; + + KeyboardController* _keyboardController; + MouseController* _mouseController; + std::vector _controllers; - glm::vec3 _lastTrackballPos; - bool _leftMouseButtonDown, _isMouseBeingPressedAndHeld; - - // used for calling when updating and deallocation - std::vector controllers_; - - // for locking and unlocking - std::mutex cameraGuard_; - - std::multimap > _keyCallbacks; - + // glm::vec3 mapToTrackball(glm::vec2 mousePos); + // glm::vec3 mapToCamera(glm::vec3 trackballPos); + // void trackballRotate(int x, int y); }; +} // namespace interaction } // namespace openspace -#endif +#endif // __INTERACTIONHANDLER_H__ diff --git a/include/openspace/interaction/keyboardcontroller.h b/include/openspace/interaction/keyboardcontroller.h new file mode 100644 index 0000000000..22314015ea --- /dev/null +++ b/include/openspace/interaction/keyboardcontroller.h @@ -0,0 +1,57 @@ +/***************************************************************************************** + * * + * 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. * + ****************************************************************************************/ + +#ifndef __KEYBOARDCONTROLLER_H__ +#define __KEYBOARDCONTROLLER_H__ + +#include + +#include + +namespace openspace { +namespace interaction { + +class KeyboardController : public Controller { +public: + virtual void keyPressed(KeyAction action, Keys key) = 0; +}; + +class KeyboardControllerFixed : public KeyboardController { +public: + void keyPressed(KeyAction action, Keys key) = 0 { + + } +}; + +class KeyboardControllerLua : public KeyboardController { +public: + void keyPressed(KeyAction action, Keys key) { + + } +}; + +} // namespace interaction +} // namespace openspace + +#endif // __KEYBOARDCONTROLLER_H__ diff --git a/include/openspace/interaction/keys.h b/include/openspace/interaction/keys.h new file mode 100644 index 0000000000..72f86ddd99 --- /dev/null +++ b/include/openspace/interaction/keys.h @@ -0,0 +1,170 @@ +/***************************************************************************************** + * * + * 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. * + ****************************************************************************************/ + +#ifndef __KEYS_H__ +#define __KEYS_H__ + +#include + +namespace openspace { +namespace interaction { + +enum class KeyAction { + Press = SGCT_PRESS, + Release = SGCT_RELEASE, + Repeat = SGCT_REPEAT +}; + +enum class Keys { + Unknown = SGCT_KEY_UNKNOWN, + Space = SGCT_KEY_SPACE, + Apostrophe = SGCT_KEY_APOSTROPHE, + Comma = SGCT_KEY_COMMA, + Minus = SGCT_KEY_MINUS, + Period = SGCT_KEY_PERIOD, + Slash = SGCT_KEY_SLASH, + Num0 = SGCT_KEY_0, + Num1 = SGCT_KEY_1, + Num2 = SGCT_KEY_2, + Num3 = SGCT_KEY_3, + Num4 = SGCT_KEY_4, + Num5 = SGCT_KEY_5, + Num6 = SGCT_KEY_6, + Num7 = SGCT_KEY_7, + Num8 = SGCT_KEY_8, + Num9 = SGCT_KEY_9, + SemiColon = SGCT_KEY_SEMICOLON, + Equal = SGCT_KEY_EQUAL, + A = SGCT_KEY_A, + B = SGCT_KEY_B, + C = SGCT_KEY_C, + D = SGCT_KEY_D, + E = SGCT_KEY_E, + F = SGCT_KEY_F, + G = SGCT_KEY_G, + H = SGCT_KEY_H, + I = SGCT_KEY_I, + J = SGCT_KEY_J, + K = SGCT_KEY_K, + L = SGCT_KEY_L, + M = SGCT_KEY_M, + N = SGCT_KEY_N, + O = SGCT_KEY_O, + P = SGCT_KEY_P, + Q = SGCT_KEY_Q, + R = SGCT_KEY_R, + S = SGCT_KEY_S, + T = SGCT_KEY_T, + U = SGCT_KEY_U, + V = SGCT_KEY_V, + W = SGCT_KEY_W, + X = SGCT_KEY_X, + Y = SGCT_KEY_Y, + Z = SGCT_KEY_Z, + LeftBracket = SGCT_KEY_LEFT_BRACKET, + BackSlash = SGCT_KEY_BACKSLASH, + RightBracket = SGCT_KEY_RIGHT_BRACKET, + GraveAccent = SGCT_KEY_GRAVE_ACCENT, + World1 = SGCT_KEY_WORLD_1, + World2 = SGCT_KEY_WORLD_2, + Escape = SGCT_KEY_ESC, + Enter = SGCT_KEY_ENTER, + Tab = SGCT_KEY_TAB, + BackSpace = SGCT_KEY_BACKSPACE, + Insert = SGCT_KEY_INSERT, + Delete = SGCT_KEY_DELETE, + Right = SGCT_KEY_RIGHT, + Left = SGCT_KEY_LEFT, + Down = SGCT_KEY_DOWN, + Up = SGCT_KEY_UP, + PageUp = SGCT_KEY_PAGE_UP, + PageDown = SGCT_KEY_PAGE_DOWN, + Home = SGCT_KEY_HOME, + End = SGCT_KEY_END, + CapsLock = SGCT_KEY_CAPS_LOCK, + ScrollLock = SGCT_KEY_SCROLL_LOCK, + NumLock = SGCT_KEY_NUM_LOCK, + PrintScreen = SGCT_KEY_PRINT_SCREEN, + Pause = SGCT_KEY_PAUSE, + F1 = SGCT_KEY_F1, + F2 = SGCT_KEY_F2, + F3 = SGCT_KEY_F3, + F4 = SGCT_KEY_F4, + F5 = SGCT_KEY_F5, + F6 = SGCT_KEY_F6, + F7 = SGCT_KEY_F7, + F8 = SGCT_KEY_F8, + F9 = SGCT_KEY_F9, + F10 = SGCT_KEY_F10, + F11 = SGCT_KEY_F11, + F12 = SGCT_KEY_F12, + F13 = SGCT_KEY_F13, + F14 = SGCT_KEY_F14, + F15 = SGCT_KEY_F15, + F16 = SGCT_KEY_F16, + F17 = SGCT_KEY_F17, + F18 = SGCT_KEY_F18, + F19 = SGCT_KEY_F19, + F20 = SGCT_KEY_F20, + F21 = SGCT_KEY_F21, + F22 = SGCT_KEY_F22, + F23 = SGCT_KEY_F23, + F24 = SGCT_KEY_F24, + F25 = SGCT_KEY_F25, + Keypad0 = SGCT_KEY_KP_0, + Keypad1 = SGCT_KEY_KP_1, + Keypad2 = SGCT_KEY_KP_2, + Keypad3 = SGCT_KEY_KP_3, + Keypad4 = SGCT_KEY_KP_4, + Keypad5 = SGCT_KEY_KP_5, + Keypad6 = SGCT_KEY_KP_6, + Keypad7 = SGCT_KEY_KP_7, + Keypad8 = SGCT_KEY_KP_8, + Keypad9 = SGCT_KEY_KP_9, + KeypadDecimal = SGCT_KEY_KP_DECIMAL, + KeypadDivide = SGCT_KEY_KP_DIVIDE, + KeypadMultiply = SGCT_KEY_KP_MULTIPLY, + KeypadSubtract = SGCT_KEY_KP_SUBTRACT, + KeypadAdd = SGCT_KEY_KP_ADD, + KeypadEnter = SGCT_KEY_KP_ENTER, + LeftShift = SGCT_KEY_LEFT_SHIFT, + LeftControl = SGCT_KEY_LEFT_CONTROL, + LeftAlt = SGCT_KEY_LEFT_ALT, + LeftSuper = SGCT_KEY_LEFT_SUPER, + RightShift = SGCT_KEY_RIGHT_SHIFT, + RightControl = SGCT_KEY_RIGHT_CONTROL, + RightAlt = SGCT_KEY_RIGHT_ALT, + RightSuper = SGCT_KEY_RIGHT_SUPER, + Menu = SGCT_KEY_MENU, + Last = SGCT_KEY_LAST, + Shift = LeftShift | RightShift, + Control = LeftControl | RightControl, + Alt = LeftAlt | RightAlt, + Super = LeftSuper | RightSuper +} + +} // namespace interaction +} // namespace openspace + +#endif // __KEYS_H__ diff --git a/include/openspace/interaction/mouse.h b/include/openspace/interaction/mouse.h new file mode 100644 index 0000000000..6c2d40cc14 --- /dev/null +++ b/include/openspace/interaction/mouse.h @@ -0,0 +1,57 @@ +/***************************************************************************************** + * * + * 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. * + ****************************************************************************************/ + +#ifndef __MOUSE_H__ +#define __MOUSE_H__ + +#include + +namespace openspace { +namespace interaction { + +enum class MouseAction { + Press = SGCT_PRESS, + Release = SGCT_RELEASE, + Repeat = SGCT_REPEAT +}; + +enum class MouseButton { + Left = SGCT_MOUSE_BUTTON_LEFT, + Right = SGCT_MOUSE_BUTTON_RIGHT, + Middle = SGCT_MOUSE_BUTTON_MIDDLE, + Button1 = SGCT_MOUSE_BUTTON_1, + Button2 = SGCT_MOUSE_BUTTON_2, + Button3 = SGCT_MOUSE_BUTTON_3, + Button4 = SGCT_MOUSE_BUTTON_4, + Button5 = SGCT_MOUSE_BUTTON_5, + Button6 = SGCT_MOUSE_BUTTON_6, + Button7 = SGCT_MOUSE_BUTTON_7, + Button8 = SGCT_MOUSE_BUTTON_8, + ButtonLast = SGCT_MOUSE_BUTTON_LAST, +} + +} // namespace interaction +} // namespace openspace + +#endif // __MOUSE_H__ diff --git a/include/openspace/interaction/mousecontroller.h b/include/openspace/interaction/mousecontroller.h new file mode 100644 index 0000000000..ebdd455e0e --- /dev/null +++ b/include/openspace/interaction/mousecontroller.h @@ -0,0 +1,45 @@ +/***************************************************************************************** + * * + * 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. * + ****************************************************************************************/ + +#ifndef __MOUSECONTROLLER_H__ +#define __MOUSECONTROLLER_H__ + +#include + +#include + +namespace openspace { +namespace interaction { + +class MouseController : public Controller { +public: + virtual void button(MouseAction action, MouseButton button) = 0; + virtual void move(float x, float y) = 0; + virtual void scrollWheel(float z) = 0; +}; + +} // namespace interaction +} // namespace openspace + +#endif // __MOUSECONTROLLER_H__ \ No newline at end of file diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index ec002b70cf..7d12dc3b73 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -307,7 +307,7 @@ bool OpenSpaceEngine::initialize() { // Initialize OpenSpace input devices DeviceIdentifier::init(); DeviceIdentifier::ref().scanDevices(); - _interactionHandler.connectDevices(); + //_interactionHandler.connectDevices(); // Run start up scripts ghoul::Dictionary scripts; @@ -341,7 +341,7 @@ ghoul::opencl::CLContext& OpenSpaceEngine::clContext() { return _context; } -InteractionHandler& OpenSpaceEngine::interactionHandler() { +interaction::InteractionHandler& OpenSpaceEngine::interactionHandler() { return _interactionHandler; } diff --git a/src/interaction/controller.cpp b/src/interaction/controller.cpp new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/interaction/interactionhandler.cpp b/src/interaction/interactionhandler.cpp index 433e03b297..d4024dd9d4 100644 --- a/src/interaction/interactionhandler.cpp +++ b/src/interaction/interactionhandler.cpp @@ -1,475 +1,479 @@ - -// open space includes -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -// std includes -#include - -std::string _loggerCat = "InteractionHandler"; - -namespace openspace { - -InteractionHandler::InteractionHandler() { - // initiate pointers - camera_ = nullptr; - enabled_ = true; - node_ = nullptr; - dt_ = 0.0; - _lastTrackballPos = glm::vec3(0.0, 0.0, 0.5); - _leftMouseButtonDown = false; - _isMouseBeingPressedAndHeld = false; -} - -InteractionHandler::~InteractionHandler() { - for (size_t i = 0; i < controllers_.size(); ++i) { - delete controllers_[i]; - } -} - -//void InteractionHandler::init() { -// assert( ! this_); -// this_ = new InteractionHandler(); +///***************************************************************************************** +// * * +// * 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. * +// ****************************************************************************************/ +// +//// open space includes +//#include +//#include +//#include +////#include +////#include +//#include +//#include +//#include +//#include +// +//#include +// +//#include +// +//namespace { +// const std::string _loggerCat = "InteractionHandler"; //} // -//void InteractionHandler::deinit() { -// assert(this_); -// delete this_; -// this_ = nullptr; +//namespace openspace { +//namespace interaction { +// +//InteractionHandler::InteractionHandler() +// : _camera(nullptr) +// , _node(nullptr) +// , _dt(0.0) +// , _lastTrackballPos(0.f) +// , _leftMouseButtonDown(false) +// , _isMouseBeingPressedAndHeld(false) +//{ //} // -//InteractionHandler& InteractionHandler::ref() { -// assert(this_); -// return *this_; +//InteractionHandler::~InteractionHandler() { +// //for (size_t i = 0; i < _controllers.size(); ++i) { +// // delete _controllers[i]; +// //} //} - -//bool InteractionHandler::isInitialized() { -// return this_ != nullptr; -//} - -void InteractionHandler::enable() { - //assert(this_); - enabled_ = true; -} - -void InteractionHandler::disable() { - //assert(this_); - enabled_ = false; -} - -const bool InteractionHandler::isEnabled() const { - //assert(this_); - if (camera_) - return false; - return enabled_; -} - -void InteractionHandler::connectDevices() { - //assert(this_); - assert(DeviceIdentifier::ref().isInitialized()); - - // for each device found - for(int i = 0; i < DeviceIdentifier::ref().numberOfDevices(); ++i) { - - // TODO - //if(DeviceIdentifier::ref().type(i) == InputDevice::XBOX) { - - // // found xbox, use xbox python controller - // JoystickExternalControl *joystickexcontrol = new JoystickExternalControl(RELATIVE_PATH"pyinput/Xbox.py"); - // joystickexcontrol->setInputDevice(i); - // addExternalControl(joystickexcontrol); - - //} else if(DeviceIdentifier::ref().type(i) == InputDevice::SPACENAVIGATOR) { - - // // found SpaceNavigator, use SpaceNavigator python controller - // JoystickExternalControl *joystickexcontrol = new JoystickExternalControl(RELATIVE_PATH"pyinput/SpaceNavigator.py"); - // joystickexcontrol->setInputDevice(i); - // addExternalControl(joystickexcontrol); - //} - - } -} - -void InteractionHandler::addExternalControl(ExternalControl* controller) { - //assert(this_); - if (controller != nullptr) { - controllers_.push_back(controller); - } -} - -void InteractionHandler::setCamera(Camera *camera) { - //assert(this_); - camera_ = camera; -} - -Camera * InteractionHandler::getCamera() const { - //assert(this_); - if (enabled_) { - return camera_; - } - return nullptr; -} - -const psc InteractionHandler::getOrigin() const { - if(node_) - return node_->worldPosition(); - return psc(); -} - -void InteractionHandler::lockControls() { - //assert(this_); - cameraGuard_.lock(); -} - -void InteractionHandler::unlockControls() { - //assert(this_); - cameraGuard_.unlock(); -} - -void InteractionHandler::setFocusNode(SceneGraphNode *node) { - //assert(this_); - node_ = node; -} - -void InteractionHandler::rotate(const glm::quat &rotation) { - //assert(this_); - lockControls(); - camera_->rotate(rotation); - unlockControls(); -} - -void InteractionHandler::orbit(const glm::quat &rotation) { - //assert(this_); - lockControls(); - - // the camera position - psc relative = camera_->position(); - - // should be changed to something more dynamic =) - psc origin; - if (node_) { - origin = node_->worldPosition(); - } - - psc relative_origin_coordinate = relative - origin; - //glm::mat4 rotation_matrix = glm::mat4_cast(glm::inverse(rotation)); - //relative_origin_coordinate = relative_origin_coordinate.vec4() * glm::inverse(rotation); - relative_origin_coordinate = glm::inverse(rotation) * relative_origin_coordinate.vec4(); - relative = relative_origin_coordinate + origin; - - camera_->setPosition(relative); - //camera_->rotate(rotation); - //camera_->setRotation(glm::mat4_cast(rotation)); - - glm::mat4 la = glm::lookAt(camera_->position().vec3(), origin.vec3(), glm::rotate(rotation, camera_->lookUpVector())); - camera_->setRotation(la); - //camera_->setLookUpVector(); - - unlockControls(); -} - -void InteractionHandler::distance(const PowerScaledScalar &distance) { - //assert(this_); - lockControls(); - - psc relative = camera_->position(); - const psc origin = (node_) ? node_->worldPosition() : psc(); - - psc relative_origin_coordinate = relative - origin; - const glm::vec3 dir(relative_origin_coordinate.direction()); - glm:: vec3 newdir = dir * distance[0]; - relative_origin_coordinate = newdir; - relative_origin_coordinate[3] = distance[1]; - relative = relative + relative_origin_coordinate; - - relative_origin_coordinate = relative - origin; - newdir = relative_origin_coordinate.direction(); - - // update only if on the same side of the origin - if(glm::angle(newdir, dir) < 90.0f) - camera_->setPosition(relative); - - unlockControls(); -} - -void InteractionHandler::lookAt(const glm::quat &rotation) { - //assert(this_); - lockControls(); - - unlockControls(); -} - -void InteractionHandler::setRotation(const glm::quat &rotation) { - //assert(this_); - lockControls(); - - unlockControls(); -} - -void InteractionHandler::update(const double dt) { - //assert(this_); - // setting dt_ for use in callbacks - dt_ = dt; - if (enabled_ && camera_) { - // fetch data from joysticks - DeviceIdentifier::ref().update(); - - // update all controllers - for (size_t i = 0; i < controllers_.size(); ++i) { - controllers_[i]->update(); - } - } -} - -double InteractionHandler::getDt() { - //assert(this_); - return dt_; -} - -glm::vec3 InteractionHandler::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 InteractionHandler::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() - node_->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 InteractionHandler::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 *= getDt()*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 - orbit(quaternion); - - _lastTrackballPos = curTrackballPos; - } -} -double acc = 1; - -void InteractionHandler::keyboardCallback(int key, int action) { - // TODO package in script - const double speed = 2.75; - const double dt = getDt(); - if(action == SGCT_PRESS || action == SGCT_REPEAT) { - if (key == SGCT_KEY_S) { - glm::vec3 euler(speed * dt, 0.0, 0.0); - glm::quat rot = glm::quat(euler); - orbit(rot); - } - if (key == SGCT_KEY_W) { - glm::vec3 euler(-speed * dt, 0.0, 0.0); - glm::quat rot = glm::quat(euler); - orbit(rot); - } - if (key == SGCT_KEY_A) { - glm::vec3 euler(0.0, -speed * dt, 0.0); - glm::quat rot = glm::quat(euler); - orbit(rot); - } - if (key == SGCT_KEY_D) { - glm::vec3 euler(0.0, speed * dt, 0.0); - glm::quat rot = glm::quat(euler); - orbit(rot); - } - if (key == SGCT_KEY_Q) { - Time::ref().advanceTime(dt); - } - if (key == 262) { - glm::vec3 euler(0.0, speed * dt, 0.0); - glm::quat rot = glm::quat(euler); - rotate(rot); - } - if (key == 263) { - glm::vec3 euler(0.0, -speed * dt, 0.0); - glm::quat rot = glm::quat(euler); - rotate(rot); - } - if (key == 264) { - glm::vec3 euler(speed * dt, 0.0, 0.0); - glm::quat rot = glm::quat(euler); - rotate(rot); - } - if (key == 265) { - glm::vec3 euler(-speed * dt, 0.0, 0.0); - glm::quat rot = glm::quat(euler); - rotate(rot); - } - if (key == SGCT_KEY_R) { - PowerScaledScalar dist(-speed * dt, 0.0); - distance(dist); - } - if (key == SGCT_KEY_F) { - PowerScaledScalar dist(speed * dt, 0.0); - distance(dist); - } - if (key == SGCT_KEY_T) { - PowerScaledScalar dist(-speed * pow(10, 11) * dt, 0.0); - distance(dist); - } - if (key == SGCT_KEY_G) { - acc += 0.001; - PowerScaledScalar dist(speed * pow(10, 8 * acc) * dt, 0.0); - distance(dist); - } - if (key == SGCT_KEY_Y) { - PowerScaledScalar dist(-speed * 100.0 * dt, 6.0); - distance(dist); - } - if (key == SGCT_KEY_H) { - PowerScaledScalar dist(speed * 100.0 * dt, 6.0); - distance(dist); - } - - if (key == SGCT_KEY_KP_SUBTRACT) { - glm::vec2 s = OsEng.renderEngine().camera()->scaling(); - s[1] -= 0.5; - OsEng.renderEngine().camera()->setScaling(s); - } - if (key == SGCT_KEY_KP_ADD) { - 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)); - } -*/ - // std::pair >::iterator, std::multimap >::iterator> ret; - if(action == SGCT_PRESS) { - auto ret = _keyCallbacks.equal_range(key); - for (auto it=ret.first; it!=ret.second; ++it) - it->second(); - } - - -} - -void InteractionHandler::mouseButtonCallback(int key, int action) { - //if(mouseControl_ != nullptr) { - // mouseControl_->mouseButtonCallback(key,action); - //} - if (key == SGCT_MOUSE_BUTTON_LEFT && action == SGCT_PRESS) - _leftMouseButtonDown = true; - else if (key == SGCT_MOUSE_BUTTON_LEFT && action == SGCT_RELEASE) { - _leftMouseButtonDown = false; - _isMouseBeingPressedAndHeld = false; - } -} - -void InteractionHandler::mousePositionCallback(int x, int y) { - if (_leftMouseButtonDown) - trackballRotate(x,y); - - //if(mouseControl_ != nullptr) { - // mouseControl_->mousePosCallback(x,y); - //} -} - -void InteractionHandler::mouseScrollWheelCallback(int pos) { - //if(mouseControl_ != nullptr) { - // mouseControl_->mouseScrollCallback(pos); - //} - const double speed = 4.75; - const double dt = getDt(); - if(pos < 0) { - PowerScaledScalar dist(speed * dt, 0.0); - distance(dist); - } else if(pos > 0) { - PowerScaledScalar dist(-speed * dt, 0.0); - distance(dist); - } -} - -void InteractionHandler::addKeyCallback(int key, std::function f) { - //std::map > > _keyCallbacks; - - _keyCallbacks.insert(std::make_pair(key, f)); -} - -} // namespace openspace +// +////void InteractionHandler::init() { +//// assert( ! this_); +//// this_ = new InteractionHandler(); +////} +//// +////void InteractionHandler::deinit() { +//// assert(this_); +//// delete this_; +//// this_ = nullptr; +////} +//// +////InteractionHandler& InteractionHandler::ref() { +//// assert(this_); +//// return *this_; +////} +// +////bool InteractionHandler::isInitialized() { +//// return this_ != nullptr; +////} +// +////void InteractionHandler::connectDevices() { +//// //assert(this_); +//// assert(DeviceIdentifier::ref().isInitialized()); +//// +//// // for each device found +//// for(int i = 0; i < DeviceIdentifier::ref().numberOfDevices(); ++i) { +//// +//// // TODO +//// //if(DeviceIdentifier::ref().type(i) == InputDevice::XBOX) { +//// +//// // // found xbox, use xbox python controller +//// // JoystickExternalControl *joystickexcontrol = new JoystickExternalControl(RELATIVE_PATH"pyinput/Xbox.py"); +//// // joystickexcontrol->setInputDevice(i); +//// // addExternalControl(joystickexcontrol); +//// +//// //} else if(DeviceIdentifier::ref().type(i) == InputDevice::SPACENAVIGATOR) { +//// +//// // // found SpaceNavigator, use SpaceNavigator python controller +//// // JoystickExternalControl *joystickexcontrol = new JoystickExternalControl(RELATIVE_PATH"pyinput/SpaceNavigator.py"); +//// // joystickexcontrol->setInputDevice(i); +//// // addExternalControl(joystickexcontrol); +//// //} +//// +//// } +////} +//// +////void InteractionHandler::addExternalControl(ExternalControl* controller) { +//// //assert(this_); +//// if (controller != nullptr) { +//// _controllers.push_back(controller); +//// } +////} +// +//void InteractionHandler::setCamera(Camera *camera) { +// //assert(this_); +// _camera = camera; +//} +// +//Camera * InteractionHandler::getCamera() const { +// return _camera; +//} +// +//const psc InteractionHandler::getOrigin() const { +// if(_node) +// return _node->worldPosition(); +// return psc(); +//} +// +//void InteractionHandler::lockControls() { +// //assert(this_); +// _cameraGuard.lock(); +//} +// +//void InteractionHandler::unlockControls() { +// //assert(this_); +// _cameraGuard.unlock(); +//} +// +//void InteractionHandler::setFocusNode(SceneGraphNode *node) { +// //assert(this_); +// _node = node; +//} +// +//void InteractionHandler::rotate(const glm::quat &rotation) { +// //assert(this_); +// lockControls(); +// _camera->rotate(rotation); +// unlockControls(); +//} +// +//void InteractionHandler::orbit(const glm::quat &rotation) { +// //assert(this_); +// lockControls(); +// +// // the camera position +// psc relative = _camera->position(); +// +// // should be changed to something more dynamic =) +// psc origin; +// if (_node) { +// origin = _node->worldPosition(); +// } +// +// psc relative_origin_coordinate = relative - origin; +// //glm::mat4 rotation_matrix = glm::mat4_cast(glm::inverse(rotation)); +// //relative_origin_coordinate = relative_origin_coordinate.vec4() * glm::inverse(rotation); +// relative_origin_coordinate = glm::inverse(rotation) * relative_origin_coordinate.vec4(); +// relative = relative_origin_coordinate + origin; +// +// _camera->setPosition(relative); +// //camera_->rotate(rotation); +// //camera_->setRotation(glm::mat4_cast(rotation)); +// +// glm::mat4 la = glm::lookAt(_camera->position().vec3(), origin.vec3(), glm::rotate(rotation, _camera->lookUpVector())); +// _camera->setRotation(la); +// //camera_->setLookUpVector(); +// +// unlockControls(); +//} +// +//void InteractionHandler::distance(const PowerScaledScalar &distance) { +// //assert(this_); +// lockControls(); +// +// psc relative = _camera->position(); +// const psc origin = (_node) ? _node->worldPosition() : psc(); +// +// psc relative_origin_coordinate = relative - origin; +// const glm::vec3 dir(relative_origin_coordinate.direction()); +// glm:: vec3 newdir = dir * distance[0]; +// relative_origin_coordinate = newdir; +// relative_origin_coordinate[3] = distance[1]; +// relative = relative + relative_origin_coordinate; +// +// relative_origin_coordinate = relative - origin; +// newdir = relative_origin_coordinate.direction(); +// +// // update only if on the same side of the origin +// if(glm::angle(newdir, dir) < 90.0f) +// _camera->setPosition(relative); +// +// unlockControls(); +//} +// +//void InteractionHandler::lookAt(const glm::quat &rotation) { +// //assert(this_); +// lockControls(); +// +// unlockControls(); +//} +// +//void InteractionHandler::setRotation(const glm::quat &rotation) { +// //assert(this_); +// lockControls(); +// +// unlockControls(); +//} +// +//void InteractionHandler::update(const double dt) { +// //assert(this_); +// // setting dt_ for use in callbacks +// _dt = dt; +// if (_camera) { +// // fetch data from joysticks +// DeviceIdentifier::ref().update(); +// +// //// update all controllers +// //for (size_t i = 0; i < _controllers.size(); ++i) { +// // _controllers[i]->update(); +// //} +// } +//} +// +//double InteractionHandler::getDt() { +// //assert(this_); +// return _dt; +//} +// +//glm::vec3 InteractionHandler::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 InteractionHandler::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() - _node->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 InteractionHandler::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 *= getDt()*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 +// orbit(quaternion); +// +// _lastTrackballPos = curTrackballPos; +// } +//} +//double acc = 1; +// +//void InteractionHandler::keyboardCallback(int key, int action) { +// // TODO package in script +// const double speed = 2.75; +// const double dt = getDt(); +// if(action == SGCT_PRESS || action == SGCT_REPEAT) { +// if (key == SGCT_KEY_S) { +// glm::vec3 euler(speed * dt, 0.0, 0.0); +// glm::quat rot = glm::quat(euler); +// orbit(rot); +// } +// if (key == SGCT_KEY_W) { +// glm::vec3 euler(-speed * dt, 0.0, 0.0); +// glm::quat rot = glm::quat(euler); +// orbit(rot); +// } +// if (key == SGCT_KEY_A) { +// glm::vec3 euler(0.0, -speed * dt, 0.0); +// glm::quat rot = glm::quat(euler); +// orbit(rot); +// } +// if (key == SGCT_KEY_D) { +// glm::vec3 euler(0.0, speed * dt, 0.0); +// glm::quat rot = glm::quat(euler); +// orbit(rot); +// } +// if (key == SGCT_KEY_Q) { +// Time::ref().advanceTime(dt); +// } +// if (key == 262) { +// glm::vec3 euler(0.0, speed * dt, 0.0); +// glm::quat rot = glm::quat(euler); +// rotate(rot); +// } +// if (key == 263) { +// glm::vec3 euler(0.0, -speed * dt, 0.0); +// glm::quat rot = glm::quat(euler); +// rotate(rot); +// } +// if (key == 264) { +// glm::vec3 euler(speed * dt, 0.0, 0.0); +// glm::quat rot = glm::quat(euler); +// rotate(rot); +// } +// if (key == 265) { +// glm::vec3 euler(-speed * dt, 0.0, 0.0); +// glm::quat rot = glm::quat(euler); +// rotate(rot); +// } +// if (key == SGCT_KEY_R) { +// PowerScaledScalar dist(-speed * dt, 0.0); +// distance(dist); +// } +// if (key == SGCT_KEY_F) { +// PowerScaledScalar dist(speed * dt, 0.0); +// distance(dist); +// } +// if (key == SGCT_KEY_T) { +// PowerScaledScalar dist(-speed * pow(10, 11) * dt, 0.0); +// distance(dist); +// } +// if (key == SGCT_KEY_G) { +// acc += 0.001; +// PowerScaledScalar dist(speed * pow(10, 8 * acc) * dt, 0.0); +// distance(dist); +// } +// if (key == SGCT_KEY_Y) { +// PowerScaledScalar dist(-speed * 100.0 * dt, 6.0); +// distance(dist); +// } +// if (key == SGCT_KEY_H) { +// PowerScaledScalar dist(speed * 100.0 * dt, 6.0); +// distance(dist); +// } +// +// if (key == SGCT_KEY_KP_SUBTRACT) { +// glm::vec2 s = OsEng.renderEngine().camera()->scaling(); +// s[1] -= 0.5; +// OsEng.renderEngine().camera()->setScaling(s); +// } +// if (key == SGCT_KEY_KP_ADD) { +// 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)); +// } +//*/ +// // std::pair >::iterator, std::multimap >::iterator> ret; +// if(action == SGCT_PRESS) { +// auto ret = _keyCallbacks.equal_range(key); +// for (auto it=ret.first; it!=ret.second; ++it) +// it->second(); +// } +// +// +//} +// +//void InteractionHandler::mouseButtonCallback(int key, int action) { +// //if(mouseControl_ != nullptr) { +// // mouseControl_->mouseButtonCallback(key,action); +// //} +// if (key == SGCT_MOUSE_BUTTON_LEFT && action == SGCT_PRESS) +// _leftMouseButtonDown = true; +// else if (key == SGCT_MOUSE_BUTTON_LEFT && action == SGCT_RELEASE) { +// _leftMouseButtonDown = false; +// _isMouseBeingPressedAndHeld = false; +// } +//} +// +//void InteractionHandler::mousePositionCallback(int x, int y) { +// if (_leftMouseButtonDown) +// trackballRotate(x,y); +// +// //if(mouseControl_ != nullptr) { +// // mouseControl_->mousePosCallback(x,y); +// //} +//} +// +//void InteractionHandler::mouseScrollWheelCallback(int pos) { +// //if(mouseControl_ != nullptr) { +// // mouseControl_->mouseScrollCallback(pos); +// //} +// const double speed = 4.75; +// const double dt = getDt(); +// if(pos < 0) { +// PowerScaledScalar dist(speed * dt, 0.0); +// distance(dist); +// } else if(pos > 0) { +// PowerScaledScalar dist(-speed * dt, 0.0); +// distance(dist); +// } +//} +// +////void InteractionHandler::addKeyCallback(int key, std::function f) { +//// //std::map > > _keyCallbacks; +//// +//// _keyCallbacks.insert(std::make_pair(key, f)); +////} +// +//} // namespace interaction +//} // namespace openspace diff --git a/src/interaction/keyboardcontroller.cpp b/src/interaction/keyboardcontroller.cpp new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/interaction/mousecontroller.cpp b/src/interaction/mousecontroller.cpp new file mode 100644 index 0000000000..4a918dcff7 --- /dev/null +++ b/src/interaction/mousecontroller.cpp @@ -0,0 +1,24 @@ +/***************************************************************************************** + * * + * 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. * + ****************************************************************************************/ + diff --git a/src/rendering/renderengine.cpp b/src/rendering/renderengine.cpp index 1a5ea93b7f..04d3617fa8 100644 --- a/src/rendering/renderengine.cpp +++ b/src/rendering/renderengine.cpp @@ -244,7 +244,6 @@ void RenderEngine::render() _abuffer->resolve(); glDisable(GL_BLEND); -#ifndef OPENSPACE_VIDEO_EXPORT // Print some useful information on the master viewport if (sgct::Engine::instance()->isMaster()) { // Apple usually has retina screens @@ -301,8 +300,6 @@ void RenderEngine::render() FONT_SIZE, FONT_SIZE * 2, "Scaling: (%.10f, %.2f)", scaling[0], scaling[1]); } -#endif - } SceneGraph* RenderEngine::sceneGraph() diff --git a/src/scenegraph/scenegraphnode.cpp b/src/scenegraph/scenegraphnode.cpp index 8a4f1acb67..21159af729 100644 --- a/src/scenegraph/scenegraphnode.cpp +++ b/src/scenegraph/scenegraphnode.cpp @@ -284,9 +284,6 @@ const std::vector& SceneGraphNode::children() const{ PowerScaledScalar SceneGraphNode::calculateBoundingSphere(){ // set the bounding sphere to 0.0 _boundingSphere = 0.0; - _boundingSphere = 1000.0; - _boundingSphere = 0.0; - if (_children.size() > 0) { // node PowerScaledScalar maxChild; @@ -296,8 +293,9 @@ PowerScaledScalar SceneGraphNode::calculateBoundingSphere(){ for (size_t i = 0; i < _children.size(); ++i) { // when positions is dynamic, change this part to fins the most distant // position - PowerScaledScalar child = _children.at(i)->position().length() - + _children.at(i)->calculateBoundingSphere(); + //PowerScaledScalar child = _children.at(i)->position().length() + // + _children.at(i)->calculateBoundingSphere(); + PowerScaledScalar child = _children.at(i)->calculateBoundingSphere(); if (child > maxChild) { maxChild = child; } @@ -311,7 +309,7 @@ PowerScaledScalar SceneGraphNode::calculateBoundingSphere(){ if(renderableBS > _boundingSphere) _boundingSphere = renderableBS; } - LWARNING(name() << ": " << _boundingSphere); + LINFO("Bounding Sphere of '" << name() << "': " << _boundingSphere); return _boundingSphere; }