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