cpp
#include <iostream>
#include <cmath>
// 简单的3D向量类
struct Vector3 {
float x, y, z;
Vector3(float x_=0.0f, float y_=0.0f, float z_=0.0f)
: x(x_), y(y_), z(z_) {}
// 向量加法
Vector3 operator+(const Vector3& other) const {
return Vector3(x + other.x, y + other.y, z + other.z);
}
// 向量相等比较
bool operator==(const Vector3& other) const {
return (std::fabs(x - other.x) < 1e-6f) &&
(std::fabs(y - other.y) < 1e-6f) &&
(std::fabs(z - other.z) < 1e-6f);
}
};
// 简单的四元数类
struct Quaternion {
float w, x, y, z;
Quaternion(float w_=1.0f, float x_=0.0f, float y_=0.0f, float z_=0.0f)
: w(w_), x(x_), y(y_), z(z_) {}
// 归一化四元数
void Normalize() {
float mag = std::sqrt(w*w + x*x + y*y + z*z);
if (mag > 0.0f) {
w /= mag;
x /= mag;
y /= mag;
z /= mag;
}
}
// 将四元数转换为旋转矩阵
struct Matrix4 toMatrix() const;
};
// 简单的4x4矩阵类
struct Matrix4 {
float m[4][4];
// 构造单位矩阵
Matrix4() {
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
m[i][j] = (i == j) ? 1.0f : 0.0f;
}
// 矩阵相乘
Matrix4 operator*(const Matrix4& other) const {
Matrix4 result;
for(int row=0; row<4; ++row){
for(int col=0; col<4; ++col){
result.m[row][col] = 0.0f;
for(int k=0; k<4; ++k){
result.m[row][col] += m[row][k] * other.m[k][col];
}
}
}
return result;
}
// 打印矩阵
void Print() const {
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) {
std::cout << m[i][j] << ' ';
}
std::cout << std::endl;
}
}
};
// 实现 Quaternion 转换为 Matrix4
Matrix4 Quaternion::toMatrix() const {
Matrix4 mat;
mat.m[0][0] = 1 - 2 * (y * y + z * z);
mat.m[0][1] = 2 * (x * y - z * w);
mat.m[0][2] = 2 * (x * z + y * w);
mat.m[0][3] = 0;
mat.m[1][0] = 2 * (x * y + z * w);
mat.m[1][1] = 1 - 2 * (x * x + z * z);
mat.m[1][2] = 2 * (y * z - x * w);
mat.m[1][3] = 0;
mat.m[2][0] = 2 * (x * z - y * w);
mat.m[2][1] = 2 * (y * z + x * w);
mat.m[2][2] = 1 - 2 * (x * x + y * y);
mat.m[2][3] = 0;
mat.m[3][0] = 0;
mat.m[3][1] = 0;
mat.m[3][2] = 0;
mat.m[3][3] = 1;
return mat;
}
// Transform 类
class Transform {
public:
Transform()
: m_Position(0.0f, 0.0f, 0.0f),
m_Rotation(1.0f, 0.0f, 0.0f, 0.0f),
m_Scale(1.0f, 1.0f, 1.0f),
m_IsDirty(true),
m_ModelMatrix()
{}
// 设置位置
void SetPosition(const Vector3& position) {
if (!(m_Position == position)) {
m_Position = position;
m_IsDirty = true;
}
}
// 设置旋转
void SetRotation(const Quaternion& rotation) {
Quaternion normalized = rotation;
normalized.Normalize();
if (!(m_Rotation.w == normalized.w && m_Rotation.x == normalized.x &&
m_Rotation.y == normalized.y && m_Rotation.z == normalized.z)) {
m_Rotation = normalized;
m_IsDirty = true;
}
}
// 设置缩放
void SetScale(const Vector3& scale) {
if (!(m_Scale == scale)) {
m_Scale = scale;
m_IsDirty = true;
}
}
// 获取模型矩阵
const Matrix4& GetModelMatrix() {
if (m_IsDirty) {
RecalculateModelMatrix();
m_IsDirty = false;
}
return m_ModelMatrix;
}
private:
Vector3 m_Position;
Quaternion m_Rotation;
Vector3 m_Scale;
bool m_IsDirty;
Matrix4 m_ModelMatrix;
// 重新计算模型矩阵
void RecalculateModelMatrix() {
// 计算缩放矩阵
Matrix4 scaleMat;
scaleMat.m[0][0] = m_Scale.x;
scaleMat.m[1][1] = m_Scale.y;
scaleMat.m[2][2] = m_Scale.z;
// 计算旋转矩阵
Matrix4 rotationMat = m_Rotation.toMatrix();
// 计算平移矩阵
Matrix4 translationMat;
translationMat.m[0][3] = m_Position.x;
translationMat.m[1][3] = m_Position.y;
translationMat.m[2][3] = m_Position.z;
// 组合矩阵:模型矩阵 = 平移 * 旋转 * 缩放
m_ModelMatrix = translationMat * rotationMat * scaleMat;
}
};
// 测试 Transform 类
int main() {
Transform transform;
// 初始模型矩阵
std::cout << "Initial Model Matrix:" << std::endl;
transform.GetModelMatrix().Print();
std::cout << std::endl;
// 设置位置
transform.SetPosition(Vector3(5.0f, 0.0f, 0.0f));
std::cout << "After Setting Position to (5,0,0):" << std::endl;
transform.GetModelMatrix().Print();
std::cout << std::endl;
// 设置旋转(绕Y轴旋转90度)
float angleRad = 90.0f * (3.14159265358979323846f / 180.0f);
float halfAngle = angleRad / 2.0f;
Quaternion rotation(std::cos(halfAngle), 0.0f, std::sin(halfAngle), 0.0f);
transform.SetRotation(rotation);
std::cout << "After Setting Rotation (90 degrees around Y-axis):" << std::endl;
transform.GetModelMatrix().Print();
std::cout << std::endl;
// 设置缩放
transform.SetScale(Vector3(2.0f, 2.0f, 2.0f));
std::cout << "After Setting Scale to (2,2,2):" << std::endl;
transform.GetModelMatrix().Print();
std::cout << std::endl;
// 调用 GetModelMatrix 再次查看缓存是否生效
std::cout << "Calling GetModelMatrix again (should use cached matrix):" << std::endl;
transform.GetModelMatrix().Print();
std::cout << std::endl;
return 0;
}