Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp6/src/orxonox/CameraManager.cc @ 5435

Last change on this file since 5435 was 3196, checked in by rgrieder, 15 years ago

Merged pch branch back to trunk.

  • 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#include "CameraManager.h"
29
30#include <OgreSceneManager.h>
31#include <OgreViewport.h>
32#include <OgreCompositorManager.h>
33
34#include "util/String.h"
35#include "core/GameMode.h"
36#include "core/ObjectList.h"
37#include "tools/Shader.h"
38#include "objects/worldentities/Camera.h"
39#include "objects/Scene.h"
40#include "gui/GUIManager.h"
41
42namespace orxonox
43{
44    CameraManager* CameraManager::singletonRef_s = 0;
45
46    CameraManager::CameraManager(Ogre::Viewport* viewport)
47        : viewport_(viewport)
48    {
49        assert(singletonRef_s == 0);
50        singletonRef_s = this;
51
52        this->fallbackCamera_ = 0;
53    }
54
55    CameraManager::~CameraManager()
56    {
57        assert(singletonRef_s != 0);
58        singletonRef_s = 0;
59
60        if (this->fallbackCamera_)
61            this->fallbackCamera_->getSceneManager()->destroyCamera(this->fallbackCamera_);
62    }
63
64    Camera* CameraManager::getActiveCamera() const
65    {
66        if (this->cameraList_.size() > 0)
67            return this->cameraList_.front();
68        else
69            return 0;
70    }
71
72    void CameraManager::requestFocus(Camera* camera)
73    {
74        if (!GameMode::showsGraphics())
75            return;
76
77        // notify old camera (if it exists)
78        if (this->cameraList_.size() > 0)
79            this->cameraList_.front()->removeFocus();
80        else if (this->fallbackCamera_)
81        {
82            this->fallbackCamera_->getSceneManager()->destroyCamera(this->fallbackCamera_);
83            this->fallbackCamera_ = 0;
84        }
85
86        camera->setFocus();
87
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
93        // add to list
94        this->cameraList_.push_front(camera);
95    }
96
97    void CameraManager::releaseFocus(Camera* camera)
98    {
99        if (!GameMode::showsGraphics())
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
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            }
118        }
119        else
120        {
121            this->cameraList_.remove(camera);
122        }
123    }
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);
141        GUIManager::getInstance().setCamera(camera);
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    }
149}
Note: See TracBrowser for help on using the repository browser.