From d7278bfcc84d1fc6ae09cdd2271636a8e8d8bdcf Mon Sep 17 00:00:00 2001 From: Kalle Bladin Date: Sun, 5 Jun 2016 21:07:36 -0400 Subject: [PATCH 1/2] Update camera class. --- include/openspace/util/camera.h | 95 +++++++++++++++++---------------- src/util/camera.cpp | 81 +++++++++++++++------------- 2 files changed, 92 insertions(+), 84 deletions(-) diff --git a/include/openspace/util/camera.h b/include/openspace/util/camera.h index 8b0fd1d867..c244f5c7d5 100644 --- a/include/openspace/util/camera.h +++ b/include/openspace/util/camera.h @@ -1,26 +1,26 @@ /***************************************************************************************** - * * - * OpenSpace * - * * - * Copyright (c) 2014-2016 * - * * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this * - * software and associated documentation files (the "Software"), to deal in the Software * - * without restriction, including without limitation the rights to use, copy, modify, * - * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * - * permit persons to whom the Software is furnished to do so, subject to the following * - * conditions: * - * * - * The above copyright notice and this permission notice shall be included in all copies * - * or substantial portions of the Software. * - * * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * - * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * - * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * - * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * - * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * - ****************************************************************************************/ +* * +* OpenSpace * +* * +* Copyright (c) 2014-2016 * +* * +* 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 __CAMERA_H__ #define __CAMERA_H__ @@ -40,27 +40,27 @@ namespace openspace { /** - This class still needs some more love. Suggested improvements: - - Remove psc from the camera class interface. - - Accessors should return constant references to double precision class members. - - Remove the scaling variable (What is it used for?) - - Remove the maxFov and sinMaxfov variables. Redundant since the fov is embedded - within the perspective projection matrix. - - Remove focusposition, part of the integration with the scale graph. The - "focus position" should not be needed since we assume the camera is always - positioned relative to its origin. When orbiting another object (not in origin), - the focus position should probably be handled outside the camera class - (interaction handler) since it does not affect the state of the camera - (only how it interacts). - - The class might need some more reasonable accessors depending on use cases. - (up vector world space?) - - Make clear which function returns a combined view matrix (things that are - dependent on the separate sgct nodes). + This class still needs some more love. Suggested improvements: + - Remove psc from the camera class interface. + - Accessors should return constant references to double precision class members. + - Remove the scaling variable (What is it used for?) + - Remove the maxFov and sinMaxfov variables. Redundant since the fov is embedded + within the perspective projection matrix. + - Remove focusposition, part of the integration with the scale graph. The + "focus position" should not be needed since we assume the camera is always + positioned relative to its origin. When orbiting another object (not in origin), + the focus position should probably be handled outside the camera class + (interaction handler) since it does not affect the state of the camera + (only how it interacts). + - The class might need some more reasonable accessors depending on use cases. + (up vector world space?) + - Make clear which function returns a combined view matrix (things that are + dependent on the separate sgct nodes). */ class Camera { /** - Used to explicitly show which variables within the Camera class that are used - for caching. + Used to explicitly show which variables within the Camera class that are used + for caching. */ template struct Cached @@ -103,15 +103,18 @@ namespace openspace { // Should return const refs const Vec3& viewDirectionWorldSpace() const; const Vec3& lookUpVectorCameraSpace() const; + const Vec3& lookUpVectorWorldSpace() const; const glm::vec2& scaling() const; const Mat4& viewRotationMatrix() const; const Quat& rotationQuaternion() const; float maxFov() const; float sinMaxFov() const; - + // @TODO this should simply be called viewMatrix! // Or it needs to be changed so that it actually is combined. Right now it is // only the view matrix that is the same for all SGCT cameras. + // Right now this function returns the actual combined matrix which makes some + // of the old calls to the function wrong.. const Mat4& combinedViewMatrix() const; // Synchronization @@ -121,9 +124,9 @@ namespace openspace { void deserialize(SyncBuffer* syncBuffer); /** - Handles SGCT's internal matrices. Also caches a calculated viewProjection - matrix. This is the data that is different for different cameras within - SGCT. + Handles SGCT's internal matrices. Also caches a calculated viewProjection + matrix. This is the data that is different for different cameras within + SGCT. */ class SgctInternal { friend class Camera; @@ -171,8 +174,8 @@ namespace openspace { const glm::mat4& viewProjectionMatrix() const; private: /** - Class encapsulating data that needs to be synched between SGCT nodes. - Are all three variables (i.e. local, shared, synced) really neccessary? /EB + Class encapsulating data that needs to be synched between SGCT nodes. + Are all three variables (i.e. local, shared, synced) really neccessary? /EB */ template struct SyncData { diff --git a/src/util/camera.cpp b/src/util/camera.cpp index b41628b968..2710cf1d36 100644 --- a/src/util/camera.cpp +++ b/src/util/camera.cpp @@ -1,26 +1,26 @@ /***************************************************************************************** - * * - * OpenSpace * - * * - * Copyright (c) 2014-2016 * - * * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this * - * software and associated documentation files (the "Software"), to deal in the Software * - * without restriction, including without limitation the rights to use, copy, modify, * - * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * - * permit persons to whom the Software is furnished to do so, subject to the following * - * conditions: * - * * - * The above copyright notice and this permission notice shall be included in all copies * - * or substantial portions of the Software. * - * * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * - * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * - * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * - * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * - * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * - ****************************************************************************************/ +* * +* OpenSpace * +* * +* Copyright (c) 2014-2016 * +* * +* 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 @@ -43,8 +43,8 @@ namespace openspace { , _focusPosition() { _scaling.local = glm::vec2(1.f, 0.f); - _position.local = Vec3(0, 0, 0); - Vec3 eulerAngles(0, 0, 0); + _position.local = Vec3(1.0, 1.0, 1.0); + Vec3 eulerAngles(1.0, 1.0, 1.0); _rotation.local = Quat(eulerAngles); } @@ -92,35 +92,39 @@ namespace openspace { // Relative mutators void Camera::rotate(Quat rotation) { std::lock_guard _lock(_mutex); - _rotation.local = _rotation.local * rotation; + _rotation.local = rotation * _rotation.local; } // Accessors - const Camera::Vec3& Camera::positionVec3() const{ + const Camera::Vec3& Camera::positionVec3() const { return _position.synced; } - const Camera::Vec3& Camera::unsynchedPositionVec3() const{ + const Camera::Vec3& Camera::unsynchedPositionVec3() const { return _position.local; } - const Camera::Vec3& Camera::focusPositionVec3() const{ + const Camera::Vec3& Camera::focusPositionVec3() const { return _focusPosition; } const Camera::Vec3& Camera::viewDirectionWorldSpace() const { if (_cachedViewDirection.isDirty) { _cachedViewDirection.datum = - glm::inverse(_rotation.local) * Vec3(_VIEW_DIRECTION_CAMERA_SPACE); + _rotation.synced * Vec3(_VIEW_DIRECTION_CAMERA_SPACE); _cachedViewDirection.datum = glm::normalize(_cachedViewDirection.datum); } return _cachedViewDirection.datum; } - + const Camera::Vec3& Camera::lookUpVectorCameraSpace() const { return _LOOKUP_VECTOR_CAMERA_SPACE; } - + + const Camera::Vec3& Camera::lookUpVectorWorldSpace() const { + return glm::normalize(_rotation.synced * Vec3(_LOOKUP_VECTOR_CAMERA_SPACE)); + } + const glm::vec2& Camera::scaling() const { return _scaling.synced; } @@ -138,7 +142,7 @@ namespace openspace { const Camera::Mat4& Camera::viewRotationMatrix() const { if (_cachedViewRotationMatrix.isDirty) { - _cachedViewRotationMatrix.datum = glm::mat4_cast(_rotation.local); + _cachedViewRotationMatrix.datum = glm::mat4_cast(glm::inverse(_rotation.synced)); } return _cachedViewRotationMatrix.datum; } @@ -149,15 +153,16 @@ namespace openspace { const Camera::Mat4& Camera::combinedViewMatrix() const { if (_cachedCombinedViewMatrix.isDirty) { - glm::vec3 cameraPosition = position().vec3(); - glm::mat4 cameraTranslation = - glm::inverse(glm::translate(glm::mat4(1.0), cameraPosition)); + Mat4 cameraTranslation = + glm::inverse(glm::translate(Mat4(1.0), _position.shared)); _cachedCombinedViewMatrix.datum = - glm::mat4(viewRotationMatrix()) * cameraTranslation; + Mat4(sgctInternal.viewMatrix()) * + Mat4(viewRotationMatrix()) * + cameraTranslation; } return _cachedCombinedViewMatrix.datum; } - + // Synchronization void Camera::serialize(SyncBuffer* syncBuffer) { std::lock_guard _lock(_mutex); @@ -266,4 +271,4 @@ namespace openspace { const glm::mat4& Camera::viewProjectionMatrix() const { return sgctInternal.viewProjectionMatrix(); } -} // namespace openspace \ No newline at end of file +} // namespace openspace From 45e7c41690006c9fd6595b06efc1bcdf1861e3eb Mon Sep 17 00:00:00 2001 From: Kalle Bladin Date: Sun, 5 Jun 2016 21:27:20 -0400 Subject: [PATCH 2/2] Update camera to use double precition. --- include/openspace/util/camera.h | 7 +++---- modules/base/rendering/renderablepath.cpp | 2 +- modules/base/rendering/renderableplane.cpp | 2 +- modules/base/rendering/renderabletrail.cpp | 2 +- modules/debugging/rendering/renderabledebugplane.cpp | 2 +- modules/newhorizons/rendering/renderablecrawlingline.cpp | 2 +- src/network/parallelconnection.cpp | 2 +- src/rendering/renderable.cpp | 2 +- src/scene/scenegraphnode.cpp | 2 +- 9 files changed, 11 insertions(+), 12 deletions(-) diff --git a/include/openspace/util/camera.h b/include/openspace/util/camera.h index c244f5c7d5..bf15d1492b 100644 --- a/include/openspace/util/camera.h +++ b/include/openspace/util/camera.h @@ -73,9 +73,9 @@ namespace openspace { // now working with float precision. To be changed to double later. // The reason double does not work yet is because of the setUniform function // in ghoul::opengl - typedef glm::quat Quat; - typedef glm::mat4 Mat4; - typedef glm::vec3 Vec3; + typedef glm::dquat Quat; + typedef glm::dmat4 Mat4; + typedef glm::dvec3 Vec3; // Static constants static const Vec3 _VIEW_DIRECTION_CAMERA_SPACE; @@ -100,7 +100,6 @@ namespace openspace { const Vec3& positionVec3() const; const Vec3& unsynchedPositionVec3() const; const Vec3& focusPositionVec3() const; - // Should return const refs const Vec3& viewDirectionWorldSpace() const; const Vec3& lookUpVectorCameraSpace() const; const Vec3& lookUpVectorWorldSpace() const; diff --git a/modules/base/rendering/renderablepath.cpp b/modules/base/rendering/renderablepath.cpp index d9f2b3b3ef..1edb53936f 100644 --- a/modules/base/rendering/renderablepath.cpp +++ b/modules/base/rendering/renderablepath.cpp @@ -154,7 +154,7 @@ void RenderablePath::render(const RenderData& data) { _programObject->activate(); psc currentPosition = data.position; - glm::mat4 camrot = data.camera.viewRotationMatrix(); + glm::mat4 camrot = glm::mat4(data.camera.viewRotationMatrix()); glm::mat4 transform = glm::mat4(1); // setup the data to the shader diff --git a/modules/base/rendering/renderableplane.cpp b/modules/base/rendering/renderableplane.cpp index e85ce0a3c3..55017bafff 100644 --- a/modules/base/rendering/renderableplane.cpp +++ b/modules/base/rendering/renderableplane.cpp @@ -187,7 +187,7 @@ bool RenderablePlane::deinitialize() { void RenderablePlane::render(const RenderData& data) { glm::mat4 transform = glm::mat4(1.0); if (_billboard) - transform = glm::inverse(data.camera.viewRotationMatrix()); + transform = glm::inverse(glm::mat4(data.camera.viewRotationMatrix())); // Activate shader _shader->activate(); diff --git a/modules/base/rendering/renderabletrail.cpp b/modules/base/rendering/renderabletrail.cpp index e22ef8c0f2..8427dfddc6 100644 --- a/modules/base/rendering/renderabletrail.cpp +++ b/modules/base/rendering/renderabletrail.cpp @@ -148,7 +148,7 @@ void RenderableTrail::render(const RenderData& data) { _programObject->activate(); psc currentPosition = data.position; psc campos = data.camera.position(); - glm::mat4 camrot = data.camera.viewRotationMatrix(); + glm::mat4 camrot = glm::mat4(data.camera.viewRotationMatrix()); glm::mat4 transform = glm::mat4(1); diff --git a/modules/debugging/rendering/renderabledebugplane.cpp b/modules/debugging/rendering/renderabledebugplane.cpp index e7dae5cd15..544dc26c72 100644 --- a/modules/debugging/rendering/renderabledebugplane.cpp +++ b/modules/debugging/rendering/renderabledebugplane.cpp @@ -150,7 +150,7 @@ bool RenderableDebugPlane::deinitialize() { void RenderableDebugPlane::render(const RenderData& data) { glm::mat4 transform = glm::mat4(1.0); if (_billboard) - transform = glm::inverse(data.camera.viewRotationMatrix()); + transform = glm::inverse(glm::mat4(data.camera.viewRotationMatrix())); // Activate shader _shader->activate(); diff --git a/modules/newhorizons/rendering/renderablecrawlingline.cpp b/modules/newhorizons/rendering/renderablecrawlingline.cpp index c4f557ba9f..7d7f8df6c1 100644 --- a/modules/newhorizons/rendering/renderablecrawlingline.cpp +++ b/modules/newhorizons/rendering/renderablecrawlingline.cpp @@ -124,7 +124,7 @@ void RenderableCrawlingLine::render(const RenderData& data) { // fetch data psc currentPosition = data.position; psc campos = data.camera.position(); - glm::mat4 camrot = data.camera.viewRotationMatrix(); + glm::mat4 camrot = glm::mat4(data.camera.viewRotationMatrix()); glm::mat4 transform = glm::mat4(1); diff --git a/src/network/parallelconnection.cpp b/src/network/parallelconnection.cpp index 6c50b40644..1b4a634350 100644 --- a/src/network/parallelconnection.cpp +++ b/src/network/parallelconnection.cpp @@ -1036,7 +1036,7 @@ void ParallelConnection::broadcast(){ //create a keyframe with current position and orientation of camera network::datamessagestructures::PositionKeyframe kf; kf._position = OsEng.interactionHandler().camera()->position(); - kf._viewRotationQuat = glm::quat_cast(OsEng.interactionHandler().camera()->viewRotationMatrix()); + kf._viewRotationQuat = glm::quat_cast(glm::mat4(OsEng.interactionHandler().camera()->viewRotationMatrix())); //timestamp as current runtime of OpenSpace instance kf._timeStamp = OsEng.runTime(); diff --git a/src/rendering/renderable.cpp b/src/rendering/renderable.cpp index 11ee989c06..e64eac80ae 100644 --- a/src/rendering/renderable.cpp +++ b/src/rendering/renderable.cpp @@ -128,7 +128,7 @@ void Renderable::setPscUniforms( { program.setUniform("campos", camera.position().vec4()); program.setUniform("objpos", position.vec4()); - program.setUniform("camrot", camera.viewRotationMatrix()); + program.setUniform("camrot", glm::mat4(camera.viewRotationMatrix())); program.setUniform("scaling", camera.scaling()); } diff --git a/src/scene/scenegraphnode.cpp b/src/scene/scenegraphnode.cpp index 0663a469c7..d314ff3327 100644 --- a/src/scene/scenegraphnode.cpp +++ b/src/scene/scenegraphnode.cpp @@ -380,7 +380,7 @@ bool SceneGraphNode::sphereInsideFrustum(const psc& s_pos, const PowerScaledScal const Camera* camera) { // direction the camera is looking at in power scale - psc psc_camdir = psc(camera->viewDirectionWorldSpace()); + psc psc_camdir = psc(glm::vec3(camera->viewDirectionWorldSpace())); // the position of the camera, moved backwards in the view direction to encapsulate // the sphere radius