-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathTransform.cpp
223 lines (187 loc) · 4.75 KB
/
Transform.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
#include "Transform.h"
using namespace DirectX;
Transform::Transform() :
position(0, 0, 0),
pitchYawRoll(0, 0, 0),
scale(1, 1, 1),
up(0, 1, 0),
right(1, 0, 0),
forward(0, 0, 1),
matricesDirty(false),
vectorsDirty(false)
{
// Start with an identity matrix and basic transform data
XMStoreFloat4x4(&worldMatrix, XMMatrixIdentity());
XMStoreFloat4x4(&worldInverseTransposeMatrix, XMMatrixIdentity());
}
void Transform::MoveAbsolute(float x, float y, float z)
{
position.x += x;
position.y += y;
position.z += z;
matricesDirty = true;
}
void Transform::MoveAbsolute(DirectX::XMFLOAT3 offset)
{
position.x += offset.x;
position.y += offset.y;
position.z += offset.z;
matricesDirty = true;
}
void Transform::MoveRelative(float x, float y, float z)
{
// Create a direction vector from the params
// and a rotation quaternion
XMVECTOR movement = XMVectorSet(x, y, z, 0);
XMVECTOR rotQuat = XMQuaternionRotationRollPitchYawFromVector(XMLoadFloat3(&pitchYawRoll));
// Rotate the movement by the quaternion
XMVECTOR dir = XMVector3Rotate(movement, rotQuat);
// Add and store, and invalidate the matrices
XMStoreFloat3(&position, XMLoadFloat3(&position) + dir);
matricesDirty = true;
}
void Transform::MoveRelative(DirectX::XMFLOAT3 offset)
{
// Call the the overload
MoveRelative(offset.x, offset.y, offset.z);
}
void Transform::Rotate(float p, float y, float r)
{
pitchYawRoll.x += p;
pitchYawRoll.y += y;
pitchYawRoll.z += r;
matricesDirty = true;
vectorsDirty = true;
}
void Transform::Rotate(DirectX::XMFLOAT3 pitchYawRoll)
{
this->pitchYawRoll.x += pitchYawRoll.x;
this->pitchYawRoll.y += pitchYawRoll.y;
this->pitchYawRoll.z += pitchYawRoll.z;
matricesDirty = true;
vectorsDirty = true;
}
void Transform::Scale(float uniformScale)
{
scale.x *= uniformScale;
scale.y *= uniformScale;
scale.z *= uniformScale;
matricesDirty = true;
}
void Transform::Scale(float x, float y, float z)
{
scale.x *= x;
scale.y *= y;
scale.z *= z;
matricesDirty = true;
}
void Transform::Scale(DirectX::XMFLOAT3 scale)
{
this->scale.x *= scale.x;
this->scale.y *= scale.y;
this->scale.z *= scale.z;
matricesDirty = true;
}
void Transform::SetPosition(float x, float y, float z)
{
position.x = x;
position.y = y;
position.z = z;
matricesDirty = true;
}
void Transform::SetPosition(DirectX::XMFLOAT3 position)
{
this->position = position;
matricesDirty = true;
}
void Transform::SetRotation(float p, float y, float r)
{
pitchYawRoll.x = p;
pitchYawRoll.y = y;
pitchYawRoll.z = r;
matricesDirty = true;
vectorsDirty = true;
}
void Transform::SetRotation(DirectX::XMFLOAT3 pitchYawRoll)
{
this->pitchYawRoll = pitchYawRoll;
matricesDirty = true;
vectorsDirty = true;
}
void Transform::SetScale(float uniformScale)
{
scale.x = uniformScale;
scale.y = uniformScale;
scale.z = uniformScale;
matricesDirty = true;
}
void Transform::SetScale(float x, float y, float z)
{
scale.x = x;
scale.y = y;
scale.z = z;
matricesDirty = true;
}
void Transform::SetScale(DirectX::XMFLOAT3 scale)
{
this->scale = scale;
matricesDirty = true;
}
DirectX::XMFLOAT3 Transform::GetPosition() { return position; }
DirectX::XMFLOAT3 Transform::GetPitchYawRoll() { return pitchYawRoll; }
DirectX::XMFLOAT3 Transform::GetScale() { return scale; }
DirectX::XMFLOAT3 Transform::GetUp()
{
UpdateVectors();
return up;
}
DirectX::XMFLOAT3 Transform::GetRight()
{
UpdateVectors();
return right;
}
DirectX::XMFLOAT3 Transform::GetForward()
{
UpdateVectors();
return forward;
}
DirectX::XMFLOAT4X4 Transform::GetWorldMatrix()
{
UpdateMatrices();
return worldMatrix;
}
DirectX::XMFLOAT4X4 Transform::GetWorldInverseTransposeMatrix()
{
UpdateMatrices();
return worldMatrix;
}
void Transform::UpdateMatrices()
{
// Anything to update?
if (!matricesDirty)
return;
// Create the three transformation pieces
XMMATRIX trans = XMMatrixTranslationFromVector(XMLoadFloat3(&position));
XMMATRIX rot = XMMatrixRotationRollPitchYawFromVector(XMLoadFloat3(&pitchYawRoll));
XMMATRIX sc = XMMatrixScalingFromVector(XMLoadFloat3(&scale));
// Combine and store the world
XMMATRIX wm = sc * rot * trans;
XMStoreFloat4x4(&worldMatrix, wm);
// Invert and transpose, too
XMStoreFloat4x4(&worldInverseTransposeMatrix, XMMatrixInverse(0, XMMatrixTranspose(wm)));
// Matrices are up to date
matricesDirty = false;
}
void Transform::UpdateVectors()
{
// Do we need to update?
if (!vectorsDirty)
return;
// Update all three vectors
XMVECTOR rotationQuat = XMQuaternionRotationRollPitchYawFromVector(XMLoadFloat3(&pitchYawRoll));
XMStoreFloat3(&up, XMVector3Rotate(XMVectorSet(0, 1, 0, 0), rotationQuat));
XMStoreFloat3(&right, XMVector3Rotate(XMVectorSet(1, 0, 0, 0), rotationQuat));
XMStoreFloat3(&forward, XMVector3Rotate(XMVectorSet(0, 0, 1, 0), rotationQuat));
// Vectors are up to date
vectorsDirty = false;
}