From 461456292908784fa5cbcb99121baef0ca592354 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sun, 6 Oct 2024 02:30:59 +0800 Subject: [PATCH] optimized SceneNode class. --- example/Basic/auto_instance.cpp | 18 ++---- example/Basic/draw_triangle_in_NDC.cpp | 16 +---- example/Basic/draw_triangle_use_UBO.cpp | 5 +- example/Gizmo/Gizmo3DRotate.cpp | 2 +- inc/hgl/graph/MaterialRenderList.h | 1 - inc/hgl/graph/SceneNode.h | 81 ++++--------------------- src/SceneGraph/RenderList.cpp | 12 ++-- src/SceneGraph/SceneNode.cpp | 76 +++-------------------- 8 files changed, 35 insertions(+), 176 deletions(-) diff --git a/example/Basic/auto_instance.cpp b/example/Basic/auto_instance.cpp index 53b27d60..cd7cf23d 100644 --- a/example/Basic/auto_instance.cpp +++ b/example/Basic/auto_instance.cpp @@ -2,7 +2,6 @@ #include"VulkanAppFramework.h" #include -#include #include #include #include @@ -94,7 +93,7 @@ private: rad=deg2rad((360/TRIANGLE_NUMBER)*i); //这里一定要加,否则结果用int保存会出现问题 mat=rotate(rad,Vector3f(0,0,1)); - render_root.CreateSubNode(mat,render_obj); + render_root.Add(new SceneNode(mat,render_obj)); } render_root.RefreshMatrix(); @@ -110,10 +109,10 @@ public: { SAFE_CLEAR(render_list); } - - bool Init() + + bool Init(uint w,uint h) { - if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT)) + if(!VulkanApplicationFramework::Init(w,h)) return(false); render_list=new RenderList(device); @@ -139,12 +138,5 @@ public: int main(int,char **) { - TestApp app; - - if(!app.Init()) - return(-1); - - while(app.Run()); - - return 0; + return RunApp(SCREEN_WIDTH,SCREEN_HEIGHT); } diff --git a/example/Basic/draw_triangle_in_NDC.cpp b/example/Basic/draw_triangle_in_NDC.cpp index e6c4e833..4039ae19 100644 --- a/example/Basic/draw_triangle_in_NDC.cpp +++ b/example/Basic/draw_triangle_in_NDC.cpp @@ -1,10 +1,7 @@ // 该范例主要演示使用NDC坐标系直接绘制一个渐变色的三角形 #include"VulkanAppFramework.h" -#include #include -#include -#include #include #include #include @@ -125,9 +122,9 @@ private: public: - bool Init() + bool Init(uint w,uint h) { - if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT)) + if(!VulkanApplicationFramework::Init(w,h)) return(false); InitVIL(); @@ -157,12 +154,5 @@ public: int main(int,char **) { - TestApp app; - - if(!app.Init()) - return(-1); - - while(app.Run()); - - return 0; + return RunApp(SCREEN_WIDTH,SCREEN_HEIGHT); } diff --git a/example/Basic/draw_triangle_use_UBO.cpp b/example/Basic/draw_triangle_use_UBO.cpp index 3dc06c88..1eb99b2c 100644 --- a/example/Basic/draw_triangle_use_UBO.cpp +++ b/example/Basic/draw_triangle_use_UBO.cpp @@ -1,9 +1,6 @@ // 该范例主要演示使用2D坐系统直接绘制一个渐变色的三角形,使用UBO传递Viewport信息 #include"VulkanAppFramework.h" -#include -#include -#include #include #include #include @@ -132,5 +129,5 @@ public: int main(int,char **) { - RunApp(1280,720); + return RunApp(1280,720); } diff --git a/example/Gizmo/Gizmo3DRotate.cpp b/example/Gizmo/Gizmo3DRotate.cpp index 56c80b4a..8e9598a4 100644 --- a/example/Gizmo/Gizmo3DRotate.cpp +++ b/example/Gizmo/Gizmo3DRotate.cpp @@ -63,7 +63,7 @@ bool InitGizmoRotateStaticMesh() white_torus->GetTransform().AddTransform(rotate_white_torus_tfc); - root_node->AddSubNode(white_torus); + root_node->Add(white_torus); } sm_gizmo_rotate=CreateGizmoStaticMesh(root_node); diff --git a/inc/hgl/graph/MaterialRenderList.h b/inc/hgl/graph/MaterialRenderList.h index f9134773..84991ca0 100644 --- a/inc/hgl/graph/MaterialRenderList.h +++ b/inc/hgl/graph/MaterialRenderList.h @@ -1,7 +1,6 @@ #pragma once #include #include -#include #include VK_NAMESPACE_BEGIN diff --git a/inc/hgl/graph/SceneNode.h b/inc/hgl/graph/SceneNode.h index d71b6ffa..7fdf3c90 100644 --- a/inc/hgl/graph/SceneNode.h +++ b/inc/hgl/graph/SceneNode.h @@ -10,7 +10,7 @@ namespace hgl namespace graph { using SceneNodeID =uint64; - using SceneNodeName =AnsiIDName; + using SceneNodeName =UTF16IDName; /** * 场景节点数据类
@@ -30,7 +30,7 @@ namespace hgl Renderable *render_obj=nullptr; ///<可渲染实例 - public: + protected: ObjectList SubNode; ///<子节点 @@ -39,16 +39,18 @@ namespace hgl const SceneNodeID & GetNodeID ()const { return NodeID; } ///<取得节点ID const SceneNodeName & GetNodeName ()const { return NodeName; } ///<取得节点名称 - private: + const ObjectList &GetSubNode()const { return SubNode; } ///<取得子节点列表 + + public: SceneNode()=default; - SceneNode(SceneNode *); + SceneNode(const SceneNode &)=delete; + SceneNode(const SceneNode *)=delete; + SceneNode(const SceneOrient &so ):SceneOrient(so) {} SceneNode( Renderable *ri ) {render_obj=ri;} SceneNode(const Matrix4f &mat ):SceneOrient(mat) {} SceneNode(const Matrix4f &mat, Renderable *ri ):SceneOrient(mat) {render_obj=ri;} - friend SceneNode *CreateSceneNode(const SceneNodeName &); - public: virtual ~SceneNode()=default; @@ -75,7 +77,7 @@ namespace hgl Renderable *GetRenderable(){return render_obj;} void SetRenderable(Renderable *); - SceneNode *AddSubNode(SceneNode *sn) + SceneNode *Add(SceneNode *sn) { if(!sn) return(nullptr); @@ -84,72 +86,11 @@ namespace hgl return sn; } - SceneNode *CreateSubNode() - { - SceneNode *sn=new SceneNode(); - - SubNode.Add(sn); - return sn; - } - - SceneNode *CreateSubNode(Renderable *ri) - { - if(!ri) - return(nullptr); - - SceneNode *sn=new SceneNode(ri); - - SubNode.Add(sn); - return sn; - } - - SceneNode *CreateSubNode(const Matrix4f &mat) - { - SceneNode *sn=new SceneNode(mat); - - SubNode.Add(sn); - return sn; - } - - SceneNode *CreateSubNode(const Matrix4f &mat,Renderable *ri) - { - if(!ri) - return(nullptr); - - SceneNode *sn=new SceneNode(mat,ri); - - SubNode.Add(sn); - return sn; - } - - SceneNode *CreateSubNode(SceneNode *node) - { - if(!node) - return(nullptr); - - SceneNode *sn=new SceneNode(node); - - SubNode.Add(sn); - return node; - } - - SceneNode *CreateSubNode(const Matrix4f &mat,SceneNode *node) - { - if(!node) - return(nullptr); - - SceneNode *sn=new SceneNode(mat); - sn->CreateSubNode(node); - - SubNode.Add(sn); - return sn; - } - public: //坐标相关方法 virtual void SetBoundingBox (const AABB &bb){BoundingBox=bb;} ///<设置绑定盒 - virtual void RefreshMatrix () override; ///<刷新世界变换 + virtual void RefreshMatrix () override; ///<刷新世界变换 virtual void RefreshBoundingBox (); ///<刷新绑定盒 virtual const AABB & GetBoundingBox ()const{return BoundingBox;} ///<取得绑定盒 @@ -157,7 +98,7 @@ namespace hgl // virtual const AABB & GetWorldBoundingBox ()const{return WorldBoundingBox;} ///<取得世界坐标绑定盒 };//class SceneNode - SceneNode *CreateSceneNode(const SceneNodeName &); + SceneNode *Duplication(const SceneNode *); ///<复制一个场景节点 }//namespace graph }//namespace hgl #endif//HGL_GRAPH_SCENE_NODE_INCLUDE diff --git a/src/SceneGraph/RenderList.cpp b/src/SceneGraph/RenderList.cpp index 536882de..5047651a 100644 --- a/src/SceneGraph/RenderList.cpp +++ b/src/SceneGraph/RenderList.cpp @@ -1,12 +1,10 @@ -#include +#include +#include #include -#include -#include +#include #include -#include -#include +#include #include -#include namespace hgl { @@ -43,7 +41,7 @@ namespace hgl ++renderable_count; } - for(SceneNode *sub:sn->SubNode) + for(SceneNode *sub:sn->GetSubNode()) ExpendNode(sub); return(true); diff --git a/src/SceneGraph/SceneNode.cpp b/src/SceneGraph/SceneNode.cpp index de2246a9..be7f3ba7 100644 --- a/src/SceneGraph/SceneNode.cpp +++ b/src/SceneGraph/SceneNode.cpp @@ -4,22 +4,21 @@ namespace hgl { namespace graph { - SceneNode::SceneNode(SceneNode *node):SceneOrient(*node) + SceneNode *Duplication(SceneNode *src_node) { - if(!node) - return; + if(!src_node) + return nullptr; - BoundingBox=node->BoundingBox; - LocalBoundingBox=node->LocalBoundingBox; + SceneNode *node=new SceneNode(*(SceneOrient *)src_node); - render_obj=node->render_obj; + node->SetRenderable(src_node->GetRenderable()); - for(SceneNode *sn:node->SubNode) + for(SceneNode *sn:src_node->GetSubNode()) { - SceneNode *new_sn=new SceneNode(sn); - - SubNode.Add(new_sn); + node->Add(Duplication(sn)); } + + return node; } void SceneNode::SetRenderable(Renderable *ri) @@ -89,62 +88,5 @@ namespace hgl LocalBoundingBox=local; } - - ///** - //* 从当前节点展开输出到一个渲染列表 - //* @param rl 渲染列表 - //* @param func 过滤函数 - //* @param func_data 过滤函数用辅助数据 - //* @return 成功与否 - //*/ - //bool SceneNode::ExpendToList(RenderList *rl,FilterSceneNodeFunc func,void *func_data) - //{ - // if(!rl)return(false); - - // if(func) - // if(!func(this,func_data)) - // return(false); - - // { - // int count=renderable_instances.GetCount(); - - // if(count>0) - // rl->Add(this); - // } - - // { - // int count=SubNode.GetCount(); - // SceneNode **sub=SubNode.GetData(); - - // for(int i=0;iExpendToList(rl,func,func_data); //展开子节点 - - // ++sub; - // } - // } - - // return(true); - //} - - ///** - //* 从当前节点展开输出到一个渲染列表 - //* @param rl 渲染列表 - //* @param cam 摄像机 - //* @param comp_func 渲染列表远近比较函数 - //*/ - //bool SceneNode::ExpendToList(RenderList *rl,Camera *cam,RenderListCompFunc comp_func) - //{ - // if(!rl||!cam)return(false); - - // if(!ExpendToList(rl)) - // return(false); - - // if(comp_func) - // { - // } - - // return(true); - //} }//namespace graph }//namespace hgl