Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10241 was 7988, checked in by landauf, 14 years ago
  • FOV and pixel aspect ratio are now configurable
  • aspect ratio and FOV of the camera are automatically adjusted if the window is resized (note: hud and gui still scale with the window which is fine I guess)
  • the config value for FOV defines FOVx which is more convenient than FOVy
  • default FOVx is now 80 degree (it was ~57.5 degree before, so the field of view is now much larger and the ship seems more distant).
  • Property svn:eol-style set to native
File size: 7.7 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"
[2019]39#include "core/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{
[2019]48    CreateFactory(Camera);
[1993]49
[2662]50    Camera::Camera(BaseObject* creator) : StaticEntity(creator)
[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());
[6501]64        static_cast<Ogre::MovableObject*>(this->camera_)->setUserAny(Ogre::Any(static_cast<OrxonoxClass*>(this)));
[2662]65        this->cameraNode_ = this->getScene()->getRootSceneNode()->createChildSceneNode();
66        this->attachNode(this->cameraNode_);
67        this->cameraNode_->attachObject(this->camera_);
[2019]68
69        this->bHasFocus_ = false;
70        this->bDrag_ = false;
[6417]71        this->lastDtLagged_ = false;
[2019]72
[5929]73        this->setSyncMode(0x0);
[2019]74
75        this->setConfigValues();
[7988]76
77        this->configvaluecallback_changedFovAndAspectRatio();
[2019]78        this->configvaluecallback_changedNearClipDistance();
79    }
80
81    Camera::~Camera()
[1989]82    {
[2019]83        if (this->isInitialized())
84        {
85            this->releaseFocus();
[2662]86
87            this->cameraNode_->detachAllObjects();
88            this->getScene()->getSceneManager()->destroyCamera(this->camera_);
89
90            if (this->bDrag_)
91                this->detachNode(this->cameraNode_);
92
93            if (this->getScene()->getSceneManager())
94                this->getScene()->getSceneManager()->destroySceneNode(this->cameraNode_->getName());
[2019]95        }
[1989]96    }
[1505]97
[2019]98    void Camera::setConfigValues()
99    {
[7988]100        SetConfigValue(fov_, 80.0f)
101            .description("Horizontal field of view in degrees")
102            .callback(this, &Camera::configvaluecallback_changedFovAndAspectRatio);
103        SetConfigValue(aspectRatio_, 1.0f)
104            .description("Aspect ratio of pixels (width / height)")
105            .callback(this, &Camera::configvaluecallback_changedFovAndAspectRatio);
106        SetConfigValue(nearClipDistance_, 1.0f)
107            .description("Distance from the camera where close objects will be clipped")
108            .callback(this, &Camera::configvaluecallback_changedNearClipDistance);
[2019]109    }
[1505]110
[7988]111    /**
112        @brief Update FOV and the aspect ratio of the camera after the config values or the window's size have changed.
113    */
114    void Camera::configvaluecallback_changedFovAndAspectRatio()
115    {
116        // the aspect ratio of the window (width / height) has to be multiplied with the pixels aspect ratio (this->aspectRatio_)
117        float aspectRatio = this->aspectRatio_ * this->getWindowWidth() / this->getWindowHeight();
118        this->camera_->setAspectRatio(aspectRatio);
119
120        // Since we use horizontal FOV, we have to calculate FOVy by dividing by the aspect ratio and using some tangents
121        Radian fovy(2 * atan(tan(Degree(this->fov_).valueRadians() / 2) / aspectRatio));
122        this->camera_->setFOVy(fovy);
123    }
124
[2019]125    void Camera::configvaluecallback_changedNearClipDistance()
126    {
127        this->camera_->setNearClipDistance(this->nearClipDistance_);
128    }
[1505]129
[7988]130    /**
131        @brief Inherited from WindowEventListener.
132    */
133    void Camera::windowResized(unsigned int newWidth, unsigned int newHeight)
134    {
135        this->configvaluecallback_changedFovAndAspectRatio();
136    }
137
[2019]138    void Camera::tick(float dt)
139    {
[2662]140        SUPER(Camera, tick, dt);
[1505]141
[7163]142        if (this->bDrag_ && this->getTimeFactor() != 0)
[2662]143        {
144            // this stuff here may need some adjustments
[6417]145            float poscoeff = 15.0f * dt / this->getTimeFactor();
146            float anglecoeff = 7.0f * dt / this->getTimeFactor();
147            // Only clamp if fps rate is actually falling. Occasional high dts should
148            // not be clamped to reducing lagging effects.
149            if (poscoeff > 1.0f)
150            {
151                if (this->lastDtLagged_)
152                    poscoeff = 1.0f;
153                else
154                    this->lastDtLagged_ = true;
155            }
156            else
157                this->lastDtLagged_ = false;
[1505]158
[6417]159            if (anglecoeff > 1.0f)
160            {
161                if (this->lastDtLagged_)
162                    anglecoeff = 1.0f;
163                else
164                    this->lastDtLagged_ = true;
165            }
166            else
167                this->lastDtLagged_ = false;
168
[2757]169            Vector3 offset = this->getWorldPosition() - this->cameraNode_->_getDerivedPosition();
[6417]170            this->cameraNode_->translate(poscoeff * offset);
[2662]171
[6417]172            this->cameraNode_->setOrientation(Quaternion::Slerp(anglecoeff, this->cameraNode_->_getDerivedOrientation(), this->getWorldOrientation(), true));
[2662]173        }
[6417]174
175        // Update sound listener transformation
176        if (GameMode::playsSound() && this->bHasFocus_)
177        {
178            SoundManager::getInstance().setListenerPosition(this->getWorldPosition());
179            SoundManager::getInstance().setListenerOrientation(this->getWorldOrientation());
180        }
[2019]181    }
[1505]182
[2019]183    void Camera::requestFocus()
184    {
[2073]185        CameraManager::getInstance().requestFocus(this);
[2019]186    }
[1989]187
[2019]188    void Camera::releaseFocus()
189    {
[2073]190        CameraManager::getInstance().releaseFocus(this);
[2019]191    }
192
193    /**
194        what to do when camera loses focus (do not request focus in this function!!)
[2073]195        this is called by the CameraManager singleton class to notify the camera
[2019]196    */
197    void Camera::removeFocus()
198    {
199        this->bHasFocus_ = false;
200    }
201
[2662]202    void Camera::setFocus()
[2019]203    {
204        this->bHasFocus_ = true;
[2662]205        CameraManager::getInstance().useCamera(this->camera_);
[2019]206    }
[2662]207
208    void Camera::setDrag(bool bDrag)
209    {
210        if (bDrag != this->bDrag_)
211        {
212            this->bDrag_ = bDrag;
213
214            if (!bDrag)
215            {
216                this->attachNode(this->cameraNode_);
217                this->cameraNode_->setPosition(Vector3::ZERO);
218                this->cameraNode_->setOrientation(Quaternion::IDENTITY);
219            }
220            else
221            {
222                this->detachNode(this->cameraNode_);
223                this->cameraNode_->setPosition(this->getWorldPosition());
224                this->cameraNode_->setOrientation(this->getWorldOrientation());
225            }
226        }
227    }
[1505]228}
Note: See TracBrowser for help on using the repository browser.