Files
DNF_DEV/source/EngineFrame/Attribute/Y_Transform.cpp

62 lines
2.3 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "Y_Transform.h"
#include <cmath>
Y_Transform::Y_Transform() : position(0.f, 0.f), scale(1.0f, 1.0f), skew(0.f, 0.f), rotation(0.f)
{
}
Matrix3x2 Y_Transform::ToMatrix() const
{
// 将角度转换为弧度
float rotRad = glm::radians(rotation);
float skewXRad = glm::radians(skew.x);
float skewYRad = glm::radians(skew.y);
// 计算旋转的正弦和余弦
float cosRot = std::cos(rotRad);
float sinRot = std::sin(rotRad);
// 计算错切的正切值
float tanSkewX = std::tan(skewXRad);
float tanSkewY = std::tan(skewYRad);
// 构造基础变换矩阵(缩放 × 错切 × 旋转的组合)
// 注意:矩阵乘法顺序为 旋转 × 错切 × 缩放(从右向左应用)
float m00 = scale.x * (cosRot - sinRot * tanSkewY);
float m01 = scale.y * (-sinRot + cosRot * tanSkewX);
float m10 = scale.x * (sinRot + cosRot * tanSkewY);
float m11 = scale.y * (cosRot + sinRot * tanSkewX);
// 平移分量
float tx = position.x;
float ty = position.y;
// 构造并返回 3x2 矩阵(假设 Matrix3x2 可通过此方式初始化)
return Matrix3x2(m00, m01, m10, m11, tx, ty);
}
glm::mat4 Y_Transform::GetTransformMatrix() const
{
// 缩放矩阵Scale
glm::mat4 scaleMat = glm::mat4(1.0f); // 单位矩阵
scaleMat[0][0] = scale.x; // x轴缩放
scaleMat[1][1] = scale.y; // y轴缩放
// 错切矩阵Skew先将角度转为弧度
float skewX = glm::radians(skew.x); // 沿y轴错切角度x方向倾斜
float skewY = glm::radians(skew.y); // 沿x轴错切角度y方向倾斜
glm::mat4 skewMat = glm::mat4(1.0f);
skewMat[0][1] = tan(skewX); // x方向错切因子影响y轴
skewMat[1][0] = tan(skewY); // y方向错切因子影响x轴
// 旋转矩阵Rotation绕z轴旋转角度转弧度
float rotRad = glm::radians(rotation);
glm::mat4 rotMat = glm::rotate(glm::mat4(1.0f), rotRad, glm::vec3(0, 0, 1));
// 平移矩阵Translation
glm::mat4 transMat = glm::translate(glm::mat4(1.0f), glm::vec3(position, 0.0f));
// 组合变换矩阵:平移 × 旋转 × 错切 × 缩放(注意乘法顺序,从右向左应用)
return transMat * rotMat * skewMat * scaleMat;
}