Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12166 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
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/config/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    RegisterClass(Camera);
49
50    Camera::Camera(Context* context) : StaticEntity(context)
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        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
70        this->cameraNode_ = this->getScene()->getRootSceneNode()->createChildSceneNode();
71        this->attachNode(this->cameraNode_);
72        this->cameraNode_->attachObject(this->camera_);
73
74        this->bHasFocus_ = false;
75        this->bDrag_ = false;
76        this->lastDtLagged_ = false;
77
78        this->setSyncMode(ObjectDirection::None);
79
80        this->setConfigValues();
81
82        this->configvaluecallback_changedFovAndAspectRatio();
83        this->configvaluecallback_changedNearClipDistance();
84    }
85
86    Camera::~Camera()
87    {
88        if (this->isInitialized())
89        {
90            this->releaseFocus();
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());
100        }
101    }
102
103    void Camera::setConfigValues()
104    {
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);
114    }
115
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
130    void Camera::configvaluecallback_changedNearClipDistance()
131    {
132        this->camera_->setNearClipDistance(this->nearClipDistance_);
133    }
134
135    /**
136        @brief Inherited from WindowEventListener.
137    */
138    void Camera::windowResized(unsigned int newWidth, unsigned int newHeight)
139    {
140        this->configvaluecallback_changedFovAndAspectRatio();
141    }
142
143    void Camera::tick(float dt)
144    {
145        SUPER(Camera, tick, dt);
146
147        if (this->bDrag_ && this->getTimeFactor() != 0)
148        {
149            // this stuff here may need some adjustments
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;
163
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
174            Vector3 offset = this->getWorldPosition() - this->cameraNode_->_getDerivedPosition();
175            this->cameraNode_->translate(poscoeff * offset);
176
177            this->cameraNode_->setOrientation(Quaternion::Slerp(anglecoeff, this->cameraNode_->_getDerivedOrientation(), this->getWorldOrientation(), true));
178        }
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        }
186    }
187
188    void Camera::requestFocus()
189    {
190        CameraManager::getInstance().requestFocus(this);
191    }
192
193    void Camera::releaseFocus()
194    {
195        CameraManager::getInstance().releaseFocus(this);
196    }
197
198    /**
199        what to do when camera loses focus (do not request focus in this function!!)
200        this is called by the CameraManager singleton class to notify the camera
201    */
202    void Camera::removeFocus()
203    {
204        this->bHasFocus_ = false;
205    }
206
207    void Camera::setFocus()
208    {
209        this->bHasFocus_ = true;
210        CameraManager::getInstance().useCamera(this->camera_);
211    }
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    }
233}
Note: See TracBrowser for help on using the repository browser.