Added VersionData<>
This commit is contained in:
parent
9276872a2f
commit
cddc558153
@ -1,67 +1,29 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include<hgl/math/Matrix.h>
|
#include<hgl/math/Matrix.h>
|
||||||
#include<hgl/type/ObjectList.h>
|
#include<hgl/type/ObjectList.h>
|
||||||
|
#include<hgl/type/VersionData.h>
|
||||||
|
|
||||||
namespace hgl
|
namespace hgl
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* 变换基类
|
* 变换基类
|
||||||
*/
|
*/
|
||||||
class TransformBase
|
class TransformBase:public VersionData<Matrix4f>
|
||||||
{
|
{
|
||||||
uint version;
|
|
||||||
uint cur_version;
|
|
||||||
|
|
||||||
Matrix4f CurMatrix;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
virtual uint UpdateVersion()
|
virtual void MakeNewestData(Matrix4f &)=0;
|
||||||
{
|
|
||||||
//版本号只是为了记录变化,让程序知道和上次不一样,所以最大值是多少其实无所谓的
|
|
||||||
version=(version >= 1000000)?0:++version;
|
|
||||||
|
|
||||||
return version;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void UpdateMatrix(Matrix4f &)=0;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
TransformBase()
|
TransformBase():VersionData(Identity4f){}
|
||||||
{
|
|
||||||
version=0;
|
|
||||||
cur_version=0;
|
|
||||||
CurMatrix=Identity4f;
|
|
||||||
}
|
|
||||||
virtual ~TransformBase()=default;
|
virtual ~TransformBase()=default;
|
||||||
|
|
||||||
virtual constexpr const size_t GetTypeHash()const=0; ///<取得类型哈希值
|
virtual constexpr const size_t GetTypeHash()const=0; ///<取得类型哈希值
|
||||||
|
|
||||||
const uint GetVersion()const{return version;} ///<取得当前数据版本号
|
const uint32 GetMatrix(Matrix4f &mat) ///<取得当前矩阵,并返回当前矩阵版本号
|
||||||
|
|
||||||
virtual const uint MatrixTransform(Matrix4f &Mat) ///<将当前矩阵与Mat相乘,并返回当前矩阵版本号
|
|
||||||
{
|
{
|
||||||
if (cur_version != version)
|
return GetNewestVersionData(mat);
|
||||||
{
|
|
||||||
UpdateMatrix(CurMatrix);
|
|
||||||
cur_version=version;
|
|
||||||
}
|
|
||||||
|
|
||||||
Mat*=CurMatrix;
|
|
||||||
return cur_version;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual const uint GetMatrix(Matrix4f &mat) ///<取得当前矩阵,并返回当前矩阵版本号
|
|
||||||
{
|
|
||||||
if (cur_version != version)
|
|
||||||
{
|
|
||||||
UpdateMatrix(CurMatrix);
|
|
||||||
cur_version=version;
|
|
||||||
}
|
|
||||||
|
|
||||||
mat=CurMatrix;
|
|
||||||
return cur_version;
|
|
||||||
}
|
}
|
||||||
};//class TransformBase
|
};//class TransformBase
|
||||||
|
|
||||||
@ -69,6 +31,13 @@ namespace hgl
|
|||||||
{
|
{
|
||||||
Matrix4f transform_matrix;
|
Matrix4f transform_matrix;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
virtual void MakeNewestData(Matrix4f &mat) override
|
||||||
|
{
|
||||||
|
mat=transform_matrix;
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
virtual constexpr const size_t GetTypeHash()const override { return hgl::GetTypeHash<TransformMatrix>(); }
|
virtual constexpr const size_t GetTypeHash()const override { return hgl::GetTypeHash<TransformMatrix>(); }
|
||||||
@ -95,17 +64,19 @@ namespace hgl
|
|||||||
transform_matrix=mat;
|
transform_matrix=mat;
|
||||||
UpdateVersion();
|
UpdateVersion();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void UpdateMatrix(Matrix4f &mat) override
|
|
||||||
{
|
|
||||||
mat=transform_matrix;
|
|
||||||
}
|
|
||||||
};//class TransformMatrix
|
};//class TransformMatrix
|
||||||
|
|
||||||
class TransformTranslate3f :public TransformBase
|
class TransformTranslate3f :public TransformBase
|
||||||
{
|
{
|
||||||
Vector3f offset;
|
Vector3f offset;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
virtual void MakeNewestData(Matrix4f &mat) override
|
||||||
|
{
|
||||||
|
mat=translate(offset);
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
virtual constexpr const size_t GetTypeHash()const override{return hgl::GetTypeHash<TransformTranslate3f>();}
|
virtual constexpr const size_t GetTypeHash()const override{return hgl::GetTypeHash<TransformTranslate3f>();}
|
||||||
@ -140,11 +111,6 @@ namespace hgl
|
|||||||
offset+=o;
|
offset+=o;
|
||||||
UpdateVersion();
|
UpdateVersion();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void UpdateMatrix(Matrix4f &mat) override
|
|
||||||
{
|
|
||||||
mat=translate(offset);
|
|
||||||
}
|
|
||||||
};//class TransformTranslate3f
|
};//class TransformTranslate3f
|
||||||
|
|
||||||
using TransformMove3f=TransformTranslate3f;
|
using TransformMove3f=TransformTranslate3f;
|
||||||
@ -153,6 +119,13 @@ namespace hgl
|
|||||||
{
|
{
|
||||||
Quatf quat;
|
Quatf quat;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
virtual void MakeNewestData(Matrix4f &mat) override
|
||||||
|
{
|
||||||
|
mat=ToMatrix(quat);
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
virtual constexpr const size_t GetTypeHash()const override{return hgl::GetTypeHash<TransformRotateQuat>();}
|
virtual constexpr const size_t GetTypeHash()const override{return hgl::GetTypeHash<TransformRotateQuat>();}
|
||||||
@ -178,11 +151,6 @@ namespace hgl
|
|||||||
quat=q;
|
quat=q;
|
||||||
UpdateVersion();
|
UpdateVersion();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void UpdateMatrix(Matrix4f &mat) override
|
|
||||||
{
|
|
||||||
mat=ToMatrix(quat);
|
|
||||||
}
|
|
||||||
};//class TransformRotateQuat
|
};//class TransformRotateQuat
|
||||||
|
|
||||||
class TransformRotateAxis :public TransformBase
|
class TransformRotateAxis :public TransformBase
|
||||||
@ -190,6 +158,13 @@ namespace hgl
|
|||||||
Vector3f axis;
|
Vector3f axis;
|
||||||
float angle;
|
float angle;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
virtual void MakeNewestData(Matrix4f &mat) override
|
||||||
|
{
|
||||||
|
mat=rotate(angle,axis);
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
virtual constexpr const size_t GetTypeHash()const override { return hgl::GetTypeHash<TransformRotateAxis>(); }
|
virtual constexpr const size_t GetTypeHash()const override { return hgl::GetTypeHash<TransformRotateAxis>(); }
|
||||||
@ -238,17 +213,19 @@ namespace hgl
|
|||||||
angle=a;
|
angle=a;
|
||||||
UpdateVersion();
|
UpdateVersion();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void UpdateMatrix(Matrix4f &mat) override
|
|
||||||
{
|
|
||||||
mat=rotate(angle,axis);
|
|
||||||
}
|
|
||||||
};//class TransformRotateAxis
|
};//class TransformRotateAxis
|
||||||
|
|
||||||
class TransformRotateEuler :public TransformBase
|
class TransformRotateEuler :public TransformBase
|
||||||
{
|
{
|
||||||
Vector3f euler;
|
Vector3f euler;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
virtual void MakeNewestData(Matrix4f &mat) override
|
||||||
|
{
|
||||||
|
mat=glm::eulerAngleXYZ(euler.x,euler.y,euler.z);
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
virtual constexpr const size_t GetTypeHash()const override { return hgl::GetTypeHash<TransformRotateEuler>(); }
|
virtual constexpr const size_t GetTypeHash()const override { return hgl::GetTypeHash<TransformRotateEuler>(); }
|
||||||
@ -275,17 +252,19 @@ namespace hgl
|
|||||||
void SetPitch (const float p ){if(IsNearlyEqual(euler.x,p))return;euler.x=p;UpdateVersion();}
|
void SetPitch (const float p ){if(IsNearlyEqual(euler.x,p))return;euler.x=p;UpdateVersion();}
|
||||||
void SetYaw (const float y ){if(IsNearlyEqual(euler.y,y))return;euler.y=y;UpdateVersion();}
|
void SetYaw (const float y ){if(IsNearlyEqual(euler.y,y))return;euler.y=y;UpdateVersion();}
|
||||||
void SetRoll (const float r ){if(IsNearlyEqual(euler.z,r))return;euler.z=r;UpdateVersion();}
|
void SetRoll (const float r ){if(IsNearlyEqual(euler.z,r))return;euler.z=r;UpdateVersion();}
|
||||||
|
|
||||||
virtual void UpdateMatrix(Matrix4f &mat) override
|
|
||||||
{
|
|
||||||
mat=glm::eulerAngleXYZ(euler.x,euler.y,euler.z);
|
|
||||||
}
|
|
||||||
};//class TransformRotateEuler
|
};//class TransformRotateEuler
|
||||||
|
|
||||||
class TransformScale3f :public TransformBase
|
class TransformScale3f :public TransformBase
|
||||||
{
|
{
|
||||||
Vector3f scale3f;
|
Vector3f scale3f;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
virtual void MakeNewestData(Matrix4f &mat) override
|
||||||
|
{
|
||||||
|
mat=scale(scale3f);
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
virtual constexpr const size_t GetTypeHash()const override { return hgl::GetTypeHash<TransformScale3f>(); }
|
virtual constexpr const size_t GetTypeHash()const override { return hgl::GetTypeHash<TransformScale3f>(); }
|
||||||
@ -311,11 +290,6 @@ namespace hgl
|
|||||||
scale3f=s;
|
scale3f=s;
|
||||||
UpdateVersion();
|
UpdateVersion();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void UpdateMatrix(Matrix4f &mat) override
|
|
||||||
{
|
|
||||||
mat=scale(scale3f);
|
|
||||||
}
|
|
||||||
};//class TransformScale
|
};//class TransformScale
|
||||||
|
|
||||||
class TransformLookAt :public TransformBase
|
class TransformLookAt :public TransformBase
|
||||||
@ -324,6 +298,13 @@ namespace hgl
|
|||||||
Vector3f center;
|
Vector3f center;
|
||||||
Vector3f up;
|
Vector3f up;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
virtual void MakeNewestData(Matrix4f &mat) override
|
||||||
|
{
|
||||||
|
mat=lookat(eye,center,up);
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
virtual constexpr const size_t GetTypeHash()const override { return hgl::GetTypeHash<TransformLookAt>(); }
|
virtual constexpr const size_t GetTypeHash()const override { return hgl::GetTypeHash<TransformLookAt>(); }
|
||||||
@ -369,51 +350,38 @@ namespace hgl
|
|||||||
up=pos;
|
up=pos;
|
||||||
UpdateVersion();
|
UpdateVersion();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void UpdateMatrix(Matrix4f &mat) override
|
|
||||||
{
|
|
||||||
mat=lookat(eye,center,up);
|
|
||||||
}
|
|
||||||
};//class TransformLookAt
|
};//class TransformLookAt
|
||||||
|
|
||||||
class TransformManager
|
class TransformManager: public TransformBase
|
||||||
{
|
{
|
||||||
uint version;
|
|
||||||
uint cur_version;
|
|
||||||
|
|
||||||
Matrix4f CurMatrix;
|
|
||||||
|
|
||||||
ObjectList<TransformBase> transform_list;
|
ObjectList<TransformBase> transform_list;
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
|
|
||||||
virtual uint UpdateVersion()
|
virtual void MakeNewestData(Matrix4f &mat) override
|
||||||
{
|
{
|
||||||
//版本号只是为了记录变化,让程序知道和上次不一样,所以最大值是多少其实无所谓的
|
mat=Identity4f;
|
||||||
version=(version >= 1000000)?0:++version;
|
|
||||||
|
|
||||||
return version;
|
|
||||||
}
|
|
||||||
|
|
||||||
void UpdateMatrix(Matrix4f &cur_matrix)
|
|
||||||
{
|
|
||||||
cur_matrix=Identity4f;
|
|
||||||
|
|
||||||
if (transform_list.IsEmpty())
|
if (transform_list.IsEmpty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
Matrix4f TempMatrix;
|
||||||
|
|
||||||
for (TransformBase *tb : transform_list)
|
for (TransformBase *tb : transform_list)
|
||||||
tb->MatrixTransform(cur_matrix);
|
{
|
||||||
|
tb->GetMatrix(TempMatrix);
|
||||||
|
|
||||||
|
mat*=TempMatrix;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
TransformManager()
|
virtual constexpr const size_t GetTypeHash()const override { return hgl::GetTypeHash<TransformManager>(); }
|
||||||
{
|
|
||||||
version=0;
|
public:
|
||||||
cur_version=0;
|
|
||||||
CurMatrix=Identity4f;
|
TransformManager()=default;
|
||||||
}
|
|
||||||
virtual ~TransformManager()=default;
|
virtual ~TransformManager()=default;
|
||||||
|
|
||||||
void Clear()
|
void Clear()
|
||||||
@ -421,8 +389,6 @@ namespace hgl
|
|||||||
transform_list.Clear();
|
transform_list.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
const uint GetVersion()const{return version;} ///<取得当前数据版本号
|
|
||||||
|
|
||||||
void AddTransform(TransformBase *tb)
|
void AddTransform(TransformBase *tb)
|
||||||
{
|
{
|
||||||
transform_list.Add(tb);
|
transform_list.Add(tb);
|
||||||
@ -510,27 +476,6 @@ namespace hgl
|
|||||||
|
|
||||||
UpdateVersion();
|
UpdateVersion();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint GetMatrix(Matrix4f &result,const uint old_version)
|
|
||||||
{
|
|
||||||
if (old_version == cur_version)
|
|
||||||
return old_version;
|
|
||||||
|
|
||||||
if (transform_list.IsEmpty())
|
|
||||||
{
|
|
||||||
result=Identity4f;
|
|
||||||
return cur_version;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cur_version != version)
|
|
||||||
{
|
|
||||||
UpdateMatrix(CurMatrix);
|
|
||||||
cur_version=version;
|
|
||||||
}
|
|
||||||
|
|
||||||
result=CurMatrix;
|
|
||||||
return cur_version;
|
|
||||||
}
|
|
||||||
};//class TransformManager
|
};//class TransformManager
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
77
inc/hgl/type/VersionData.h
Normal file
77
inc/hgl/type/VersionData.h
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace hgl
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 版本数据基类模板
|
||||||
|
*/
|
||||||
|
template<typename T> class VersionData
|
||||||
|
{
|
||||||
|
uint32 version;
|
||||||
|
uint32 cur_version;
|
||||||
|
|
||||||
|
T cur_data;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
const uint32 UpdateVersion() ///<更新版本号
|
||||||
|
{
|
||||||
|
//版本号只是为了记录变化,让程序知道和上次不一样,所以最大值是多少其实无所谓的
|
||||||
|
version=(version >= 1000000000u)?0:++version;
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void MakeNewestData(T &)=0; ///<生成最新的数据(需要派生类重载)
|
||||||
|
|
||||||
|
void UpdateVersionData()
|
||||||
|
{
|
||||||
|
if(IsNewestVersion())
|
||||||
|
return;
|
||||||
|
|
||||||
|
MakeNewestData(cur_data);
|
||||||
|
cur_version=version;
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
VersionData(const T &init_data)
|
||||||
|
{
|
||||||
|
version=0;
|
||||||
|
cur_version=0;
|
||||||
|
cur_data=init_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~VersionData()=default;
|
||||||
|
|
||||||
|
const uint32 GetNewestVersion()const { return version; } ///<取得最新的版本号(注意数据可能不是最新的)
|
||||||
|
const uint32 GetCurrentVersion()const { return cur_version; } ///<取得当前数据的版本号
|
||||||
|
|
||||||
|
const bool IsNewestVersion()const { return cur_version == version; } ///<当前数据是否最新版本
|
||||||
|
|
||||||
|
const T & GetCurrentVersionData()const { return cur_data; } ///<取得当前版本的数据(注意可能不是最新的)
|
||||||
|
|
||||||
|
const T & GetNewestVersionData(){UpdateVersionData();return cur_data;} ///<取得最新版本的数据
|
||||||
|
|
||||||
|
const uint32 GetNewestVersionData(T &result) ///<取得最新版本的数据
|
||||||
|
{
|
||||||
|
UpdateVersionData();
|
||||||
|
|
||||||
|
result=cur_data;
|
||||||
|
return cur_version;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 取得最新版本的数据(如果已有数据已经是最新版本,那么不会产生任何操作)
|
||||||
|
* @param result 数据结果
|
||||||
|
* @param had_version 已有数据的版本号
|
||||||
|
* @return 返回最新的版本号
|
||||||
|
*/
|
||||||
|
const uint32 GetNewestVersionData(T &result,const uint32 had_version)
|
||||||
|
{
|
||||||
|
if (had_version == cur_version)
|
||||||
|
return cur_version;
|
||||||
|
|
||||||
|
return GetNewestVersionData(result);
|
||||||
|
}
|
||||||
|
};//class VersionData
|
||||||
|
}//namespace hgl
|
Loading…
x
Reference in New Issue
Block a user