-
Notifications
You must be signed in to change notification settings - Fork 0
/
Camera3D.cpp
94 lines (80 loc) · 1.97 KB
/
Camera3D.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
#include "Camera3D.h"
#include <QDebug>
const QVector3D Camera3D::LocalForward(0.0f, 0.0f, -1.0f);
const QVector3D Camera3D::LocalUp(0.0f, 1.0f, 0.0f);
const QVector3D Camera3D::LocalRight(1.0f, 0.0f, 0.0f);
// Transform By (Add/Scale)
void Camera3D::translate(const QVector3D &dt)
{
m_dirty = true;
m_translation += dt;
}
void Camera3D::rotate(const QQuaternion &dr)
{
m_dirty = true;
m_rotation = dr * m_rotation;
}
// Transform To (Setters)
void Camera3D::setTranslation(const QVector3D &t)
{
m_dirty = true;
m_translation = t;
}
void Camera3D::setRotation(const QQuaternion &r)
{
m_dirty = true;
m_rotation = r;
}
// Accessors
const QMatrix4x4 &Camera3D::toMatrix()
{
if (m_dirty)
{
m_dirty = false;
m_world.setToIdentity();
m_world.rotate(m_rotation.conjugated());
m_world.translate(-m_translation);
}
return m_world;
}
// Queries
QVector3D Camera3D::forward() const
{
return m_rotation.rotatedVector(LocalForward);
}
QVector3D Camera3D::up() const
{
return m_rotation.rotatedVector(LocalUp);
}
QVector3D Camera3D::right() const
{
return m_rotation.rotatedVector(LocalRight);
}
// Qt Streams
QDebug operator<<(QDebug dbg, const Camera3D &transform)
{
dbg << "Camera3D\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;
}
QDataStream &operator<<(QDataStream &out, const Camera3D &transform)
{
out << transform.m_translation;
out << transform.m_rotation;
return out;
}
QDataStream &operator>>(QDataStream &in, Camera3D &transform)
{
in >> transform.m_translation;
in >> transform.m_rotation;
transform.m_dirty = true;
return in;
}