[1505] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * Benjamin Knecht |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "Camera.h" |
---|
| 30 | |
---|
[2023] | 31 | #include <OgreCamera.h> |
---|
[1505] | 32 | #include <OgreSceneManager.h> |
---|
| 33 | #include <OgreSceneNode.h> |
---|
| 34 | |
---|
[2171] | 35 | #include "util/Exception.h" |
---|
[3196] | 36 | #include "util/String.h" |
---|
[1505] | 37 | #include "core/CoreIncludes.h" |
---|
[2019] | 38 | #include "core/ConfigValueIncludes.h" |
---|
| 39 | #include "objects/Scene.h" |
---|
[2073] | 40 | #include "CameraManager.h" |
---|
[1505] | 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
[2019] | 44 | CreateFactory(Camera); |
---|
[1993] | 45 | |
---|
[2662] | 46 | Camera::Camera(BaseObject* creator) : StaticEntity(creator) |
---|
[2019] | 47 | { |
---|
| 48 | RegisterObject(Camera); |
---|
[1505] | 49 | |
---|
[2662] | 50 | if (!this->getScene()) |
---|
| 51 | ThrowException(AbortLoading, "Can't create Camera, no scene."); |
---|
| 52 | if (!this->getScene()->getSceneManager()) |
---|
| 53 | ThrowException(AbortLoading, "Can't create Camera, no scene-manager given."); |
---|
| 54 | if (!this->getScene()->getRootSceneNode()) |
---|
| 55 | ThrowException(AbortLoading, "Can't create Camera, no root-scene-node given."); |
---|
[1505] | 56 | |
---|
[2019] | 57 | this->camera_ = this->getScene()->getSceneManager()->createCamera(getUniqueNumberString()); |
---|
[2662] | 58 | this->cameraNode_ = this->getScene()->getRootSceneNode()->createChildSceneNode(); |
---|
| 59 | this->attachNode(this->cameraNode_); |
---|
| 60 | this->cameraNode_->attachObject(this->camera_); |
---|
[2019] | 61 | |
---|
| 62 | this->bHasFocus_ = false; |
---|
| 63 | this->bDrag_ = false; |
---|
| 64 | this->nearClipDistance_ = 1; |
---|
| 65 | |
---|
| 66 | this->setObjectMode(0x0); |
---|
| 67 | |
---|
| 68 | this->setConfigValues(); |
---|
| 69 | this->configvaluecallback_changedNearClipDistance(); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | Camera::~Camera() |
---|
[1989] | 73 | { |
---|
[2019] | 74 | if (this->isInitialized()) |
---|
| 75 | { |
---|
| 76 | this->releaseFocus(); |
---|
[2662] | 77 | |
---|
| 78 | this->cameraNode_->detachAllObjects(); |
---|
| 79 | this->getScene()->getSceneManager()->destroyCamera(this->camera_); |
---|
| 80 | |
---|
| 81 | if (this->bDrag_) |
---|
| 82 | this->detachNode(this->cameraNode_); |
---|
| 83 | |
---|
| 84 | if (this->getScene()->getSceneManager()) |
---|
| 85 | this->getScene()->getSceneManager()->destroySceneNode(this->cameraNode_->getName()); |
---|
[2019] | 86 | } |
---|
[1989] | 87 | } |
---|
[1505] | 88 | |
---|
[2019] | 89 | void Camera::setConfigValues() |
---|
| 90 | { |
---|
| 91 | SetConfigValue(nearClipDistance_, 1.0f).callback(this, &Camera::configvaluecallback_changedNearClipDistance); |
---|
| 92 | } |
---|
[1505] | 93 | |
---|
[2019] | 94 | void Camera::configvaluecallback_changedNearClipDistance() |
---|
| 95 | { |
---|
| 96 | this->camera_->setNearClipDistance(this->nearClipDistance_); |
---|
| 97 | } |
---|
[1505] | 98 | |
---|
[2019] | 99 | void Camera::tick(float dt) |
---|
| 100 | { |
---|
[2662] | 101 | SUPER(Camera, tick, dt); |
---|
[1505] | 102 | |
---|
[2662] | 103 | if (this->bDrag_) |
---|
| 104 | { |
---|
| 105 | // this stuff here may need some adjustments |
---|
| 106 | float coeff = min(1.0f, 15.0f * dt); |
---|
[1505] | 107 | |
---|
[2757] | 108 | Vector3 offset = this->getWorldPosition() - this->cameraNode_->_getDerivedPosition(); |
---|
[2662] | 109 | this->cameraNode_->translate(coeff * offset); |
---|
| 110 | |
---|
[2757] | 111 | this->cameraNode_->setOrientation(Quaternion::Slerp(coeff, this->cameraNode_->_getDerivedOrientation(), this->getWorldOrientation(), true)); |
---|
[2662] | 112 | //this->cameraNode_->setOrientation(this->getWorldOrientation()); |
---|
| 113 | } |
---|
[2019] | 114 | } |
---|
[1505] | 115 | |
---|
[2019] | 116 | void Camera::requestFocus() |
---|
| 117 | { |
---|
[2073] | 118 | CameraManager::getInstance().requestFocus(this); |
---|
[2019] | 119 | } |
---|
[1989] | 120 | |
---|
[2019] | 121 | void Camera::releaseFocus() |
---|
| 122 | { |
---|
[2073] | 123 | CameraManager::getInstance().releaseFocus(this); |
---|
[2019] | 124 | } |
---|
| 125 | |
---|
| 126 | /** |
---|
| 127 | what to do when camera loses focus (do not request focus in this function!!) |
---|
[2073] | 128 | this is called by the CameraManager singleton class to notify the camera |
---|
[2019] | 129 | */ |
---|
| 130 | void Camera::removeFocus() |
---|
| 131 | { |
---|
| 132 | this->bHasFocus_ = false; |
---|
| 133 | } |
---|
| 134 | |
---|
[2662] | 135 | void Camera::setFocus() |
---|
[2019] | 136 | { |
---|
| 137 | this->bHasFocus_ = true; |
---|
[2662] | 138 | CameraManager::getInstance().useCamera(this->camera_); |
---|
[2019] | 139 | } |
---|
[2662] | 140 | |
---|
| 141 | void Camera::setDrag(bool bDrag) |
---|
| 142 | { |
---|
| 143 | if (bDrag != this->bDrag_) |
---|
| 144 | { |
---|
| 145 | this->bDrag_ = bDrag; |
---|
| 146 | |
---|
| 147 | if (!bDrag) |
---|
| 148 | { |
---|
| 149 | this->attachNode(this->cameraNode_); |
---|
| 150 | this->cameraNode_->setPosition(Vector3::ZERO); |
---|
| 151 | this->cameraNode_->setOrientation(Quaternion::IDENTITY); |
---|
| 152 | } |
---|
| 153 | else |
---|
| 154 | { |
---|
| 155 | this->detachNode(this->cameraNode_); |
---|
| 156 | this->cameraNode_->setPosition(this->getWorldPosition()); |
---|
| 157 | this->cameraNode_->setOrientation(this->getWorldOrientation()); |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | } |
---|
[1505] | 161 | } |
---|