[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 | |
---|
[3280] | 31 | #include <algorithm> |
---|
[2023] | 32 | #include <OgreCamera.h> |
---|
[1505] | 33 | #include <OgreSceneManager.h> |
---|
| 34 | #include <OgreSceneNode.h> |
---|
| 35 | |
---|
[2171] | 36 | #include "util/Exception.h" |
---|
[3280] | 37 | #include "util/StringUtils.h" |
---|
[1505] | 38 | #include "core/CoreIncludes.h" |
---|
[2019] | 39 | #include "core/ConfigValueIncludes.h" |
---|
[5929] | 40 | #include "core/GameMode.h" |
---|
| 41 | #include "core/GUIManager.h" |
---|
[5735] | 42 | #include "Scene.h" |
---|
[2073] | 43 | #include "CameraManager.h" |
---|
[6417] | 44 | #include "sound/SoundManager.h" |
---|
[1505] | 45 | |
---|
| 46 | namespace orxonox |
---|
| 47 | { |
---|
[2019] | 48 | CreateFactory(Camera); |
---|
[1993] | 49 | |
---|
[2662] | 50 | Camera::Camera(BaseObject* creator) : StaticEntity(creator) |
---|
[2019] | 51 | { |
---|
| 52 | RegisterObject(Camera); |
---|
[1505] | 53 | |
---|
[5929] | 54 | if (!GameMode::showsGraphics()) |
---|
| 55 | ThrowException(AbortLoading, "Can't create Camera, no graphics."); |
---|
[2662] | 56 | if (!this->getScene()) |
---|
| 57 | ThrowException(AbortLoading, "Can't create Camera, no scene."); |
---|
| 58 | if (!this->getScene()->getSceneManager()) |
---|
| 59 | ThrowException(AbortLoading, "Can't create Camera, no scene-manager given."); |
---|
| 60 | if (!this->getScene()->getRootSceneNode()) |
---|
| 61 | ThrowException(AbortLoading, "Can't create Camera, no root-scene-node given."); |
---|
[1505] | 62 | |
---|
[2019] | 63 | this->camera_ = this->getScene()->getSceneManager()->createCamera(getUniqueNumberString()); |
---|
[6417] | 64 | this->camera_->setUserObject(this); |
---|
[2662] | 65 | this->cameraNode_ = this->getScene()->getRootSceneNode()->createChildSceneNode(); |
---|
| 66 | this->attachNode(this->cameraNode_); |
---|
| 67 | this->cameraNode_->attachObject(this->camera_); |
---|
[2019] | 68 | |
---|
| 69 | this->bHasFocus_ = false; |
---|
| 70 | this->bDrag_ = false; |
---|
| 71 | this->nearClipDistance_ = 1; |
---|
[6417] | 72 | this->lastDtLagged_ = false; |
---|
[2019] | 73 | |
---|
[5929] | 74 | this->setSyncMode(0x0); |
---|
[2019] | 75 | |
---|
| 76 | this->setConfigValues(); |
---|
| 77 | this->configvaluecallback_changedNearClipDistance(); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | Camera::~Camera() |
---|
[1989] | 81 | { |
---|
[2019] | 82 | if (this->isInitialized()) |
---|
| 83 | { |
---|
[5929] | 84 | if (GUIManager::getInstance().getCamera() == this->camera_) |
---|
| 85 | GUIManager::getInstance().setCamera(NULL); |
---|
[2019] | 86 | this->releaseFocus(); |
---|
[2662] | 87 | |
---|
| 88 | this->cameraNode_->detachAllObjects(); |
---|
| 89 | this->getScene()->getSceneManager()->destroyCamera(this->camera_); |
---|
| 90 | |
---|
| 91 | if (this->bDrag_) |
---|
| 92 | this->detachNode(this->cameraNode_); |
---|
| 93 | |
---|
| 94 | if (this->getScene()->getSceneManager()) |
---|
| 95 | this->getScene()->getSceneManager()->destroySceneNode(this->cameraNode_->getName()); |
---|
[2019] | 96 | } |
---|
[1989] | 97 | } |
---|
[1505] | 98 | |
---|
[2019] | 99 | void Camera::setConfigValues() |
---|
| 100 | { |
---|
| 101 | SetConfigValue(nearClipDistance_, 1.0f).callback(this, &Camera::configvaluecallback_changedNearClipDistance); |
---|
| 102 | } |
---|
[1505] | 103 | |
---|
[2019] | 104 | void Camera::configvaluecallback_changedNearClipDistance() |
---|
| 105 | { |
---|
| 106 | this->camera_->setNearClipDistance(this->nearClipDistance_); |
---|
| 107 | } |
---|
[1505] | 108 | |
---|
[2019] | 109 | void Camera::tick(float dt) |
---|
| 110 | { |
---|
[2662] | 111 | SUPER(Camera, tick, dt); |
---|
[1505] | 112 | |
---|
[2662] | 113 | if (this->bDrag_) |
---|
| 114 | { |
---|
| 115 | // this stuff here may need some adjustments |
---|
[6417] | 116 | float poscoeff = 15.0f * dt / this->getTimeFactor(); |
---|
| 117 | float anglecoeff = 7.0f * dt / this->getTimeFactor(); |
---|
| 118 | // Only clamp if fps rate is actually falling. Occasional high dts should |
---|
| 119 | // not be clamped to reducing lagging effects. |
---|
| 120 | if (poscoeff > 1.0f) |
---|
| 121 | { |
---|
| 122 | if (this->lastDtLagged_) |
---|
| 123 | poscoeff = 1.0f; |
---|
| 124 | else |
---|
| 125 | this->lastDtLagged_ = true; |
---|
| 126 | } |
---|
| 127 | else |
---|
| 128 | this->lastDtLagged_ = false; |
---|
[1505] | 129 | |
---|
[6417] | 130 | if (anglecoeff > 1.0f) |
---|
| 131 | { |
---|
| 132 | if (this->lastDtLagged_) |
---|
| 133 | anglecoeff = 1.0f; |
---|
| 134 | else |
---|
| 135 | this->lastDtLagged_ = true; |
---|
| 136 | } |
---|
| 137 | else |
---|
| 138 | this->lastDtLagged_ = false; |
---|
| 139 | |
---|
[2757] | 140 | Vector3 offset = this->getWorldPosition() - this->cameraNode_->_getDerivedPosition(); |
---|
[6417] | 141 | this->cameraNode_->translate(poscoeff * offset); |
---|
[2662] | 142 | |
---|
[6417] | 143 | this->cameraNode_->setOrientation(Quaternion::Slerp(anglecoeff, this->cameraNode_->_getDerivedOrientation(), this->getWorldOrientation(), true)); |
---|
[2662] | 144 | } |
---|
[6417] | 145 | |
---|
| 146 | // Update sound listener transformation |
---|
| 147 | if (GameMode::playsSound() && this->bHasFocus_) |
---|
| 148 | { |
---|
| 149 | SoundManager::getInstance().setListenerPosition(this->getWorldPosition()); |
---|
| 150 | SoundManager::getInstance().setListenerOrientation(this->getWorldOrientation()); |
---|
| 151 | } |
---|
[2019] | 152 | } |
---|
[1505] | 153 | |
---|
[2019] | 154 | void Camera::requestFocus() |
---|
| 155 | { |
---|
[2073] | 156 | CameraManager::getInstance().requestFocus(this); |
---|
[2019] | 157 | } |
---|
[1989] | 158 | |
---|
[2019] | 159 | void Camera::releaseFocus() |
---|
| 160 | { |
---|
[2073] | 161 | CameraManager::getInstance().releaseFocus(this); |
---|
[2019] | 162 | } |
---|
| 163 | |
---|
| 164 | /** |
---|
| 165 | what to do when camera loses focus (do not request focus in this function!!) |
---|
[2073] | 166 | this is called by the CameraManager singleton class to notify the camera |
---|
[2019] | 167 | */ |
---|
| 168 | void Camera::removeFocus() |
---|
| 169 | { |
---|
| 170 | this->bHasFocus_ = false; |
---|
| 171 | } |
---|
| 172 | |
---|
[2662] | 173 | void Camera::setFocus() |
---|
[2019] | 174 | { |
---|
| 175 | this->bHasFocus_ = true; |
---|
[2662] | 176 | CameraManager::getInstance().useCamera(this->camera_); |
---|
[2019] | 177 | } |
---|
[2662] | 178 | |
---|
| 179 | void Camera::setDrag(bool bDrag) |
---|
| 180 | { |
---|
| 181 | if (bDrag != this->bDrag_) |
---|
| 182 | { |
---|
| 183 | this->bDrag_ = bDrag; |
---|
| 184 | |
---|
| 185 | if (!bDrag) |
---|
| 186 | { |
---|
| 187 | this->attachNode(this->cameraNode_); |
---|
| 188 | this->cameraNode_->setPosition(Vector3::ZERO); |
---|
| 189 | this->cameraNode_->setOrientation(Quaternion::IDENTITY); |
---|
| 190 | } |
---|
| 191 | else |
---|
| 192 | { |
---|
| 193 | this->detachNode(this->cameraNode_); |
---|
| 194 | this->cameraNode_->setPosition(this->getWorldPosition()); |
---|
| 195 | this->cameraNode_->setOrientation(this->getWorldOrientation()); |
---|
| 196 | } |
---|
| 197 | } |
---|
| 198 | } |
---|
[1505] | 199 | } |
---|