Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/orxonox/CameraManager.cc @ 5918

Last change on this file since 5918 was 5911, checked in by rgrieder, 15 years ago

Fixed CameraManager issue with the fallback scene by assigning each Scene a CameraManager directly.
Use this→getScene()→getCameraManager() to get the camera manager.

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