-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTransform.cpp
More file actions
172 lines (121 loc) · 3.35 KB
/
Transform.cpp
File metadata and controls
172 lines (121 loc) · 3.35 KB
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
#include "Transform.h"
#include <QMatrix3x3>
Transform::Transform(QVector3D position, QQuaternion rotation, QVector3D scale)
: objectPosition(position)
, objectRotation(rotation)
, objectScale(scale)
{
}
QVector3D Transform::position() const
{
return objectPosition;
}
QQuaternion Transform::rotation() const
{
return objectRotation;
}
QVector3D Transform::scale() const
{
return objectScale;
}
void Transform::setPosition(QVector3D position)
{
objectPosition = position;
}
void Transform::setPosition(float x, float y, float z)
{
objectPosition = QVector3D(x, y, z);
}
void Transform::setRotation(QQuaternion rotation)
{
objectRotation = rotation;
}
void Transform::setRotation(QVector3D axis, float angle)
{
objectRotation = QQuaternion::fromAxisAndAngle(axis, angle);
}
void Transform::setRotation(float xAngle, float yAngle, float zAngle)
{
objectRotation = QQuaternion::fromEulerAngles(xAngle, yAngle, zAngle);
}
void Transform::setRotation(QVector3D xyzAngles)
{
objectRotation = QQuaternion::fromEulerAngles(xyzAngles);
}
void Transform::setScale(QVector3D scale)
{
objectScale = scale;
}
void Transform::setScale(float x, float y, float z)
{
objectScale = QVector3D(x, y, z);
}
void Transform::setScale(float scale)
{
objectScale = QVector3D(scale, scale, scale);
}
void Transform::move(float x, float y, float z)
{
objectPosition += QVector3D(x, y, z);
}
void Transform::move(QVector3D offset)
{
objectPosition += offset;
}
void Transform::scaleBy(float value)
{
objectScale *= value;
}
void Transform::scaleBy(QVector3D values)
{
objectScale *= values;
}
void Transform::rotate(QQuaternion rotation, Space space)
{
if (space == Space::Local)
{
objectRotation = objectRotation * rotation;
}
else
{
objectRotation = rotation * objectRotation;
}
}
void Transform::rotate(QVector3D axis, float angle, Space space)
{
rotate(QQuaternion::fromAxisAndAngle(axis.x(), axis.y(), axis.z(), angle), space);
}
void Transform::rotate(float xAngle, float yAngle, float zAngle, Space space)
{
rotate(QQuaternion::fromEulerAngles(xAngle, yAngle, zAngle), space);
}
void Transform::lookAt(QVector3D direction, QVector3D upVector, bool orthogonalize)
{
upVector = upVector.normalized();
QVector3D forward = direction.normalized();
QVector3D up = orthogonalize? (upVector - QVector3D::dotProduct(forward, upVector) * forward).normalized() : upVector;
QVector3D right = QVector3D::crossProduct(up, forward);
float lookRotation[] =
{
forward.x(), right.x(), up.x(),
forward.y(), right.y(), up.y(),
forward.z(), right.z(), up.z()
};
objectRotation = QQuaternion::fromRotationMatrix(QMatrix3x3(lookRotation));
}
void Transform::lookAt(float directionX, float directionY, float directionZ, float upVectorX, float upVectorY, float upVectorZ, bool orthogonalize)
{
lookAt(QVector3D(directionX, directionY, directionZ), QVector3D(upVectorX, upVectorY, upVectorZ), orthogonalize);
}
QVector3D Transform::localX() const
{
return objectRotation.rotatedVector(QVector3D(1.0f, 0.0f, 0.0f));
}
QVector3D Transform::localY() const
{
return objectRotation.rotatedVector(QVector3D(0.0f, 1.0f, 0.0f));
}
QVector3D Transform::localZ() const
{
return objectRotation.rotatedVector(QVector3D(0.0f, 0.0f, 1.0f));
}