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