Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gameimmersion/src/orxonox/graphics/Camera.cc @ 8474

Last change on this file since 8474 was 8138, checked in by dboehi, 14 years ago

First version of the camera shake effect for the afterburner

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