-
Notifications
You must be signed in to change notification settings - Fork 6
/
camera.cpp
201 lines (156 loc) · 5.24 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
#include "camera.h"
#include <cmath>
// directions in local OpenGL coordinates
// default: up is the Y axis, forward is Z
const QVector3D Camera::LocalForward(0, 0, -1);
const QVector3D Camera::LocalRight (1, 0, 0);
const QVector3D Camera::LocalUp (0, 1, 0);
const float PI_q = std::atan(1.0f); // PI/4
const auto degToRad = [](const float deg) {
return deg * PI_q / 45;
};
// camera config defaults
CameraConfig::CameraConfig()
: c_mode(CameraMode::Target),
p_mode(ProjectionMode::Perspective),
fov(45),
nearPlane(0.1f),
farPlane(1000),
initialTranslation(QVector3D()),
WorldForward(Camera::LocalForward),
WorldRight(Camera::LocalRight),
WorldUp(Camera::LocalUp)
{}
Camera::Camera()
: m_config(),
aspectRatio(1),
distance(0),
m_dirty(true)
{
setConfig(m_config);
}
void Camera::setConfig(const CameraConfig &config) {
m_config = config;
QQuaternion localRotation = QQuaternion::fromAxes(-LocalForward, LocalRight, LocalUp);
QQuaternion worldRotation = QQuaternion::fromAxes(-m_config.WorldForward, m_config.WorldRight, m_config.WorldUp);
// from world to local OpenGL coordinates
m_worldToLocal = localRotation * worldRotation.conjugated();
reset();
}
// Transform By
void Camera::translate(const QVector3D &dt) {
m_dirty = true;
m_translation += dt;
distance = (m_target - m_translation).length();
updateFrustum();
}
void Camera::rotate(const QQuaternion &dr) {
m_dirty = true;
m_rotation = dr * m_rotation;
if (m_config.c_mode == CameraMode::Target) {
// rotation around target changes the translation of the camera,
// but keeps the distance from the target
QVector3D delta_old = m_target - m_translation;
QVector3D delta_new = dr.rotatedVector(delta_old);
m_translation += delta_old - delta_new;
}
}
// Setters
void Camera::setTranslation(const QVector3D &t) {
m_dirty = true;
m_translation = t;
distance = (m_target - m_translation).length();
updateFrustum();
}
void Camera::setRotation(const QQuaternion &r) {
m_dirty = true;
m_rotation = r;
if (m_config.c_mode == CameraMode::Target) {
QVector3D delta_old = (m_target - m_translation);
QVector3D delta_new = forwardVector();
m_translation += delta_old - delta_new;
}
}
void Camera::setTarget(const QVector3D &t) {
m_dirty = true;
m_target = t;
QVector3D delta = m_translation - m_target;
m_rotation = QQuaternion::fromDirection(delta, upVector());
distance = delta.length();
updateFrustum();
emit targetChanged(m_target);
}
void Camera::setCameraMode(CameraMode mode) {
m_config.c_mode = mode;
emit cameraModeChanged(mode);
if (mode == CameraMode::Target) {
// (re)setting to target chooses a new target in front of the camera
// with the initial distance + log of current distance from center
setTarget(m_translation
+ forwardVector() * m_config.initialTranslation.length()
+ forwardVector() * std::log(m_translation.length()));
}
}
void Camera::setProjectionMode(ProjectionMode mode) {
m_config.p_mode = mode;
setAspectRatio(aspectRatio);
emit projectionModeChanged(mode);
}
void Camera::setAspectRatio(float r) {
m_dirty = true;
aspectRatio = r;
updateFrustum();
}
void Camera::updateFrustum() {
float zNear = m_config.nearPlane;
float zFar = std::max(2.0f * distance, m_config.farPlane);
m_projection.setToIdentity();
if (m_config.p_mode == ProjectionMode::Perspective) {
m_projection.perspective(m_config.fov, aspectRatio, zNear, zFar);
} else if (m_config.p_mode == ProjectionMode::Orthographic) {
float left = -aspectRatio * distance * std::tan(degToRad(m_config.fov / 2.0f));
float right = aspectRatio * distance * std::tan(degToRad(m_config.fov / 2.0f));
float bottom = -distance * std::tan(degToRad(m_config.fov / 2.0f));
float top = distance * std::tan(degToRad(m_config.fov / 2.0f));
m_projection.ortho(left, right, bottom, top, zNear, zFar);
}
}
void Camera::reset() {
setTranslation(m_config.initialTranslation);
setTarget(0, 0, 0);
m_rotation = m_worldToLocal.conjugated();
}
// Accessors
const QMatrix4x4 &Camera::toMatrix() {
if (m_dirty) {
m_dirty = false;
m_world.setToIdentity();
m_world.rotate(m_rotation.conjugated());
m_world.translate(-m_translation);
m_world = m_projection * m_world;
}
return m_world;
}
// Queries
QVector3D Camera::forwardVector() const {
return m_rotation.rotatedVector(LocalForward);
}
QVector3D Camera::rightVector() const {
return m_rotation.rotatedVector(LocalRight);
}
QVector3D Camera::upVector() const {
return m_rotation.rotatedVector(LocalUp);
}
bool Camera::upsideDown() const {
// if the up vector actually points down, we are upside down
return QVector3D::dotProduct(m_rotation.conjugated() * m_config.WorldUp, m_worldToLocal * m_config.WorldUp) < 0;
}
// Qt Streams
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const Camera &transform) {
dbg << "Camera\n{\n";
dbg << "Position: <" << transform.translation().x() << ", " << transform.translation().y() << ", " << transform.translation().z() << ">\n";
dbg << "Rotation: <" << transform.rotation().x() << ", " << transform.rotation().y() << ", " << transform.rotation().z() << " | " << transform.rotation().scalar() << ">\n}";
return dbg;
}
#endif