diff --git a/include/openspace/scenegraph/scenegraphnode.h b/include/openspace/scenegraph/scenegraphnode.h index a436173cc5..2fb36c7fe7 100644 --- a/include/openspace/scenegraph/scenegraphnode.h +++ b/include/openspace/scenegraph/scenegraphnode.h @@ -63,6 +63,7 @@ public: void update(const UpdateData& data); void evaluate(const Camera* camera, const psc& parentPosition = psc()); void render(const RenderData& data); + void updateCamera(Camera* camera) const; void addNode(SceneGraphNode* child); diff --git a/include/openspace/util/camera.h b/include/openspace/util/camera.h index 786c0709ae..3566212e90 100644 --- a/include/openspace/util/camera.h +++ b/include/openspace/util/camera.h @@ -115,6 +115,9 @@ public: void setCameraDirection(glm::vec3 cameraDirection); glm::vec3 cameraDirection() const; + void setFocusPosition(psc pos); + const psc& focusPosition() const; + void setViewRotationMatrix(glm::mat4 m); const glm::mat4& viewRotationMatrix() const; void compileViewRotationMatrix(); @@ -149,6 +152,7 @@ private: glm::mat4 _projectionMatrix; glm::vec3 _viewDirection; glm::vec3 _cameraDirection; + psc _focusPosition; // glm::quat _viewRotation; glm::vec3 _lookUp; diff --git a/include/openspace/util/time.h b/include/openspace/util/time.h index b9da19ed0d..34d6f3612c 100644 --- a/include/openspace/util/time.h +++ b/include/openspace/util/time.h @@ -27,6 +27,7 @@ #include #include +#include namespace openspace { @@ -49,6 +50,9 @@ namespace openspace { * advanceTime(double) method is called each frame, the tickTime has to be * equal to the frame time. */ + + class SyncBuffer; + class Time { public: /** @@ -146,6 +150,14 @@ public: */ double retreatTime(double tickTime); + void serialize(SyncBuffer* syncBuffer); + + void deserialize(SyncBuffer* syncBuffer); + + void postSynchronizationPreDraw(); + + void preSynchronization(); + /** * Returns the Lua library that contains all Lua functions available to change the * current time, retrieve the current time etc. The functions contained are @@ -166,9 +178,25 @@ private: Time& operator=(const Time& rhs) = delete; static Time* _instance; ///< The singleton instance - double _time; ///< The time stored as the number of seconds past the J2000 epoch + double _deltaTimePerSecond; ///< The delta time that is used to advance the time + + //sync variables + + //local copies + double _time; ///< The time stored as the number of seconds past the J2000 epoch + double _dt; + + //shared copies + double _sharedTime; + double _sharedDt; + + //synched copies + double _syncedTime; + double _syncedDt; + + std::mutex _syncMutex; }; } // namespace openspace diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index c710b7a942..12fa601db3 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -501,13 +501,19 @@ void OpenSpaceEngine::preSynchronization() { Time::ref().advanceTime(_dt); + Time::ref().preSynchronization(); + _renderEngine.preSynchronization(); } } void OpenSpaceEngine::postSynchronizationPreDraw() { + //time must be called first + Time::ref().postSynchronizationPreDraw(); + _renderEngine.postSynchronizationPreDraw(); + if (sgct::Engine::instance()->isMaster() && _gui.isEnabled()) { double posX, posY; sgct::Engine::instance()->getMousePos(0, &posX, &posY); @@ -613,7 +619,7 @@ void OpenSpaceEngine::mouseScrollWheelCallback(int pos) { void OpenSpaceEngine::encode() { if (_syncBuffer) { _renderEngine.serialize(_syncBuffer); - + Time::ref().serialize(_syncBuffer); _syncBuffer->write(); } } @@ -622,6 +628,7 @@ void OpenSpaceEngine::decode() { if (_syncBuffer) { _syncBuffer->read(); _renderEngine.deserialize(_syncBuffer); + Time::ref().deserialize(_syncBuffer); } } diff --git a/src/interaction/interactionhandler.cpp b/src/interaction/interactionhandler.cpp index 90ec4435b9..276062a58f 100644 --- a/src/interaction/interactionhandler.cpp +++ b/src/interaction/interactionhandler.cpp @@ -480,6 +480,7 @@ void InteractionHandler::update(double deltaTime) { void InteractionHandler::setFocusNode(SceneGraphNode* node) { _focusNode = node; + _camera->setFocusPosition(node->worldPosition()); } const SceneGraphNode* const InteractionHandler::focusNode() const { @@ -523,12 +524,6 @@ void InteractionHandler::orbit(const float &dx, const float &dy, const float &dz lockControls(); - // should be changed to something more dynamic =) - psc origin; - if (_focusNode) { - origin = _focusNode->worldPosition(); - } - glm::vec3 cameraUp = glm::normalize((glm::inverse(_camera->viewRotationMatrix()) * glm::vec4(_camera->lookUpVector(), 0))).xyz; glm::vec3 cameraRight = glm::cross(_camera->viewDirection(), cameraUp); @@ -537,6 +532,13 @@ void InteractionHandler::orbit(const float &dx, const float &dy, const float &dz transform = glm::rotate(dy, cameraRight) * transform; transform = glm::rotate(dz, _camera->viewDirection()) * transform; + // should be changed to something more dynamic =) + psc origin; + if (_focusNode) { + origin = _focusNode->worldPosition(); + } + _camera->setFocusPosition(origin); + // the camera position psc relative = _camera->position(); psc relative_origin_coordinate = relative - origin; diff --git a/src/interaction/mousecontroller.cpp b/src/interaction/mousecontroller.cpp index e8d622f66b..ff45f21f75 100644 --- a/src/interaction/mousecontroller.cpp +++ b/src/interaction/mousecontroller.cpp @@ -222,6 +222,7 @@ void OrbitalMouseController::scrollWheel(int pos) { } void OrbitalMouseController::update(const double& dt){ + if (_leftMouseButtonDown || _rightMouseButtonDown || _middleMouseButtonDown){ _handler->orbit( static_cast(_leftMouseButtonDown) * static_cast(dt) * _currentCursorDiff[MouseButtons::ButtonLeft].x * _rotationSpeed, diff --git a/src/rendering/renderengine.cpp b/src/rendering/renderengine.cpp index 93ef5b981a..fef98db20f 100644 --- a/src/rendering/renderengine.cpp +++ b/src/rendering/renderengine.cpp @@ -334,6 +334,11 @@ void RenderEngine::postSynchronizationPreDraw() // clear the abuffer before rendering the scene _abuffer->clear(); + + if (const SceneGraphNode* node = OsEng.ref().interactionHandler().focusNode()){ + node->updateCamera(_mainCamera); + } + } void RenderEngine::render() diff --git a/src/scenegraph/scenegraphnode.cpp b/src/scenegraph/scenegraphnode.cpp index 08fb6aae85..3a1ef11d61 100644 --- a/src/scenegraph/scenegraphnode.cpp +++ b/src/scenegraph/scenegraphnode.cpp @@ -411,4 +411,23 @@ SceneGraphNode* SceneGraphNode::childNode(const std::string& name) return nullptr; } +void SceneGraphNode::updateCamera(Camera* camera) const{ + + psc origin = worldPosition(); + int i = 0; + // the camera position + + psc relative = camera->position(); + psc focus = camera->focusPosition(); + psc relative_focus = relative - focus; + + psc target = origin + relative_focus; + + camera->setPosition(target); + camera->setFocusPosition(origin); + + //printf("target: %f, %f, %f, %f\n", target.vec4().x, target.vec4().y, target.vec4().z, target.vec4().w); + +} + } // namespace openspace diff --git a/src/util/camera.cpp b/src/util/camera.cpp index 18df55243f..248a6d1198 100644 --- a/src/util/camera.cpp +++ b/src/util/camera.cpp @@ -43,10 +43,10 @@ Camera::Camera() , _modelMatrix() , _viewMatrix() , _projectionMatrix() - , _viewDirection() + , _viewDirection(0,0,-1) , _cameraDirection(0.f, 0.f, 0.f) , _localScaling(1.f, 0.f) - //, _viewRotation(glm::quat(glm::vec3(0.f, 0.f, 0.f))) + //, _viewRotation(glm::quat(glm::vec3(0.f, 0.f, 0.f))) , _localViewRotationMatrix(1.f) , _sharedPosition() , _sharedScaling(1.f, 0.f) @@ -54,6 +54,7 @@ Camera::Camera() , _syncedPosition() , _syncedScaling(1.f, 0.f) , _syncedViewRotationMatrix(1.f) + , _focusPosition() { } @@ -64,11 +65,15 @@ Camera::~Camera() void Camera::setPosition(psc pos) { _localPosition = std::move(pos); + _syncedPosition = _localPosition; } const psc& Camera::position() const { return _syncedPosition; + ///* FIXA HÄR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! (litet compileringsfel på slutet också så jag inte ska glömma!)*/ hej här är ett kompileringsfel! + + //return _localPosition; } void Camera::setModelMatrix(glm::mat4 modelMatrix){ @@ -159,6 +164,15 @@ void Camera::setRotation(glm::mat4 rotation) // return _viewRotation; //} +void Camera::setFocusPosition(psc pos){ + _focusPosition = pos; +} + +const psc& Camera::focusPosition() const{ + return _focusPosition; +} + + const glm::vec3& Camera::viewDirection() const { return _viewDirection; diff --git a/src/util/time.cpp b/src/util/time.cpp index c91b601dbb..3eb6115460 100644 --- a/src/util/time.cpp +++ b/src/util/time.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include @@ -144,8 +145,13 @@ int time_currentTimeUTC(lua_State* L) { Time* Time::_instance = nullptr; -Time::Time() +Time::Time() : _time(-1.0) + , _dt(1.0) + , _sharedTime(-1.0) + , _sharedDt(1.0) + , _syncedTime(-1.0) + , _syncedDt(1.0) , _deltaTimePerSecond(1.0) { } @@ -177,23 +183,23 @@ void Time::setTime(double value) { double Time::currentTime() const { assert(_instance); - return _time; + return _syncedTime; } double Time::advanceTime(double tickTime) { - return _time += _deltaTimePerSecond * tickTime; + return _time += _dt * tickTime; } double Time::retreatTime(double tickTime) { - return _time -= _deltaTimePerSecond * tickTime; + return _time -= _dt * tickTime; } void Time::setDeltaTime(double deltaT) { - _deltaTimePerSecond = std::move(deltaT); + _dt = std::move(deltaT); } double Time::deltaTime() const { - return _deltaTimePerSecond; + return _syncedDt; } void Time::setTime(std::string time) { @@ -202,10 +208,46 @@ void Time::setTime(std::string time) { std::string Time::currentTimeUTC() const { std::string date; - SpiceManager::ref().getDateFromET(_time, date); + SpiceManager::ref().getDateFromET(_syncedTime, date); return date; } +void Time::serialize(SyncBuffer* syncBuffer){ + _syncMutex.lock(); + + syncBuffer->encode(_sharedTime); + syncBuffer->encode(_sharedDt); + + _syncMutex.unlock(); +} + +void Time::deserialize(SyncBuffer* syncBuffer){ + _syncMutex.lock(); + + syncBuffer->decode(_sharedTime); + syncBuffer->decode(_sharedDt); + + _syncMutex.unlock(); +} + +void Time::postSynchronizationPreDraw(){ + _syncMutex.lock(); + + _syncedTime = _sharedTime; + _syncedDt = _sharedDt; + + _syncMutex.unlock(); +} + +void Time::preSynchronization(){ + _syncMutex.lock(); + + _sharedTime = _time; + _sharedDt = _dt; + + _syncMutex.unlock(); +} + scripting::ScriptEngine::LuaLibrary Time::luaLibrary() { scripting::ScriptEngine::LuaLibrary timeLibrary = { "time",