| [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" | 
|---|
| [1505] | 44 |  | 
|---|
 | 45 | namespace orxonox | 
|---|
 | 46 | { | 
|---|
| [2019] | 47 |     CreateFactory(Camera); | 
|---|
| [1993] | 48 |  | 
|---|
| [2662] | 49 |     Camera::Camera(BaseObject* creator) : StaticEntity(creator) | 
|---|
| [2019] | 50 |     { | 
|---|
 | 51 |         RegisterObject(Camera); | 
|---|
| [1505] | 52 |  | 
|---|
| [5929] | 53 |         if (!GameMode::showsGraphics()) | 
|---|
 | 54 |             ThrowException(AbortLoading, "Can't create Camera, no graphics."); | 
|---|
| [2662] | 55 |         if (!this->getScene()) | 
|---|
 | 56 |             ThrowException(AbortLoading, "Can't create Camera, no scene."); | 
|---|
 | 57 |         if (!this->getScene()->getSceneManager()) | 
|---|
 | 58 |             ThrowException(AbortLoading, "Can't create Camera, no scene-manager given."); | 
|---|
 | 59 |         if (!this->getScene()->getRootSceneNode()) | 
|---|
 | 60 |             ThrowException(AbortLoading, "Can't create Camera, no root-scene-node given."); | 
|---|
| [1505] | 61 |  | 
|---|
| [2019] | 62 |         this->camera_ = this->getScene()->getSceneManager()->createCamera(getUniqueNumberString()); | 
|---|
| [2662] | 63 |         this->cameraNode_ = this->getScene()->getRootSceneNode()->createChildSceneNode(); | 
|---|
 | 64 |         this->attachNode(this->cameraNode_); | 
|---|
 | 65 |         this->cameraNode_->attachObject(this->camera_); | 
|---|
| [2019] | 66 |  | 
|---|
 | 67 |         this->bHasFocus_ = false; | 
|---|
 | 68 |         this->bDrag_ = false; | 
|---|
 | 69 |         this->nearClipDistance_ = 1; | 
|---|
 | 70 |  | 
|---|
| [5929] | 71 |         this->setSyncMode(0x0); | 
|---|
| [2019] | 72 |  | 
|---|
 | 73 |         this->setConfigValues(); | 
|---|
 | 74 |         this->configvaluecallback_changedNearClipDistance(); | 
|---|
 | 75 |     } | 
|---|
 | 76 |  | 
|---|
 | 77 |     Camera::~Camera() | 
|---|
| [1989] | 78 |     { | 
|---|
| [2019] | 79 |         if (this->isInitialized()) | 
|---|
 | 80 |         { | 
|---|
| [5929] | 81 |             if (GUIManager::getInstance().getCamera() == this->camera_) | 
|---|
 | 82 |                 GUIManager::getInstance().setCamera(NULL); | 
|---|
| [2019] | 83 |             this->releaseFocus(); | 
|---|
| [2662] | 84 |  | 
|---|
 | 85 |             this->cameraNode_->detachAllObjects(); | 
|---|
 | 86 |             this->getScene()->getSceneManager()->destroyCamera(this->camera_); | 
|---|
 | 87 |  | 
|---|
 | 88 |             if (this->bDrag_) | 
|---|
 | 89 |                 this->detachNode(this->cameraNode_); | 
|---|
 | 90 |  | 
|---|
 | 91 |             if (this->getScene()->getSceneManager()) | 
|---|
 | 92 |                 this->getScene()->getSceneManager()->destroySceneNode(this->cameraNode_->getName()); | 
|---|
| [2019] | 93 |         } | 
|---|
| [1989] | 94 |     } | 
|---|
| [1505] | 95 |  | 
|---|
| [2019] | 96 |     void Camera::setConfigValues() | 
|---|
 | 97 |     { | 
|---|
 | 98 |         SetConfigValue(nearClipDistance_, 1.0f).callback(this, &Camera::configvaluecallback_changedNearClipDistance); | 
|---|
 | 99 |     } | 
|---|
| [1505] | 100 |  | 
|---|
| [2019] | 101 |     void Camera::configvaluecallback_changedNearClipDistance() | 
|---|
 | 102 |     { | 
|---|
 | 103 |         this->camera_->setNearClipDistance(this->nearClipDistance_); | 
|---|
 | 104 |     } | 
|---|
| [1505] | 105 |  | 
|---|
| [2019] | 106 |     void Camera::tick(float dt) | 
|---|
 | 107 |     { | 
|---|
| [2662] | 108 |         SUPER(Camera, tick, dt); | 
|---|
| [1505] | 109 |  | 
|---|
| [2662] | 110 |         if (this->bDrag_) | 
|---|
 | 111 |         { | 
|---|
 | 112 |             // this stuff here may need some adjustments | 
|---|
| [3280] | 113 |             float coeff = std::min(1.0f, 15.0f * dt); | 
|---|
| [1505] | 114 |  | 
|---|
| [2757] | 115 |             Vector3 offset = this->getWorldPosition() - this->cameraNode_->_getDerivedPosition(); | 
|---|
| [2662] | 116 |             this->cameraNode_->translate(coeff * offset); | 
|---|
 | 117 |  | 
|---|
| [2757] | 118 |             this->cameraNode_->setOrientation(Quaternion::Slerp(coeff, this->cameraNode_->_getDerivedOrientation(), this->getWorldOrientation(), true)); | 
|---|
| [2662] | 119 |             //this->cameraNode_->setOrientation(this->getWorldOrientation()); | 
|---|
 | 120 |         } | 
|---|
| [2019] | 121 |     } | 
|---|
| [1505] | 122 |  | 
|---|
| [2019] | 123 |     void Camera::requestFocus() | 
|---|
 | 124 |     { | 
|---|
| [2073] | 125 |         CameraManager::getInstance().requestFocus(this); | 
|---|
| [2019] | 126 |     } | 
|---|
| [1989] | 127 |  | 
|---|
| [2019] | 128 |     void Camera::releaseFocus() | 
|---|
 | 129 |     { | 
|---|
| [2073] | 130 |         CameraManager::getInstance().releaseFocus(this); | 
|---|
| [2019] | 131 |     } | 
|---|
 | 132 |  | 
|---|
 | 133 |     /** | 
|---|
 | 134 |         what to do when camera loses focus (do not request focus in this function!!) | 
|---|
| [2073] | 135 |         this is called by the CameraManager singleton class to notify the camera | 
|---|
| [2019] | 136 |     */ | 
|---|
 | 137 |     void Camera::removeFocus() | 
|---|
 | 138 |     { | 
|---|
 | 139 |         this->bHasFocus_ = false; | 
|---|
 | 140 |     } | 
|---|
 | 141 |  | 
|---|
| [2662] | 142 |     void Camera::setFocus() | 
|---|
| [2019] | 143 |     { | 
|---|
 | 144 |         this->bHasFocus_ = true; | 
|---|
| [2662] | 145 |         CameraManager::getInstance().useCamera(this->camera_); | 
|---|
| [2019] | 146 |     } | 
|---|
| [2662] | 147 |  | 
|---|
 | 148 |     void Camera::setDrag(bool bDrag) | 
|---|
 | 149 |     { | 
|---|
 | 150 |         if (bDrag != this->bDrag_) | 
|---|
 | 151 |         { | 
|---|
 | 152 |             this->bDrag_ = bDrag; | 
|---|
 | 153 |  | 
|---|
 | 154 |             if (!bDrag) | 
|---|
 | 155 |             { | 
|---|
 | 156 |                 this->attachNode(this->cameraNode_); | 
|---|
 | 157 |                 this->cameraNode_->setPosition(Vector3::ZERO); | 
|---|
 | 158 |                 this->cameraNode_->setOrientation(Quaternion::IDENTITY); | 
|---|
 | 159 |             } | 
|---|
 | 160 |             else | 
|---|
 | 161 |             { | 
|---|
 | 162 |                 this->detachNode(this->cameraNode_); | 
|---|
 | 163 |                 this->cameraNode_->setPosition(this->getWorldPosition()); | 
|---|
 | 164 |                 this->cameraNode_->setOrientation(this->getWorldOrientation()); | 
|---|
 | 165 |             } | 
|---|
 | 166 |         } | 
|---|
 | 167 |     } | 
|---|
| [1505] | 168 | } | 
|---|