-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathogldev_world_transform.cpp
116 lines (83 loc) · 2.93 KB
/
ogldev_world_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
/*
Copyright 2021 Etay Meiri
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ogldev_world_transform.h"
void WorldTrans::SetScale(float scale)
{
m_scale = scale;
}
void WorldTrans::SetRotation(float x, float y, float z)
{
m_rotation.x = x;
m_rotation.y = y;
m_rotation.z = z;
}
void WorldTrans::SetPosition(float x, float y, float z)
{
m_pos.x = x;
m_pos.y = y;
m_pos.z = z;
}
void WorldTrans::SetPosition(const Vector3f& WorldPos)
{
m_pos = WorldPos;
}
void WorldTrans::Rotate(float x, float y, float z)
{
m_rotation.x += x;
m_rotation.y += y;
m_rotation.z += z;
}
Matrix4f WorldTrans::GetMatrix() const
{
Matrix4f Scale;
Scale.InitScaleTransform(m_scale, m_scale, m_scale);
Matrix4f Rotation;
Rotation.InitRotateTransform(m_rotation.x, m_rotation.y, m_rotation.z);
Matrix4f Translation;
Translation.InitTranslationTransform(m_pos.x, m_pos.y, m_pos.z);
Matrix4f WorldTransformation = Translation * Rotation * Scale;
return WorldTransformation;
}
Matrix4f WorldTrans::GetReversedTranslationMatrix() const
{
Matrix4f ReversedTranslation;
ReversedTranslation.InitTranslationTransform(m_pos.Negate());
return ReversedTranslation;
}
Matrix4f WorldTrans::GetReversedRotationMatrix() const
{
Matrix4f ReversedRotation;
ReversedRotation.InitRotateTransformZYX(-m_rotation.x, -m_rotation.y, -m_rotation.z);
return ReversedRotation;
}
Vector3f WorldTrans::WorldPosToLocalPos(const Vector3f& WorldPos) const
{
Matrix4f WorldToLocalTranslation = GetReversedTranslationMatrix();
Matrix4f WorldToLocalRotation = GetReversedRotationMatrix();
Matrix4f WorldToLocalTransformation = WorldToLocalRotation * WorldToLocalTranslation;
Vector4f WorldPos4f = Vector4f(WorldPos, 1.0f);
Vector4f LocalPos4f = WorldToLocalTransformation * WorldPos4f;
Vector3f LocalPos3f(LocalPos4f);
return LocalPos3f;
}
Vector3f WorldTrans::WorldDirToLocalDir(const Vector3f& WorldDirection) const
{
Matrix3f World3f(GetMatrix()); // Initialize using the top left corner
// Inverse local-to-world transformation using transpose
// (assuming uniform scaling)
Matrix3f WorldToLocal = World3f.Transpose();
Vector3f LocalDirection = WorldToLocal * WorldDirection;
LocalDirection = LocalDirection.Normalize();
return LocalDirection;
}