From 806e691bbf2a9d5f249f4b4c1fc2727f4b97d1df Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 7 Jan 2015 13:41:07 -0800 Subject: [PATCH] restore camera lookAt and keepLookingAt features --- .../example/avatarcontrol/lookAtExample.js | 4 +- interface/src/Camera.cpp | 54 ++++++++++++++++--- interface/src/Camera.h | 24 ++++++--- 3 files changed, 68 insertions(+), 14 deletions(-) diff --git a/examples/example/avatarcontrol/lookAtExample.js b/examples/example/avatarcontrol/lookAtExample.js index 087cd3fc90..e21341469d 100644 --- a/examples/example/avatarcontrol/lookAtExample.js +++ b/examples/example/avatarcontrol/lookAtExample.js @@ -22,7 +22,7 @@ var oldMode = Camera.mode; function cancelLookAt() { if (lookingAtSomething) { lookingAtSomething = false; - //Camera.stopLooking(); + Camera.stopLooking(); Camera.mode = oldMode; releaseMovementKeys(); } @@ -74,7 +74,7 @@ function mousePressEvent(event) { Camera.mode = "independent"; // tell the camera to fix it's look at on the point we clicked - //Camera.keepLookingAt(intersection.intersection); + Camera.keepLookingAt(intersection.intersection); // keep track of the fact that we're in this looking at mode lookingAtSomething = true; diff --git a/interface/src/Camera.cpp b/interface/src/Camera.cpp index 313b7b7774..d08c59eb95 100644 --- a/interface/src/Camera.cpp +++ b/interface/src/Camera.cpp @@ -55,16 +55,44 @@ Camera::Camera() : _farClip(DEFAULT_FAR_CLIP), // default _hmdPosition(), _hmdRotation(), - _targetPosition(), - _targetRotation(), - _scale(1.0f) + _scale(1.0f), + _isKeepLookingAt(false), + _lookingAt(0.0f, 0.0f, 0.0f) { } void Camera::update(float deltaTime) { + if (_isKeepLookingAt) { + lookAt(_lookingAt); + } return; } +void Camera::setPosition(const glm::vec3& position) { + _position = position; +} + +void Camera::setRotation(const glm::quat& rotation) { + _rotation = rotation; + if (_isKeepLookingAt) { + lookAt(_lookingAt); + } +} + +void Camera::setHmdPosition(const glm::vec3& hmdPosition) { + _hmdPosition = hmdPosition; + if (_isKeepLookingAt) { + lookAt(_lookingAt); + } +} + +void Camera::setHmdRotation(const glm::quat& hmdRotation) { + _hmdRotation = hmdRotation; + if (_isKeepLookingAt) { + lookAt(_lookingAt); + } +} + float Camera::getFarClip() const { return (_scale * _farClip < std::numeric_limits::max()) ? _scale * _farClip @@ -81,15 +109,15 @@ void Camera::setFieldOfView(float f) { _fieldOfView = f; } -void Camera::setAspectRatio(float a) { +void Camera::setAspectRatio(float a) { _aspectRatio = a; } -void Camera::setNearClip(float n) { +void Camera::setNearClip(float n) { _nearClip = n; } -void Camera::setFarClip(float f) { +void Camera::setFarClip(float f) { _farClip = f; } @@ -136,3 +164,17 @@ void Camera::setModeString(const QString& mode) { QString Camera::getModeString() const { return modeToString(_mode); } + +void Camera::lookAt(const glm::vec3& lookAt) { + glm::vec3 up = IDENTITY_UP; + glm::mat4 lookAtMatrix = glm::lookAt(_position, lookAt, up); + glm::quat rotation = glm::quat_cast(lookAtMatrix); + rotation.w = -rotation.w; // Rosedale approved + _rotation = rotation; +} + +void Camera::keepLookingAt(const glm::vec3& point) { + lookAt(point); + _isKeepLookingAt = true; + _lookingAt = point; +} diff --git a/interface/src/Camera.h b/interface/src/Camera.h index 7fcf9404cd..7c6951b920 100644 --- a/interface/src/Camera.h +++ b/interface/src/Camera.h @@ -43,9 +43,9 @@ public: void update( float deltaTime ); - void setRotation(const glm::quat& rotation) { _rotation = rotation; }; - void setHmdPosition(const glm::vec3& hmdPosition) { _hmdPosition = hmdPosition; } - void setHmdRotation(const glm::quat& hmdRotation) { _hmdRotation = hmdRotation; }; + void setRotation(const glm::quat& rotation); + void setHmdPosition(const glm::vec3& hmdPosition); + void setHmdRotation(const glm::quat& hmdRotation); void setMode(CameraMode m); void setFieldOfView(float f); @@ -73,13 +73,25 @@ public slots: void setModeString(const QString& mode); glm::vec3 getPosition() const { return _position + _hmdPosition; } - void setPosition(const glm::vec3& position) { _position = position; } + void setPosition(const glm::vec3& position); void setOrientation(const glm::quat& orientation) { setRotation(orientation); } glm::quat getOrientation() const { return getRotation(); } PickRay computePickRay(float x, float y); PickRay computeViewPickRay(float xRatio, float yRatio); + + // These only work on independent cameras + /// one time change to what the camera is looking at + void lookAt(const glm::vec3& value); + + /// fix what the camera is looking at, and keep the camera looking at this even if position changes + void keepLookingAt(const glm::vec3& value); + + /// stops the keep looking at feature, doesn't change what's being looked at, but will stop camera from + /// continuing to update it's orientation to keep looking at the item + void stopLooking() { _isKeepLookingAt = false; } + signals: void modeUpdated(const QString& newMode); @@ -95,9 +107,9 @@ private: glm::quat _rotation; glm::vec3 _hmdPosition; glm::quat _hmdRotation; - glm::vec3 _targetPosition; - glm::quat _targetRotation; float _scale; + bool _isKeepLookingAt; + glm::vec3 _lookingAt; }; #endif // hifi_Camera_h