From 0215c63bab65857aae7dc454b1ccca5b74a911a8 Mon Sep 17 00:00:00 2001 From: Kalle Bladin Date: Thu, 19 May 2016 11:29:31 -0400 Subject: [PATCH] Camera class compiles but missing implementation of two functions. --- include/openspace/util/camera.h | 254 +++++++++++--------- src/rendering/renderengine.cpp | 7 +- src/util/camera.cpp | 413 +++++++++++++------------------- 3 files changed, 311 insertions(+), 363 deletions(-) diff --git a/include/openspace/util/camera.h b/include/openspace/util/camera.h index de53056dbf..b9a97e64c6 100644 --- a/include/openspace/util/camera.h +++ b/include/openspace/util/camera.h @@ -29,6 +29,8 @@ // open space includes #include +#include +#include // glm includes #include @@ -38,144 +40,172 @@ 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 Camera { public: Camera(); + Camera(const Camera& o) + : sgctInternal(o.sgctInternal) + , _viewDirectionInCameraSpace(o._viewDirectionInCameraSpace) + , _focusPosition(o._focusPosition) + , _viewDirection(o._viewDirection) + , _lookUp(o._lookUp) + , _viewRotationMatrix(o._viewRotationMatrix) + , _scaling(o._scaling) + , _position(o._position) + , _maxFov(o._maxFov) + , _sinMaxFov(o._sinMaxFov) + { } + ~Camera(); + + + // MUTATORS (SETTERS) + void setPosition(psc pos); - const psc& position() const; - - const psc& unsynchedPosition() const; - - void setModelMatrix(glm::mat4 modelMatrix); - const glm::mat4& modelMatrix() const; - - void setViewMatrix(glm::mat4 viewMatrix); - const glm::mat4& viewMatrix() const; - - void setProjectionMatrix(glm::mat4 projectionMatrix); - const glm::mat4& projectionMatrix() const; - - const glm::mat4& viewProjectionMatrix() const; - - void setCameraDirection(glm::vec3 cameraDirection); - glm::vec3 cameraDirection() const; - void setFocusPosition(psc pos); - const psc& focusPosition() const; + void setRotation(glm::quat rotation); + void setLookUpVector(glm::vec3 lookUp); + void setScaling(glm::vec2 scaling); + void setMaxFov(float fov); - void setViewRotationMatrix(glm::mat4 m); - const glm::mat4& viewRotationMatrix() const; - void compileViewRotationMatrix(); + + + // RELATIVE MUTATORS void rotate(const glm::quat& rotation); - void setRotation(glm::quat rotation); - // const glm::quat& rotation() const; - void setRotation(glm::mat4 rotation); + + + + // ACCESSORS (GETTERS) + + const psc& position() const; + const psc& unsynchedPosition() const; + const psc& focusPosition() const; 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; + const glm::vec2& scaling() const; + float maxFov() const; + float sinMaxFov() const; + const glm::mat4& viewRotationMatrix() const; + + + // TEMPORARY + void setViewRotationMatrix(glm::mat4 m); + void compileViewRotationMatrix(); + + //@TODO this should simply be called viewMatrix! + //Rename after removing deprecated methods + const glm::mat4& combinedViewMatrix() const; + + + + + + // DEPRECATED ACCESSORS (GETTERS) + // @TODO use Camera::SgctInternal interface instead + + [[deprecated("Replaced by Camera::SgctInternal::viewMatrix()")]] + const glm::mat4& viewMatrix() const; + + [[deprecated("Replaced by Camera::SgctInternal::projectionMatrix()")]] + const glm::mat4& projectionMatrix() const; + + [[deprecated("Replaced by Camera::SgctInternal::viewProjectionMatrix()")]] + const glm::mat4& viewProjectionMatrix() const; + + + + + // SYNCHRONIZATION void postSynchronizationPreDraw(); void preSynchronization(); void serialize(SyncBuffer* syncBuffer); void deserialize(SyncBuffer* syncBuffer); + + + // Handles SGCT's internal matrices. Also caches a calculated viewProjection matrix. + 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(); + SgctInternal(const SgctInternal& o) + : _viewMatrix(o._viewMatrix) + , _projectionMatrix(o._projectionMatrix) + , _dirtyViewProjectionMatrix(o._dirtyViewProjectionMatrix) + , _viewProjectionMatrix(o._viewProjectionMatrix) + {} + + + glm::mat4 _viewMatrix; + glm::mat4 _projectionMatrix; + + mutable bool _dirtyViewProjectionMatrix; + mutable glm::mat4 _viewProjectionMatrix; + mutable std::mutex _mutex; + + } sgctInternal; + + 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 { + + SyncData() {} + + // copy constructor + SyncData(const SyncData& d) + : local(d.local), shared(d.shared), synced(d.synced) {} + + 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; + }; + + + SyncData _viewRotationMatrix; + SyncData _scaling; + SyncData _position; + + 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; 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; - }; } // namespace openspace diff --git a/src/rendering/renderengine.cpp b/src/rendering/renderengine.cpp index b738a8f070..a4f3fa6f69 100644 --- a/src/rendering/renderengine.cpp +++ b/src/rendering/renderengine.cpp @@ -283,7 +283,7 @@ bool RenderEngine::initializeGL() { //_mainCamera->setCameraDirection(glm::normalize(-viewdir)); - _mainCamera->setCameraDirection(glm::vec3(0.f, 0.f, -1.f)); + //_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)); @@ -377,9 +377,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)) { _renderer->render(_globalBlackOutFactor, _doPerformanceMeasurements); diff --git a/src/util/camera.cpp b/src/util/camera.cpp index 7cf0f1d1ad..c36ad93671 100644 --- a/src/util/camera.cpp +++ b/src/util/camera.cpp @@ -30,211 +30,177 @@ #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))) - , _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) + : _maxFov(0.f) + , _sinMaxFov(0.f) + , _viewDirection(0,0,-1) + , _viewDirectionInCameraSpace(0.f, 0.f, -1.f) + , _focusPosition() { + _scaling.local = glm::vec2(1.f, 0.f); + _viewRotationMatrix.local = glm::mat4(1.0f); + _position.local = psc(); } -Camera::~Camera() -{ +Camera::~Camera() { } + + + +////////////////////////////////////////////////////////////////////////////////////////// +// CAMERA MUTATORS (SETTERS) // +////////////////////////////////////////////////////////////////////////////////////////// + +void Camera::setPosition(psc pos){ + std::lock_guard _lock(_mutex); + _position.local = std::move(pos); } -void Camera::setPosition(psc pos) -{ - std::lock_guard _lock(_mutex); - _localPosition = std::move(pos); +void Camera::setFocusPosition(psc pos) { + std::lock_guard _lock(_mutex); + _focusPosition = pos; } -const psc& Camera::position() const -{ - return _syncedPosition; +void Camera::setRotation(glm::quat rotation) { + std::lock_guard _lock(_mutex); + _viewRotationMatrix.local = glm::mat4_cast(glm::normalize(rotation)); } -const psc& Camera::unsynchedPosition() const{ - return _localPosition; +void Camera::setLookUpVector(glm::vec3 lookUp) { + std::lock_guard _lock(_mutex); + _lookUp = std::move(lookUp); } -void Camera::setModelMatrix(glm::mat4 modelMatrix){ - std::lock_guard _lock(_mutex); - _modelMatrix = std::move(modelMatrix); +void Camera::setScaling(glm::vec2 scaling) { + std::lock_guard _lock(_mutex); + _scaling.local = std::move(scaling); } -const glm::mat4& Camera::modelMatrix() const{ - return _modelMatrix; +void Camera::setMaxFov(float fov) { + std::lock_guard _lock(_mutex); + _maxFov = fov; + _sinMaxFov = sin(_maxFov); } -void Camera::setViewMatrix(glm::mat4 viewMatrix){ - std::lock_guard _lock(_mutex); - _viewMatrix = std::move(viewMatrix); - _dirtyViewProjectionMatrix = true; + +////////////////////////////////////////////////////////////////////////////////////////// +// CAMERA ACCESSORS (GETTERS) // +////////////////////////////////////////////////////////////////////////////////////////// + + +const psc& Camera::position() const { + return _position.synced; + } -const glm::mat4& Camera::viewMatrix() const{ - return _viewMatrix; +const psc& Camera::unsynchedPosition() const { + return _position.local; } -void Camera::setProjectionMatrix(glm::mat4 projectionMatrix){ - std::lock_guard _lock(_mutex); - _projectionMatrix = std::move(projectionMatrix); - _dirtyViewProjectionMatrix = true; +const psc& Camera::focusPosition() const { + return _focusPosition; } -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; +const glm::vec3& Camera::viewDirection() const { + return _viewDirection; } -void Camera::setCameraDirection(glm::vec3 cameraDirection) -{ - std::lock_guard _lock(_mutex); - _cameraDirection = std::move(cameraDirection); +const glm::vec3& Camera::lookUpVector() const { + return _lookUp; } -glm::vec3 Camera::cameraDirection() const -{ - return _cameraDirection; +const glm::vec2& Camera::scaling() const { + return _scaling.synced; +} + +float Camera::maxFov() const { + return _maxFov; +} + +float Camera::sinMaxFov() const { + return _sinMaxFov; +} + +const glm::mat4& Camera::viewRotationMatrix() const { + return _viewRotationMatrix.synced; } 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::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); - _focusPosition = pos; -} - -const psc& Camera::focusPosition() const{ - return _focusPosition; + */ } -const glm::vec3& Camera::viewDirection() const -{ - return _viewDirection; +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; } -const float& Camera::maxFov() const -{ - return _maxFov; + + + + +////////////////////////////////////////////////////////////////////////////////////////// +// DEPRECATED CAMERA ACCESSORS (GETTERS) // +////////////////////////////////////////////////////////////////////////////////////////// + +const glm::mat4& Camera::viewMatrix() const { + return sgctInternal.viewMatrix(); } -const float& Camera::sinMaxFov() const -{ - return _sinMaxFov; +const glm::mat4& Camera::projectionMatrix() const { + return sgctInternal.projectionMatrix(); } -void Camera::setMaxFov(float fov) -{ - std::lock_guard _lock(_mutex); - _maxFov = fov; - _sinMaxFov = sin(_maxFov); +const glm::mat4& Camera::viewProjectionMatrix() const { + return sgctInternal.viewProjectionMatrix(); } -void Camera::setScaling(glm::vec2 scaling) -{ - std::lock_guard _lock(_mutex); - _localScaling = std::move(scaling); + + +////////////////////////////////////////////////////////////////////////////////////////// +// CAMERA RELATICVE MUTATORS // +////////////////////////////////////////////////////////////////////////////////////////// + + +void Camera::rotate(const glm::quat& rotation) { + std::lock_guard _lock(_mutex); + glm::mat4 tmp = glm::mat4_cast(rotation); + _viewRotationMatrix.local = _viewRotationMatrix.local * tmp; } -const glm::vec2& Camera::scaling() const -{ - //return _localScaling; - return _syncedScaling; -} -void Camera::setLookUpVector(glm::vec3 lookUp) -{ - std::lock_guard _lock(_mutex); - _lookUp = std::move(lookUp); -} -const glm::vec3& Camera::lookUpVector() const -{ - return _lookUp; -} + +////////////////////////////////////////////////////////////////////////////////////////// +// CAMERA SYNCHRONIZATION // +////////////////////////////////////////////////////////////////////////////////////////// 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(); } @@ -242,9 +208,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(); } @@ -252,9 +218,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(_viewRotationMatrix.local) * localViewDir).xyz(); + _viewDirection = glm::normalize(_viewDirection); _mutex.unlock(); } @@ -262,107 +232,56 @@ void Camera::postSynchronizationPreDraw(){ void Camera::preSynchronization(){ _mutex.lock(); - _sharedViewRotationMatrix = _localViewRotationMatrix; - _sharedPosition = _localPosition; - _sharedScaling = _localScaling; + _viewRotationMatrix.preSynchronization(); + _position.preSynchronization(); + _scaling.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