From 299cd27f2cc0b598ed5786113d34d0d10cedc983 Mon Sep 17 00:00:00 2001 From: Erik Broberg Date: Thu, 14 Apr 2016 12:47:12 -0400 Subject: [PATCH 1/2] Fixed bug Camera (view rot matrix had translation components) and structured Camera code --- data/scene/debugglobe/debugglobe.mod | 1 + data/scene/default.scene | 2 +- include/openspace/util/camera.h | 194 ++++---- .../globebrowsing/rendering/patchrenderer.cpp | 2 +- openspace.cfg | 2 +- src/interaction/interactionhandler.cpp | 4 +- src/rendering/renderengine.cpp | 9 +- src/scene/scene.cpp | 8 +- src/util/camera.cpp | 418 +++++++----------- 9 files changed, 266 insertions(+), 374 deletions(-) diff --git a/data/scene/debugglobe/debugglobe.mod b/data/scene/debugglobe/debugglobe.mod index 0f6cad8b01..6ad361aa8d 100644 --- a/data/scene/debugglobe/debugglobe.mod +++ b/data/scene/debugglobe/debugglobe.mod @@ -7,6 +7,7 @@ return { Type = "RenderableGlobe", Frame = "IAU_EARTH", Body = "EARTH", + Radius = 6.316e6, }, Ephemeris = { Type = "Spice", diff --git a/data/scene/default.scene b/data/scene/default.scene index da737c5c01..c9c24a68a4 100644 --- a/data/scene/default.scene +++ b/data/scene/default.scene @@ -3,7 +3,7 @@ return { CommonFolder = "common", Camera = { Focus = "Earth", - Position = {1, 0, 0, 5}, + -- Position = {1, 0, 0, 5}, }, Modules = { "sun", diff --git a/include/openspace/util/camera.h b/include/openspace/util/camera.h index 6e63e1714d..7be75e7d23 100644 --- a/include/openspace/util/camera.h +++ b/include/openspace/util/camera.h @@ -29,6 +29,7 @@ // open space includes #include +#include // glm includes #include @@ -38,129 +39,118 @@ namespace openspace { -//class Camera { -//public: -// enum class ProjectionMode { -// Perspective, -// Orthographic, -// Frustum, -// FixedPerspective -// }; -// -// Camera(); -// -// void setPosition(psc pos); -// const psc& position() const; -// -// void setFocus(psc focus); -// const psc& focus() const; -// -// void setUpVector(psc upVector); -// const psc& upVector() const; -// -// void setScaling(float scaling); -// float scaling() const; -// -// const glm::mat4& viewMatrix() const; -// -// void setProjectionMatrix(glm::mat4 projectionMatrix); -// const glm::mat4& projectionMatrix() const; -// -// void setMaxFox(float fov); -// float maxFov() const; -// -// -// // derived values -// psc lookVector() const; -// -//private: -// void invalidateViewMatrix(); -// void updateViewMatrix() const; // has to be constant to be called from getter methods -// -// psc _position; -// psc _focus; -// psc _upVector; -// -// glm::mat4 _projectionMatrix; -// mutable glm::mat4 _viewMatrix; -// float _scaling; -// -// float _maxFov; -// -// mutable bool _viewMatrixIsDirty; -//}; - - class SyncBuffer; +class SyncBuffer; class Camera { public: - Camera(); - ~Camera(); + Camera(); + ~Camera(); - void setPosition(psc pos); - const psc& position() const; - + + + // MUTATORS (SETTERS) + + void setPosition(psc pos); + void setFocusPosition(psc pos); + void setRotation(glm::quat rotation); + void setLookUpVector(glm::vec3 lookUp); + void setScaling(glm::vec2 scaling); + void setMaxFov(float fov); + + + + + // RELATIVE MUTATORS + + void rotate(const glm::quat& rotation); + + + + + // ACCESSORS (GETTERS) + + const psc& position() const; const psc& unsynchedPosition() const; + const psc& focusPosition() const; + const glm::vec3& viewDirection() const; + const glm::vec3& lookUpVector() const; + const glm::vec2& scaling() const; + float maxFov() const; + float sinMaxFov() const; + const glm::mat4& viewRotationMatrix() const; + //const glm::mat4& viewTranslationMatrix() const; + //const glm::mat4& viewScalingMatrix() const; - void setModelMatrix(glm::mat4 modelMatrix); - const glm::mat4& modelMatrix() const; + //@TODO this should simply be called viewMatrix. + //Rename after removing deprecated methods + //const glm::mat4& combinedViewMatrix() const; - void setViewMatrix(glm::mat4 viewMatrix); + + + + // DEPRECATED ACCESSORS (GETTERS) + // @TODO use Camera::SgctInternal interface instead + + [[deprecated("Replaced by Camera::SgctInternal::viewMatrix()")]] const glm::mat4& viewMatrix() const; - void setProjectionMatrix(glm::mat4 projectionMatrix); + [[deprecated("Replaced by Camera::SgctInternal::projectionMatrix()")]] const glm::mat4& projectionMatrix() const; - const glm::mat4& viewProjectionMatrix() const; + [[deprecated("Replaced by Camera::SgctInternal::viewProjectionMatrix()")]] + const glm::mat4& viewProjectionMatrix() const; - 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(); - void rotate(const glm::quat& rotation); - void setRotation(glm::quat rotation); - // const glm::quat& rotation() const; - void setRotation(glm::mat4 rotation); - - const glm::vec3& viewDirection() const; - - const float& maxFov() const; - const float& sinMaxFov() const; - void setMaxFov(float fov); - void setScaling(glm::vec2 scaling); - const glm::vec2& scaling() const; - - void setLookUpVector(glm::vec3 lookUp); - const glm::vec3& lookUpVector() const; + // SYNCHRONIZATION void postSynchronizationPreDraw(); void preSynchronization(); void serialize(SyncBuffer* syncBuffer); void deserialize(SyncBuffer* syncBuffer); + + + // Handles SGCT's internal matrices. + class SgctInternal { + friend class Camera; + + public: + + void setViewMatrix(glm::mat4 viewMatrix); + void setProjectionMatrix(glm::mat4 projectionMatrix); + + const glm::mat4& viewMatrix() const; + const glm::mat4& projectionMatrix() const; + const glm::mat4& viewProjectionMatrix() const; + + private: + SgctInternal(); + + glm::mat4 _viewMatrix; + glm::mat4 _projectionMatrix; + + mutable bool _dirtyViewProjectionMatrix; + mutable glm::mat4 _viewProjectionMatrix; + mutable std::mutex _mutex; + + } sgctInternal; + + private: - float _maxFov; - float _sinMaxFov; - mutable glm::mat4 _viewProjectionMatrix; - glm::mat4 _modelMatrix; - glm::mat4 _viewMatrix; - glm::mat4 _projectionMatrix; - mutable bool _dirtyViewProjectionMatrix; - glm::vec3 _viewDirection; - glm::vec3 _cameraDirection; + psc _focusPosition; - // glm::quat _viewRotation; - - glm::vec3 _lookUp; + glm::vec3 _viewDirection; + glm::vec3 _lookUp; + + + + mutable std::mutex _mutex; - + + //local variables glm::mat4 _localViewRotationMatrix; glm::vec2 _localScaling; @@ -175,6 +165,14 @@ private: glm::vec2 _syncedScaling; psc _syncedPosition; glm::mat4 _syncedViewRotationMatrix; + + + + float _maxFov; + float _sinMaxFov; + + // Defines what direction in local camera space the camera is looking in. + const glm::vec3 _viewDirectionInCameraSpace; }; diff --git a/modules/globebrowsing/rendering/patchrenderer.cpp b/modules/globebrowsing/rendering/patchrenderer.cpp index ec8da49191..64b76adef2 100644 --- a/modules/globebrowsing/rendering/patchrenderer.cpp +++ b/modules/globebrowsing/rendering/patchrenderer.cpp @@ -103,7 +103,7 @@ namespace openspace { // Get camera transform matrix // TODO : Should only need to fetch the camera transform and use directly // but this is not currently possible in the camera class. - vec3 cameraPosition = data.camera.unsynchedPosition().vec3(); + vec3 cameraPosition = data.camera.position().vec3(); mat4 viewTransform = inverse(translate(mat4(1.0), cameraPosition)); viewTransform = mat4(data.camera.viewRotationMatrix()) * viewTransform; diff --git a/openspace.cfg b/openspace.cfg index 5fc1a77c02..45bced767e 100644 --- a/openspace.cfg +++ b/openspace.cfg @@ -12,7 +12,7 @@ return { -- Scene = "${SCENE}/default-modified.scene", -- Scene = "${SCENE}/rosetta.scene", -- Scene = "${SCENE}/dawn.scene", - Scene = "${SCENE}/globebrowsing.scene", + Scene = "${SCENE}/globebrowsing.scene", Paths = { SGCT = "${BASE_PATH}/config/sgct", diff --git a/src/interaction/interactionhandler.cpp b/src/interaction/interactionhandler.cpp index 0a032ffa2a..00990d8ac4 100644 --- a/src/interaction/interactionhandler.cpp +++ b/src/interaction/interactionhandler.cpp @@ -283,7 +283,7 @@ void InteractionHandler::update(double deltaTime) { if(hasKeys){ _camera->setPosition(pos); - _camera->setViewRotationMatrix(glm::mat4_cast(q)); + _camera->setRotation(q); } @@ -395,7 +395,7 @@ void InteractionHandler::orbit(const float &dx, const float &dy, const float &dz //new camera position relative = origin + relative_focus_coordinate; - float bounds = 2.f * (_focusNode ? _focusNode->boundingSphere().lengthf() : 0.f) / 10.f; + float bounds = _focusNode ? _focusNode->boundingSphere().lengthf() : 0.f; psc target = relative + relative_focus_coordinate * dist; //don't fly into objects diff --git a/src/rendering/renderengine.cpp b/src/rendering/renderengine.cpp index f39e8171cd..5babd39c35 100644 --- a/src/rendering/renderengine.cpp +++ b/src/rendering/renderengine.cpp @@ -283,7 +283,6 @@ bool RenderEngine::initializeGL() { //_mainCamera->setCameraDirection(glm::normalize(-viewdir)); - _mainCamera->setCameraDirection(glm::vec3(0.f, 0.f, -1.f)); //_mainCamera->setLookUpVector(glm::normalize(upVector)); _mainCamera->setLookUpVector(glm::vec3(0.f, 1.f, 0.f)); @@ -347,10 +346,6 @@ void RenderEngine::postSynchronizationPreDraw() { ghoul::fontrendering::FontRenderer::defaultRenderer().setWindowSize(glm::vec2(res)); } - // converts the quaternion used to rotation matrices - if (_mainCamera) - _mainCamera->compileViewRotationMatrix(); - // update and evaluate the scene starting from the root node _sceneGraph->update({ Time::ref().currentTime(), @@ -377,8 +372,8 @@ void RenderEngine::postSynchronizationPreDraw() { } void RenderEngine::render(const glm::mat4 &projectionMatrix, const glm::mat4 &viewMatrix) { - _mainCamera->setViewMatrix(viewMatrix); - _mainCamera->setProjectionMatrix(projectionMatrix); + _mainCamera->sgctInternal.setViewMatrix(viewMatrix); + _mainCamera->sgctInternal.setProjectionMatrix(projectionMatrix); if (!(OsEng.isMaster() && _disableMasterRendering)) { diff --git a/src/scene/scene.cpp b/src/scene/scene.cpp index d1724f5b50..263d28ae52 100644 --- a/src/scene/scene.cpp +++ b/src/scene/scene.cpp @@ -204,6 +204,7 @@ bool Scene::loadSceneInternal(const std::string& sceneDescriptionFilePath) { } } + // update the position of all nodes // TODO need to check this; unnecessary? (ab) for (SceneGraphNode* node : _graph.nodes()) { @@ -247,7 +248,7 @@ bool Scene::loadSceneInternal(const std::string& sceneDescriptionFilePath) { glm::vec2 boundf = bound.vec2(); //glm::vec2 scaling{1.0f, -boundf[1]}; cameraScaling = glm::vec2(1.f, -boundf[1]); - boundf[0] *= 5.0f; + //boundf[0] *= 5.0f; //psc cameraPosition = focusNode->position(); //cameraPosition += psc(glm::vec4(0.f, 0.f, boundf)); @@ -255,7 +256,7 @@ bool Scene::loadSceneInternal(const std::string& sceneDescriptionFilePath) { //cameraPosition = psc(glm::vec4(0.f, 0.f, 1.f,0.f)); cameraPosition = focusNode->position(); - cameraPosition += psc(glm::vec4(0.f, 0.f, boundf)); + cameraPosition += psc(glm::vec4(boundf[0], 0.f, 0.f, boundf[1])); //why this line? (JK) //cameraPosition = psc(glm::vec4(0.f, 0.f, 1.f, 0.f)); @@ -292,7 +293,8 @@ bool Scene::loadSceneInternal(const std::string& sceneDescriptionFilePath) { const SceneGraphNode* fn = OsEng.interactionHandler().focusNode(); // Check crash for when fn == nullptr - glm::mat4 la = glm::lookAt(cameraPosition.vec3(), fn->worldPosition().vec3(), c->lookUpVector()); + + glm::mat4 la = glm::lookAt(glm::vec3(0,0,0), fn->worldPosition().vec3() - cameraPosition.vec3(), c->lookUpVector()); c->setRotation(la); c->setPosition(cameraPosition); diff --git a/src/util/camera.cpp b/src/util/camera.cpp index 388bb36cbd..1c3ae6ae32 100644 --- a/src/util/camera.cpp +++ b/src/util/camera.cpp @@ -30,205 +30,147 @@ #include namespace openspace { - + Camera::Camera() : _maxFov(0.f) , _sinMaxFov(0.f) - , _viewProjectionMatrix() - , _modelMatrix() - , _viewMatrix() - , _projectionMatrix() - , _dirtyViewProjectionMatrix(true) , _viewDirection(0,0,-1) - , _cameraDirection(0.f, 0.f, 0.f) - , _focusPosition() - //, _viewRotation(glm::quat(glm::vec3(0.f, 0.f, 0.f))) + , _viewDirectionInCameraSpace(0.f, 0.f, -1.f) + , _focusPosition() , _localViewRotationMatrix(1.f) - , _localScaling(1.f, 0.f) - , _localPosition() - , _sharedScaling(1.f, 0.f) + , _localScaling(1.f, 0.f) + , _localPosition() + , _sharedScaling(1.f, 0.f) , _sharedPosition() , _sharedViewRotationMatrix(1.f) , _syncedScaling(1.f, 0.f) - , _syncedPosition() + , _syncedPosition() , _syncedViewRotationMatrix(1.f) { } -Camera::~Camera() -{ -} +Camera::~Camera() { } -void Camera::setPosition(psc pos) -{ - std::lock_guard _lock(_mutex); + + +////////////////////////////////////////////////////////////////////////////////////////// +// CAMERA MUTATORS (SETTERS) // +////////////////////////////////////////////////////////////////////////////////////////// + +void Camera::setPosition(psc pos){ + std::lock_guard _lock(_mutex); _localPosition = std::move(pos); } -const psc& Camera::position() const -{ - return _syncedPosition; -} - -const psc& Camera::unsynchedPosition() const{ - return _localPosition; -} - -void Camera::setModelMatrix(glm::mat4 modelMatrix){ - std::lock_guard _lock(_mutex); - _modelMatrix = std::move(modelMatrix); -} - -const glm::mat4& Camera::modelMatrix() const{ - return _modelMatrix; -} - -void Camera::setViewMatrix(glm::mat4 viewMatrix){ - std::lock_guard _lock(_mutex); - _viewMatrix = std::move(viewMatrix); - _dirtyViewProjectionMatrix = true; -} - -const glm::mat4& Camera::viewMatrix() const{ - return _viewMatrix; -} - -void Camera::setProjectionMatrix(glm::mat4 projectionMatrix){ - std::lock_guard _lock(_mutex); - _projectionMatrix = std::move(projectionMatrix); - _dirtyViewProjectionMatrix = true; -} - -const glm::mat4& Camera::projectionMatrix() const{ - return _projectionMatrix; -} - -const glm::mat4& Camera::viewProjectionMatrix() const { - if (_dirtyViewProjectionMatrix) { - std::lock_guard _lock(_mutex); - _viewProjectionMatrix = _projectionMatrix * _viewMatrix; - _dirtyViewProjectionMatrix = false; - } - return _viewProjectionMatrix; -} - -void Camera::setCameraDirection(glm::vec3 cameraDirection) -{ - std::lock_guard _lock(_mutex); - _cameraDirection = std::move(cameraDirection); -} - -glm::vec3 Camera::cameraDirection() const -{ - return _cameraDirection; -} - -void Camera::setViewRotationMatrix(glm::mat4 m) { - std::lock_guard _lock(_mutex); - _localViewRotationMatrix = m; -} - -const glm::mat4& Camera::viewRotationMatrix() const -{ - //return _localViewRotationMatrix; - return _syncedViewRotationMatrix; -} - -void Camera::compileViewRotationMatrix() -{ - std::lock_guard _lock(_mutex); - // convert from quaternion to rotation matrix using glm - //_viewRotationMatrix = glm::mat4_cast(_viewRotation); - - // the camera matrix needs to be rotated inverse to the world - // _viewDirection = glm::rotate(glm::inverse(_viewRotation), _cameraDirection); - //_viewDirection = (glm::inverse(_localViewRotationMatrix) * glm::vec4(_cameraDirection, 0.f)).xyz; - _viewDirection = (glm::inverse(_localViewRotationMatrix) * glm::vec4(_cameraDirection, 0.f)).xyz(); - _viewDirection = glm::normalize(_viewDirection); -} - -void Camera::rotate(const glm::quat& rotation) -{ - std::lock_guard _lock(_mutex); - glm::mat4 tmp = glm::mat4_cast(rotation); - _localViewRotationMatrix = _localViewRotationMatrix * tmp; - //_viewRotation = rotation * _viewRotation; - //_viewRotation = glm::normalize(_viewRotation); -} - -void Camera::setRotation(glm::quat rotation) -{ - std::lock_guard _lock(_mutex); - //_viewRotation = glm::normalize(std::move(rotation)); - _localViewRotationMatrix = glm::mat4_cast(rotation); -} - -void Camera::setRotation(glm::mat4 rotation) -{ - std::lock_guard _lock(_mutex); - _localViewRotationMatrix = std::move(rotation); -} - -//const glm::quat& Camera::rotation() const -//{ - // return _viewRotation; -//} - -void Camera::setFocusPosition(psc pos){ - std::lock_guard _lock(_mutex); +void Camera::setFocusPosition(psc pos) { + std::lock_guard _lock(_mutex); _focusPosition = pos; } -const psc& Camera::focusPosition() const{ - return _focusPosition; +void Camera::setRotation(glm::quat rotation) { + std::lock_guard _lock(_mutex); + _localViewRotationMatrix = glm::mat4_cast(glm::normalize(rotation)); } - -const glm::vec3& Camera::viewDirection() const -{ - return _viewDirection; +void Camera::setLookUpVector(glm::vec3 lookUp) { + std::lock_guard _lock(_mutex); + _lookUp = std::move(lookUp); } -const float& Camera::maxFov() const -{ - return _maxFov; -} - -const float& Camera::sinMaxFov() const -{ - return _sinMaxFov; -} - -void Camera::setMaxFov(float fov) -{ - std::lock_guard _lock(_mutex); - _maxFov = fov; - _sinMaxFov = sin(_maxFov); -} - -void Camera::setScaling(glm::vec2 scaling) -{ - std::lock_guard _lock(_mutex); +void Camera::setScaling(glm::vec2 scaling) { + std::lock_guard _lock(_mutex); _localScaling = std::move(scaling); } -const glm::vec2& Camera::scaling() const -{ - //return _localScaling; +void Camera::setMaxFov(float fov) { + std::lock_guard _lock(_mutex); + _maxFov = fov; + _sinMaxFov = sin(_maxFov); +} + + +////////////////////////////////////////////////////////////////////////////////////////// +// CAMERA ACCESSORS (GETTERS) // +////////////////////////////////////////////////////////////////////////////////////////// + + +const psc& Camera::position() const { + return _syncedPosition; +} + +const psc& Camera::unsynchedPosition() const { + return _localPosition; +} + +const psc& Camera::focusPosition() const { + return _focusPosition; +} + +const glm::vec3& Camera::viewDirection() const { + return _viewDirection; +} + +const glm::vec3& Camera::lookUpVector() const { + return _lookUp; +} + +const glm::vec2& Camera::scaling() const { return _syncedScaling; } -void Camera::setLookUpVector(glm::vec3 lookUp) -{ - std::lock_guard _lock(_mutex); - _lookUp = std::move(lookUp); +float Camera::maxFov() const { + return _maxFov; } -const glm::vec3& Camera::lookUpVector() const -{ - return _lookUp; +float Camera::sinMaxFov() const { + return _sinMaxFov; } +const glm::mat4& Camera::viewRotationMatrix() const { + return _syncedViewRotationMatrix; +} + + + + + +////////////////////////////////////////////////////////////////////////////////////////// +// DEPRECATED CAMERA ACCESSORS (GETTERS) // +////////////////////////////////////////////////////////////////////////////////////////// + +const glm::mat4& Camera::viewMatrix() const { + return sgctInternal.viewMatrix(); +} + +const glm::mat4& Camera::projectionMatrix() const { + return sgctInternal.projectionMatrix(); +} + +const glm::mat4& Camera::viewProjectionMatrix() const { + return sgctInternal.viewProjectionMatrix(); +} + + + + +////////////////////////////////////////////////////////////////////////////////////////// +// CAMERA RELATICVE MUTATORS // +////////////////////////////////////////////////////////////////////////////////////////// + + +void Camera::rotate(const glm::quat& rotation) { + std::lock_guard _lock(_mutex); + glm::mat4 tmp = glm::mat4_cast(rotation); + _localViewRotationMatrix = _localViewRotationMatrix * tmp; +} + + + + +////////////////////////////////////////////////////////////////////////////////////////// +// CAMERA SYNCHRONIZATION // +////////////////////////////////////////////////////////////////////////////////////////// + void Camera::serialize(SyncBuffer* syncBuffer){ _mutex.lock(); @@ -255,6 +197,11 @@ void Camera::postSynchronizationPreDraw(){ _syncedViewRotationMatrix = _sharedViewRotationMatrix; _syncedPosition = _sharedPosition; _syncedScaling = _sharedScaling; + + + glm::vec4 localViewDir = glm::vec4(_viewDirectionInCameraSpace, 0.f); + _viewDirection = (glm::inverse(_localViewRotationMatrix) * localViewDir).xyz(); + _viewDirection = glm::normalize(_viewDirection); _mutex.unlock(); } @@ -269,100 +216,49 @@ void Camera::preSynchronization(){ _mutex.unlock(); } -// -//Camera::Camera() -// : _position(0.f, 0.f, 1.f, 0.f) -// , _focus(0.f, 0.f, 0.f, 0.f) -// , _upVector(0.f, 1.f, 0.f, 0.f) -// , _projectionMatrix(glm::mat4(1.f)) -// , _viewMatrix(glm::mat4(1.f)) -// , _scaling(0.f) -// , _maxFov(0.f) -// , _viewMatrixIsDirty(false) -//{ -// -//} -// -//void Camera::setPosition(psc pos) -//{ -// _position = std::move(pos); -//} -// -//const psc& Camera::position() const -//{ -// return _position; -//} -// -//void Camera::setFocus(psc focus) -//{ -// _focus = std::move(focus); -//} -// -//const psc& Camera::focus() const -//{ -// return _focus; -//} -// -//void Camera::setUpVector(psc upVector) -//{ -// _upVector = std::move(upVector); -//} -// -//const psc& Camera::upVector() const -//{ -// return _upVector; -//} -// -//void Camera::setScaling(float scaling) -//{ -// _scaling = scaling; -//} -// -//float Camera::scaling() const -//{ -// return _scaling; -//} -// -//const glm::mat4& Camera::viewMatrix() const -//{ -// -// return _viewMatrix; -//} -// -//void Camera::setProjectionMatrix(glm::mat4 projectionMatrix) -//{ -// _projectionMatrix = std::move(projectionMatrix); -//} -// -//const glm::mat4& Camera::projectionMatrix() const -//{ -// return _projectionMatrix; -//} -// -//void Camera::setMaxFox(float fov) -//{ -// _maxFov = fov; -//} -// -//float Camera::maxFov() const -//{ -// return _maxFov; -//} -// -//psc Camera::lookVector() const -//{ -// return _focus - _position; -//} -// -//void Camera::invalidateViewMatrix() { -// _viewMatrixIsDirty = true; -//} -// -//void Camera::updateViewMatrix() const { -// if (_viewMatrixIsDirty) { -// _viewMatrix = glm::lookAt(_position.getVec3f(), _focus.getVec3f(), _upVector.getVec3f()); -// _viewMatrixIsDirty = false; -// } -//} + + + + +////////////////////////////////////////////////////////////////////////////////////////// +// SGCT NODE DEPENTENT // +////////////////////////////////////////////////////////////////////////////////////////// +Camera::SgctInternal::SgctInternal() + : _viewMatrix() + , _projectionMatrix() + , _dirtyViewProjectionMatrix(true) +{ + +} + +void Camera::SgctInternal::setViewMatrix(glm::mat4 viewMatrix) { + std::lock_guard _lock(_mutex); + _viewMatrix = std::move(viewMatrix); + _dirtyViewProjectionMatrix = true; +} + + +void Camera::SgctInternal::setProjectionMatrix(glm::mat4 projectionMatrix) { + std::lock_guard _lock(_mutex); + _projectionMatrix = std::move(projectionMatrix); + _dirtyViewProjectionMatrix = true; +} + +const glm::mat4& Camera::SgctInternal::viewMatrix() const { + return _viewMatrix; +} + +const glm::mat4& Camera::SgctInternal::projectionMatrix() const { + return _projectionMatrix; +} + +const glm::mat4& Camera::SgctInternal::viewProjectionMatrix() const { + if (_dirtyViewProjectionMatrix) { + std::lock_guard _lock(_mutex); + _viewProjectionMatrix = _projectionMatrix * _viewMatrix; + _dirtyViewProjectionMatrix = false; + } + return _viewProjectionMatrix; +} } // namespace openspace From 3ec7badcf00b04345181998236a81e32694d6d3a Mon Sep 17 00:00:00 2001 From: Erik Broberg Date: Thu, 14 Apr 2016 17:33:52 -0400 Subject: [PATCH 2/2] Camera: added convenience method for getting viewRotation and added inner class SyncData --- include/openspace/util/camera.h | 53 ++++++++-------- .../globebrowsing/rendering/chunklodglobe.cpp | 2 +- .../globebrowsing/rendering/patchrenderer.cpp | 16 +---- .../rendering/renderableglobe.cpp | 4 +- src/util/camera.cpp | 61 ++++++++++--------- 5 files changed, 63 insertions(+), 73 deletions(-) diff --git a/include/openspace/util/camera.h b/include/openspace/util/camera.h index 7be75e7d23..37e24fa54d 100644 --- a/include/openspace/util/camera.h +++ b/include/openspace/util/camera.h @@ -78,12 +78,12 @@ public: float maxFov() const; float sinMaxFov() const; const glm::mat4& viewRotationMatrix() const; - //const glm::mat4& viewTranslationMatrix() const; - //const glm::mat4& viewScalingMatrix() const; - //@TODO this should simply be called viewMatrix. + + //@TODO this should simply be called viewMatrix! //Rename after removing deprecated methods - //const glm::mat4& combinedViewMatrix() const; + const glm::mat4& combinedViewMatrix() const; + @@ -112,7 +112,7 @@ public: - // Handles SGCT's internal matrices. + // Handles SGCT's internal matrices. Also caches a calculated viewProjection matrix. class SgctInternal { friend class Camera; @@ -140,39 +140,40 @@ public: private: + // Defines what direction in local camera space the camera is looking in. + const glm::vec3 _viewDirectionInCameraSpace; + + psc _focusPosition; glm::vec3 _viewDirection; glm::vec3 _lookUp; + // Class encapsulating the synced data. Are all three variables + // (i.e. local, shared, synced) really neccessary? /EB + template + struct SyncData { + void serialize(SyncBuffer* syncBuffer) { syncBuffer->encode(shared); } + void deserialize(SyncBuffer* syncBuffer) { syncBuffer->decode(shared); } + void postSynchronizationPreDraw() { synced = shared; } + void preSynchronization() { shared = local; } + + T local; + T shared; + T synced; + }; - - mutable std::mutex _mutex; - - - //local variables - glm::mat4 _localViewRotationMatrix; - glm::vec2 _localScaling; - psc _localPosition; - - //shared copies of local variables - glm::vec2 _sharedScaling; - psc _sharedPosition; - glm::mat4 _sharedViewRotationMatrix; - - //synced copies of local variables - glm::vec2 _syncedScaling; - psc _syncedPosition; - glm::mat4 _syncedViewRotationMatrix; - + SyncData _viewRotationMatrix; + SyncData _scaling; + SyncData _position; float _maxFov; float _sinMaxFov; + - // Defines what direction in local camera space the camera is looking in. - const glm::vec3 _viewDirectionInCameraSpace; + mutable std::mutex _mutex; }; diff --git a/modules/globebrowsing/rendering/chunklodglobe.cpp b/modules/globebrowsing/rendering/chunklodglobe.cpp index 48f61c1317..1d4e1aa4e9 100644 --- a/modules/globebrowsing/rendering/chunklodglobe.cpp +++ b/modules/globebrowsing/rendering/chunklodglobe.cpp @@ -119,7 +119,7 @@ namespace openspace { //LDEBUG("min distnace to camera: " << minDistToCamera); Vec3 cameraPos = data.camera.position().dvec3(); - //LDEBUG("cam pos x: " << cameraPos.x << " y: " << cameraPos.y << " z: " << cameraPos.z); + LDEBUG("cam pos x: " << cameraPos.x << " y: " << cameraPos.y << " z: " << cameraPos.z); //LDEBUG("ChunkNode count: " << ChunkNode::instanceCount); } diff --git a/modules/globebrowsing/rendering/patchrenderer.cpp b/modules/globebrowsing/rendering/patchrenderer.cpp index 1e5a63ee1f..93fa3e8083 100644 --- a/modules/globebrowsing/rendering/patchrenderer.cpp +++ b/modules/globebrowsing/rendering/patchrenderer.cpp @@ -95,16 +95,11 @@ namespace openspace { using namespace glm; - // Get camera transform matrix - // TODO : Should only need to fetch the camera transform and use directly - // but this is not currently possible in the camera class. - vec3 cameraPosition = data.camera.position().vec3(); - mat4 viewTransform = inverse(translate(mat4(1.0), cameraPosition)); - viewTransform = mat4(data.camera.viewRotationMatrix()) * viewTransform; // TODO : Model transform should be fetched as a matrix directly. mat4 modelTransform = translate(mat4(1), data.position.vec3()); + mat4 viewTransform = data.camera.combinedViewMatrix(); mat4 modelViewProjectionTransform = data.camera.projectionMatrix() * viewTransform * modelTransform; @@ -162,14 +157,7 @@ namespace openspace { _programObject->activate(); using namespace glm; - Vec3 cameraPos = data.camera.position().dvec3(); - - // Get camera transform matrix - // TODO : Should only need to fetch the camera transform and use directly - // but this is not currently possible in the camera class. - vec3 cameraPosition = data.camera.position().vec3(); - mat4 viewTransform = inverse(translate(mat4(1.0), cameraPosition)); - viewTransform = mat4(data.camera.viewRotationMatrix()) * viewTransform; + const mat4& viewTransform = data.camera.combinedViewMatrix(); // TODO : Model transform should be fetched as a matrix directly. mat4 modelTransform = translate(mat4(1), data.position.vec3()); diff --git a/modules/globebrowsing/rendering/renderableglobe.cpp b/modules/globebrowsing/rendering/renderableglobe.cpp index c22f8d8490..2a7ab9a3f1 100644 --- a/modules/globebrowsing/rendering/renderableglobe.cpp +++ b/modules/globebrowsing/rendering/renderableglobe.cpp @@ -71,8 +71,8 @@ namespace openspace { // Mainly for debugging purposes @AA addProperty(_rotation); - addSwitchValue(std::shared_ptr(new ClipMapGlobe(dictionary)), 1e9); - //addSwitchValue(std::shared_ptr(new ChunkLodGlobe(dictionary)), 1e9); + //addSwitchValue(std::shared_ptr(new ClipMapGlobe(dictionary)), 1e9); + addSwitchValue(std::shared_ptr(new ChunkLodGlobe(dictionary)), 1e9); addSwitchValue(std::shared_ptr(new GlobeMesh(dictionary)), 1e10); } diff --git a/src/util/camera.cpp b/src/util/camera.cpp index 1c3ae6ae32..78c0e3f05e 100644 --- a/src/util/camera.cpp +++ b/src/util/camera.cpp @@ -37,16 +37,10 @@ Camera::Camera() , _viewDirection(0,0,-1) , _viewDirectionInCameraSpace(0.f, 0.f, -1.f) , _focusPosition() - , _localViewRotationMatrix(1.f) - , _localScaling(1.f, 0.f) - , _localPosition() - , _sharedScaling(1.f, 0.f) - , _sharedPosition() - , _sharedViewRotationMatrix(1.f) - , _syncedScaling(1.f, 0.f) - , _syncedPosition() - , _syncedViewRotationMatrix(1.f) { + _scaling.local = glm::vec2(1.f, 0.f); + _viewRotationMatrix.local = glm::mat4(1.0f); + _position.local = psc(); } Camera::~Camera() { } @@ -59,7 +53,7 @@ Camera::~Camera() { } void Camera::setPosition(psc pos){ std::lock_guard _lock(_mutex); - _localPosition = std::move(pos); + _position.local = std::move(pos); } void Camera::setFocusPosition(psc pos) { @@ -69,7 +63,7 @@ void Camera::setFocusPosition(psc pos) { void Camera::setRotation(glm::quat rotation) { std::lock_guard _lock(_mutex); - _localViewRotationMatrix = glm::mat4_cast(glm::normalize(rotation)); + _viewRotationMatrix.local = glm::mat4_cast(glm::normalize(rotation)); } void Camera::setLookUpVector(glm::vec3 lookUp) { @@ -79,7 +73,7 @@ void Camera::setLookUpVector(glm::vec3 lookUp) { void Camera::setScaling(glm::vec2 scaling) { std::lock_guard _lock(_mutex); - _localScaling = std::move(scaling); + _scaling.local = std::move(scaling); } void Camera::setMaxFov(float fov) { @@ -95,11 +89,11 @@ void Camera::setMaxFov(float fov) { const psc& Camera::position() const { - return _syncedPosition; + return _position.synced; } const psc& Camera::unsynchedPosition() const { - return _localPosition; + return _position.local; } const psc& Camera::focusPosition() const { @@ -115,7 +109,7 @@ const glm::vec3& Camera::lookUpVector() const { } const glm::vec2& Camera::scaling() const { - return _syncedScaling; + return _scaling.synced; } float Camera::maxFov() const { @@ -127,7 +121,14 @@ float Camera::sinMaxFov() const { } const glm::mat4& Camera::viewRotationMatrix() const { - return _syncedViewRotationMatrix; + return _viewRotationMatrix.synced; +} + +const glm::mat4& Camera::combinedViewMatrix() const { + glm::vec3 cameraPosition = position().vec3(); + glm::mat4 viewTransform = glm::inverse(glm::translate(glm::mat4(1.0), cameraPosition)); + viewTransform = glm::mat4(viewRotationMatrix()) * viewTransform; + return viewTransform; } @@ -161,7 +162,7 @@ const glm::mat4& Camera::viewProjectionMatrix() const { void Camera::rotate(const glm::quat& rotation) { std::lock_guard _lock(_mutex); glm::mat4 tmp = glm::mat4_cast(rotation); - _localViewRotationMatrix = _localViewRotationMatrix * tmp; + _viewRotationMatrix.local = _viewRotationMatrix.local * tmp; } @@ -174,9 +175,9 @@ void Camera::rotate(const glm::quat& rotation) { void Camera::serialize(SyncBuffer* syncBuffer){ _mutex.lock(); - syncBuffer->encode(_sharedViewRotationMatrix); - syncBuffer->encode(_sharedPosition); - syncBuffer->encode(_sharedScaling); + _viewRotationMatrix.serialize(syncBuffer); + _position.serialize(syncBuffer); + _scaling.serialize(syncBuffer); _mutex.unlock(); } @@ -184,9 +185,9 @@ void Camera::serialize(SyncBuffer* syncBuffer){ void Camera::deserialize(SyncBuffer* syncBuffer){ _mutex.lock(); - syncBuffer->decode(_sharedViewRotationMatrix); - syncBuffer->decode(_sharedPosition); - syncBuffer->decode(_sharedScaling); + _viewRotationMatrix.deserialize(syncBuffer); + _position.deserialize(syncBuffer); + _scaling.deserialize(syncBuffer); _mutex.unlock(); } @@ -194,13 +195,13 @@ void Camera::deserialize(SyncBuffer* syncBuffer){ void Camera::postSynchronizationPreDraw(){ _mutex.lock(); - _syncedViewRotationMatrix = _sharedViewRotationMatrix; - _syncedPosition = _sharedPosition; - _syncedScaling = _sharedScaling; + _viewRotationMatrix.postSynchronizationPreDraw(); + _position.postSynchronizationPreDraw(); + _scaling.postSynchronizationPreDraw(); glm::vec4 localViewDir = glm::vec4(_viewDirectionInCameraSpace, 0.f); - _viewDirection = (glm::inverse(_localViewRotationMatrix) * localViewDir).xyz(); + _viewDirection = (glm::inverse(_viewRotationMatrix.local) * localViewDir).xyz(); _viewDirection = glm::normalize(_viewDirection); _mutex.unlock(); @@ -209,9 +210,9 @@ void Camera::postSynchronizationPreDraw(){ void Camera::preSynchronization(){ _mutex.lock(); - _sharedViewRotationMatrix = _localViewRotationMatrix; - _sharedPosition = _localPosition; - _sharedScaling = _localScaling; + _viewRotationMatrix.preSynchronization(); + _position.preSynchronization(); + _scaling.preSynchronization(); _mutex.unlock(); }