/
Camera.cpp
266 lines (213 loc) · 5.26 KB
/
Camera.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "Camera.h"
#include <functional>
#include "iregistry.h"
#include "registry/registry.h"
#include "CameraManager.h"
#include "render/View.h"
#include "selection/SelectionVolume.h"
#include "Rectangle.h"
namespace camera
{
namespace
{
const std::string RKEY_SELECT_EPSILON = "user/ui/selectionEpsilon";
const Matrix4 g_radiant2opengl = Matrix4::byColumns(
0, -1, 0, 0,
0, 0, 1, 0,
-1, 0, 0, 0,
0, 0, 0, 1
);
const Matrix4 g_opengl2radiant = Matrix4::byColumns(
0, 0, -1, 0,
-1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 0, 1
);
inline Matrix4 projection_for_camera(float near_z, float far_z, float fieldOfView, int width, int height)
{
const auto half_width = near_z * tan(degrees_to_radians(fieldOfView * 0.5));
const auto half_height = half_width * (static_cast<double>(height) / static_cast<double>(width));
return Matrix4::getProjectionForFrustum(
-half_width,
half_width,
-half_height,
half_height,
near_z,
far_z
);
}
}
Vector3 Camera::_prevOrigin(0,0,0);
Vector3 Camera::_prevAngles(0,0,0);
Camera::Camera(render::IRenderView& view, const std::function<void(bool)>& requestRedraw) :
_origin(_prevOrigin), // Use previous origin for camera position
_angles(_prevAngles),
_requestRedraw(requestRedraw),
_fieldOfView(75.0f),
_farClipPlane(32768),
_width(0),
_height(0),
_projection(Matrix4::getIdentity()),
_modelview(Matrix4::getIdentity()),
_view(view)
{}
void Camera::updateModelview()
{
_prevAngles = _angles;
_prevOrigin = _origin;
_modelview = Matrix4::getIdentity();
// roll, pitch, yaw
Vector3 radiant_eulerXYZ(0, -_angles[camera::CAMERA_PITCH], _angles[camera::CAMERA_YAW]);
_modelview.translateBy(_origin);
_modelview.rotateByEulerXYZDegrees(radiant_eulerXYZ);
_modelview.multiplyBy(g_radiant2opengl);
_modelview.invert();
updateVectors();
_view.construct(_projection, _modelview, _width, _height);
}
void Camera::updateVectors()
{
for (int i = 0; i < 3; i++)
{
_vright[i] = _modelview[(i<<2)+0];
_vup[i] = _modelview[(i<<2)+1];
_vpn[i] = _modelview[(i<<2)+2];
}
}
void Camera::freemoveUpdateAxes()
{
_right = _vright;
_forward = -_vpn;
}
const Vector3& Camera::getCameraOrigin() const
{
return _origin;
}
void Camera::setCameraOrigin(const Vector3& newOrigin)
{
doSetOrigin(newOrigin, true);
queueDraw();
CameraManager::GetInstanceInternal().onCameraViewChanged();
}
const Vector3& Camera::getCameraAngles() const
{
return _angles;
}
void Camera::setCameraAngles(const Vector3& newAngles)
{
doSetAngles(newAngles, true);
queueDraw();
CameraManager::GetInstanceInternal().onCameraViewChanged();
}
void Camera::doSetOrigin(const Vector3& origin, bool updateModelView)
{
_origin = origin;
_prevOrigin = _origin;
if (updateModelView)
{
updateModelview();
queueDraw();
}
}
void Camera::doSetAngles(const Vector3& angles, bool updateModelView)
{
_angles = angles;
_prevAngles = _angles;
if (updateModelView)
{
updateModelview();
freemoveUpdateAxes();
}
}
void Camera::setOriginAndAngles(const Vector3& newOrigin, const Vector3& newAngles)
{
doSetOrigin(newOrigin, false); // hold back matrix recalculation
doSetAngles(newAngles, false); // hold back matrix recalculation
updateModelview();
freemoveUpdateAxes();
queueDraw();
CameraManager::GetInstanceInternal().onCameraViewChanged();
}
const Vector3& Camera::getRightVector() const
{
return _vright;
}
const Vector3& Camera::getUpVector() const
{
return _vup;
}
const Vector3& Camera::getForwardVector() const
{
return _vpn;
}
const Matrix4& Camera::getModelView() const
{
return _modelview;
}
const Matrix4& Camera::getProjection() const
{
return _projection;
}
int Camera::getDeviceWidth() const
{
return _width;
}
int Camera::getDeviceHeight() const
{
return _height;
}
void Camera::setDeviceDimensions(int width, int height)
{
_width = width;
_height = height;
updateProjection();
}
SelectionTestPtr Camera::createSelectionTestForPoint(const Vector2& point)
{
float selectEpsilon = registry::getValue<float>(RKEY_SELECT_EPSILON);
// Get the mouse position
Vector2 deviceEpsilon(selectEpsilon / getDeviceWidth(), selectEpsilon / getDeviceHeight());
// Copy the current view and constrain it to a small rectangle
render::View scissored(_view);
auto rect = selection::Rectangle::ConstructFromPoint(point, deviceEpsilon);
scissored.EnableScissor(rect.min[0], rect.max[0], rect.min[1], rect.max[1]);
return SelectionTestPtr(new SelectionVolume(scissored));
}
const VolumeTest& Camera::getVolumeTest() const
{
return _view;
}
void Camera::queueDraw()
{
_requestRedraw(false);
}
void Camera::forceRedraw()
{
_requestRedraw(true);
}
void Camera::moveUpdateAxes()
{
double ya = degrees_to_radians(_angles[camera::CAMERA_YAW]);
// the movement matrix is kept 2d
_forward[0] = static_cast<float>(cos(ya));
_forward[1] = static_cast<float>(sin(ya));
_forward[2] = 0;
_right[0] = _forward[1];
_right[1] = -_forward[0];
}
float Camera::getFarClipPlaneDistance() const
{
return _farClipPlane;
}
void Camera::setFarClipPlaneDistance(float distance)
{
_farClipPlane = distance;
updateProjection();
}
void Camera::updateProjection()
{
auto farClip = getFarClipPlaneDistance();
_projection = projection_for_camera(farClip / 4096.0f, farClip, _fieldOfView, _width, _height);
_view.construct(_projection, _modelview, _width, _height);
}
} // namespace