-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransform.cpp
210 lines (178 loc) · 5.54 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
#include "Transform.h"
using namespace DirectX;
Transform::Transform(const DirectX::XMFLOAT3& scale, const DirectX::XMFLOAT3& rotation, const DirectX::XMFLOAT3& position)
: m_Scale(scale), m_Rotation(rotation), m_Position(position)
{
}
XMFLOAT3 Transform::GetScale() const
{
return m_Scale;
}
DirectX::XMVECTOR Transform::GetScaleXM() const
{
return XMLoadFloat3(&m_Scale);
}
XMFLOAT3 Transform::GetRotation() const
{
return m_Rotation;
}
DirectX::XMVECTOR Transform::GetRotationXM() const
{
return XMLoadFloat3(&m_Rotation);
}
XMFLOAT3 Transform::GetPosition() const
{
return m_Position;
}
DirectX::XMVECTOR Transform::GetPositionXM() const
{
return XMLoadFloat3(&m_Position);
}
XMFLOAT3 Transform::GetRightAxis() const
{
XMMATRIX R = XMMatrixRotationRollPitchYawFromVector(XMLoadFloat3(&m_Rotation));
XMFLOAT3 right;
XMStoreFloat3(&right, R.r[0]);
return right;
}
DirectX::XMVECTOR Transform::GetRightAxisXM() const
{
XMFLOAT3 right = GetRightAxis();
return XMLoadFloat3(&right);
}
XMFLOAT3 Transform::GetUpAxis() const
{
XMMATRIX R = XMMatrixRotationRollPitchYawFromVector(XMLoadFloat3(&m_Rotation));
XMFLOAT3 up;
XMStoreFloat3(&up, R.r[1]);
return up;
}
DirectX::XMVECTOR Transform::GetUpAxisXM() const
{
XMFLOAT3 up = GetUpAxis();
return XMLoadFloat3(&up);
}
XMFLOAT3 Transform::GetForwardAxis() const
{
XMMATRIX R = XMMatrixRotationRollPitchYawFromVector(XMLoadFloat3(&m_Rotation));
XMFLOAT3 forward;
XMStoreFloat3(&forward, R.r[2]);
return forward;
}
DirectX::XMVECTOR Transform::GetForwardAxisXM() const
{
XMFLOAT3 forward = GetForwardAxis();
return XMLoadFloat3(&forward);
}
XMFLOAT4X4 Transform::GetLocalToWorldMatrix() const
{
XMFLOAT4X4 res;
XMStoreFloat4x4(&res, GetLocalToWorldMatrixXM());
return res;
}
XMMATRIX Transform::GetLocalToWorldMatrixXM() const
{
XMVECTOR scaleVec = XMLoadFloat3(&m_Scale);
XMVECTOR rotationVec = XMLoadFloat3(&m_Rotation);
XMVECTOR positionVec = XMLoadFloat3(&m_Position);
XMMATRIX World = XMMatrixScalingFromVector(scaleVec) * XMMatrixRotationRollPitchYawFromVector(rotationVec) * XMMatrixTranslationFromVector(positionVec);
return World;
}
XMFLOAT4X4 Transform::GetWorldToLocalMatrix() const
{
XMFLOAT4X4 res;
XMStoreFloat4x4(&res, GetWorldToLocalMatrixXM());
return res;
}
XMMATRIX Transform::GetWorldToLocalMatrixXM() const
{
XMMATRIX InvWorld = XMMatrixInverse(nullptr, GetLocalToWorldMatrixXM());
return InvWorld;
}
void Transform::SetScale(const XMFLOAT3& scale)
{
m_Scale = scale;
}
void Transform::SetScale(float x, float y, float z)
{
m_Scale = XMFLOAT3(x, y, z);
}
void Transform::SetRotation(const XMFLOAT3& eulerAnglesInRadian)
{
m_Rotation = eulerAnglesInRadian;
}
void Transform::SetRotation(float x, float y, float z)
{
m_Rotation = XMFLOAT3(x, y, z);
}
void Transform::SetPosition(const XMFLOAT3& position)
{
m_Position = position;
}
void Transform::SetPosition(float x, float y, float z)
{
m_Position = XMFLOAT3(x, y, z);
}
void Transform::Rotate(const XMFLOAT3& eulerAnglesInRadian)
{
XMVECTOR newRotationVec = XMVectorAdd(XMLoadFloat3(&m_Rotation), XMLoadFloat3(&eulerAnglesInRadian));
XMStoreFloat3(&m_Rotation, newRotationVec);
}
void Transform::RotateAxis(const XMFLOAT3& axis, float radian)
{
XMVECTOR rotationVec = XMLoadFloat3(&m_Rotation);
XMMATRIX R = XMMatrixRotationRollPitchYawFromVector(rotationVec) *
XMMatrixRotationAxis(XMLoadFloat3(&axis), radian);
XMFLOAT4X4 rotMatrix;
XMStoreFloat4x4(&rotMatrix, R);
m_Rotation = GetEulerAnglesFromRotationMatrix(rotMatrix);
}
void Transform::RotateAround(const XMFLOAT3& point, const XMFLOAT3& axis, float radian)
{
XMVECTOR rotationVec = XMLoadFloat3(&m_Rotation);
XMVECTOR positionVec = XMLoadFloat3(&m_Position);
XMVECTOR centerVec = XMLoadFloat3(&point);
// 以point作为原点进行旋转
XMMATRIX RT = XMMatrixRotationRollPitchYawFromVector(rotationVec) * XMMatrixTranslationFromVector(positionVec - centerVec);
RT *= XMMatrixRotationAxis(XMLoadFloat3(&axis), radian);
RT *= XMMatrixTranslationFromVector(centerVec);
XMFLOAT4X4 rotMatrix;
XMStoreFloat4x4(&rotMatrix, RT);
m_Rotation = GetEulerAnglesFromRotationMatrix(rotMatrix);
XMStoreFloat3(&m_Position, RT.r[3]);
}
void Transform::Translate(const XMFLOAT3& direction, float magnitude)
{
XMVECTOR directionVec = XMVector3Normalize(XMLoadFloat3(&direction));
XMVECTOR newPosition = XMVectorMultiplyAdd(XMVectorReplicate(magnitude), directionVec, XMLoadFloat3(&m_Position));
XMStoreFloat3(&m_Position, newPosition);
}
void Transform::LookAt(const XMFLOAT3& target, const XMFLOAT3& up)
{
XMMATRIX View = XMMatrixLookAtLH(XMLoadFloat3(&m_Position), XMLoadFloat3(&target), XMLoadFloat3(&up));
XMMATRIX InvView = XMMatrixInverse(nullptr, View);
XMFLOAT4X4 rotMatrix;
XMStoreFloat4x4(&rotMatrix, InvView);
m_Rotation = GetEulerAnglesFromRotationMatrix(rotMatrix);
}
void Transform::LookTo(const XMFLOAT3& direction, const XMFLOAT3& up)
{
XMMATRIX View = XMMatrixLookToLH(XMLoadFloat3(&m_Position), XMLoadFloat3(&direction), XMLoadFloat3(&up));
XMMATRIX InvView = XMMatrixInverse(nullptr, View);
XMFLOAT4X4 rotMatrix;
XMStoreFloat4x4(&rotMatrix, InvView);
m_Rotation = GetEulerAnglesFromRotationMatrix(rotMatrix);
}
XMFLOAT3 Transform::GetEulerAnglesFromRotationMatrix(const XMFLOAT4X4& rotationMatrix)
{
// 通过旋转矩阵反求欧拉角
float c = sqrtf(1.0f - rotationMatrix(2, 1) * rotationMatrix(2, 1));
// 防止r[2][1]出现大于1的情况
if (isnan(c))
c = 0.0f;
XMFLOAT3 rotation;
rotation.z = atan2f(rotationMatrix(0, 1), rotationMatrix(1, 1));
rotation.x = atan2f(-rotationMatrix(2, 1), c);
rotation.y = atan2f(rotationMatrix(2, 0), rotationMatrix(2, 2));
return rotation;
}