-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathComponentTransform.cpp
More file actions
331 lines (268 loc) · 8 KB
/
ComponentTransform.cpp
File metadata and controls
331 lines (268 loc) · 8 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include "Application.h"
#include "ModuleInput.h"
#include "ModuleCamera3D.h"
#include "ModuleEditor.h"
#include "GameObject.h"
#include "ComponentTransform.h"
#include "ComponentCamera.h"
#include "ComponentMesh.h"
#include "Globals.h"
#include "Data.h"
#include "imgui\imgui.h"
#include "ImGuizmo\ImGuizmo.h"
#include "Brofiler\include\Brofiler.h"
ComponentTransform::ComponentTransform(ComponentType type, GameObject* game_object, math::float4x4** global_matrix) : Component(type, game_object)
{
CalculateFinalTransform();
*global_matrix = &final_transform_matrix;
}
ComponentTransform::~ComponentTransform()
{
}
void ComponentTransform::Update()
{
BROFILER_CATEGORY("ComponentTransform::Update", Profiler::Color::Tomato)
UpdateMatrix();
}
void ComponentTransform::OnInspector(bool debug)
{
string str = (string("Transform") + string("##") + std::to_string(uuid));
if (ImGui::CollapsingHeader(str.c_str(), ImGuiTreeNodeFlags_DefaultOpen))
{
if (ImGui::IsItemClicked(1))
{
ImGui::OpenPopup("reset##transform");
}
if (ImGui::BeginPopup("reset##transform"))
{
if (ImGui::MenuItem("Reset"))
{
Reset();
}
ImGui::EndPopup();
}
ImVec4 white = ImVec4(1, 1, 1, 1);
//CHANGE - PEP
//---------Transform operation
ImGuizmo::BeginFrame();
//Inspector selection
if (ImGui::RadioButton("Translate", App->editor->gizmo_operation == TRANSLATE))
{
App->editor->gizmo_operation = ImGuizmo::OPERATION::TRANSLATE;
}
ImGui::SameLine();
if (ImGui::RadioButton("Rotate", App->editor->gizmo_operation == ROTATION))
{
App->editor->gizmo_operation = ImGuizmo::OPERATION::ROTATE;
}
ImGui::SameLine();
if (ImGui::RadioButton("Scale", App->editor->gizmo_operation == SCALE))
{
App->editor->gizmo_operation = ImGuizmo::OPERATION::SCALE;
}
ImGui::Checkbox("Enable Gizmo", &App->editor->gizmo_enabled);
//Position
ImGui::TextColored(white, "Position: ");
ImGui::SameLine();
float3 position = this->position;
if (ImGui::DragFloat3("##pos", position.ptr()))
{
App->input->InfiniteHorizontal();
SetPosition(position);
}
//Rotation
ImGui::TextColored(white, "Rotation: ");
ImGui::SameLine();
float3 rotation = this->rotation_euler;
if (ImGui::DragFloat3("##rot", rotation_euler.ptr(), 1.0f, -360.0f, 360.0f))
{
App->input->InfiniteHorizontal();
SetRotation(rotation_euler);
}
//Scale
ImGui::TextColored(white, "Scale: ");
ImGui::SameLine();
float3 scale = this->scale;
if (ImGui::DragFloat3("##scale", scale.ptr()))
{
App->input->InfiniteHorizontal();
SetScale(scale);
}
if (ImGui::RadioButton("World", App->editor->gizmo_mode == ImGuizmo::MODE::WORLD))
{
App->editor->gizmo_mode = ImGuizmo::MODE::WORLD;
}
ImGui::SameLine();
if (ImGui::RadioButton("Local", App->editor->gizmo_mode == ImGuizmo::MODE::LOCAL))
{
App->editor->gizmo_mode = ImGuizmo::MODE::LOCAL;
}
//Local Matrix
if (debug == true)
{
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(255, 255, 0, 255));
ImGui::Text("%0.2f %0.2f %0.2f %0.2f", transform_matrix.v[0][0], transform_matrix.v[0][1], transform_matrix.v[0][2], transform_matrix.v[0][3]);
ImGui::Text("%0.2f %0.2f %0.2f %0.2f", transform_matrix.v[1][0], transform_matrix.v[1][1], transform_matrix.v[1][2], transform_matrix.v[1][3]);
ImGui::Text("%0.2f %0.2f %0.2f %0.2f", transform_matrix.v[2][0], transform_matrix.v[2][1], transform_matrix.v[2][2], transform_matrix.v[2][3]);
ImGui::Text("%0.2f %0.2f %0.2f %0.2f", transform_matrix.v[3][0], transform_matrix.v[3][1], transform_matrix.v[3][2], transform_matrix.v[3][3]);
ImGui::PopStyleColor();
}
}
}
void ComponentTransform::SetPosition(const math::float3& pos)
{
position = pos;
transform_modified = true;
}
void ComponentTransform::SetRotation(const math::float3& rot_euler)
{
rotation_euler = rot_euler;
float3 rot_deg = DegToRad(rot_euler);
rotation = rotation.FromEulerXYZ(rot_deg.x, rot_deg.y, rot_deg.z);
transform_modified = true;
}
void ComponentTransform::SetRotation(const math::Quat& rot)
{
rotation = rot;
rotation_euler = RadToDeg(rotation.ToEulerXYZ());
transform_modified = true;
}
void ComponentTransform::SetScale(const math::float3& scale)
{
this->scale = scale;
transform_modified = true;
}
void ComponentTransform::Set(math::float4x4 matrix)
{
transform_modified = true;
float3 pos, scal;
Quat rot;
matrix.Decompose(pos, rot, scal);
SetPosition(pos);
SetRotation(rot);
SetScale(scal);
}
void ComponentTransform::SetGlobal(float4x4 global)
{
GameObject* parent = game_object->GetParent();
if (parent != nullptr)
{
float4x4 new_local = ((ComponentTransform*)parent->GetComponent(C_TRANSFORM))->GetGlobalMatrix().Inverted() * global;
float3 translate, scale;
Quat rotation;
new_local.Decompose(translate, rotation, scale);
SetPosition(translate);
SetRotation(rotation);
SetScale(scale);
}
}
math::float3 ComponentTransform::GetPosition() const
{
return position;
}
math::float3 ComponentTransform::GetRotationEuler() const
{
return rotation_euler;
}
math::Quat ComponentTransform::GetRotation() const
{
return rotation;
}
math::float3 ComponentTransform::GetScale()const
{
return scale;
}
math::float4x4 ComponentTransform::GetLocalTransformMatrix() const
{
return transform_matrix;
}
math::float4x4 ComponentTransform::GetTransformMatrix()const
{
return final_transform_matrix.Transposed();
}
math::float4x4 ComponentTransform::GetGlobalMatrix()const
{
return final_transform_matrix;
}
math::float3 ComponentTransform::GetForward() const
{
return final_transform_matrix.WorldZ();
}
void ComponentTransform::Save(Data & file) const
{
Data data;
data.AppendInt("type", type);
data.AppendUInt("UUID", uuid);
data.AppendBool("active", active);
data.AppendMatrix("matrix", transform_matrix);
file.AppendArrayValue(data);
}
void ComponentTransform::Load(Data & conf)
{
uuid = conf.GetUInt("UUID");
active = conf.GetBool("active");
transform_matrix = conf.GetMatrix("matrix");
position = transform_matrix.TranslatePart();
rotation_euler = transform_matrix.ToEulerXYZ(); //In radians for now.
rotation = Quat::FromEulerXYZ(rotation_euler.x, rotation_euler.y, rotation_euler.z);
rotation_euler = RadToDeg(rotation_euler); //To degrees
scale = transform_matrix.GetScale();
CalculateFinalTransform();
}
void ComponentTransform::Reset()
{
SetPosition(float3::zero);
SetRotation(Quat::identity);
SetScale(float3::one);
}
void ComponentTransform::Remove()
{
LOG("[WARNING] Component Transform from gameobject %s can't be removed.", GetGameObject()->name);
App->editor->DisplayWarning(WarningType::W_WARNING, "Component Transform from gameobject %s can't be removed.", GetGameObject()->name);
}
void ComponentTransform::SaveAsPrefab(Data & file) const
{
Data data;
data.AppendFloat3("position", position.ptr());
data.AppendFloat3("rotation", rotation_euler.ptr()); //Euler rotation
file.AppendArrayValue(data);
}
void ComponentTransform::UpdateMatrix()
{
if (transform_modified)
{
transform_matrix = transform_matrix.FromTRS(position, rotation, scale);
CalculateFinalTransform();
transform_modified = false;
}
}
void ComponentTransform::CalculateFinalTransform()
{
BROFILER_CATEGORY("ComponentTransform::CalculateFinalTransform", Profiler::Color::Chocolate)
GameObject* game_object = GetGameObject();
if (game_object)
{
if (game_object->GetParent())
{
final_transform_matrix = game_object->GetParent()->transform->final_transform_matrix * transform_matrix;
std::vector<GameObject*>::const_iterator go_childs = game_object->GetChilds()->begin();
for (go_childs; go_childs != game_object->GetChilds()->end(); ++go_childs)
(*go_childs)->transform->CalculateFinalTransform();
}
else
{
final_transform_matrix = transform_matrix;
}
game_object->TransformModified();
}
else
{
LOG("[WARNING] Component created but not attached to any gameobject");
App->editor->DisplayWarning(WarningType::W_WARNING, "Component created but not attached to any gameobject");
}
}
void ComponentTransform::Rotate(const math::Quat & quaternion)
{
rotation = quaternion * rotation;
transform_modified = true;
}