-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransform.cpp
More file actions
44 lines (39 loc) · 1.46 KB
/
Transform.cpp
File metadata and controls
44 lines (39 loc) · 1.46 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
#include "Defines.h"
#include "Transform.h"
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;
}
void Transform::SetPosition(const XMFLOAT3& position)
{
m_Position = position;
}
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;
}
XMMATRIX Transform::GetWorldToLocalMatrixXM() const
{
XMMATRIX invWorld = XMMatrixInverse(nullptr, GetLocalToWorldMatrixXM());
return invWorld;
}