From 8a7bcc0036f4d1adb905aa98dec93a9fafc9820a Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Sun, 4 May 2014 17:06:35 +0200 Subject: [PATCH] Cleanup of Camera class --- include/openspace/util/camera.h | 100 +++++++++------ src/interaction/interactionhandler.cpp | 10 +- src/rendering/renderableplanet.cpp | 8 +- src/rendering/renderablevolumecl.cpp | 2 +- src/rendering/renderablevolumeexpert.cpp | 6 +- src/rendering/renderablevolumegl.cpp | 2 +- src/rendering/renderengine.cpp | 8 +- src/scenegraph/scenegraph.cpp | 33 ----- src/scenegraph/scenegraphnode.cpp | 39 +++--- src/util/camera.cpp | 149 +++++++++++++++-------- 10 files changed, 192 insertions(+), 165 deletions(-) diff --git a/include/openspace/util/camera.h b/include/openspace/util/camera.h index a5e6e0e2c5..cf0feecb56 100644 --- a/include/openspace/util/camera.h +++ b/include/openspace/util/camera.h @@ -1,5 +1,29 @@ -#ifndef CAMERA_H -#define CAMERA_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. * + ****************************************************************************************/ + +#ifndef __CAMERA_H__ +#define __CAMERA_H__ // open space includes #include @@ -14,50 +38,50 @@ namespace openspace { class Camera { public: + Camera(); + ~Camera(); - // constructors & destructor - Camera(); - ~Camera(); + void setPosition(psc pos); + const psc& position() const; - void setPosition(psc pos); - const psc& getPosition() const; - - void setViewProjectionMatrix(const glm::mat4 &viewProjectionMatrix); - void setCameraDirection(const glm::vec3 &cameraDirection); - - const glm::mat4 & getViewProjectionMatrix() const; - const glm::mat4 & getViewRotationMatrix() const; - void compileViewRotationMatrix(); - - void rotate(glm::quat rotation); - void setRotation(glm::quat rotation); - const glm::quat & getRotation() const; + void setViewProjectionMatrix(glm::mat4 viewProjectionMatrix); + const glm::mat4& viewProjectionMatrix() const; - const glm::vec3 & getViewDirection() const; - const float & getMaxFov() const; - const float & getSinMaxFov() const; - void setMaxFov(const float &fov); - void setScaling(const glm::vec2 &scaling); - const glm::vec2 & getScaling() const; - - void setLookUp(glm::vec3 lookUp); - const glm::vec3 getLookUp() const; + void setCameraDirection(glm::vec3 cameraDirection); + glm::vec3 cameraDirection() const; + + const glm::mat4& viewRotationMatrix() const; + void compileViewRotationMatrix(); + + void rotate(const glm::quat& rotation); + void setRotation(glm::quat rotation); + const glm::quat& rotation() 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; private: - float maxFov_; - float sinMaxFov_; - psc position_; - glm::mat4 viewProjectionMatrix_; - glm::vec3 viewDirection_; - glm::vec3 cameraDirection_; - glm::vec2 scaling_; + float _maxFov; + float _sinMaxFov; + psc _position; + glm::mat4 _viewProjectionMatrix; + glm::vec3 _viewDirection; + glm::vec3 _cameraDirection; + glm::vec2 _scaling; - glm::quat viewRotation_; - glm::mat4 viewRotationMatrix_; // compiled from the quaternion + glm::quat _viewRotation; + glm::mat4 _viewRotationMatrix; // compiled from the quaternion - glm::vec3 lookUp_; + glm::vec3 _lookUp; }; } // namespace openspace -#endif +#endif // __CAMERA_H__ diff --git a/src/interaction/interactionhandler.cpp b/src/interaction/interactionhandler.cpp index 65a7cc38e1..ae6f22928b 100644 --- a/src/interaction/interactionhandler.cpp +++ b/src/interaction/interactionhandler.cpp @@ -150,7 +150,7 @@ void InteractionHandler::orbit(const glm::quat &rotation) { lockControls(); // the camera position - psc relative = camera_->getPosition(); + psc relative = camera_->position(); // should be changed to something more dynamic =) psc origin; @@ -172,7 +172,7 @@ void InteractionHandler::distance(const pss &distance) { //assert(this_); lockControls(); - psc relative = camera_->getPosition(); + psc relative = camera_->position(); psc origin; if(node_) { origin = node_->getWorldPosition(); @@ -245,8 +245,8 @@ 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_->getLookUp()); - psc viewDir = camera_->getPosition() - node_->getWorldPosition(); + glm::vec3 currentViewYaxis = glm::normalize(camera_->lookUpVector()); + psc viewDir = camera_->position() - node_->getWorldPosition(); glm::vec3 currentViewZaxis = glm::normalize(viewDir.getVec3f()); glm::vec3 currentViewXaxis = glm::normalize(glm::cross(currentViewYaxis, currentViewZaxis)); @@ -293,7 +293,7 @@ void InteractionHandler::trackballRotate(int x, int y) { // Apply quaternion to camera orbit(glm::inverse(quaternion)); camera_->rotate(quaternion); - camera_->setLookUp(glm::rotate(quaternion, camera_->getLookUp())); + camera_->setLookUpVector(glm::rotate(quaternion, camera_->lookUpVector())); _lastTrackballPos = curTrackballPos; } diff --git a/src/rendering/renderableplanet.cpp b/src/rendering/renderableplanet.cpp index b94a97dca3..515e279128 100644 --- a/src/rendering/renderableplanet.cpp +++ b/src/rendering/renderableplanet.cpp @@ -133,16 +133,16 @@ void RenderablePlanet::render(const Camera *camera, const psc &thisPosition) { // fetch data psc currentPosition = thisPosition; - psc campos = camera->getPosition(); - glm::mat4 camrot = camera->getViewRotationMatrix(); - pss scaling = camera->getScaling(); + psc campos = camera->position(); + glm::mat4 camrot = camera->viewRotationMatrix(); + pss scaling = camera->scaling(); // scale the planet to appropriate size since the planet is a unit sphere glm::mat4 transform = glm::mat4(1); // transform = glm::rotate(transform, 4.1f*static_cast(sgct::Engine::instance()->getTime()), glm::vec3(0.0f, 1.0f, 0.0f)); // setup the data to the shader - _programObject->setUniform("ViewProjection", camera->getViewProjectionMatrix()); + _programObject->setUniform("ViewProjection", camera->viewProjectionMatrix()); _programObject->setUniform("ModelTransform", transform); _programObject->setUniform("campos", campos.getVec4f()); _programObject->setUniform("objpos", currentPosition.getVec4f()); diff --git a/src/rendering/renderablevolumecl.cpp b/src/rendering/renderablevolumecl.cpp index 42f90e8b82..b87c395705 100644 --- a/src/rendering/renderablevolumecl.cpp +++ b/src/rendering/renderablevolumecl.cpp @@ -252,7 +252,7 @@ void RenderableVolumeCL::render(const Camera *camera, const psc &thisPosition) { float speed = 50.0f; float time = sgct::Engine::getTime(); - glm::mat4 transform = camera->getViewProjectionMatrix(); + glm::mat4 transform = camera->viewProjectionMatrix(); double factor = pow(10.0,thisPosition[3]); transform = glm::translate(transform, glm::vec3(thisPosition[0]*factor, thisPosition[1]*factor, thisPosition[2]*factor)); diff --git a/src/rendering/renderablevolumeexpert.cpp b/src/rendering/renderablevolumeexpert.cpp index b2be33c6e3..49847911c2 100644 --- a/src/rendering/renderablevolumeexpert.cpp +++ b/src/rendering/renderablevolumeexpert.cpp @@ -341,9 +341,9 @@ void RenderableVolumeExpert::render(const Camera *camera, const psc &thisPositio if( ! _kernel.isValidKernel()) return; - glm::mat4 transform = camera->getViewProjectionMatrix(); - glm::mat4 camTransform = camera->getViewRotationMatrix(); - psc relative = thisPosition-camera->getPosition(); + glm::mat4 transform = camera->viewProjectionMatrix(); + glm::mat4 camTransform = camera->viewRotationMatrix(); + psc relative = thisPosition-camera->position(); transform = transform*camTransform; transform = glm::translate(transform, relative.getVec3f()); diff --git a/src/rendering/renderablevolumegl.cpp b/src/rendering/renderablevolumegl.cpp index 443baa25a2..0d1ae60475 100644 --- a/src/rendering/renderablevolumegl.cpp +++ b/src/rendering/renderablevolumegl.cpp @@ -199,7 +199,7 @@ void RenderableVolumeGL::render(const Camera *camera, const psc &thisPosition) { float speed = 50.0f; float time = sgct::Engine::getTime(); - glm::mat4 transform = camera->getViewProjectionMatrix(); + glm::mat4 transform = camera->viewProjectionMatrix(); double factor = pow(10.0,thisPosition[3]); transform = glm::translate(transform, glm::vec3(thisPosition[0]*factor, thisPosition[1]*factor, thisPosition[2]*factor)); diff --git a/src/rendering/renderengine.cpp b/src/rendering/renderengine.cpp index 03b27413f3..c237dbd851 100644 --- a/src/rendering/renderengine.cpp +++ b/src/rendering/renderengine.cpp @@ -110,7 +110,7 @@ bool RenderEngine::initializeGL() // get viewdirection, stores the direction in the camera, used for culling const glm::vec3 viewdir = glm::normalize(eyePosition - center); _mainCamera->setCameraDirection(-viewdir); - _mainCamera->setLookUp(glm::vec3(0.0, 1.0, 0.0)); + _mainCamera->setLookUpVector(glm::vec3(0.0, 1.0, 0.0)); // set the initial fov to be 0.0 which means everything will be culled float maxFov = 0.0f; @@ -172,9 +172,9 @@ void RenderEngine::render() #define FONT_SIZE 10 #endif - const glm::vec2 scaling = _mainCamera->getScaling(); - const glm::vec3 viewdirection = _mainCamera->getViewDirection(); - const psc position = _mainCamera->getPosition(); + const glm::vec2 scaling = _mainCamera->scaling(); + const glm::vec3 viewdirection = _mainCamera->viewDirection(); + const psc position = _mainCamera->position(); const psc origin = OsEng.interactionHandler().getOrigin(); const pss pssl = (position - origin).length(); diff --git a/src/scenegraph/scenegraph.cpp b/src/scenegraph/scenegraph.cpp index 6e6f7fa37a..bf78480298 100644 --- a/src/scenegraph/scenegraph.cpp +++ b/src/scenegraph/scenegraph.cpp @@ -314,39 +314,6 @@ void SceneGraph::loadModule(const std::string& modulePath) _allNodes.emplace(node->nodeName(), node); _nodes.push_back(node); - - - // std::string moduleName; - // if (element.getValue(configuration::nameKey, moduleName)) { - // std::string parentName; - // if (!element.getValue(configuration::parentKey, parentName)) { - // LWARNING("Could not find 'Parent' key, using 'Root'."); - // parentName = "Root"; - // } - - // auto parentIterator = _allNodes.find(parentName); - // if (parentIterator == _allNodes.end()) { - // LFATAL("Could not find parent named '" - // << parentName << "' for '" << moduleName << "'." - // << " Check module definition order. Skipping module."); - // continue; - // } - - // // allocate SceneGraphNode and initialize with Dictionary - // element.setValue("Path", modulePath); - // SceneGraphNode* node = nullptr; - // node = new SceneGraphNode(element); - // if (node != nullptr) { - // // add to internal data structures - // _allNodes.insert(std::make_pair(moduleName, node)); - // _nodes.push_back(node); - - // // set child and parent - // SceneGraphNode* parentNode = parentIterator->second; - // parentNode->addNode(node); - // } - // } - //} } // Print the tree diff --git a/src/scenegraph/scenegraphnode.cpp b/src/scenegraph/scenegraphnode.cpp index 78dedf9510..6c4942e8a9 100644 --- a/src/scenegraph/scenegraphnode.cpp +++ b/src/scenegraph/scenegraphnode.cpp @@ -130,7 +130,6 @@ bool SceneGraphNode::initialize() if (_renderable != nullptr) _renderable->initialize(); - // deallocate position if (_ephemeris != nullptr) _ephemeris->initialize(); return true; @@ -140,26 +139,19 @@ bool SceneGraphNode::deinitialize() { LDEBUG("Deinitialize: " << _nodeName); - // deallocate the renderable - if (_renderable != nullptr) - delete _renderable; - - // deallocate position - if (_ephemeris != nullptr) - delete _ephemeris; + delete _renderable; + _renderable = nullptr; + delete _ephemeris; + _ephemeris = nullptr; // deallocate the child nodes and delete them, iterate c++11 style - for (auto child : _children) + for (SceneGraphNode* child : _children) delete child; - - // empty the children vector - _children.erase(_children.begin(), _children.end()); + _children.clear(); // reset variables _parent = nullptr; - _renderable = nullptr; - _ephemeris = nullptr; - _nodeName = "Unnamed OpenSpace SceneGraphNode"; + _nodeName = ""; _renderableVisible = false; _boundingSphereVisible = false; _boundingSphere = pss(0.0, 0.0); @@ -176,7 +168,7 @@ void SceneGraphNode::update() void SceneGraphNode::evaluate(const Camera* camera, const psc& parentPosition) { const psc thisPosition = parentPosition + _ephemeris->position(); - const psc camPos = camera->getPosition(); + const psc camPos = camera->position(); const psc toCamera = thisPosition - camPos; // init as not visible @@ -234,7 +226,6 @@ void SceneGraphNode::render(const Camera* camera, const psc& parentPosition) void SceneGraphNode::addNode(SceneGraphNode* child) { // add a child node and set this node to be the parent - child->setParent(this); _children.push_back(child); } @@ -281,7 +272,7 @@ const std::vector& SceneGraphNode::children() const // bounding sphere pss SceneGraphNode::calculateBoundingSphere() { - // set the vounding sphere to 0.0 + // set the bounding sphere to 0.0 _boundingSphere = 0.0; if (_children.size() > 0) { // node @@ -290,7 +281,7 @@ pss SceneGraphNode::calculateBoundingSphere() // loop though all children and find the one furthest away/with the largest // bounding sphere for (size_t i = 0; i < _children.size(); ++i) { - // when positions is dynamix, change this part to fins the most distant + // when positions is dynamic, change this part to fins the most distant // position pss child = _children.at(i)->getPosition().length() + _children.at(i)->calculateBoundingSphere(); @@ -326,20 +317,20 @@ bool SceneGraphNode::sphereInsideFrustum(const psc s_pos, const pss& s_rad, const Camera* camera) { // direction the camera is looking at in power scale - psc psc_camdir = psc(camera->getViewDirection()); + psc psc_camdir = psc(camera->viewDirection()); // the position of the camera, moved backwards in the view direction to encapsulate // the sphere radius - psc U = camera->getPosition() - psc_camdir * s_rad * (1.0 / camera->getSinMaxFov()); + psc U = camera->position() - psc_camdir * s_rad * (1.0 / camera->sinMaxFov()); // the vector to the object from the new position psc D = s_pos - U; const double a = psc_camdir.angle(D); - if (a < camera->getMaxFov()) { + if (a < camera->maxFov()) { // center is inside K'' - D = s_pos - camera->getPosition(); - if (D.length() * psc_camdir.length() * camera->getSinMaxFov() + D = s_pos - camera->position(); + if (D.length() * psc_camdir.length() * camera->sinMaxFov() <= -psc_camdir.dot(D)) { // center is inside K'' and inside K' return D.length() <= s_rad; diff --git a/src/util/camera.cpp b/src/util/camera.cpp index 58ea592662..0c373df8d1 100644 --- a/src/util/camera.cpp +++ b/src/util/camera.cpp @@ -1,3 +1,26 @@ +/***************************************************************************************** + * * + * 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 @@ -8,95 +31,117 @@ #include namespace openspace { - -Camera::Camera() { - scaling_ = glm::vec2(1.0,0.0); - glm::vec3 EulerAngles(0, 0, 0); - viewRotation_ = glm::quat(EulerAngles); + +Camera::Camera() + : _scaling(1.f, 0.f) + , _viewRotation(glm::quat(glm::vec3(0.f, 0.f, 0.f))) +{ } -Camera::~Camera() { - +Camera::~Camera() +{ } -void Camera::setPosition(psc pos) { - position_ = pos; +void Camera::setPosition(psc pos) +{ + _position = std::move(pos); } -const psc& Camera::getPosition() const { - return position_; +const psc& Camera::position() const +{ + return _position; } - -void Camera::setViewProjectionMatrix(const glm::mat4 &viewProjectionMatrix) { - viewProjectionMatrix_ = viewProjectionMatrix; +void Camera::setViewProjectionMatrix(glm::mat4 viewProjectionMatrix) +{ + _viewProjectionMatrix = std::move(viewProjectionMatrix); } -void Camera::setCameraDirection(const glm::vec3 &cameraDirection) { - cameraDirection_ = cameraDirection; +const glm::mat4& Camera::viewProjectionMatrix() const +{ + return _viewProjectionMatrix; } -const glm::mat4 & Camera::getViewProjectionMatrix() const { - return viewProjectionMatrix_; +void Camera::setCameraDirection(glm::vec3 cameraDirection) +{ + _cameraDirection = std::move(cameraDirection); } -const glm::mat4 & Camera::getViewRotationMatrix() const { - return viewRotationMatrix_; +glm::vec3 Camera::cameraDirection() const +{ + return _cameraDirection; } -void Camera::compileViewRotationMatrix() { - // convert from quaternion to rotationmatrix 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::normalize(viewDirection_); +const glm::mat4& Camera::viewRotationMatrix() const +{ + return _viewRotationMatrix; } -void Camera::rotate(glm::quat rotation) { - viewRotation_ = rotation * viewRotation_; - viewRotation_ = glm::normalize(viewRotation_); +void Camera::compileViewRotationMatrix() +{ + // 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::normalize(_viewDirection); } -void Camera::setRotation(glm::quat rotation) { - viewRotation_ = glm::normalize(rotation); +void Camera::rotate(const glm::quat& rotation) +{ + _viewRotation = rotation * _viewRotation; + _viewRotation = glm::normalize(_viewRotation); } -const glm::quat & Camera::getRotation() const { - return viewRotation_; +void Camera::setRotation(glm::quat rotation) +{ + _viewRotation = glm::normalize(std::move(rotation)); } -const glm::vec3 & Camera::getViewDirection() const { - return viewDirection_; +const glm::quat& Camera::rotation() const +{ + return _viewRotation; } -const float & Camera::getMaxFov() const { - return maxFov_; +const glm::vec3& Camera::viewDirection() const +{ + return _viewDirection; } -const float & Camera::getSinMaxFov() const { - return sinMaxFov_; +const float& Camera::maxFov() const +{ + return _maxFov; } -void Camera::setMaxFov(const float &fov) { - maxFov_ = fov; - sinMaxFov_ = sin(maxFov_); +const float& Camera::sinMaxFov() const +{ + return _sinMaxFov; } -void Camera::setScaling(const glm::vec2 &scaling) { - scaling_ = scaling; +void Camera::setMaxFov(float fov) +{ + _maxFov = fov; + _sinMaxFov = sin(_maxFov); } -const glm::vec2 & Camera::getScaling() const { - return scaling_; +void Camera::setScaling(glm::vec2 scaling) +{ + _scaling = std::move(scaling); } -void Camera::setLookUp(glm::vec3 lookUp) { - lookUp_ = lookUp; +const glm::vec2& Camera::scaling() const +{ + return _scaling; } -const glm::vec3 Camera::getLookUp() const { - return lookUp_; +void Camera::setLookUpVector(glm::vec3 lookUp) +{ + _lookUp = std::move(lookUp); } - + +const glm::vec3 Camera::lookUpVector() const +{ + return _lookUp; +} + } // namespace openspace