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