#ifndef MATH_TFORM_H #define MATH_TFORM_H #include #include "math/vector.hpp" #include "math/mat4.hpp" #include "math/quat.hpp" namespace engine::math { class Transform { public: Vector3 loc; Quaternion rot; Vector3 scale; constexpr Transform(Vector3 loc, Quaternion rot, Vector3 scale) : loc{loc}, rot{rot}, scale{scale} {} constexpr Transform opposite() const & { return {-loc, rot.conjugate(), 1.f / scale}; } constexpr Matrix4 to_mat4() const & { return { scale.x * (2.f * (rot.w * rot.w + rot.x * rot.x) - 1.f), scale.x * (2.f * (rot.x * rot.y + rot.w * rot.z) ), scale.x * (2.f * (rot.x * rot.z - rot.w * rot.y) ), 0.f, scale.y * (2.f * (rot.x * rot.y - rot.w * rot.z) ), scale.y * (2.f * (rot.w * rot.w + rot.y * rot.y) - 1.f), scale.y * (2.f * (rot.y * rot.z + rot.w * rot.x) ), 0.f, scale.z * (2.f * (rot.x * rot.z + rot.w * rot.y) ), scale.z * (2.f * (rot.y * rot.z - rot.w * rot.x) ), scale.z * (2.f * (rot.w * rot.w + rot.z * rot.z) - 1.f), 0.f, loc.x, loc.y, loc.z, 1.f, }; } constexpr Matrix4 to_inverse_mat4() const & { std::array m{{ (2.f * (rot.w * rot.w + rot.x * rot.x) - 1.f) / scale.x, (2.f * (rot.x * rot.y - rot.w * rot.z) ) / scale.y, (2.f * (rot.x * rot.z + rot.w * rot.y) ) / scale.z, (2.f * (rot.x * rot.y + rot.w * rot.z) ) / scale.x, (2.f * (rot.w * rot.w + rot.y * rot.y) - 1.f) / scale.y, (2.f * (rot.y * rot.z - rot.w * rot.x) ) / scale.z, (2.f * (rot.x * rot.z - rot.w * rot.y) ) / scale.x, (2.f * (rot.y * rot.z + rot.w * rot.x) ) / scale.y, (2.f * (rot.w * rot.w + rot.z * rot.z) - 1.f) / scale.z, }}; return { m[0], m[1], m[2], 0.f, m[3], m[4], m[5], 0.f, m[6], m[7], m[8], 0.f, -loc.dot({ m[0], m[3], m[6] }), -loc.dot({ m[1], m[4], m[7] }), -loc.dot({ m[2], m[5], m[8] }), 1.f, }; } }; } #endif // MATH_TFORM_H