Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/usability/src/orxonox/graphics/Camera.cc @ 7966

Last change on this file since 7966 was 7966, checked in by landauf, 13 years ago

some changes related to camera switching:

  • added ViewportEventListener (currently only listens to camera changes in a viewport)
  • Shader now correctly updates its visibility if the camera changes the scene
  • (the same lines of code also fix the weird Ogre behavior which was originally fixed in CameraManager)
  • GraphicsManager also updates the GUIManager's camera
  • if all cameras are destroyed, CameraManager now officially switches to NULL camera
  • Property svn:eol-style set to native
File size: 6.2 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 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      Benjamin Knecht
26 *
27 */
28
29#include "Camera.h"
30
31#include <algorithm>
32#include <OgreCamera.h>
33#include <OgreSceneManager.h>
34#include <OgreSceneNode.h>
35
36#include "util/Exception.h"
37#include "util/StringUtils.h"
38#include "core/CoreIncludes.h"
39#include "core/ConfigValueIncludes.h"
40#include "core/GameMode.h"
41#include "core/GUIManager.h"
42#include "Scene.h"
43#include "CameraManager.h"
44#include "sound/SoundManager.h"
45
46namespace orxonox
47{
48    CreateFactory(Camera);
49
50    Camera::Camera(BaseObject* creator) : StaticEntity(creator)
51    {
52        RegisterObject(Camera);
53
54        if (!GameMode::showsGraphics())
55            ThrowException(AbortLoading, "Can't create Camera, no graphics.");
56        if (!this->getScene())
57            ThrowException(AbortLoading, "Can't create Camera, no scene.");
58        if (!this->getScene()->getSceneManager())
59            ThrowException(AbortLoading, "Can't create Camera, no scene-manager given.");
60        if (!this->getScene()->getRootSceneNode())
61            ThrowException(AbortLoading, "Can't create Camera, no root-scene-node given.");
62
63        this->camera_ = this->getScene()->getSceneManager()->createCamera(getUniqueNumberString());
64        static_cast<Ogre::MovableObject*>(this->camera_)->setUserAny(Ogre::Any(static_cast<OrxonoxClass*>(this)));
65        this->cameraNode_ = this->getScene()->getRootSceneNode()->createChildSceneNode();
66        this->attachNode(this->cameraNode_);
67        this->cameraNode_->attachObject(this->camera_);
68
69        this->bHasFocus_ = false;
70        this->bDrag_ = false;
71        this->nearClipDistance_ = 1;
72        this->lastDtLagged_ = false;
73
74        this->setSyncMode(0x0);
75
76        this->setConfigValues();
77        this->configvaluecallback_changedNearClipDistance();
78    }
79
80    Camera::~Camera()
81    {
82        if (this->isInitialized())
83        {
84            this->releaseFocus();
85
86            this->cameraNode_->detachAllObjects();
87            this->getScene()->getSceneManager()->destroyCamera(this->camera_);
88
89            if (this->bDrag_)
90                this->detachNode(this->cameraNode_);
91
92            if (this->getScene()->getSceneManager())
93                this->getScene()->getSceneManager()->destroySceneNode(this->cameraNode_->getName());
94        }
95    }
96
97    void Camera::setConfigValues()
98    {
99        SetConfigValue(nearClipDistance_, 1.0f).callback(this, &Camera::configvaluecallback_changedNearClipDistance);
100    }
101
102    void Camera::configvaluecallback_changedNearClipDistance()
103    {
104        this->camera_->setNearClipDistance(this->nearClipDistance_);
105    }
106
107    void Camera::tick(float dt)
108    {
109        SUPER(Camera, tick, dt);
110
111        if (this->bDrag_ && this->getTimeFactor() != 0)
112        {
113            // this stuff here may need some adjustments
114            float poscoeff = 15.0f * dt / this->getTimeFactor();
115            float anglecoeff = 7.0f * dt / this->getTimeFactor();
116            // Only clamp if fps rate is actually falling. Occasional high dts should
117            // not be clamped to reducing lagging effects.
118            if (poscoeff > 1.0f)
119            {
120                if (this->lastDtLagged_)
121                    poscoeff = 1.0f;
122                else
123                    this->lastDtLagged_ = true;
124            }
125            else
126                this->lastDtLagged_ = false;
127
128            if (anglecoeff > 1.0f)
129            {
130                if (this->lastDtLagged_)
131                    anglecoeff = 1.0f;
132                else
133                    this->lastDtLagged_ = true;
134            }
135            else
136                this->lastDtLagged_ = false;
137
138            Vector3 offset = this->getWorldPosition() - this->cameraNode_->_getDerivedPosition();
139            this->cameraNode_->translate(poscoeff * offset);
140
141            this->cameraNode_->setOrientation(Quaternion::Slerp(anglecoeff, this->cameraNode_->_getDerivedOrientation(), this->getWorldOrientation(), true));
142        }
143
144        // Update sound listener transformation
145        if (GameMode::playsSound() && this->bHasFocus_)
146        {
147            SoundManager::getInstance().setListenerPosition(this->getWorldPosition());
148            SoundManager::getInstance().setListenerOrientation(this->getWorldOrientation());
149        }
150    }
151
152    void Camera::requestFocus()
153    {
154        CameraManager::getInstance().requestFocus(this);
155    }
156
157    void Camera::releaseFocus()
158    {
159        CameraManager::getInstance().releaseFocus(this);
160    }
161
162    /**
163        what to do when camera loses focus (do not request focus in this function!!)
164        this is called by the CameraManager singleton class to notify the camera
165    */
166    void Camera::removeFocus()
167    {
168        this->bHasFocus_ = false;
169    }
170
171    void Camera::setFocus()
172    {
173        this->bHasFocus_ = true;
174        CameraManager::getInstance().useCamera(this->camera_);
175    }
176
177    void Camera::setDrag(bool bDrag)
178    {
179        if (bDrag != this->bDrag_)
180        {
181            this->bDrag_ = bDrag;
182
183            if (!bDrag)
184            {
185                this->attachNode(this->cameraNode_);
186                this->cameraNode_->setPosition(Vector3::ZERO);
187                this->cameraNode_->setOrientation(Quaternion::IDENTITY);
188            }
189            else
190            {
191                this->detachNode(this->cameraNode_);
192                this->cameraNode_->setPosition(this->getWorldPosition());
193                this->cameraNode_->setOrientation(this->getWorldOrientation());
194            }
195        }
196    }
197}
Note: See TracBrowser for help on using the repository browser.