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