Added VersionData<>
This commit is contained in:
parent
9276872a2f
commit
cddc558153
@ -1,67 +1,29 @@
|
||||
#pragma once
|
||||
#include<hgl/math/Matrix.h>
|
||||
#include<hgl/type/ObjectList.h>
|
||||
#include<hgl/type/VersionData.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
/**
|
||||
* 变换基类
|
||||
*/
|
||||
class TransformBase
|
||||
class TransformBase:public VersionData<Matrix4f>
|
||||
{
|
||||
uint version;
|
||||
uint cur_version;
|
||||
|
||||
Matrix4f CurMatrix;
|
||||
|
||||
protected:
|
||||
|
||||
virtual uint UpdateVersion()
|
||||
{
|
||||
//版本号只是为了记录变化,让程序知道和上次不一样,所以最大值是多少其实无所谓的
|
||||
version=(version >= 1000000)?0:++version;
|
||||
|
||||
return version;
|
||||
}
|
||||
|
||||
virtual void UpdateMatrix(Matrix4f &)=0;
|
||||
virtual void MakeNewestData(Matrix4f &)=0;
|
||||
|
||||
public:
|
||||
|
||||
TransformBase()
|
||||
{
|
||||
version=0;
|
||||
cur_version=0;
|
||||
CurMatrix=Identity4f;
|
||||
}
|
||||
TransformBase():VersionData(Identity4f){}
|
||||
virtual ~TransformBase()=default;
|
||||
|
||||
virtual constexpr const size_t GetTypeHash()const=0; ///<取得类型哈希值
|
||||
|
||||
const uint GetVersion()const{return version;} ///<取得当前数据版本号
|
||||
|
||||
virtual const uint MatrixTransform(Matrix4f &Mat) ///<将当前矩阵与Mat相乘,并返回当前矩阵版本号
|
||||
const uint32 GetMatrix(Matrix4f &mat) ///<取得当前矩阵,并返回当前矩阵版本号
|
||||
{
|
||||
if (cur_version != version)
|
||||
{
|
||||
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;
|
||||
return GetNewestVersionData(mat);
|
||||
}
|
||||
};//class TransformBase
|
||||
|
||||
@ -69,6 +31,13 @@ namespace hgl
|
||||
{
|
||||
Matrix4f transform_matrix;
|
||||
|
||||
protected:
|
||||
|
||||
virtual void MakeNewestData(Matrix4f &mat) override
|
||||
{
|
||||
mat=transform_matrix;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
virtual constexpr const size_t GetTypeHash()const override { return hgl::GetTypeHash<TransformMatrix>(); }
|
||||
@ -95,17 +64,19 @@ namespace hgl
|
||||
transform_matrix=mat;
|
||||
UpdateVersion();
|
||||
}
|
||||
|
||||
virtual void UpdateMatrix(Matrix4f &mat) override
|
||||
{
|
||||
mat=transform_matrix;
|
||||
}
|
||||
};//class TransformMatrix
|
||||
|
||||
class TransformTranslate3f :public TransformBase
|
||||
{
|
||||
Vector3f offset;
|
||||
|
||||
protected:
|
||||
|
||||
virtual void MakeNewestData(Matrix4f &mat) override
|
||||
{
|
||||
mat=translate(offset);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
virtual constexpr const size_t GetTypeHash()const override{return hgl::GetTypeHash<TransformTranslate3f>();}
|
||||
@ -140,11 +111,6 @@ namespace hgl
|
||||
offset+=o;
|
||||
UpdateVersion();
|
||||
}
|
||||
|
||||
virtual void UpdateMatrix(Matrix4f &mat) override
|
||||
{
|
||||
mat=translate(offset);
|
||||
}
|
||||
};//class TransformTranslate3f
|
||||
|
||||
using TransformMove3f=TransformTranslate3f;
|
||||
@ -153,6 +119,13 @@ namespace hgl
|
||||
{
|
||||
Quatf quat;
|
||||
|
||||
protected:
|
||||
|
||||
virtual void MakeNewestData(Matrix4f &mat) override
|
||||
{
|
||||
mat=ToMatrix(quat);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
virtual constexpr const size_t GetTypeHash()const override{return hgl::GetTypeHash<TransformRotateQuat>();}
|
||||
@ -178,11 +151,6 @@ namespace hgl
|
||||
quat=q;
|
||||
UpdateVersion();
|
||||
}
|
||||
|
||||
virtual void UpdateMatrix(Matrix4f &mat) override
|
||||
{
|
||||
mat=ToMatrix(quat);
|
||||
}
|
||||
};//class TransformRotateQuat
|
||||
|
||||
class TransformRotateAxis :public TransformBase
|
||||
@ -190,6 +158,13 @@ namespace hgl
|
||||
Vector3f axis;
|
||||
float angle;
|
||||
|
||||
protected:
|
||||
|
||||
virtual void MakeNewestData(Matrix4f &mat) override
|
||||
{
|
||||
mat=rotate(angle,axis);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
virtual constexpr const size_t GetTypeHash()const override { return hgl::GetTypeHash<TransformRotateAxis>(); }
|
||||
@ -238,17 +213,19 @@ namespace hgl
|
||||
angle=a;
|
||||
UpdateVersion();
|
||||
}
|
||||
|
||||
virtual void UpdateMatrix(Matrix4f &mat) override
|
||||
{
|
||||
mat=rotate(angle,axis);
|
||||
}
|
||||
};//class TransformRotateAxis
|
||||
|
||||
class TransformRotateEuler :public TransformBase
|
||||
{
|
||||
Vector3f euler;
|
||||
|
||||
protected:
|
||||
|
||||
virtual void MakeNewestData(Matrix4f &mat) override
|
||||
{
|
||||
mat=glm::eulerAngleXYZ(euler.x,euler.y,euler.z);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
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 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();}
|
||||
|
||||
virtual void UpdateMatrix(Matrix4f &mat) override
|
||||
{
|
||||
mat=glm::eulerAngleXYZ(euler.x,euler.y,euler.z);
|
||||
}
|
||||
};//class TransformRotateEuler
|
||||
|
||||
class TransformScale3f :public TransformBase
|
||||
{
|
||||
Vector3f scale3f;
|
||||
|
||||
protected:
|
||||
|
||||
virtual void MakeNewestData(Matrix4f &mat) override
|
||||
{
|
||||
mat=scale(scale3f);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
virtual constexpr const size_t GetTypeHash()const override { return hgl::GetTypeHash<TransformScale3f>(); }
|
||||
@ -311,11 +290,6 @@ namespace hgl
|
||||
scale3f=s;
|
||||
UpdateVersion();
|
||||
}
|
||||
|
||||
virtual void UpdateMatrix(Matrix4f &mat) override
|
||||
{
|
||||
mat=scale(scale3f);
|
||||
}
|
||||
};//class TransformScale
|
||||
|
||||
class TransformLookAt :public TransformBase
|
||||
@ -324,6 +298,13 @@ namespace hgl
|
||||
Vector3f center;
|
||||
Vector3f up;
|
||||
|
||||
protected:
|
||||
|
||||
virtual void MakeNewestData(Matrix4f &mat) override
|
||||
{
|
||||
mat=lookat(eye,center,up);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
virtual constexpr const size_t GetTypeHash()const override { return hgl::GetTypeHash<TransformLookAt>(); }
|
||||
@ -369,51 +350,38 @@ namespace hgl
|
||||
up=pos;
|
||||
UpdateVersion();
|
||||
}
|
||||
|
||||
virtual void UpdateMatrix(Matrix4f &mat) override
|
||||
{
|
||||
mat=lookat(eye,center,up);
|
||||
}
|
||||
};//class TransformLookAt
|
||||
|
||||
class TransformManager
|
||||
class TransformManager: public TransformBase
|
||||
{
|
||||
uint version;
|
||||
uint cur_version;
|
||||
|
||||
Matrix4f CurMatrix;
|
||||
|
||||
ObjectList<TransformBase> transform_list;
|
||||
|
||||
private:
|
||||
protected:
|
||||
|
||||
virtual uint UpdateVersion()
|
||||
virtual void MakeNewestData(Matrix4f &mat) override
|
||||
{
|
||||
//版本号只是为了记录变化,让程序知道和上次不一样,所以最大值是多少其实无所谓的
|
||||
version=(version >= 1000000)?0:++version;
|
||||
|
||||
return version;
|
||||
}
|
||||
|
||||
void UpdateMatrix(Matrix4f &cur_matrix)
|
||||
{
|
||||
cur_matrix=Identity4f;
|
||||
mat=Identity4f;
|
||||
|
||||
if (transform_list.IsEmpty())
|
||||
return;
|
||||
|
||||
Matrix4f TempMatrix;
|
||||
|
||||
for (TransformBase *tb : transform_list)
|
||||
tb->MatrixTransform(cur_matrix);
|
||||
{
|
||||
tb->GetMatrix(TempMatrix);
|
||||
|
||||
mat*=TempMatrix;
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
TransformManager()
|
||||
{
|
||||
version=0;
|
||||
cur_version=0;
|
||||
CurMatrix=Identity4f;
|
||||
}
|
||||
virtual constexpr const size_t GetTypeHash()const override { return hgl::GetTypeHash<TransformManager>(); }
|
||||
|
||||
public:
|
||||
|
||||
TransformManager()=default;
|
||||
virtual ~TransformManager()=default;
|
||||
|
||||
void Clear()
|
||||
@ -421,8 +389,6 @@ namespace hgl
|
||||
transform_list.Clear();
|
||||
}
|
||||
|
||||
const uint GetVersion()const{return version;} ///<取得当前数据版本号
|
||||
|
||||
void AddTransform(TransformBase *tb)
|
||||
{
|
||||
transform_list.Add(tb);
|
||||
@ -510,27 +476,6 @@ namespace hgl
|
||||
|
||||
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
|
||||
|
||||
/**
|
||||
|
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