From 410b94a1276086a84b2d985ea9d35843cba85e82 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sun, 25 Aug 2024 04:03:57 +0800 Subject: [PATCH] [WIP] improving SceneOrient --- CMCore | 2 +- inc/hgl/graph/SceneOrient.h | 35 ++++++++++------ src/SceneGraph/SceneOrient.cpp | 76 +++++++++++++++++++++++----------- 3 files changed, 75 insertions(+), 38 deletions(-) diff --git a/CMCore b/CMCore index 1fd9db05..529ad6b4 160000 --- a/CMCore +++ b/CMCore @@ -1 +1 @@ -Subproject commit 1fd9db054591942c3f4746052def3b621a1dbaff +Subproject commit 529ad6b4037865b871f3406fa528ef1ec80d56a8 diff --git a/inc/hgl/graph/SceneOrient.h b/inc/hgl/graph/SceneOrient.h index 571abf67..60b25ca6 100644 --- a/inc/hgl/graph/SceneOrient.h +++ b/inc/hgl/graph/SceneOrient.h @@ -1,9 +1,8 @@ #ifndef HGL_GRAPH_SCENE_ORIENT_INCLUDE #define HGL_GRAPH_SCENE_ORIENT_INCLUDE -//#include -#include #include +#include namespace hgl { namespace graph @@ -14,15 +13,23 @@ namespace hgl class SceneOrient ///场景定位类 { protected: + + Matrix4f parent_matrix; + bool parent_matrix_dirty; + + Matrix4f local_matrix; + bool local_matrix_dirty; TransformManager transform_manager; + uint32 transform_version; - Matrix4f ParentMatrix; ///<上级矩阵 + uint32 local_to_world_matrix_version; - Matrix4f LocalMatrix; ///<本地到上一级矩阵 - Matrix4f LocalToWorldMatrix; ///<本地到世界矩阵 - Matrix4f InverseLocalToWorldMatrix; ///<世界到本地矩阵 - Matrix4f InverseTransposeLocalToWorldMatrix; ///<世界到本地矩阵的转置矩阵 + // LocalToWorld = ParentMatrix * LocalMatrix * TransformMatrix + + Matrix4f local_to_world_matrix; ///<本地到世界矩阵 + Matrix4f inverse_local_to_world_matrix; ///<世界到本地矩阵 + Matrix4f inverse_transpose_local_to_world_matrix; ///<世界到本地矩阵的转置矩阵 virtual void SetWorldMatrix(const Matrix4f &); @@ -33,18 +40,20 @@ namespace hgl SceneOrient(const Matrix4f &); virtual ~SceneOrient()=default; + void SetLocalMatrix(const Matrix4f &); ///<设置本地矩阵 + public: + const Matrix4f & GetLocalMatrix ()const{return local_matrix;} - TransformManager * GetTransform (){return &transform_manager;} ///<取得变换管理器 + TransformManager & GetTransform () {return transform_manager;} ///<取得变换管理器 - const Matrix4f & GetLocalMatrix ()const{return LocalMatrix;} - const Matrix4f & GetLocalToWorldMatrix ()const{return LocalToWorldMatrix;} - const Matrix4f & GetInverseLocalToWorldMatrix ()const{return InverseLocalToWorldMatrix;} - const Matrix4f & GetInverseTransposeLocalToWorldMatrix ()const{return InverseTransposeLocalToWorldMatrix;} + const Matrix4f & GetLocalToWorldMatrix ()const{return local_to_world_matrix;} + const Matrix4f & GetInverseLocalToWorldMatrix ()const{return inverse_local_to_world_matrix;} + const Matrix4f & GetInverseTransposeLocalToWorldMatrix ()const{return inverse_transpose_local_to_world_matrix;} public: - virtual bool RefreshMatrix (const Matrix4f &); ///<刷新到世界空间变换 + virtual bool RefreshMatrix (const Matrix4f &); ///<刷新到世界空间变换 };//class SceneOrient }//namespace graph }//namespace hgl diff --git a/src/SceneGraph/SceneOrient.cpp b/src/SceneGraph/SceneOrient.cpp index bec49d89..ce4e9e4f 100644 --- a/src/SceneGraph/SceneOrient.cpp +++ b/src/SceneGraph/SceneOrient.cpp @@ -5,60 +5,88 @@ namespace hgl { SceneOrient::SceneOrient() { - LocalMatrix=Identity4f; + parent_matrix=Identity4f; + local_matrix=Identity4f; SetWorldMatrix(Identity4f); + + parent_matrix_dirty=false; + local_matrix_dirty=false; + transform_version=transform_manager.GetCurrentVersion(); } SceneOrient::SceneOrient(const SceneOrient &so) { - hgl_cpy(*this,so); + #define SO_COPY(value) value=so.value; + + SO_COPY(parent_matrix) + SO_COPY(parent_matrix_dirty) + + SO_COPY(local_matrix) + SO_COPY(local_matrix_dirty) + + SO_COPY(transform_manager) + SO_COPY(transform_version) + + SO_COPY(local_to_world_matrix) + SO_COPY(inverse_local_to_world_matrix) + SO_COPY(inverse_transpose_local_to_world_matrix) + + #undef SO_COPY } - SceneOrient::SceneOrient(const Matrix4f &mat) + SceneOrient::SceneOrient(const Matrix4f &mat):SceneOrient() { SetLocalMatrix(mat); } void SceneOrient::SetLocalMatrix(const Matrix4f &mat) { - if(IsNearlyEqual(LocalMatrix,mat)) + if(IsNearlyEqual(local_matrix,mat)) return; - LocalMatrix=mat; + local_matrix=mat; } void SceneOrient::SetWorldMatrix(const Matrix4f &wm) { - LocalToWorldMatrix =wm; - InverseLocalToWorldMatrix =inverse(LocalToWorldMatrix); - InverseTransposeLocalToWorldMatrix =transpose(InverseLocalToWorldMatrix); + local_to_world_matrix =wm; + inverse_local_to_world_matrix =inverse(local_to_world_matrix); + inverse_transpose_local_to_world_matrix=transpose(inverse_local_to_world_matrix); } /** * 刷新世界变换矩阵 * @param parent_matrix 上一级LocalToWorld变换矩阵 */ - bool SceneOrient::RefreshMatrix(const Matrix4f &parent_matrix) + bool SceneOrient::RefreshMatrix(const Matrix4f &pm) { - if(hgl_cmp(ParentMatrix,parent_matrix)==0) //如果上一级到这一级没变,自然是可以用memcmp比较的 + if(hgl_cmp(pm,parent_matrix)==0) //如果上一级到这一级没变,自然是可以用memcmp比较的 return(true); - ParentMatrix=parent_matrix; + parent_matrix=pm; - if(IsIdentityMatrix(LocalMatrix)) - { - SetWorldMatrix(parent_matrix); - } - else - if(IsIdentityMatrix(parent_matrix)) - { - SetWorldMatrix(LocalMatrix); - } - else - { - SetWorldMatrix(MatrixMult(parent_matrix,LocalMatrix)); - } + Matrix4f tm; + + transform_manager.GetMatrix(tm); + + SetWorldMatrix(parent_matrix*local_matrix*tm); + + //if(IsIdentityMatrix(LocalMatrix)) + //{ + // SetWorldMatrix(parent_matrix); + // return(true); + //} + // + + //if(IsIdentityMatrix(parent_matrix)) + //{ + // SetWorldMatrix(LocalMatrix); + //} + //else + //{ + // SetWorldMatrix(parent_matrix*LocalMatrix); + //} return(true); }