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