Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/CameraManager.cc @ 3201

Last change on this file since 3201 was 3196, checked in by rgrieder, 16 years ago

Merged pch branch back to trunk.

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