2024-12-27 01:34:34 +00:00
|
|
|
// TransformComponent.cpp
|
|
|
|
#include "Transform.h"
|
2025-01-05 04:04:21 +00:00
|
|
|
#define GLM_ENABLE_EXPERIMENTAL
|
|
|
|
#include <glm/glm.hpp>
|
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
#include <glm/gtc/quaternion.hpp>
|
|
|
|
#include <glm/gtx/quaternion.hpp>
|
2024-12-27 01:34:34 +00:00
|
|
|
|
|
|
|
const std::string TransformComponent::name = "Transform";
|
|
|
|
|
|
|
|
TransformComponent::TransformComponent()
|
|
|
|
: position(0.0f), rotation(0.0f), scale(1.0f)
|
|
|
|
{
|
|
|
|
position = glm::vec3(0.0f, 0.0f, 0.0f);
|
|
|
|
rotation = glm::vec3(0.0f, 0.0f, 0.0f);
|
2025-01-05 04:04:21 +00:00
|
|
|
scale = glm::vec3(1.0f, 1.0f, 1.0f);
|
2024-12-27 01:34:34 +00:00
|
|
|
}
|
|
|
|
|
2025-01-05 04:04:21 +00:00
|
|
|
const std::string &TransformComponent::GetName() const
|
2024-12-27 01:34:34 +00:00
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2025-01-05 04:04:21 +00:00
|
|
|
const std::string &TransformComponent::GetStaticName()
|
2024-12-27 01:34:34 +00:00
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2024-12-30 04:25:16 +00:00
|
|
|
void TransformComponent::Update(float _deltaTime)
|
|
|
|
{
|
|
|
|
(void)_deltaTime; // Suppress unused parameter warning
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-01-05 04:04:21 +00:00
|
|
|
// New Methods
|
|
|
|
glm::mat4 TransformComponent::GetTransformMatrix() const
|
|
|
|
{
|
|
|
|
glm::mat4 translation = glm::translate(glm::mat4(1.0f), position);
|
|
|
|
|
|
|
|
// Convert Euler angles (in degrees) to a quaternion and then to a rotation matrix
|
|
|
|
glm::quat quatRotation = glm::quat(glm::radians(rotation));
|
|
|
|
glm::mat4 rotationMatrix = glm::toMat4(quatRotation);
|
|
|
|
|
|
|
|
glm::mat4 scaling = glm::scale(glm::mat4(1.0f), scale);
|
|
|
|
|
|
|
|
// Note: The order is Scale -> Rotate -> Translate
|
|
|
|
return translation * rotationMatrix * scaling;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformComponent::SetTransformMatrix(const glm::mat4 &transform)
|
|
|
|
{
|
|
|
|
// Extract translation
|
|
|
|
position = glm::vec3(transform[3]);
|
|
|
|
|
|
|
|
// Extract scale factors
|
|
|
|
scale.x = glm::length(glm::vec3(transform[0]));
|
|
|
|
scale.y = glm::length(glm::vec3(transform[1]));
|
|
|
|
scale.z = glm::length(glm::vec3(transform[2]));
|
|
|
|
|
|
|
|
// Remove scale from rotation matrix
|
|
|
|
glm::mat4 rotationMatrix = transform;
|
|
|
|
rotationMatrix[0] /= scale.x;
|
|
|
|
rotationMatrix[1] /= scale.y;
|
|
|
|
rotationMatrix[2] /= scale.z;
|
|
|
|
|
|
|
|
// Convert rotation matrix to quaternion and then to Euler angles (in degrees)
|
|
|
|
glm::quat quatRotation = glm::quat_cast(rotationMatrix);
|
|
|
|
glm::vec3 euler = glm::degrees(glm::eulerAngles(quatRotation));
|
|
|
|
rotation = euler;
|
|
|
|
}
|
|
|
|
|
2024-12-27 01:34:34 +00:00
|
|
|
YAML::Node TransformComponent::Serialize()
|
|
|
|
{
|
|
|
|
YAML::Node node;
|
|
|
|
|
|
|
|
// Position
|
|
|
|
{
|
|
|
|
YAML::Node posNode;
|
|
|
|
posNode.SetStyle(YAML::EmitterStyle::Flow);
|
|
|
|
posNode.push_back(position.x);
|
|
|
|
posNode.push_back(position.y);
|
|
|
|
posNode.push_back(position.z);
|
|
|
|
node["Position"] = posNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rotation
|
|
|
|
{
|
|
|
|
YAML::Node rotNode;
|
|
|
|
rotNode.SetStyle(YAML::EmitterStyle::Flow);
|
|
|
|
rotNode.push_back(rotation.x);
|
|
|
|
rotNode.push_back(rotation.y);
|
|
|
|
rotNode.push_back(rotation.z);
|
|
|
|
node["Rotation"] = rotNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scale
|
|
|
|
{
|
|
|
|
YAML::Node scaleNode;
|
|
|
|
scaleNode.SetStyle(YAML::EmitterStyle::Flow);
|
|
|
|
scaleNode.push_back(scale.x);
|
|
|
|
scaleNode.push_back(scale.y);
|
|
|
|
scaleNode.push_back(scale.z);
|
|
|
|
node["Scale"] = scaleNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2025-01-05 04:04:21 +00:00
|
|
|
void TransformComponent::Deserialize(const YAML::Node &node)
|
2024-12-27 01:34:34 +00:00
|
|
|
{
|
|
|
|
if (node["Position"])
|
|
|
|
{
|
|
|
|
auto pos = node["Position"].as<std::vector<float>>();
|
|
|
|
if (pos.size() == 3)
|
|
|
|
position = glm::vec3(pos[0], pos[1], pos[2]);
|
|
|
|
}
|
|
|
|
if (node["Rotation"])
|
|
|
|
{
|
|
|
|
auto rot = node["Rotation"].as<std::vector<float>>();
|
|
|
|
if (rot.size() == 3)
|
|
|
|
rotation = glm::vec3(rot[0], rot[1], rot[2]);
|
|
|
|
}
|
|
|
|
if (node["Scale"])
|
|
|
|
{
|
|
|
|
auto scl = node["Scale"].as<std::vector<float>>();
|
|
|
|
if (scl.size() == 3)
|
|
|
|
scale = glm::vec3(scl[0], scl[1], scl[2]);
|
|
|
|
}
|
|
|
|
}
|