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 "OrxonoxStableHeaders.h" |
---|
30 | #include "Camera.h" |
---|
31 | |
---|
32 | #include <string> |
---|
33 | #include <cassert> |
---|
34 | |
---|
35 | #include <OgreCamera.h> |
---|
36 | #include <OgreSceneManager.h> |
---|
37 | #include <OgreSceneNode.h> |
---|
38 | #include <OgreViewport.h> |
---|
39 | #include <OgreCompositorManager.h> |
---|
40 | #include <OgreResource.h> |
---|
41 | |
---|
42 | #include "util/Exception.h" |
---|
43 | #include "core/CoreIncludes.h" |
---|
44 | #include "core/ConfigValueIncludes.h" |
---|
45 | #include "objects/Scene.h" |
---|
46 | #include "tools/Shader.h" |
---|
47 | #include "CameraManager.h" |
---|
48 | |
---|
49 | namespace orxonox |
---|
50 | { |
---|
51 | CreateFactory(Camera); |
---|
52 | |
---|
53 | Camera::Camera(BaseObject* creator) : PositionableEntity(creator) |
---|
54 | { |
---|
55 | RegisterObject(Camera); |
---|
56 | COUT(0) << this << ": created camera" << std::endl; |
---|
57 | if (!this->getScene() || !this->getScene()->getSceneManager()) |
---|
58 | ThrowException(AbortLoading, "Can't create Camera, no scene or no scene manager given."); |
---|
59 | |
---|
60 | this->camera_ = this->getScene()->getSceneManager()->createCamera(getUniqueNumberString()); |
---|
61 | this->getNode()->attachObject(this->camera_); |
---|
62 | |
---|
63 | this->bHasFocus_ = false; |
---|
64 | this->bDrag_ = false; |
---|
65 | this->nearClipDistance_ = 1; |
---|
66 | |
---|
67 | this->setObjectMode(0x0); |
---|
68 | |
---|
69 | this->setConfigValues(); |
---|
70 | this->configvaluecallback_changedNearClipDistance(); |
---|
71 | |
---|
72 | // this->requestFocus(); // ! HACK ! REMOVE THIS ! |
---|
73 | } |
---|
74 | |
---|
75 | Camera::~Camera() |
---|
76 | { |
---|
77 | if (this->isInitialized()) |
---|
78 | { |
---|
79 | this->releaseFocus(); |
---|
80 | this->getScene()->getSceneManager()->destroyCamera(this->camera_); |
---|
81 | } |
---|
82 | COUT(0) << this << ": destroyed camera" << std::endl; |
---|
83 | } |
---|
84 | |
---|
85 | void Camera::setConfigValues() |
---|
86 | { |
---|
87 | SetConfigValue(nearClipDistance_, 1.0f).callback(this, &Camera::configvaluecallback_changedNearClipDistance); |
---|
88 | } |
---|
89 | |
---|
90 | void Camera::configvaluecallback_changedNearClipDistance() |
---|
91 | { |
---|
92 | this->camera_->setNearClipDistance(this->nearClipDistance_); |
---|
93 | } |
---|
94 | |
---|
95 | void Camera::tick(float dt) |
---|
96 | { |
---|
97 | SUPER(Camera, tick, dt); |
---|
98 | /* |
---|
99 | // this stuff here may need some adjustments |
---|
100 | float coeff = (this->bDrag_) ? min(1.0f, 15.0f * dt) : (1.0f); |
---|
101 | |
---|
102 | Vector3 offset = this->getNode()->getWorldPosition() - this->cameraNode_->getWorldPosition(); |
---|
103 | this->cameraNode_->translate(coeff * offset); |
---|
104 | |
---|
105 | this->cameraNode_->setOrientation(Quaternion::Slerp(coeff, this->cameraNode_->getWorldOrientation(), this->getWorldOrientation(), false)); |
---|
106 | */ |
---|
107 | } |
---|
108 | |
---|
109 | void Camera::requestFocus() |
---|
110 | { |
---|
111 | CameraManager::getInstance().requestFocus(this); |
---|
112 | } |
---|
113 | |
---|
114 | void Camera::releaseFocus() |
---|
115 | { |
---|
116 | CameraManager::getInstance().releaseFocus(this); |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | what to do when camera loses focus (do not request focus in this function!!) |
---|
121 | this is called by the CameraManager singleton class to notify the camera |
---|
122 | */ |
---|
123 | void Camera::removeFocus() |
---|
124 | { |
---|
125 | COUT(0) << this << ": remove focus" << std::endl; |
---|
126 | this->bHasFocus_ = false; |
---|
127 | } |
---|
128 | |
---|
129 | void Camera::setFocus(Ogre::Viewport* viewport) |
---|
130 | { |
---|
131 | // This workaround is needed to avoid weird behaviour with active compositors while |
---|
132 | // switching the camera (like freezing the image) |
---|
133 | // |
---|
134 | // Last known Ogre version needing this workaround: |
---|
135 | // 1.4.8 |
---|
136 | |
---|
137 | // deactivate all compositors |
---|
138 | { |
---|
139 | Ogre::ResourceManager::ResourceMapIterator iterator = Ogre::CompositorManager::getSingleton().getResourceIterator(); |
---|
140 | while (iterator.hasMoreElements()) |
---|
141 | Ogre::CompositorManager::getSingleton().setCompositorEnabled(viewport, iterator.getNext()->getName(), false); |
---|
142 | } |
---|
143 | |
---|
144 | this->bHasFocus_ = true; |
---|
145 | viewport->setCamera(this->camera_); |
---|
146 | COUT(0) << this << ": set focus" << std::endl; |
---|
147 | |
---|
148 | // reactivate all visible compositors |
---|
149 | { |
---|
150 | for (ObjectList<Shader>::iterator it = ObjectList<Shader>::begin(); it != ObjectList<Shader>::end(); ++it) |
---|
151 | it->updateVisibility(); |
---|
152 | } |
---|
153 | } |
---|
154 | } |
---|