Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/graphics/Camera.cc @ 12141

Last change on this file since 12141 was 11795, checked in by landauf, 7 years ago

merged ogre1.9 (including cegui0.8) into new branch

  • Property svn:eol-style set to native
File size: 7.9 KB
RevLine 
[1505]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
[3280]31#include <algorithm>
[2023]32#include <OgreCamera.h>
[1505]33#include <OgreSceneManager.h>
34#include <OgreSceneNode.h>
35
[2171]36#include "util/Exception.h"
[3280]37#include "util/StringUtils.h"
[1505]38#include "core/CoreIncludes.h"
[9667]39#include "core/config/ConfigValueIncludes.h"
[5929]40#include "core/GameMode.h"
41#include "core/GUIManager.h"
[5735]42#include "Scene.h"
[2073]43#include "CameraManager.h"
[6417]44#include "sound/SoundManager.h"
[1505]45
46namespace orxonox
47{
[9667]48    RegisterClass(Camera);
[1993]49
[9667]50    Camera::Camera(Context* context) : StaticEntity(context)
[2019]51    {
52        RegisterObject(Camera);
[1505]53
[5929]54        if (!GameMode::showsGraphics())
55            ThrowException(AbortLoading, "Can't create Camera, no graphics.");
[2662]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.");
[1505]62
[2019]63        this->camera_ = this->getScene()->getSceneManager()->createCamera(getUniqueNumberString());
[11795]64        Ogre::MovableObject* movable = static_cast<Ogre::MovableObject*>(this->camera_);
65#if OGRE_VERSION >= 0x010900
66        movable->getUserObjectBindings().setUserAny(Ogre::Any(static_cast<OrxonoxClass*>(this)));
67#else
68        movable->setUserAny(Ogre::Any(static_cast<OrxonoxClass*>(this)));
69#endif
[2662]70        this->cameraNode_ = this->getScene()->getRootSceneNode()->createChildSceneNode();
71        this->attachNode(this->cameraNode_);
72        this->cameraNode_->attachObject(this->camera_);
[2019]73
74        this->bHasFocus_ = false;
75        this->bDrag_ = false;
[6417]76        this->lastDtLagged_ = false;
[2019]77
[8706]78        this->setSyncMode(ObjectDirection::None);
[2019]79
80        this->setConfigValues();
[8079]81
82        this->configvaluecallback_changedFovAndAspectRatio();
[2019]83        this->configvaluecallback_changedNearClipDistance();
84    }
85
86    Camera::~Camera()
[1989]87    {
[2019]88        if (this->isInitialized())
89        {
90            this->releaseFocus();
[2662]91
92            this->cameraNode_->detachAllObjects();
93            this->getScene()->getSceneManager()->destroyCamera(this->camera_);
94
95            if (this->bDrag_)
96                this->detachNode(this->cameraNode_);
97
98            if (this->getScene()->getSceneManager())
99                this->getScene()->getSceneManager()->destroySceneNode(this->cameraNode_->getName());
[2019]100        }
[1989]101    }
[1505]102
[2019]103    void Camera::setConfigValues()
104    {
[8079]105        SetConfigValue(fov_, 80.0f)
106            .description("Horizontal field of view in degrees")
107            .callback(this, &Camera::configvaluecallback_changedFovAndAspectRatio);
108        SetConfigValue(aspectRatio_, 1.0f)
109            .description("Aspect ratio of pixels (width / height)")
110            .callback(this, &Camera::configvaluecallback_changedFovAndAspectRatio);
111        SetConfigValue(nearClipDistance_, 1.0f)
112            .description("Distance from the camera where close objects will be clipped")
113            .callback(this, &Camera::configvaluecallback_changedNearClipDistance);
[2019]114    }
[1505]115
[8079]116    /**
117        @brief Update FOV and the aspect ratio of the camera after the config values or the window's size have changed.
118    */
119    void Camera::configvaluecallback_changedFovAndAspectRatio()
120    {
121        // the aspect ratio of the window (width / height) has to be multiplied with the pixels aspect ratio (this->aspectRatio_)
122        float aspectRatio = this->aspectRatio_ * this->getWindowWidth() / this->getWindowHeight();
123        this->camera_->setAspectRatio(aspectRatio);
124
125        // Since we use horizontal FOV, we have to calculate FOVy by dividing by the aspect ratio and using some tangents
126        Radian fovy(2 * atan(tan(Degree(this->fov_).valueRadians() / 2) / aspectRatio));
127        this->camera_->setFOVy(fovy);
128    }
129
[2019]130    void Camera::configvaluecallback_changedNearClipDistance()
131    {
132        this->camera_->setNearClipDistance(this->nearClipDistance_);
133    }
[1505]134
[8079]135    /**
136        @brief Inherited from WindowEventListener.
137    */
138    void Camera::windowResized(unsigned int newWidth, unsigned int newHeight)
139    {
140        this->configvaluecallback_changedFovAndAspectRatio();
141    }
142
[2019]143    void Camera::tick(float dt)
144    {
[2662]145        SUPER(Camera, tick, dt);
[1505]146
[7163]147        if (this->bDrag_ && this->getTimeFactor() != 0)
[2662]148        {
149            // this stuff here may need some adjustments
[6417]150            float poscoeff = 15.0f * dt / this->getTimeFactor();
151            float anglecoeff = 7.0f * dt / this->getTimeFactor();
152            // Only clamp if fps rate is actually falling. Occasional high dts should
153            // not be clamped to reducing lagging effects.
154            if (poscoeff > 1.0f)
155            {
156                if (this->lastDtLagged_)
157                    poscoeff = 1.0f;
158                else
159                    this->lastDtLagged_ = true;
160            }
161            else
162                this->lastDtLagged_ = false;
[1505]163
[6417]164            if (anglecoeff > 1.0f)
165            {
166                if (this->lastDtLagged_)
167                    anglecoeff = 1.0f;
168                else
169                    this->lastDtLagged_ = true;
170            }
171            else
172                this->lastDtLagged_ = false;
173
[2757]174            Vector3 offset = this->getWorldPosition() - this->cameraNode_->_getDerivedPosition();
[6417]175            this->cameraNode_->translate(poscoeff * offset);
[2662]176
[6417]177            this->cameraNode_->setOrientation(Quaternion::Slerp(anglecoeff, this->cameraNode_->_getDerivedOrientation(), this->getWorldOrientation(), true));
[2662]178        }
[6417]179
180        // Update sound listener transformation
181        if (GameMode::playsSound() && this->bHasFocus_)
182        {
183            SoundManager::getInstance().setListenerPosition(this->getWorldPosition());
184            SoundManager::getInstance().setListenerOrientation(this->getWorldOrientation());
185        }
[2019]186    }
[1505]187
[2019]188    void Camera::requestFocus()
189    {
[2073]190        CameraManager::getInstance().requestFocus(this);
[2019]191    }
[1989]192
[2019]193    void Camera::releaseFocus()
194    {
[2073]195        CameraManager::getInstance().releaseFocus(this);
[2019]196    }
197
198    /**
199        what to do when camera loses focus (do not request focus in this function!!)
[2073]200        this is called by the CameraManager singleton class to notify the camera
[2019]201    */
202    void Camera::removeFocus()
203    {
204        this->bHasFocus_ = false;
205    }
206
[2662]207    void Camera::setFocus()
[2019]208    {
209        this->bHasFocus_ = true;
[2662]210        CameraManager::getInstance().useCamera(this->camera_);
[2019]211    }
[2662]212
213    void Camera::setDrag(bool bDrag)
214    {
215        if (bDrag != this->bDrag_)
216        {
217            this->bDrag_ = bDrag;
218
219            if (!bDrag)
220            {
221                this->attachNode(this->cameraNode_);
222                this->cameraNode_->setPosition(Vector3::ZERO);
223                this->cameraNode_->setOrientation(Quaternion::IDENTITY);
224            }
225            else
226            {
227                this->detachNode(this->cameraNode_);
228                this->cameraNode_->setPosition(this->getWorldPosition());
229                this->cameraNode_->setOrientation(this->getWorldOrientation());
230            }
231        }
232    }
[1505]233}
Note: See TracBrowser for help on using the repository browser.