forked from vixorien/D3D11Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransform.cpp
209 lines (176 loc) · 7.71 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
#include "Transform.h"
// --------------------------------------------------------
// Updates the world and world inverse transpose matrices
// --------------------------------------------------------
void Transform::UpdateMatrices() {
//Create matrices for translation, scale, and rotation
DirectX::XMMATRIX translation = DirectX::XMMatrixTranslationFromVector(DirectX::XMLoadFloat3(&position));
DirectX::XMMATRIX rotation = DirectX::XMMatrixRotationRollPitchYawFromVector(DirectX::XMLoadFloat3(&this->rotation));
DirectX::XMMATRIX scale = DirectX::XMMatrixScalingFromVector(DirectX::XMLoadFloat3(&this->scale));
//Apply the transformation matrices
DirectX::XMMATRIX world = scale * rotation * translation;
//Sore the final matrix and its inverse transpose as a 4x4 float
DirectX::XMStoreFloat4x4(&this->world, world);
DirectX::XMStoreFloat4x4(&this->worldInverseTranspose,
DirectX::XMMatrixInverse(0, DirectX::XMMatrixTranspose(world)));
}
Transform::Transform() {
position = DirectX::XMFLOAT3(0.0f, 0.0f, 0.0f);
scale = DirectX::XMFLOAT3(1.0f, 1.0f, 1.0f);
rotation = DirectX::XMFLOAT3(0.0f, 0.0f, 0.0f);
right = DirectX::XMFLOAT3(1.0f, 0.0f, 0.0f);
up = DirectX::XMFLOAT3(0.0f, 1.0f, 0.0f);
forward = DirectX::XMFLOAT3(0.0f, 0.0f, 1.0f);
DirectX::XMStoreFloat4x4(&world, DirectX::XMMatrixIdentity());
DirectX::XMStoreFloat4x4(&worldInverseTranspose, DirectX::XMMatrixIdentity());
}
Transform::~Transform() {
}
// --------------------------------------------------------
// Sets the position using the provided x, y, and z params
// --------------------------------------------------------
void Transform::SetPosition(float x, float y, float z) {
position = DirectX::XMFLOAT3(x, y, z);
}
// --------------------------------------------------------
// Sets the position using the provided position
// --------------------------------------------------------
void Transform::SetPosition(DirectX::XMFLOAT3 position) {
this->position = DirectX::XMFLOAT3(position);
}
// -----------------------------------------------------------------
// Sets the rotation using the provided pitch, yaw, and roll params
// -----------------------------------------------------------------
void Transform::SetRotation(float pitch, float yaw, float roll) {
rotation = DirectX::XMFLOAT3(pitch, yaw, roll);
}
// --------------------------------------------------------
// Sets the rotation using the provided rotation
// --------------------------------------------------------
void Transform::SetRotation(DirectX::XMFLOAT3 rotation) {
this->rotation = DirectX::XMFLOAT3(rotation);
}
// --------------------------------------------------------
// Sets the scale using the provided x, y, and z params
// --------------------------------------------------------
void Transform::SetScale(float x, float y, float z) {
scale = DirectX::XMFLOAT3(x, y, z);
}
// --------------------------------------------------------
// Sets the scale using the provided scale
// --------------------------------------------------------
void Transform::SetScale(DirectX::XMFLOAT3 scale) {
this->scale = DirectX::XMFLOAT3(scale);
}
// --------------------------------------------------------
// Gets the position
// --------------------------------------------------------
DirectX::XMFLOAT3 Transform::GetPosition() {
return position;
}
// --------------------------------------------------------
// Gets the rotation
// --------------------------------------------------------
DirectX::XMFLOAT3 Transform::GetPitchYawRoll() {
return rotation;
}
// --------------------------------------------------------
// Gets the scale
// --------------------------------------------------------
DirectX::XMFLOAT3 Transform::GetScale() {
return scale;
}
// --------------------------------------------------------
// Updates and gets the world matrix
// --------------------------------------------------------
DirectX::XMFLOAT4X4 Transform::GetWorldMatrix() {
UpdateMatrices();
return world;
}
// --------------------------------------------------------
// Updates and gets the world inverse transpose matrix
// --------------------------------------------------------
DirectX::XMFLOAT4X4 Transform::GetWorldInverseTransposeMatrix() {
UpdateMatrices();
return worldInverseTranspose;
}
DirectX::XMFLOAT3 Transform::GetRight() {
return right;
}
DirectX::XMFLOAT3 Transform::GetUp() {
return up;
}
DirectX::XMFLOAT3 Transform::GetForward() {
return forward;
}
// -------------------------------------------------------------
// Translates the position with the provided x, y, and z params
// -------------------------------------------------------------
void Transform::MoveAbsolute(float x, float y, float z) {
MoveAbsolute(DirectX::XMFLOAT3(x, y, z));
}
// --------------------------------------------------------
// Translates the position with the provided offset
// --------------------------------------------------------
void Transform::MoveAbsolute(DirectX::XMFLOAT3 offset) {
DirectX::XMStoreFloat3(
&this->position,
DirectX::XMVectorAdd(
DirectX::XMLoadFloat3(&this->position),
DirectX::XMLoadFloat3(&offset)
)
);
}
void Transform::MoveRelative(float x, float y, float z) {
DirectX::XMFLOAT3 input(x, y, z);
DirectX::XMVECTOR direction = DirectX::XMLoadFloat3(&input);
DirectX::XMVECTOR quaternion = DirectX::XMQuaternionRotationRollPitchYaw(rotation.x, rotation.y, rotation.z);
DirectX::XMVECTOR rotated = DirectX::XMVector3Rotate(direction, quaternion);
DirectX::XMVECTOR pos = DirectX::XMLoadFloat3(&position);
DirectX::XMStoreFloat3(&position, DirectX::XMVectorAdd(pos, rotated));
}
// --------------------------------------------------------
// Rotates with the provided pitch, yaw, and roll params
// --------------------------------------------------------
void Transform::Rotate(float pitch, float yaw, float roll) {
Rotate(DirectX::XMFLOAT3(pitch, yaw, roll));
}
// --------------------------------------------------------
// Rotates with the provided rotation
// --------------------------------------------------------
void Transform::Rotate(DirectX::XMFLOAT3 rotation) {
//Update the rotation
DirectX::XMStoreFloat3(
&this->rotation,
DirectX::XMVectorAdd(
DirectX::XMLoadFloat3(&this->rotation),
DirectX::XMLoadFloat3(&rotation)
)
);
//Update right, up, and forward relative to transform
DirectX::XMVECTOR worldRight = DirectX::XMVectorSet(1.0f, 0.0f, 0.0f, 0.0f);
DirectX::XMVECTOR worldUp = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
DirectX::XMVECTOR worldForward = DirectX::XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);
DirectX::XMVECTOR quaternion = DirectX::XMQuaternionRotationRollPitchYaw(this->rotation.x, this->rotation.y, this->rotation.z);
DirectX::XMStoreFloat3(&this->right, DirectX::XMVector3Rotate(worldRight, quaternion));
DirectX::XMStoreFloat3(&this->up, DirectX::XMVector3Rotate(worldUp, quaternion));
DirectX::XMStoreFloat3(&this->forward, DirectX::XMVector3Rotate(worldForward, quaternion));
}
// --------------------------------------------------------
// Scales with the provided x, y, and z params
// --------------------------------------------------------
void Transform::Scale(float x, float y, float z) {
Scale(DirectX::XMFLOAT3(x, y, z));
}
// --------------------------------------------------------
// Scales with the provided scale
// --------------------------------------------------------
void Transform::Scale(DirectX::XMFLOAT3 scale) {
DirectX::XMStoreFloat3(
&this->scale,
DirectX::XMVectorMultiply(
DirectX::XMLoadFloat3(&this->scale),
DirectX::XMLoadFloat3(&scale)
)
);
}