[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 | |
---|
[8138] | 50 | //Camera::Camera(BaseObject* creator) : StaticEntity(creator) |
---|
| 51 | Camera::Camera(BaseObject* creator) : MovableEntity(creator) |
---|
[2019] | 52 | { |
---|
| 53 | RegisterObject(Camera); |
---|
[1505] | 54 | |
---|
[5929] | 55 | if (!GameMode::showsGraphics()) |
---|
| 56 | ThrowException(AbortLoading, "Can't create Camera, no graphics."); |
---|
[2662] | 57 | if (!this->getScene()) |
---|
| 58 | ThrowException(AbortLoading, "Can't create Camera, no scene."); |
---|
| 59 | if (!this->getScene()->getSceneManager()) |
---|
| 60 | ThrowException(AbortLoading, "Can't create Camera, no scene-manager given."); |
---|
| 61 | if (!this->getScene()->getRootSceneNode()) |
---|
| 62 | ThrowException(AbortLoading, "Can't create Camera, no root-scene-node given."); |
---|
[1505] | 63 | |
---|
[2019] | 64 | this->camera_ = this->getScene()->getSceneManager()->createCamera(getUniqueNumberString()); |
---|
[6501] | 65 | static_cast<Ogre::MovableObject*>(this->camera_)->setUserAny(Ogre::Any(static_cast<OrxonoxClass*>(this))); |
---|
[2662] | 66 | this->cameraNode_ = this->getScene()->getRootSceneNode()->createChildSceneNode(); |
---|
| 67 | this->attachNode(this->cameraNode_); |
---|
| 68 | this->cameraNode_->attachObject(this->camera_); |
---|
[2019] | 69 | |
---|
| 70 | this->bHasFocus_ = false; |
---|
| 71 | this->bDrag_ = false; |
---|
[6417] | 72 | this->lastDtLagged_ = false; |
---|
[2019] | 73 | |
---|
[5929] | 74 | this->setSyncMode(0x0); |
---|
[2019] | 75 | |
---|
| 76 | this->setConfigValues(); |
---|
[8079] | 77 | |
---|
| 78 | this->configvaluecallback_changedFovAndAspectRatio(); |
---|
[2019] | 79 | this->configvaluecallback_changedNearClipDistance(); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | Camera::~Camera() |
---|
[1989] | 83 | { |
---|
[2019] | 84 | if (this->isInitialized()) |
---|
| 85 | { |
---|
| 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 | { |
---|
[8079] | 101 | SetConfigValue(fov_, 80.0f) |
---|
| 102 | .description("Horizontal field of view in degrees") |
---|
| 103 | .callback(this, &Camera::configvaluecallback_changedFovAndAspectRatio); |
---|
| 104 | SetConfigValue(aspectRatio_, 1.0f) |
---|
| 105 | .description("Aspect ratio of pixels (width / height)") |
---|
| 106 | .callback(this, &Camera::configvaluecallback_changedFovAndAspectRatio); |
---|
| 107 | SetConfigValue(nearClipDistance_, 1.0f) |
---|
| 108 | .description("Distance from the camera where close objects will be clipped") |
---|
| 109 | .callback(this, &Camera::configvaluecallback_changedNearClipDistance); |
---|
[2019] | 110 | } |
---|
[1505] | 111 | |
---|
[8079] | 112 | /** |
---|
| 113 | @brief Update FOV and the aspect ratio of the camera after the config values or the window's size have changed. |
---|
| 114 | */ |
---|
| 115 | void Camera::configvaluecallback_changedFovAndAspectRatio() |
---|
| 116 | { |
---|
| 117 | // the aspect ratio of the window (width / height) has to be multiplied with the pixels aspect ratio (this->aspectRatio_) |
---|
| 118 | float aspectRatio = this->aspectRatio_ * this->getWindowWidth() / this->getWindowHeight(); |
---|
| 119 | this->camera_->setAspectRatio(aspectRatio); |
---|
| 120 | |
---|
| 121 | // Since we use horizontal FOV, we have to calculate FOVy by dividing by the aspect ratio and using some tangents |
---|
| 122 | Radian fovy(2 * atan(tan(Degree(this->fov_).valueRadians() / 2) / aspectRatio)); |
---|
| 123 | this->camera_->setFOVy(fovy); |
---|
| 124 | } |
---|
| 125 | |
---|
[2019] | 126 | void Camera::configvaluecallback_changedNearClipDistance() |
---|
| 127 | { |
---|
| 128 | this->camera_->setNearClipDistance(this->nearClipDistance_); |
---|
| 129 | } |
---|
[1505] | 130 | |
---|
[8079] | 131 | /** |
---|
| 132 | @brief Inherited from WindowEventListener. |
---|
| 133 | */ |
---|
| 134 | void Camera::windowResized(unsigned int newWidth, unsigned int newHeight) |
---|
| 135 | { |
---|
| 136 | this->configvaluecallback_changedFovAndAspectRatio(); |
---|
| 137 | } |
---|
| 138 | |
---|
[2019] | 139 | void Camera::tick(float dt) |
---|
| 140 | { |
---|
[2662] | 141 | SUPER(Camera, tick, dt); |
---|
[1505] | 142 | |
---|
[7163] | 143 | if (this->bDrag_ && this->getTimeFactor() != 0) |
---|
[2662] | 144 | { |
---|
| 145 | // this stuff here may need some adjustments |
---|
[6417] | 146 | float poscoeff = 15.0f * dt / this->getTimeFactor(); |
---|
| 147 | float anglecoeff = 7.0f * dt / this->getTimeFactor(); |
---|
| 148 | // Only clamp if fps rate is actually falling. Occasional high dts should |
---|
| 149 | // not be clamped to reducing lagging effects. |
---|
| 150 | if (poscoeff > 1.0f) |
---|
| 151 | { |
---|
| 152 | if (this->lastDtLagged_) |
---|
| 153 | poscoeff = 1.0f; |
---|
| 154 | else |
---|
| 155 | this->lastDtLagged_ = true; |
---|
| 156 | } |
---|
| 157 | else |
---|
| 158 | this->lastDtLagged_ = false; |
---|
[1505] | 159 | |
---|
[6417] | 160 | if (anglecoeff > 1.0f) |
---|
| 161 | { |
---|
| 162 | if (this->lastDtLagged_) |
---|
| 163 | anglecoeff = 1.0f; |
---|
| 164 | else |
---|
| 165 | this->lastDtLagged_ = true; |
---|
| 166 | } |
---|
| 167 | else |
---|
| 168 | this->lastDtLagged_ = false; |
---|
| 169 | |
---|
[2757] | 170 | Vector3 offset = this->getWorldPosition() - this->cameraNode_->_getDerivedPosition(); |
---|
[6417] | 171 | this->cameraNode_->translate(poscoeff * offset); |
---|
[2662] | 172 | |
---|
[6417] | 173 | this->cameraNode_->setOrientation(Quaternion::Slerp(anglecoeff, this->cameraNode_->_getDerivedOrientation(), this->getWorldOrientation(), true)); |
---|
[2662] | 174 | } |
---|
[6417] | 175 | |
---|
| 176 | // Update sound listener transformation |
---|
| 177 | if (GameMode::playsSound() && this->bHasFocus_) |
---|
| 178 | { |
---|
| 179 | SoundManager::getInstance().setListenerPosition(this->getWorldPosition()); |
---|
| 180 | SoundManager::getInstance().setListenerOrientation(this->getWorldOrientation()); |
---|
| 181 | } |
---|
[2019] | 182 | } |
---|
[1505] | 183 | |
---|
[2019] | 184 | void Camera::requestFocus() |
---|
| 185 | { |
---|
[2073] | 186 | CameraManager::getInstance().requestFocus(this); |
---|
[2019] | 187 | } |
---|
[1989] | 188 | |
---|
[2019] | 189 | void Camera::releaseFocus() |
---|
| 190 | { |
---|
[2073] | 191 | CameraManager::getInstance().releaseFocus(this); |
---|
[2019] | 192 | } |
---|
| 193 | |
---|
| 194 | /** |
---|
| 195 | what to do when camera loses focus (do not request focus in this function!!) |
---|
[2073] | 196 | this is called by the CameraManager singleton class to notify the camera |
---|
[2019] | 197 | */ |
---|
| 198 | void Camera::removeFocus() |
---|
| 199 | { |
---|
| 200 | this->bHasFocus_ = false; |
---|
| 201 | } |
---|
| 202 | |
---|
[2662] | 203 | void Camera::setFocus() |
---|
[2019] | 204 | { |
---|
| 205 | this->bHasFocus_ = true; |
---|
[2662] | 206 | CameraManager::getInstance().useCamera(this->camera_); |
---|
[2019] | 207 | } |
---|
[2662] | 208 | |
---|
| 209 | void Camera::setDrag(bool bDrag) |
---|
| 210 | { |
---|
| 211 | if (bDrag != this->bDrag_) |
---|
| 212 | { |
---|
| 213 | this->bDrag_ = bDrag; |
---|
| 214 | |
---|
| 215 | if (!bDrag) |
---|
| 216 | { |
---|
| 217 | this->attachNode(this->cameraNode_); |
---|
| 218 | this->cameraNode_->setPosition(Vector3::ZERO); |
---|
| 219 | this->cameraNode_->setOrientation(Quaternion::IDENTITY); |
---|
| 220 | } |
---|
| 221 | else |
---|
| 222 | { |
---|
| 223 | this->detachNode(this->cameraNode_); |
---|
| 224 | this->cameraNode_->setPosition(this->getWorldPosition()); |
---|
| 225 | this->cameraNode_->setOrientation(this->getWorldOrientation()); |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | } |
---|
[1505] | 229 | } |
---|