-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transform3D.cpp
122 lines (102 loc) · 2.5 KB
/
Transform3D.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
#include "Transform3D.h"
#include <QDebug>
const QVector3D Transform3D::LocalForward(0.0f, 0.0f, 1.0f);
const QVector3D Transform3D::LocalUp(0.0f, 1.0f, 0.0f);
const QVector3D Transform3D::LocalRight(1.0f, 0.0f, 0.0f);
// Transform By (Add/Scale)
void Transform3D::translate(const QVector3D &dt)
{
m_dirty = true;
m_translation += dt;
}
void Transform3D::scale(const QVector3D &ds)
{
m_dirty = true;
m_scale *= ds;
}
void Transform3D::rotate(const QQuaternion &dr)
{
m_dirty = true;
m_rotation = dr * m_rotation;
}
void Transform3D::grow(const QVector3D &ds)
{
m_dirty = true;
m_scale += ds;
}
// Transform To (Setters)
void Transform3D::setTranslation(const QVector3D &t)
{
m_dirty = true;
m_translation = t;
}
void Transform3D::setScale(const QVector3D &s)
{
m_dirty = true;
m_scale = s;
}
void Transform3D::setRotation(const QQuaternion &r)
{
m_dirty = true;
m_rotation = r;
}
// Accessors
const QMatrix4x4 &Transform3D::toMatrix()
{
if (m_dirty)
{
m_dirty = false;
m_world.setToIdentity();
m_world.translate(m_translation);
m_world.rotate(m_rotation);
m_world.scale(m_scale);
}
return m_world;
}
// Queries
QVector3D Transform3D::forward() const
{
return m_rotation.rotatedVector(LocalForward);
}
QVector3D Transform3D::up() const
{
return m_rotation.rotatedVector(LocalUp);
}
QVector3D Transform3D::right() const
{
return m_rotation.rotatedVector(LocalRight);
}
// Qt Streams
QDebug operator<<(QDebug dbg, const Transform3D &transform)
{
dbg << "Transform3D\n{\n";
dbg << "Position: <"
<< transform.translation().x() << ", "
<< transform.translation().y() << ", "
<< transform.translation().z() << ">\n";
dbg << "Scale: <"
<< transform.scale().x() << ", "
<< transform.scale().y() << ", "
<< transform.scale().z() << ">\n";
dbg << "Rotation: <"
<< transform.rotation().x() << ", "
<< transform.rotation().y() << ", "
<< transform.rotation().z() << " | "
<< transform.rotation().scalar() << ">\n}";
return dbg;
}
QDataStream &operator<<(QDataStream &out, const Transform3D &transform)
{
out << transform.m_translation;
out << transform.m_scale;
out << transform.m_rotation;
return out;
}
QDataStream &operator>>(QDataStream &in, Transform3D &transform)
{
in >> transform.m_translation;
in >> transform.m_scale;
in >> transform.m_rotation;
transform.m_dirty = true;
return in;
}