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