-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponents.cpp
More file actions
185 lines (154 loc) · 5.01 KB
/
Components.cpp
File metadata and controls
185 lines (154 loc) · 5.01 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
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
#include "pch.h"
#include "Components.h"
using namespace DirectX;
using namespace DirectX::SimpleMath;
void PrimitiveRenderer::RenderStart()
{
m_model = GeometricPrimitive::CreateTeapot(GameContext::Get<DX::DeviceResources>().GetD3DDeviceContext());
}
void PrimitiveRenderer::Render(Camera& camera)
{
if (m_model)
m_model->Draw(gameObject.GetComponent<Transform>().GetMatrix(), camera.view, camera.projection);
}
void MoveUpdater::Start()
{
vel += Vector3::Right * .1f;
}
void MoveUpdater::Update()
{
gameObject.GetComponent<Transform>().position += vel;
}
void MoveDownUpdater::Update()
{
gameObject.GetComponent<Transform>().position.y -= .1f;
}
namespace
{
DirectX::SimpleMath::Quaternion ToQuaternion(const DirectX::SimpleMath::Vector3& euler) // yaw (Z), pitch (Y), roll (X)
{
// Abbreviations for the various angular functions
float cy = std::cos(euler.z * 0.5f);
float sy = std::sin(euler.z * 0.5f);
float cp = std::cos(euler.y * 0.5f);
float sp = std::sin(euler.y * 0.5f);
float cr = std::cos(euler.x * 0.5f);
float sr = std::sin(euler.x * 0.5f);
DirectX::SimpleMath::Quaternion q;
q.w = cy * cp * cr + sy * sp * sr;
q.x = cy * cp * sr - sy * sp * cr;
q.y = sy * cp * sr + cy * sp * cr;
q.z = sy * cp * cr - cy * sp * sr;
return q;
}
DirectX::SimpleMath::Vector3 ToEulerAngles(const DirectX::SimpleMath::Quaternion& q) {
DirectX::SimpleMath::Vector3 angles;
// roll (x-axis rotation)
float sinr_cosp = 2 * (q.w * q.x + q.y * q.z);
float cosr_cosp = 1 - 2 * (q.x * q.x + q.y * q.y);
angles.x = std::atan2(sinr_cosp, cosr_cosp);
// pitch (y-axis rotation)
float sinp = 2 * (q.w * q.y - q.z * q.x);
if (std::abs(sinp) >= 1)
angles.y = std::copysign(DirectX::XM_PI / 2, sinp); // use 90 degrees if out of range
else
angles.y = std::asin(sinp);
// yaw (z-axis rotation)
float siny_cosp = 2 * (q.w * q.z + q.x * q.y);
float cosy_cosp = 1 - 2 * (q.y * q.y + q.z * q.z);
angles.z = std::atan2(siny_cosp, cosy_cosp);
return angles;
}
}
DirectX::SimpleMath::Matrix Transform::GetLocalMatrix() const
{
return Matrix::CreateScale(scale)
* Matrix::CreateFromQuaternion(rotation)
* Matrix::CreateTranslation(position);
}
DirectX::SimpleMath::Matrix Transform::GetMatrix() const
{
return GameContext::Get<TransformResolver>().Resolve(gameObject);
}
void Transform::EditorGui()
{
auto& t = *this;
auto& reg = *gameObject.registry;
std::string tmpname = t.name;
tmpname.resize(16);
ImGui::InputText("Name##Transform", &tmpname[0], tmpname.size());
t.name = std::string(tmpname.c_str());
{
auto& e = t.parent;
int iid = (e == entt::null) ? -1 : int(reg.entity(e));
ImGui::InputInt("Parent##Transform", &iid);
if (iid < 0)
e = entt::null;
else
{
auto id = entt::entity(iid);
e = id < reg.size() ? (id | reg.current(id) << entt::entt_traits<entt::entity>::entity_shift) : id;
}
if (ImGui::BeginDragDropTarget())
{
if (const ImGuiPayload * payload = ImGui::AcceptDragDropPayload("DND_Hierarchy"))
e = *(static_cast<const entt::entity*>(payload->Data));
ImGui::EndDragDropTarget();
}
}
// the "##Transform" ensures that you can use the name "x" in multiple lables
ImGui::DragFloat3("Position##Transform", &t.position.x, 0.1f);
{
auto euler = ToEulerAngles(DirectX::SimpleMath::Quaternion(t.rotation.x, t.rotation.y, t.rotation.z, t.rotation.w)) * (180.f / DirectX::XM_PI);
float rot[] = { euler.x, euler.y, euler.z };
// the "##Transform" ensures that you can use the name "x" in multiple lables
ImGui::DragFloat3("Rotation##Transform", &rot[0], 0.1f);
auto quat = ToQuaternion(DirectX::SimpleMath::Vector3(rot[0], rot[1], rot[2]) * (DirectX::XM_PI / 180.f));
t.rotation.x = quat.x;
t.rotation.y = quat.y;
t.rotation.z = quat.z;
t.rotation.w = quat.w;
}
// the "##Transform" ensures that you can use the name "x" in multiple lables
ImGui::DragFloat3("Scale##Transform", &t.scale.x, 0.1f);
}
void TransformResolver::ClearCache()
{
matrixMap.reset();
}
DirectX::SimpleMath::Matrix TransformResolver::Resolve(const GameObject& gameObject)
{
if (matrixMap.has(gameObject.entity))
return matrixMap.get(gameObject.entity);
if (gameObject.HasComponent<Transform>())
{
auto& transform = gameObject.GetComponent<Transform>();
auto matrix = transform.GetLocalMatrix();
if (gameObject.registry->valid(transform.parent))
matrix *= Resolve(gameObject.Wrap(transform.parent));
matrixMap.construct(gameObject.entity, matrix);
return matrix;
}
else
{
auto matrix = DirectX::SimpleMath::Matrix::Identity;
matrixMap.construct(gameObject.entity, matrix);
return matrix;
}
}
void CameraComponent::Render(Camera& camera)
{
cameraptr = &camera;
}
void CameraComponent::Update()
{
if (cameraptr)
{
Vector3 s, t;
Quaternion r;
gameObject.GetComponent<Transform>().GetMatrix().Decompose(s, r, t);
cameraptr->view = Matrix::CreateScale(Vector3::One / s) * DirectX::SimpleMath::Matrix::CreateLookAt(t,
t + Vector3::Transform(Vector3::Forward, Matrix::CreateFromQuaternion(r)),
Vector3::Transform(Vector3::Up, Matrix::CreateFromQuaternion(r)));
}
}