dl
dl_transform.h
浏览该文件的文档.
1
18#pragma once
19
20#include "dl_matrix.h"
21
22
23namespace dl
24{
25
27class Transform
28{
29public:
34 static Transform* Create()
35 {
36 Transform* ret = new Transform;
37 return ret;
38 }
39
40 Position3 GetPosition() { return _translation; }
41 Position3 GetRotate() { return _rotation; }
42 Position3 GetScale() { return _scaling; }
43 Transform* GetParent() { return _parent; }
44
45 void SetPosition(const Position3& position)
46 {
47 _translation = position;
48 _bNeedUpdate = true;
49 }
50
51 void SetRotate(const Position3& rotate)
52 {
53 _rotation = rotate;
54 _bNeedUpdate = true;
55 }
56
57 void SetScale(const Position3& scale)
58 {
59 _scaling = scale;
60 _bNeedUpdate = true;
61 }
62 void SetScaleX(Float scale)
63 {
64 _scaling.x = scale;
65 _bNeedUpdate = true;
66 }
67 void SetScaleY(Float scale)
68 {
69 _scaling.y = scale;
70 _bNeedUpdate = true;
71 }
72 void SetScaleZ(Float scale)
73 {
74 _scaling.z = scale;
75 _bNeedUpdate = true;
76 }
77
78 void SetParent(Transform* parent) { _parent = parent; }
79
81
84 {
85 if (_bNeedUpdate)
86 {//需要更新时,重新计算并缓存
87 _mat = Mat::CreateTranslation(_translation);
88 //D3DXMatrixRotationYawPitchRoll也只是按y、x、z顺序组合
89 _mat *= Mat::CreateRotation(AXIS_Y, _rotation[1]);
90 _mat *= Mat::CreateRotation(AXIS_X, _rotation[0]);
91 _mat *= Mat::CreateRotation(AXIS_Z, _rotation[2]);
92
93 _mat *= Mat::CreateScaling(_scaling);
94
95 _bNeedUpdate = false;
96 }
97 //不需要则直接使用
98 return _mat;
99 }
100
103 {
104 Matrix4 mat = GetMatrix();
105
106 //一直转到世界
107 Transform* t = GetParent();
108 while (t)
109 {
110 mat = t->GetMatrix() * mat;
111 t = t->GetParent();
112 }
113
114 return mat;
115 }
116
117private:
118 Transform() :
119 _translation{},
120 _rotation{},
121 _scaling{ 1.0f, 1.0f, 1.0f },
122 _parent{ nullptr },
123 _bNeedUpdate{ true }
124 {}
125
126 Position3 _translation;
127 Position3 _rotation;
128 Position3 _scaling;
129 Transform* _parent;
130 bool _bNeedUpdate;
131 Matrix4 _mat;//使用时更新
132};
133
134}
Position3 ToWorld(const Position3 &pos)
Matrix4 GetMatrix()
返回变换矩阵(到父节点)
void SetScaleY(Float scale)
Matrix4 GetMatrixToWorld()
返回到世界的变换矩阵
void SetParent(Transform *parent)
void SetPosition(const Position3 &position)
void SetScale(const Position3 &scale)
void SetRotate(const Position3 &rotate)
Position3 GetPosition()
Transform * GetParent()
void SetScaleZ(Float scale)
static Transform * Create()
创建3d变换
Position3 GetRotate()
Position3 GetScale()
void SetScaleX(Float scale)
矩阵(列存储)
Matrix4 CreateRotation(const Position3 &axis, Float angle)
创建旋转矩阵
Matrix4 CreateTranslation(const Position3 &t)
创建平移矩阵
Matrix4 CreateScaling(const Position3 &s)
创建缩放矩阵
float Float
constexpr Position3 AXIS_Z
constexpr Position3 AXIS_X
Matrix< Float, 4, 4 > Matrix4
Array< Float, 3 > Position3
constexpr Position3 AXIS_Y