[1202] | 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 | * Benjamin Knecht |
---|
| 24 | * Co-authors: |
---|
[2073] | 25 | * Fabian 'x3n' Landau |
---|
[1202] | 26 | * |
---|
| 27 | */ |
---|
[2073] | 28 | #include "CameraManager.h" |
---|
[1502] | 29 | |
---|
[2662] | 30 | #include <OgreSceneManager.h> |
---|
[2023] | 31 | #include <OgreViewport.h> |
---|
[2662] | 32 | #include <OgreCamera.h> |
---|
| 33 | #include <OgreCompositorManager.h> |
---|
| 34 | #include <OgreResource.h> |
---|
[1202] | 35 | |
---|
[2896] | 36 | #include "core/GameMode.h" |
---|
[2662] | 37 | #include "core/Iterator.h" |
---|
[2027] | 38 | #include "objects/worldentities/Camera.h" |
---|
[2662] | 39 | #include "objects/Scene.h" |
---|
| 40 | #include "tools/Shader.h" |
---|
| 41 | #include "util/String.h" |
---|
[2896] | 42 | #include "gui/GUIManager.h" |
---|
[1202] | 43 | |
---|
[2019] | 44 | namespace orxonox |
---|
| 45 | { |
---|
[2073] | 46 | CameraManager* CameraManager::singletonRef_s = 0; |
---|
[2023] | 47 | |
---|
[2073] | 48 | CameraManager::CameraManager(Ogre::Viewport* viewport) |
---|
[2023] | 49 | : viewport_(viewport) |
---|
[2019] | 50 | { |
---|
[2023] | 51 | assert(singletonRef_s == 0); |
---|
| 52 | singletonRef_s = this; |
---|
[2662] | 53 | |
---|
| 54 | this->fallbackCamera_ = 0; |
---|
[2019] | 55 | } |
---|
[1202] | 56 | |
---|
[2073] | 57 | CameraManager::~CameraManager() |
---|
[2019] | 58 | { |
---|
[2023] | 59 | assert(singletonRef_s != 0); |
---|
| 60 | singletonRef_s = 0; |
---|
[2662] | 61 | |
---|
| 62 | if (this->fallbackCamera_) |
---|
| 63 | this->fallbackCamera_->getSceneManager()->destroyCamera(this->fallbackCamera_); |
---|
[2019] | 64 | } |
---|
[1202] | 65 | |
---|
[2073] | 66 | Camera* CameraManager::getActiveCamera() const |
---|
[2019] | 67 | { |
---|
| 68 | if (this->cameraList_.size() > 0) |
---|
| 69 | return this->cameraList_.front(); |
---|
| 70 | else |
---|
| 71 | return 0; |
---|
| 72 | } |
---|
[1202] | 73 | |
---|
[2073] | 74 | void CameraManager::requestFocus(Camera* camera) |
---|
[2019] | 75 | { |
---|
[2896] | 76 | if (!GameMode::showsGraphics()) |
---|
[2019] | 77 | return; |
---|
| 78 | |
---|
| 79 | // notify old camera (if it exists) |
---|
| 80 | if (this->cameraList_.size() > 0) |
---|
| 81 | this->cameraList_.front()->removeFocus(); |
---|
[2662] | 82 | else if (this->fallbackCamera_) |
---|
| 83 | { |
---|
| 84 | this->fallbackCamera_->getSceneManager()->destroyCamera(this->fallbackCamera_); |
---|
| 85 | this->fallbackCamera_ = 0; |
---|
| 86 | } |
---|
[2019] | 87 | |
---|
[2662] | 88 | camera->setFocus(); |
---|
[2064] | 89 | |
---|
[2662] | 90 | // make sure we don't add it twice |
---|
| 91 | for (std::list<Camera*>::iterator it = this->cameraList_.begin(); it != this->cameraList_.end(); ++it) |
---|
| 92 | if ((*it) == camera) |
---|
| 93 | return; |
---|
| 94 | |
---|
[2019] | 95 | // add to list |
---|
| 96 | this->cameraList_.push_front(camera); |
---|
[1202] | 97 | } |
---|
| 98 | |
---|
[2073] | 99 | void CameraManager::releaseFocus(Camera* camera) |
---|
[1202] | 100 | { |
---|
[2896] | 101 | if (!GameMode::showsGraphics()) |
---|
[2019] | 102 | return; |
---|
| 103 | |
---|
| 104 | // notify the cam of releasing the focus |
---|
| 105 | if (this->cameraList_.front() == camera) |
---|
| 106 | { |
---|
| 107 | camera->removeFocus(); |
---|
| 108 | this->cameraList_.pop_front(); |
---|
| 109 | |
---|
[2662] | 110 | // set new focus if possible |
---|
| 111 | if (this->cameraList_.size() > 0) |
---|
| 112 | this->cameraList_.front()->setFocus(); |
---|
| 113 | else |
---|
| 114 | { |
---|
| 115 | // there are no more cameras, create a fallback |
---|
| 116 | if (!this->fallbackCamera_) |
---|
| 117 | this->fallbackCamera_ = camera->getScene()->getSceneManager()->createCamera(getUniqueNumberString()); |
---|
| 118 | this->useCamera(this->fallbackCamera_); |
---|
| 119 | } |
---|
[2019] | 120 | } |
---|
| 121 | else |
---|
| 122 | { |
---|
| 123 | this->cameraList_.remove(camera); |
---|
| 124 | } |
---|
[1202] | 125 | } |
---|
[2662] | 126 | |
---|
| 127 | void CameraManager::useCamera(Ogre::Camera* camera) |
---|
| 128 | { |
---|
| 129 | // This workaround is needed to avoid weird behaviour with active compositors while |
---|
| 130 | // switching the camera (like freezing the image) |
---|
| 131 | // |
---|
| 132 | // Last known Ogre version needing this workaround: |
---|
| 133 | // 1.4.8 |
---|
| 134 | |
---|
| 135 | // deactivate all compositors |
---|
| 136 | { |
---|
| 137 | Ogre::ResourceManager::ResourceMapIterator iterator = Ogre::CompositorManager::getSingleton().getResourceIterator(); |
---|
| 138 | while (iterator.hasMoreElements()) |
---|
| 139 | Ogre::CompositorManager::getSingleton().setCompositorEnabled(this->viewport_, iterator.getNext()->getName(), false); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | this->viewport_->setCamera(camera); |
---|
[2896] | 143 | GUIManager::getInstance().setCamera(camera); |
---|
[2662] | 144 | |
---|
| 145 | // reactivate all visible compositors |
---|
| 146 | { |
---|
| 147 | for (ObjectList<Shader>::iterator it = ObjectList<Shader>::begin(); it != ObjectList<Shader>::end(); ++it) |
---|
| 148 | it->updateVisibility(); |
---|
| 149 | } |
---|
| 150 | } |
---|
[1202] | 151 | } |
---|