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