xoreos  0.0.5
camera.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include <cstring>
26 
27 #include "glm/vec3.hpp"
28 #include "glm/mat4x4.hpp"
29 #include "glm/gtc/type_ptr.hpp"
30 #include "glm/gtc/matrix_transform.hpp"
31 
32 #include "src/common/util.h"
33 #include "src/common/maths.h"
34 
35 #include "src/graphics/camera.h"
36 #include "src/graphics/graphics.h"
37 
38 #include "src/events/events.h"
40 
42 
43 namespace Graphics {
44 
45 CameraManager::CameraManager() : _lastChanged(0), _needUpdate(false) {
46  _minPosition[0] = -FLT_MAX;
47  _minPosition[1] = -FLT_MAX;
48  _minPosition[2] = -FLT_MAX;
49  _maxPosition[0] = FLT_MAX;
50  _maxPosition[1] = FLT_MAX;
51  _maxPosition[2] = FLT_MAX;
52 
53  _position [0] = 0.0f;
54  _position [1] = 0.0f;
55  _position [2] = 0.0f;
56  _orientation[0] = 0.0f;
57  _orientation[1] = 0.0f;
58  _orientation[2] = 0.0f;
59 
60  _positionCache [0] = 0.0f;
61  _positionCache [1] = 0.0f;
62  _positionCache [2] = 0.0f;
63  _orientationCache[0] = 0.0f;
64  _orientationCache[1] = 0.0f;
65  _orientationCache[2] = 0.0f;
66 }
67 
69  GfxMan.lockFrame();
70 
71  if (!_needUpdate) {
72  GfxMan.unlockFrame();
73  return;
74  }
75 
76  _needUpdate = false;
77 
78  memcpy(_positionCache , _position , sizeof(_positionCache));
80 
81  GfxMan.recalculateObjectDistances();
82  NotificationMan.cameraMoved();
83 
84  GfxMan.unlockFrame();
85 }
86 
87 const float *CameraManager::getPosition() const {
88  return _positionCache;
89 }
90 
91 const float *CameraManager::getOrientation() const {
92  return _orientationCache;
93 }
94 
96  _minPosition[0] = -FLT_MAX;
97  _minPosition[1] = -FLT_MAX;
98  _minPosition[2] = -FLT_MAX;
99  _maxPosition[0] = FLT_MAX;
100  _maxPosition[1] = FLT_MAX;
101  _maxPosition[2] = FLT_MAX;
102 
103  _position [0] = 0.0f;
104  _position [1] = 0.0f;
105  _position [2] = 0.0f;
106  _orientation[0] = 0.0f;
107  _orientation[1] = 0.0f;
108  _orientation[2] = 0.0f;
109 
110  _lastChanged = EventMan.getTimestamp();
111 
112  _needUpdate = true;
113 }
114 
115 void CameraManager::limit(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
116  _minPosition[0] = minX;
117  _minPosition[1] = minY;
118  _minPosition[2] = minZ;
119  _maxPosition[0] = maxX;
120  _maxPosition[1] = maxY;
121  _maxPosition[2] = maxZ;
122 }
123 
124 void CameraManager::setPosition(float x, float y, float z) {
125  _position[0] = CLIP(x, _minPosition[0], _maxPosition[0]);
126  _position[1] = CLIP(y, _minPosition[1], _maxPosition[1]);
127  _position[2] = CLIP(z, _minPosition[2], _maxPosition[2]);
128 
129  _lastChanged = EventMan.getTimestamp();
130 
131  _needUpdate = true;
132 }
133 
134 void CameraManager::setOrientation(float x, float y, float z) {
135  _orientation[0] = fmodf(x, 360.0f);
136  _orientation[1] = fmodf(y, 360.0f);
137  _orientation[2] = fmodf(z, 360.0f);
138 
139  _lastChanged = EventMan.getTimestamp();
140 
141  _needUpdate = true;
142 }
143 
144 void CameraManager::turn(float x, float y, float z) {
145  setOrientation(_orientation[0] + x, _orientation[1] + y, _orientation[2] + z);
146 }
147 
148 void CameraManager::move(float x, float y, float z) {
149  setPosition(_position[0] + x, _position[1] + y, _position[2] + z);
150 }
151 
152 void CameraManager::moveRelative(float x, float y, float z) {
153  glm::mat4 orientation;
154 
155  orientation = glm::rotate(orientation, Common::deg2rad(_orientation[2]), glm::vec3(0.0f, 0.0f, 1.0f));
156  orientation = glm::rotate(orientation, Common::deg2rad(_orientation[1]), glm::vec3(0.0f, 1.0f, 0.0f));
157  orientation = glm::rotate(orientation, Common::deg2rad(_orientation[0]), glm::vec3(1.0f, 0.0f, 0.0f));
158 
159  const glm::vec4 relative = orientation * glm::vec4(x, y, z, 0);
160 
161  move(relative[0], relative[1], relative[2]);
162 }
163 
165  return _lastChanged;
166 }
167 
168 } // End of namespace Graphics
void setOrientation(float x, float y, float z)
Set the camera orientation.
Definition: camera.cpp:134
const float * getOrientation() const
Get the current camera orientation cache.
Definition: camera.cpp:91
float _orientation[3]
Current orientation.
Definition: camera.h:72
The global graphics manager.
const float * getPosition() const
Get the current camera position cache.
Definition: camera.cpp:87
Mathematical helpers.
Camera management.
#define NotificationMan
Shortcut for accessing the notification manager.
Definition: notifications.h:75
void reset()
Reset the current position and orientation.
Definition: camera.cpp:95
void moveRelative(float x, float y, float z)
Move relative to the current view axis.
Definition: camera.cpp:152
float _position[3]
Current position.
Definition: camera.h:71
float _minPosition[3]
Definition: camera.h:68
uint32 lastChanged() const
The timestamp the camera was changed last.
Definition: camera.cpp:164
float _orientationCache[3]
Current orientation, cached.
Definition: camera.h:75
The notification manager, handling all notifications.
void move(float x, float y, float z)
Move along axes.
Definition: camera.cpp:148
void turn(float x, float y, float z)
Turn along axes.
Definition: camera.cpp:144
Utility templates and functions.
#define DECLARE_SINGLETON(T)
Note that you need to use this macro from the global namespace.
Definition: singleton.h:122
The global events manager.
float _positionCache[3]
Current position, cached.
Definition: camera.h:74
void limit(float minX=-FLT_MAX, float minY=-FLT_MAX, float minZ=-FLT_MAX, float maxX=FLT_MAX, float maxY=FLT_MAX, float maxZ=FLT_MAX)
Set limits on the camera position.
Definition: camera.cpp:115
#define EventMan
Shortcut for accessing the events manager.
Definition: events.h:210
uint32_t uint32
Definition: types.h:204
float _maxPosition[3]
Definition: camera.h:69
T CLIP(T v, T amin, T amax)
Definition: util.h:72
void setPosition(float x, float y, float z)
Set the camera position.
Definition: camera.cpp:124
static float deg2rad(float deg)
Definition: maths.h:97
#define FLT_MAX
Definition: maths.h:47
#define GfxMan
Shortcut for accessing the graphics manager.
Definition: graphics.h:299
void update()
Update the caches with the current position and orientation.
Definition: camera.cpp:68