Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/worldentities/Camera.cc @ 3143

Last change on this file since 3143 was 3110, checked in by rgrieder, 15 years ago

Removed old msvc specific support for precompiled header files.

  • Property svn:eol-style set to native
File size: 4.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
31#include <string>
[2019]32#include <cassert>
[1505]33
[2023]34#include <OgreCamera.h>
[1505]35#include <OgreSceneManager.h>
36#include <OgreSceneNode.h>
37
[2171]38#include "util/Exception.h"
[1505]39#include "core/CoreIncludes.h"
[2019]40#include "core/ConfigValueIncludes.h"
41#include "objects/Scene.h"
[2073]42#include "CameraManager.h"
[1505]43
44namespace orxonox
45{
[2019]46    CreateFactory(Camera);
[1993]47
[2662]48    Camera::Camera(BaseObject* creator) : StaticEntity(creator)
[2019]49    {
50        RegisterObject(Camera);
[1505]51
[2662]52        if (!this->getScene())
53            ThrowException(AbortLoading, "Can't create Camera, no scene.");
54        if (!this->getScene()->getSceneManager())
55            ThrowException(AbortLoading, "Can't create Camera, no scene-manager given.");
56        if (!this->getScene()->getRootSceneNode())
57            ThrowException(AbortLoading, "Can't create Camera, no root-scene-node given.");
[1505]58
[2019]59        this->camera_ = this->getScene()->getSceneManager()->createCamera(getUniqueNumberString());
[2662]60        this->cameraNode_ = this->getScene()->getRootSceneNode()->createChildSceneNode();
61        this->attachNode(this->cameraNode_);
62        this->cameraNode_->attachObject(this->camera_);
[2019]63
64        this->bHasFocus_ = false;
65        this->bDrag_ = false;
66        this->nearClipDistance_ = 1;
67
68        this->setObjectMode(0x0);
69
70        this->setConfigValues();
71        this->configvaluecallback_changedNearClipDistance();
72    }
73
74    Camera::~Camera()
[1989]75    {
[2019]76        if (this->isInitialized())
77        {
78            this->releaseFocus();
[2662]79
80            this->cameraNode_->detachAllObjects();
81            this->getScene()->getSceneManager()->destroyCamera(this->camera_);
82
83            if (this->bDrag_)
84                this->detachNode(this->cameraNode_);
85
86            if (this->getScene()->getSceneManager())
87                this->getScene()->getSceneManager()->destroySceneNode(this->cameraNode_->getName());
[2019]88        }
[1989]89    }
[1505]90
[2019]91    void Camera::setConfigValues()
92    {
93        SetConfigValue(nearClipDistance_, 1.0f).callback(this, &Camera::configvaluecallback_changedNearClipDistance);
94    }
[1505]95
[2019]96    void Camera::configvaluecallback_changedNearClipDistance()
97    {
98        this->camera_->setNearClipDistance(this->nearClipDistance_);
99    }
[1505]100
[2019]101    void Camera::tick(float dt)
102    {
[2662]103        SUPER(Camera, tick, dt);
[1505]104
[2662]105        if (this->bDrag_)
106        {
107            // this stuff here may need some adjustments
108            float coeff = min(1.0f, 15.0f * dt);
[1505]109
[2757]110            Vector3 offset = this->getWorldPosition() - this->cameraNode_->_getDerivedPosition();
[2662]111            this->cameraNode_->translate(coeff * offset);
112
[2757]113            this->cameraNode_->setOrientation(Quaternion::Slerp(coeff, this->cameraNode_->_getDerivedOrientation(), this->getWorldOrientation(), true));
[2662]114            //this->cameraNode_->setOrientation(this->getWorldOrientation());
115        }
[2019]116    }
[1505]117
[2019]118    void Camera::requestFocus()
119    {
[2073]120        CameraManager::getInstance().requestFocus(this);
[2019]121    }
[1989]122
[2019]123    void Camera::releaseFocus()
124    {
[2073]125        CameraManager::getInstance().releaseFocus(this);
[2019]126    }
127
128    /**
129        what to do when camera loses focus (do not request focus in this function!!)
[2073]130        this is called by the CameraManager singleton class to notify the camera
[2019]131    */
132    void Camera::removeFocus()
133    {
134        this->bHasFocus_ = false;
135    }
136
[2662]137    void Camera::setFocus()
[2019]138    {
139        this->bHasFocus_ = true;
[2662]140        CameraManager::getInstance().useCamera(this->camera_);
[2019]141    }
[2662]142
143    void Camera::setDrag(bool bDrag)
144    {
145        if (bDrag != this->bDrag_)
146        {
147            this->bDrag_ = bDrag;
148
149            if (!bDrag)
150            {
151                this->attachNode(this->cameraNode_);
152                this->cameraNode_->setPosition(Vector3::ZERO);
153                this->cameraNode_->setOrientation(Quaternion::IDENTITY);
154            }
155            else
156            {
157                this->detachNode(this->cameraNode_);
158                this->cameraNode_->setPosition(this->getWorldPosition());
159                this->cameraNode_->setOrientation(this->getWorldOrientation());
160            }
161        }
162    }
[1505]163}
Note: See TracBrowser for help on using the repository browser.