Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/steering/src/orxonox/graphics/Camera.cc @ 6372

Last change on this file since 6372 was 6052, checked in by rgrieder, 15 years ago

Making use of Ogre::UserDefinedObject by subclassing it in WorldEntity: This way we can access a WE from an Ogre::MovableObject (e.g. Ogre::Entity or Ogre::ParticleSystem), but with a dynamic_cast of course.

  • Property svn:eol-style set to native
File size: 5.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
45namespace orxonox
46{
47    CreateFactory(Camera);
48
49    Camera::Camera(BaseObject* creator) : StaticEntity(creator)
50    {
51        RegisterObject(Camera);
52
53        if (!GameMode::showsGraphics())
54            ThrowException(AbortLoading, "Can't create Camera, no graphics.");
55        if (!this->getScene())
56            ThrowException(AbortLoading, "Can't create Camera, no scene.");
57        if (!this->getScene()->getSceneManager())
58            ThrowException(AbortLoading, "Can't create Camera, no scene-manager given.");
59        if (!this->getScene()->getRootSceneNode())
60            ThrowException(AbortLoading, "Can't create Camera, no root-scene-node given.");
61
62        this->camera_ = this->getScene()->getSceneManager()->createCamera(getUniqueNumberString());
63        this->camera_->setUserObject(this);
64        this->cameraNode_ = this->getScene()->getRootSceneNode()->createChildSceneNode();
65        this->attachNode(this->cameraNode_);
66        this->cameraNode_->attachObject(this->camera_);
67
68        this->bHasFocus_ = false;
69        this->bDrag_ = false;
70        this->nearClipDistance_ = 1;
71
72        this->setSyncMode(0x0);
73
74        this->setConfigValues();
75        this->configvaluecallback_changedNearClipDistance();
76    }
77
78    Camera::~Camera()
79    {
80        if (this->isInitialized())
81        {
82            if (GUIManager::getInstance().getCamera() == this->camera_)
83                GUIManager::getInstance().setCamera(NULL);
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_)
112        {
113            // this stuff here may need some adjustments
114            float coeff = std::min(1.0f, 15.0f * dt);
115
116            Vector3 offset = this->getWorldPosition() - this->cameraNode_->_getDerivedPosition();
117            this->cameraNode_->translate(coeff * offset);
118
119            this->cameraNode_->setOrientation(Quaternion::Slerp(coeff, this->cameraNode_->_getDerivedOrientation(), this->getWorldOrientation(), true));
120            //this->cameraNode_->setOrientation(this->getWorldOrientation());
121        }
122    }
123
124    void Camera::requestFocus()
125    {
126        CameraManager::getInstance().requestFocus(this);
127    }
128
129    void Camera::releaseFocus()
130    {
131        CameraManager::getInstance().releaseFocus(this);
132    }
133
134    /**
135        what to do when camera loses focus (do not request focus in this function!!)
136        this is called by the CameraManager singleton class to notify the camera
137    */
138    void Camera::removeFocus()
139    {
140        this->bHasFocus_ = false;
141    }
142
143    void Camera::setFocus()
144    {
145        this->bHasFocus_ = true;
146        CameraManager::getInstance().useCamera(this->camera_);
147    }
148
149    void Camera::setDrag(bool bDrag)
150    {
151        if (bDrag != this->bDrag_)
152        {
153            this->bDrag_ = bDrag;
154
155            if (!bDrag)
156            {
157                this->attachNode(this->cameraNode_);
158                this->cameraNode_->setPosition(Vector3::ZERO);
159                this->cameraNode_->setOrientation(Quaternion::IDENTITY);
160            }
161            else
162            {
163                this->detachNode(this->cameraNode_);
164                this->cameraNode_->setPosition(this->getWorldPosition());
165                this->cameraNode_->setOrientation(this->getWorldOrientation());
166            }
167        }
168    }
169}
Note: See TracBrowser for help on using the repository browser.