zeus/include/CTransform.hpp

215 lines
6.7 KiB
C++
Raw Normal View History

2015-04-19 20:39:16 +00:00
#ifndef CTRANSFORM_HPP
#define CTRANSFORM_HPP
#include "Global.hpp"
#include "CMatrix3f.hpp"
#include "CMatrix4f.hpp"
2015-04-19 20:39:16 +00:00
#include "CVector3f.hpp"
2015-08-25 22:04:15 +00:00
#include "CQuaternion.hpp"
2015-04-19 20:39:16 +00:00
namespace Zeus
{
2015-10-25 19:31:41 +00:00
class alignas(16) CTransform
2015-04-19 20:39:16 +00:00
{
public:
ZE_DECLARE_ALIGNED_ALLOCATOR();
2015-07-29 21:59:03 +00:00
2015-04-19 20:39:16 +00:00
CTransform() : m_basis(false) {}
CTransform(const CMatrix3f& basis, const CVector3f& offset=CVector3f::skZero) :
m_basis(basis), m_origin(offset) {}
2015-10-24 01:23:23 +00:00
#if ZE_ATHENA_TYPES
CTransform(const atVec4f* mtx)
: m_basis(mtx[0], mtx[1], mtx[2]), m_origin(mtx[0].vec[3], mtx[1].vec[3], mtx[2].vec[3]) {}
#endif
2015-07-29 21:59:03 +00:00
2016-02-08 05:09:40 +00:00
static inline CTransform Identity()
{
return CTransform(CMatrix3f::skIdentityMatrix3f);
}
2015-04-19 20:39:16 +00:00
inline CTransform operator*(const CTransform& rhs) const
{return CTransform(m_basis * rhs.m_basis, m_origin + (m_basis * rhs.m_origin));}
inline CTransform inverse() const
{
CMatrix3f inv = m_basis.inverted();
return CTransform(inv, inv * -m_origin);
}
2016-02-13 00:56:30 +00:00
static inline CTransform Translate(const CVector3f& position)
{
2016-02-13 00:56:30 +00:00
return {CMatrix3f::skIdentityMatrix3f, position};
}
2015-08-25 22:04:15 +00:00
2016-02-13 00:56:30 +00:00
static inline CTransform Translate(float x, float y, float z) { return Translate({x, y, z}); }
inline CTransform& operator+=(const CVector3f& other)
{
m_origin += other;
return *this;
}
inline void rotate(const CVector3f& euler) { *this = *this * CMatrix3f(CQuaternion(euler)); }
2015-08-25 22:04:15 +00:00
2016-02-13 00:56:30 +00:00
static inline CTransform RotateX(float theta)
{
float sinT = sinf(theta);
float cosT = cosf(theta);
return CTransform(CMatrix3f({1.f, 0.f, 0.f, 0.f},
{0.f, cosT, sinT, 0.f},
{0.f, -sinT, cosT, 0.f}));
}
static inline CTransform RotateY(float theta)
{
float sinT = sinf(theta);
float cosT = cosf(theta);
return CTransform(CMatrix3f({cosT, 0.f, -sinT, 0.f},
{0.f, 1.f, 0.f, 0.f},
{sinT, 0.f, cosT, 0.f}));
}
static inline CTransform RotateZ(float theta)
{
float sinT = sinf(theta);
float cosT = cosf(theta);
return CTransform(CMatrix3f({cosT, sinT, 0.f, 0.f},
{-sinT, cosT, 0.f, 0.f},
{0.f, 0.f, 1.f, 0.f}));
}
2016-02-11 02:35:37 +00:00
inline void rotateLocalX(float theta)
{
float sinT = sinf(theta);
float cosT = cosf(theta);
Zeus::CVector3f b2 = m_basis[2] * sinT;
Zeus::CVector3f b1 = m_basis[1] * sinT;
Zeus::CVector3f cosV(cosT);
m_basis[1] *= cosV;
m_basis[2] *= cosV;
m_basis[1] += b2;
m_basis[2] -= b1;
}
inline void rotateLocalY(float theta)
{
float sinT = sinf(theta);
float cosT = cosf(theta);
Zeus::CVector3f b0 = m_basis[0] * sinT;
Zeus::CVector3f b2 = m_basis[2] * sinT;
Zeus::CVector3f cosV(cosT);
m_basis[0] *= cosV;
m_basis[2] *= cosV;
m_basis[2] += b0;
m_basis[0] -= b2;
}
inline void rotateLocalZ(float theta)
{
float sinT = sinf(theta);
float cosT = cosf(theta);
Zeus::CVector3f b0 = m_basis[0] * sinT;
Zeus::CVector3f b1 = m_basis[1] * sinT;
Zeus::CVector3f cosV(cosT);
m_basis[0] *= cosV;
m_basis[1] *= cosV;
m_basis[0] += b1;
m_basis[1] -= b0;
}
2015-08-25 22:04:15 +00:00
inline void scaleBy(float factor)
{ CTransform xfrm(CMatrix3f(CVector3f(factor, factor, factor))); *this = *this * xfrm; }
2015-08-25 22:04:15 +00:00
2016-02-13 00:56:30 +00:00
static inline CTransform Scale(const CVector3f& factor)
2015-08-25 22:04:15 +00:00
{
2016-02-13 00:56:30 +00:00
return CTransform(CMatrix3f({factor.x, 0.f, 0.f, 0.f},
{0.f, factor.y, 0.f, 0.f},
{0.f, 0.f, factor.z, 0.f}));
2015-08-25 22:04:15 +00:00
}
2016-02-13 00:56:30 +00:00
static inline CTransform Scale(float x, float y, float z)
{
return CTransform(CMatrix3f({x, 0.f, 0.f, 0.f},
{0.f, y, 0.f, 0.f},
{0.f, 0.f, z, 0.f}));
}
2015-08-25 22:04:15 +00:00
2016-02-13 00:56:30 +00:00
static inline CTransform Scale(float factor)
{
return CTransform(CMatrix3f({factor, 0.f, 0.f, 0.f},
{0.f, factor, 0.f, 0.f},
{0.f, 0.f, factor, 0.f}));
}
2015-08-25 22:04:15 +00:00
inline void multiplyIgnoreTranslation(const CTransform& xfrm) { m_basis = m_basis*xfrm.m_basis; }
inline CTransform getRotation() { CTransform ret = *this; ret.m_origin.zeroOut(); return ret; }
void setRotation(const CMatrix3f& mat) { m_basis = mat; }
void setRotation(const CTransform& xfrm) { setRotation(xfrm.m_basis); }
2015-08-25 22:04:15 +00:00
/**
* @brief buildMatrix3f Returns the stored matrix
* buildMatrix3f is here for compliance with Retro's Math API
* @return The Matrix (Neo, you are the one)
*/
inline CMatrix3f buildMatrix3f() { return m_basis; }
2015-08-25 22:04:15 +00:00
inline CVector3f operator*(const CVector3f& other) const {return m_origin + m_basis * other;}
2015-04-19 20:39:16 +00:00
inline void toMatrix4f(CMatrix4f& mat) const
2015-04-19 20:39:16 +00:00
{
#if __SSE__
mat.vec[0].mVec128 = m_basis[0].mVec128; mat.m[0][3] = 0.0f;
mat.vec[1].mVec128 = m_basis[1].mVec128; mat.m[1][3] = 0.0f;
mat.vec[2].mVec128 = m_basis[2].mVec128; mat.m[2][3] = 0.0f;
mat.vec[3].mVec128 = m_origin.mVec128; mat.m[3][3] = 1.0f;
2015-04-19 20:39:16 +00:00
#else
mat.m[0][0] = m_basis[0][0]; mat.m[0][1] = m_basis[0][1]; mat.m[0][2] = m_basis[0][2]; mat.m[0][3] = 0.0f;
mat.m[1][0] = m_basis[1][0]; mat.m[1][1] = m_basis[1][1]; mat.m[1][2] = m_basis[1][2]; mat.m[1][3] = 0.0f;
mat.m[2][0] = m_basis[2][0]; mat.m[2][1] = m_basis[2][1]; mat.m[2][2] = m_basis[2][2]; mat.m[2][3] = 0.0f;
mat.m[3][0] = m_origin[0]; mat.m[3][1] = m_origin[1]; mat.m[3][2] = m_origin[2]; mat.m[3][3] = 1.0f;
#endif
}
2015-08-25 22:04:15 +00:00
static inline CTransform fromColumns(const CVector3f& m0, const CVector3f& m1, const CVector3f& m2, const CVector3f& m3)
{
CTransform ret;
ret.m_basis[0][0] = m0[0];
ret.m_basis[0][1] = m1[0];
ret.m_basis[0][2] = m2[0];
ret.m_origin[0] = m3[0];
ret.m_basis[1][0] = m0[1];
ret.m_basis[1][1] = m1[1];
ret.m_basis[1][2] = m2[1];
ret.m_origin[1] = m3[1];
ret.m_basis[2][0] = m0[2];
ret.m_basis[2][1] = m1[2];
ret.m_basis[2][2] = m2[2];
ret.m_origin[2] = m3[2];
return ret;
}
2015-04-19 20:39:16 +00:00
CMatrix3f m_basis;
CVector3f m_origin;
};
static inline CTransform CTransformFromScaleVector(const CVector3f& scale)
{
return CTransform(CMatrix3f(scale));
}
CTransform CTransformFromEditorEuler(const CVector3f& eulerVec);
2015-08-29 04:49:19 +00:00
CTransform CTransformFromEditorEulers(const CVector3f& eulerVec, const CVector3f& origin);
2015-04-19 20:39:16 +00:00
CTransform CTransformFromAxisAngle(const CVector3f& axis, float angle);
}
2015-04-19 20:39:16 +00:00
#endif // CTRANSFORM_HPP