[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 | #include <OgreViewport.h> |
---|
[2350] | 39 | #include <OgreCompositorManager.h> |
---|
| 40 | #include <OgreResource.h> |
---|
[1505] | 41 | |
---|
[2171] | 42 | #include "util/Exception.h" |
---|
[1505] | 43 | #include "core/CoreIncludes.h" |
---|
[2019] | 44 | #include "core/ConfigValueIncludes.h" |
---|
| 45 | #include "objects/Scene.h" |
---|
[2350] | 46 | #include "tools/Shader.h" |
---|
[2073] | 47 | #include "CameraManager.h" |
---|
[1505] | 48 | |
---|
| 49 | namespace orxonox |
---|
| 50 | { |
---|
[2019] | 51 | CreateFactory(Camera); |
---|
[1993] | 52 | |
---|
[2019] | 53 | Camera::Camera(BaseObject* creator) : PositionableEntity(creator) |
---|
| 54 | { |
---|
| 55 | RegisterObject(Camera); |
---|
[2369] | 56 | COUT(0) << this << ": created camera" << std::endl; |
---|
[2171] | 57 | if (!this->getScene() || !this->getScene()->getSceneManager()) |
---|
| 58 | ThrowException(AbortLoading, "Can't create Camera, no scene or no scene manager given."); |
---|
[1505] | 59 | |
---|
[2019] | 60 | this->camera_ = this->getScene()->getSceneManager()->createCamera(getUniqueNumberString()); |
---|
| 61 | this->getNode()->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 | |
---|
[2369] | 72 | // this->requestFocus(); // ! HACK ! REMOVE THIS ! |
---|
[2019] | 73 | } |
---|
| 74 | |
---|
| 75 | Camera::~Camera() |
---|
[1989] | 76 | { |
---|
[2019] | 77 | if (this->isInitialized()) |
---|
| 78 | { |
---|
| 79 | this->releaseFocus(); |
---|
[2350] | 80 | this->getScene()->getSceneManager()->destroyCamera(this->camera_); |
---|
[2019] | 81 | } |
---|
[2369] | 82 | COUT(0) << this << ": destroyed camera" << std::endl; |
---|
[1989] | 83 | } |
---|
[1505] | 84 | |
---|
[2019] | 85 | void Camera::setConfigValues() |
---|
| 86 | { |
---|
| 87 | SetConfigValue(nearClipDistance_, 1.0f).callback(this, &Camera::configvaluecallback_changedNearClipDistance); |
---|
| 88 | } |
---|
[1505] | 89 | |
---|
[2019] | 90 | void Camera::configvaluecallback_changedNearClipDistance() |
---|
| 91 | { |
---|
| 92 | this->camera_->setNearClipDistance(this->nearClipDistance_); |
---|
| 93 | } |
---|
[1505] | 94 | |
---|
[2019] | 95 | void Camera::tick(float dt) |
---|
| 96 | { |
---|
[2361] | 97 | SUPER(Camera, tick, dt); |
---|
[2019] | 98 | /* |
---|
| 99 | // this stuff here may need some adjustments |
---|
| 100 | float coeff = (this->bDrag_) ? min(1.0f, 15.0f * dt) : (1.0f); |
---|
[1505] | 101 | |
---|
[2019] | 102 | Vector3 offset = this->getNode()->getWorldPosition() - this->cameraNode_->getWorldPosition(); |
---|
| 103 | this->cameraNode_->translate(coeff * offset); |
---|
[1505] | 104 | |
---|
[2019] | 105 | this->cameraNode_->setOrientation(Quaternion::Slerp(coeff, this->cameraNode_->getWorldOrientation(), this->getWorldOrientation(), false)); |
---|
| 106 | */ |
---|
| 107 | } |
---|
[1505] | 108 | |
---|
[2019] | 109 | void Camera::requestFocus() |
---|
| 110 | { |
---|
[2073] | 111 | CameraManager::getInstance().requestFocus(this); |
---|
[2019] | 112 | } |
---|
[1989] | 113 | |
---|
[2019] | 114 | void Camera::releaseFocus() |
---|
| 115 | { |
---|
[2073] | 116 | CameraManager::getInstance().releaseFocus(this); |
---|
[2019] | 117 | } |
---|
| 118 | |
---|
| 119 | /** |
---|
| 120 | what to do when camera loses focus (do not request focus in this function!!) |
---|
[2073] | 121 | this is called by the CameraManager singleton class to notify the camera |
---|
[2019] | 122 | */ |
---|
| 123 | void Camera::removeFocus() |
---|
| 124 | { |
---|
[2369] | 125 | COUT(0) << this << ": remove focus" << std::endl; |
---|
[2019] | 126 | this->bHasFocus_ = false; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | void Camera::setFocus(Ogre::Viewport* viewport) |
---|
| 130 | { |
---|
[2350] | 131 | // This workaround is needed to avoid weird behaviour with active compositors while |
---|
| 132 | // switching the camera (like freezing the image) |
---|
| 133 | // |
---|
| 134 | // Last known Ogre version needing this workaround: |
---|
| 135 | // 1.4.8 |
---|
| 136 | |
---|
| 137 | // deactivate all compositors |
---|
| 138 | { |
---|
| 139 | Ogre::ResourceManager::ResourceMapIterator iterator = Ogre::CompositorManager::getSingleton().getResourceIterator(); |
---|
| 140 | while (iterator.hasMoreElements()) |
---|
| 141 | Ogre::CompositorManager::getSingleton().setCompositorEnabled(viewport, iterator.getNext()->getName(), false); |
---|
| 142 | } |
---|
| 143 | |
---|
[2019] | 144 | this->bHasFocus_ = true; |
---|
| 145 | viewport->setCamera(this->camera_); |
---|
[2369] | 146 | COUT(0) << this << ": set focus" << std::endl; |
---|
[2350] | 147 | |
---|
| 148 | // reactivate all visible compositors |
---|
| 149 | { |
---|
| 150 | for (ObjectList<Shader>::iterator it = ObjectList<Shader>::begin(); it != ObjectList<Shader>::end(); ++it) |
---|
| 151 | it->updateVisibility(); |
---|
| 152 | } |
---|
[2019] | 153 | } |
---|
[1505] | 154 | } |
---|