removed Transform class. improved TransformBase/VersionData
This commit is contained in:
parent
cddc558153
commit
529ad6b403
@ -17,6 +17,7 @@ namespace hgl
|
||||
public:
|
||||
|
||||
TransformBase():VersionData(Identity4f){}
|
||||
TransformBase(const TransformBase *tb):VersionData(tb){}
|
||||
virtual ~TransformBase()=default;
|
||||
|
||||
virtual constexpr const size_t GetTypeHash()const=0; ///<取得类型哈希值
|
||||
@ -25,6 +26,8 @@ namespace hgl
|
||||
{
|
||||
return GetNewestVersionData(mat);
|
||||
}
|
||||
|
||||
virtual TransformBase *CreateSelfCopy()const=0; ///<创建一个自身的复制
|
||||
};//class TransformBase
|
||||
|
||||
class TransformMatrix :public TransformBase
|
||||
@ -44,16 +47,26 @@ namespace hgl
|
||||
|
||||
public:
|
||||
|
||||
TransformMatrix() :TransformBase()
|
||||
TransformMatrix():TransformBase()
|
||||
{
|
||||
transform_matrix=Identity4f;
|
||||
}
|
||||
|
||||
TransformMatrix(const Matrix4f &mat) :TransformBase()
|
||||
TransformMatrix(const Matrix4f &mat):TransformBase()
|
||||
{
|
||||
transform_matrix=mat;
|
||||
}
|
||||
|
||||
TransformMatrix(const TransformMatrix *tm):TransformBase(tm)
|
||||
{
|
||||
transform_matrix=tm->transform_matrix;
|
||||
}
|
||||
|
||||
TransformBase *CreateSelfCopy()const
|
||||
{
|
||||
return(new TransformMatrix(this));
|
||||
}
|
||||
|
||||
const Matrix4f &GetTransformMatrix()const { return transform_matrix; }
|
||||
|
||||
void SetTransformMatrix(const Matrix4f &mat)
|
||||
@ -88,11 +101,21 @@ namespace hgl
|
||||
offset=ZeroVector3f;
|
||||
}
|
||||
|
||||
TransformTranslate3f(const Vector3f &o) :TransformBase()
|
||||
TransformTranslate3f(const Vector3f &o):TransformBase()
|
||||
{
|
||||
offset=o;
|
||||
}
|
||||
|
||||
TransformTranslate3f(const TransformTranslate3f *tt):TransformBase(tt)
|
||||
{
|
||||
offset=tt->offset;
|
||||
}
|
||||
|
||||
TransformBase *CreateSelfCopy()const
|
||||
{
|
||||
return(new TransformTranslate3f(this));
|
||||
}
|
||||
|
||||
const Vector3f &GetOffset()const { return offset; }
|
||||
void SetOffset(const Vector3f &o)
|
||||
{
|
||||
@ -132,16 +155,26 @@ namespace hgl
|
||||
|
||||
public:
|
||||
|
||||
TransformRotateQuat() :TransformBase()
|
||||
TransformRotateQuat():TransformBase()
|
||||
{
|
||||
quat=IdentityQuatf;
|
||||
}
|
||||
|
||||
TransformRotateQuat(const Quatf &q) :TransformBase()
|
||||
TransformRotateQuat(const Quatf &q):TransformBase()
|
||||
{
|
||||
quat=q;
|
||||
}
|
||||
|
||||
TransformRotateQuat(const TransformRotateQuat *trq):TransformBase(trq)
|
||||
{
|
||||
quat=trq->quat;
|
||||
}
|
||||
|
||||
TransformBase *CreateSelfCopy()const
|
||||
{
|
||||
return(new TransformRotateQuat(quat));
|
||||
}
|
||||
|
||||
const Quatf &GetQuat()const { return quat; }
|
||||
void SetQuat(const Quatf &q)
|
||||
{
|
||||
@ -177,12 +210,23 @@ namespace hgl
|
||||
angle=0;
|
||||
}
|
||||
|
||||
TransformRotateAxis(const Vector3f &a,float ang) :TransformBase()
|
||||
TransformRotateAxis(const Vector3f &a,float ang):TransformBase()
|
||||
{
|
||||
axis=a;
|
||||
angle=ang;
|
||||
}
|
||||
|
||||
TransformRotateAxis(const TransformRotateAxis *tra):TransformBase(tra)
|
||||
{
|
||||
axis=tra->axis;
|
||||
angle=tra->angle;
|
||||
}
|
||||
|
||||
TransformBase *CreateSelfCopy()const
|
||||
{
|
||||
return(new TransformRotateAxis(axis,angle));
|
||||
}
|
||||
|
||||
const Vector3f &GetAxis()const { return axis; }
|
||||
const float GetAngle()const { return angle; }
|
||||
|
||||
@ -232,16 +276,26 @@ namespace hgl
|
||||
|
||||
public:
|
||||
|
||||
TransformRotateEuler() :TransformBase()
|
||||
TransformRotateEuler():TransformBase()
|
||||
{
|
||||
euler=ZeroVector3f;
|
||||
}
|
||||
|
||||
TransformRotateEuler(const Vector3f &e) :TransformBase()
|
||||
TransformRotateEuler(const Vector3f &e):TransformBase()
|
||||
{
|
||||
euler=e;
|
||||
}
|
||||
|
||||
TransformRotateEuler(const TransformRotateEuler *tre):TransformBase(tre)
|
||||
{
|
||||
euler=tre->euler;
|
||||
}
|
||||
|
||||
TransformBase *CreateSelfCopy()const
|
||||
{
|
||||
return(new TransformRotateEuler(euler));
|
||||
}
|
||||
|
||||
const Vector3f &GetEuler()const { return euler; }
|
||||
|
||||
const float GetPitch()const { return euler.x; }
|
||||
@ -271,16 +325,26 @@ namespace hgl
|
||||
|
||||
public:
|
||||
|
||||
TransformScale3f() :TransformBase()
|
||||
TransformScale3f():TransformBase()
|
||||
{
|
||||
scale3f=Vector3f(1,1,1);
|
||||
}
|
||||
|
||||
TransformScale3f(const Vector3f &s) :TransformBase()
|
||||
TransformScale3f(const Vector3f &s):TransformBase()
|
||||
{
|
||||
scale3f=s;
|
||||
}
|
||||
|
||||
TransformScale3f(const TransformScale3f *ts):TransformBase(ts)
|
||||
{
|
||||
scale3f=ts->scale3f;
|
||||
}
|
||||
|
||||
TransformBase *CreateSelfCopy()const
|
||||
{
|
||||
return(new TransformScale3f(scale3f));
|
||||
}
|
||||
|
||||
const Vector3f &GetScale()const { return scale3f; }
|
||||
void SetScale(const Vector3f &s)
|
||||
{
|
||||
@ -309,6 +373,34 @@ namespace hgl
|
||||
|
||||
virtual constexpr const size_t GetTypeHash()const override { return hgl::GetTypeHash<TransformLookAt>(); }
|
||||
|
||||
public:
|
||||
|
||||
TransformLookAt():TransformBase()
|
||||
{
|
||||
eye =OneVector3f;
|
||||
center =ZeroVector3f;
|
||||
up =AxisVector::Z;
|
||||
}
|
||||
|
||||
TransformLookAt(const Vector3f &e,const Vector3f &c,const Vector3f &u=AxisVector::Z)
|
||||
{
|
||||
eye =e;
|
||||
center =c;
|
||||
up =u;
|
||||
}
|
||||
|
||||
TransformLookAt(const TransformLookAt *tla):TransformBase(tla)
|
||||
{
|
||||
eye =tla->eye;
|
||||
center =tla->center;
|
||||
up =tla->up;
|
||||
}
|
||||
|
||||
TransformBase *CreateSelfCopy()const
|
||||
{
|
||||
return(new TransformLookAt(eye,center,up));
|
||||
}
|
||||
|
||||
const Vector3f &GetEye()const { return eye; }
|
||||
const Vector3f &GetCenter()const { return center; }
|
||||
const Vector3f &GetUp()const { return up; }
|
||||
@ -384,6 +476,30 @@ namespace hgl
|
||||
TransformManager()=default;
|
||||
virtual ~TransformManager()=default;
|
||||
|
||||
TransformManager(const TransformManager *tm):TransformBase(tm)
|
||||
{
|
||||
for(TransformBase *tb:tm->transform_list)
|
||||
AddTransform(tb->CreateSelfCopy());
|
||||
}
|
||||
|
||||
TransformBase *CreateSelfCopy()const
|
||||
{
|
||||
TransformManager *tm=new TransformManager;
|
||||
|
||||
for(TransformBase *tb:transform_list)
|
||||
tm->AddTransform(tb->CreateSelfCopy());
|
||||
|
||||
return tm;
|
||||
}
|
||||
|
||||
void operator = (const TransformManager &tm)
|
||||
{
|
||||
Clear();
|
||||
|
||||
for(TransformBase *tb:tm.transform_list)
|
||||
AddTransform(tb->CreateSelfCopy());
|
||||
}
|
||||
|
||||
void Clear()
|
||||
{
|
||||
transform_list.Clear();
|
||||
@ -477,302 +593,4 @@ namespace hgl
|
||||
UpdateVersion();
|
||||
}
|
||||
};//class TransformManager
|
||||
|
||||
/**
|
||||
* 变换矩阵<Br>
|
||||
* 便于分散管理平移、旋转、缩放等数值
|
||||
*/
|
||||
class Transform
|
||||
{
|
||||
uint32 version; ///<版本号(注:用于记录任何变化,而不是matrix)
|
||||
|
||||
void UpdateVersion()
|
||||
{
|
||||
if(version>=0x70000000)
|
||||
version=0;
|
||||
else
|
||||
++version;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
bool is_identity;
|
||||
bool is_zero_rotate;
|
||||
bool matrix_dirty;
|
||||
|
||||
//matrix其实用的少,所以在不取matrix时,并不会计算。
|
||||
Matrix4f matrix;
|
||||
Matrix4f inverse_matrix;
|
||||
Matrix4f transpose_inverse_matrix;
|
||||
|
||||
Vector3f translation_vector;
|
||||
Quatf rotation_quat;
|
||||
Vector3f rotation_axis;
|
||||
float rotate_angle;
|
||||
|
||||
Vector3f scale_vector;
|
||||
|
||||
protected:
|
||||
|
||||
void UpdateQuat();
|
||||
|
||||
public:
|
||||
|
||||
void UpdateMatrix();
|
||||
|
||||
const uint32 GetVersion (){UpdateMatrix();return version;}
|
||||
const bool IsIdentity (){UpdateMatrix();return is_identity;}
|
||||
const Matrix4f &GetMatrix (){UpdateMatrix();return matrix;}
|
||||
|
||||
const Matrix4f GetMatrix()const //不能执行UpdateMatrix时的获取
|
||||
{
|
||||
if(matrix_dirty)
|
||||
return translate(translation_vector)*ToMatrix(rotation_quat)*scale(scale_vector);
|
||||
else
|
||||
return matrix;
|
||||
}
|
||||
|
||||
operator const Matrix4f & (){return GetMatrix();}
|
||||
operator const Transform & (){UpdateMatrix();return *this;}
|
||||
|
||||
const Matrix4f &GetInverseMatrix()
|
||||
{
|
||||
UpdateMatrix();
|
||||
return inverse_matrix;
|
||||
}
|
||||
|
||||
|
||||
const Vector3f &GetTranslation ()const{return translation_vector;}
|
||||
const Vector3f &GetScale ()const{return scale_vector;}
|
||||
|
||||
const Quatf & GetRotationQuat ()const{return rotation_quat;}
|
||||
const Vector3f &GetRotationAxis ()const{return rotation_axis;}
|
||||
const float GetRotateAngle ()const{return rotate_angle;}
|
||||
|
||||
void SetTranslation(const float x,const float y,const float z)
|
||||
{
|
||||
if(is_identity)
|
||||
{
|
||||
if(IsNearlyZero(x)
|
||||
&&IsNearlyZero(y)
|
||||
&&IsNearlyZero(z))
|
||||
return;
|
||||
}
|
||||
|
||||
is_identity=false;
|
||||
|
||||
translation_vector.x=x;
|
||||
translation_vector.y=y;
|
||||
translation_vector.z=z;
|
||||
matrix_dirty=true;
|
||||
}
|
||||
|
||||
void SetTranslation(const Vector3f &v)
|
||||
{
|
||||
if(is_identity)
|
||||
{
|
||||
if(IsNearlyZero(v.x)
|
||||
&&IsNearlyZero(v.y)
|
||||
&&IsNearlyZero(v.z))
|
||||
return;
|
||||
}
|
||||
|
||||
is_identity=false;
|
||||
|
||||
translation_vector=v;
|
||||
matrix_dirty=true;
|
||||
}
|
||||
|
||||
void ClearRotation()
|
||||
{
|
||||
rotation_quat=IdentityQuatf;
|
||||
rotation_axis=ZeroVector3f;
|
||||
rotate_angle=0;
|
||||
matrix_dirty=true;
|
||||
}
|
||||
|
||||
void SetRotation(const Quatf &q)
|
||||
{
|
||||
if(is_identity)
|
||||
{
|
||||
if(q==IdentityQuatf)
|
||||
return;
|
||||
}
|
||||
|
||||
is_identity=false;
|
||||
|
||||
rotation_quat=q;
|
||||
ExtractedQuat(q,rotation_axis,rotate_angle);
|
||||
matrix_dirty=true;
|
||||
}
|
||||
|
||||
void SetRotation(const Vector3f &axis,const float angle)
|
||||
{
|
||||
if(is_identity)
|
||||
{
|
||||
if(IsNearlyZero(angle)==0)
|
||||
return;
|
||||
}
|
||||
|
||||
is_identity=false;
|
||||
|
||||
rotation_axis=axis;
|
||||
rotate_angle=angle;
|
||||
UpdateQuat();
|
||||
}
|
||||
|
||||
void SetRotation(const AXIS &axis,const float angle)
|
||||
{
|
||||
if(is_identity)
|
||||
{
|
||||
if(IsNearlyZero(angle)==0)
|
||||
return;
|
||||
}
|
||||
|
||||
is_identity=false;
|
||||
|
||||
rotation_axis=GetAxisVector(axis);
|
||||
rotate_angle=angle;
|
||||
UpdateQuat();
|
||||
}
|
||||
|
||||
void SetRotateAngle(float angle)
|
||||
{
|
||||
if(is_identity)
|
||||
{
|
||||
if(IsNearlyZero(angle)==0)
|
||||
return;
|
||||
}
|
||||
|
||||
is_identity=false;
|
||||
|
||||
rotate_angle=angle;
|
||||
UpdateQuat();
|
||||
}
|
||||
|
||||
void SetScale(const float &v)
|
||||
{
|
||||
if(is_identity)
|
||||
{
|
||||
if(IsNearlyEqual(v,1.0f))
|
||||
return;
|
||||
}
|
||||
is_identity=false;
|
||||
|
||||
scale_vector=Vector3f(v,v,v);
|
||||
matrix_dirty=true;
|
||||
}
|
||||
|
||||
void SetScale(const float x,const float y,const float z)
|
||||
{
|
||||
if(is_identity)
|
||||
{
|
||||
if(IsNearlyEqual(x,1.0f)
|
||||
&&IsNearlyEqual(y,1.0f)
|
||||
&&IsNearlyEqual(z,1.0f))
|
||||
return;
|
||||
}
|
||||
|
||||
is_identity=false;
|
||||
|
||||
scale_vector.x=x;
|
||||
scale_vector.y=y;
|
||||
scale_vector.z=z;
|
||||
|
||||
matrix_dirty=true;
|
||||
}
|
||||
|
||||
void SetScale(const Vector3f &v)
|
||||
{
|
||||
if(is_identity)
|
||||
{
|
||||
if(IsNearlyEqual(v.x,1.0f)
|
||||
&&IsNearlyEqual(v.y,1.0f)
|
||||
&&IsNearlyEqual(v.z,1.0f))
|
||||
return;
|
||||
}
|
||||
|
||||
is_identity=false;
|
||||
|
||||
scale_vector=v;
|
||||
matrix_dirty=true;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
Transform()
|
||||
{
|
||||
SetToIdentity();
|
||||
version=0;
|
||||
}
|
||||
|
||||
Transform(const Matrix4f &m)
|
||||
{
|
||||
SetFromMatrix4f(m);
|
||||
version=0;
|
||||
}
|
||||
|
||||
Transform(const Transform &t)
|
||||
{
|
||||
hgl_cpy(*this,t);
|
||||
version=0;
|
||||
}
|
||||
|
||||
void SetToIdentity();
|
||||
|
||||
const bool operator == (const Transform &t);
|
||||
|
||||
void operator = (const Transform &t)
|
||||
{
|
||||
if(operator==(t))
|
||||
return;
|
||||
|
||||
uint32 old_version=version;
|
||||
|
||||
hgl_cpy(*this,t);
|
||||
|
||||
version=++old_version;
|
||||
UpdateMatrix();
|
||||
}
|
||||
|
||||
void SetFromMatrix4f(const Matrix4f &m);
|
||||
|
||||
const bool IsIdentity()const{return is_identity;}
|
||||
|
||||
const bool IsLastVersion()const{return !matrix_dirty;}
|
||||
|
||||
inline Vector3f TransformPosition (const Vector3f &v){UpdateMatrix();return Vector3f(matrix*Vector4f(v,1.0f));}
|
||||
inline Vector3f TransformDirection (const Vector3f &v){UpdateMatrix();return Vector3f(matrix*Vector4f(v,0.0f));}
|
||||
inline Vector3f TransformNormal (const Vector3f &v){UpdateMatrix();return normalize(Vector3f(transpose_inverse_matrix*Vector4f(v,0.0f)));}
|
||||
inline Matrix3f TransformMatrix (const Matrix3f &child){UpdateMatrix();return Matrix3f(matrix*Matrix4f(child));}
|
||||
inline Matrix4f TransformMatrix (const Matrix4f &child){UpdateMatrix();return matrix*child;}
|
||||
|
||||
inline Vector3f InverseTransformPosition (const Vector3f &v){UpdateMatrix();return Vector3f(inverse_matrix*Vector4f(v,1.0f));}
|
||||
inline Vector3f InverseTransformDirection (const Vector3f &v){UpdateMatrix();return Vector3f(inverse_matrix*Vector4f(v,0.0f));}
|
||||
inline Vector3f InverseTransformNormal (const Vector3f &v){UpdateMatrix();return normalize(Vector3f(transpose_inverse_matrix*Vector4f(v,0.0f)));}
|
||||
inline Matrix3f InverseTransformMatrix (const Matrix3f &child){UpdateMatrix();return Matrix3f(inverse_matrix*Matrix4f(child));}
|
||||
inline Matrix4f InverseTransformMatrix (const Matrix4f &child){UpdateMatrix();return inverse_matrix*child;}
|
||||
|
||||
inline Transform TransformTransform(const Transform &child)
|
||||
{
|
||||
UpdateMatrix();
|
||||
const Matrix4f &child_matrix=child.GetMatrix();
|
||||
|
||||
return Transform(matrix*child_matrix);
|
||||
}
|
||||
|
||||
inline Transform TransformTransform(const Transform &child)const
|
||||
{
|
||||
const Matrix4f &cur_matrix=GetMatrix();
|
||||
const Matrix4f &child_matrix=child.GetMatrix();
|
||||
|
||||
return Transform(cur_matrix*child_matrix);
|
||||
}
|
||||
};//Transform
|
||||
|
||||
const Transform IdentityTransform;
|
||||
|
||||
constexpr const size_t TransformMatrix4fLength=sizeof(Transform);
|
||||
|
||||
Transform Lerp(const Transform &from,const Transform &to,const float t);
|
||||
}//namespace hgl
|
||||
|
@ -41,6 +41,14 @@ namespace hgl
|
||||
cur_data=init_data;
|
||||
}
|
||||
|
||||
VersionData(const VersionData *vd)
|
||||
{
|
||||
version=vd->version;
|
||||
cur_version=vd->cur_version;
|
||||
|
||||
cur_data=vd->cur_data;
|
||||
}
|
||||
|
||||
virtual ~VersionData()=default;
|
||||
|
||||
const uint32 GetNewestVersion()const { return version; } ///<取得最新的版本号(注意数据可能不是最新的)
|
||||
|
@ -2,131 +2,5 @@
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
Transform Lerp(const Transform &from,const Transform &to,const float t)
|
||||
{
|
||||
Transform result;
|
||||
|
||||
result.SetTranslation( from.GetTranslation()*(1.0f-t) + to.GetTranslation() *t);
|
||||
result.SetRotation( SLerpQuat(from.GetRotationQuat(),to.GetRotationQuat(),t));
|
||||
result.SetScale( from.GetScale() *(1.0f-t) + to.GetScale() *t);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void Transform::UpdateQuat()
|
||||
{
|
||||
if(IsNearlyZero(rotate_angle))
|
||||
{
|
||||
if(is_zero_rotate)
|
||||
return;
|
||||
|
||||
is_zero_rotate=true;
|
||||
rotation_quat=IdentityQuatf;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
is_zero_rotate=false;
|
||||
rotation_quat=RotationQuat(rotate_angle,rotation_axis);
|
||||
}
|
||||
|
||||
matrix_dirty=true;
|
||||
}
|
||||
|
||||
void Transform::UpdateMatrix()
|
||||
{
|
||||
if(!matrix_dirty)
|
||||
return;
|
||||
|
||||
if(translation_vector==ZeroVector3f
|
||||
&&rotation_quat==IdentityQuatf
|
||||
&&scale_vector==OneVector3f)
|
||||
{
|
||||
if(is_identity) //如果之前就是空的,那不更新版本号
|
||||
{
|
||||
matrix_dirty=false;
|
||||
return;
|
||||
}
|
||||
|
||||
SetToIdentity();
|
||||
}
|
||||
else
|
||||
{
|
||||
matrix=translate(translation_vector)*ToMatrix(rotation_quat)*scale(scale_vector);
|
||||
inverse_matrix=inverse(matrix);
|
||||
transpose_inverse_matrix=transpose(inverse_matrix);
|
||||
|
||||
is_identity=false;
|
||||
}
|
||||
|
||||
matrix_dirty=false;
|
||||
|
||||
UpdateVersion();
|
||||
}
|
||||
|
||||
void Transform::SetToIdentity()
|
||||
{
|
||||
is_identity=true;
|
||||
is_zero_rotate=true;
|
||||
|
||||
matrix=Identity4f;
|
||||
inverse_matrix=Identity4f;
|
||||
|
||||
matrix_dirty=false;
|
||||
|
||||
translation_vector=ZeroVector3f;
|
||||
rotation_quat=IdentityQuatf;
|
||||
rotation_axis=ZeroVector3f;
|
||||
rotate_angle=0;
|
||||
scale_vector=OneVector3f;
|
||||
}
|
||||
|
||||
const bool Transform::operator == (const Transform &t)
|
||||
{
|
||||
if(is_identity)
|
||||
{
|
||||
if(t.is_identity)
|
||||
return(true);
|
||||
else
|
||||
return(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(t.is_identity)
|
||||
return(false);
|
||||
}
|
||||
|
||||
if(translation_vector!=t.translation_vector)return(false);
|
||||
if(rotation_quat!=t.rotation_quat)return(false);
|
||||
if(scale_vector!=t.scale_vector)return(false);
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
void Transform::SetFromMatrix4f(const Matrix4f &m)
|
||||
{
|
||||
if(is_identity)
|
||||
{
|
||||
if(IsIdentityMatrix(m))
|
||||
return;
|
||||
}
|
||||
|
||||
is_identity=IsIdentityMatrix(m);
|
||||
|
||||
if(is_identity)
|
||||
{
|
||||
SetToIdentity();
|
||||
UpdateVersion();
|
||||
return;
|
||||
}
|
||||
|
||||
matrix=m;
|
||||
inverse_matrix=inverse(m);
|
||||
|
||||
matrix_dirty=false;
|
||||
|
||||
DecomposeTransform(m,translation_vector,rotation_quat,scale_vector);
|
||||
|
||||
ExtractedQuat(rotation_quat,rotation_axis,rotate_angle);
|
||||
}
|
||||
}//namespace hgl
|
||||
|
Loading…
x
Reference in New Issue
Block a user