From 2ab09c63675038dda916a90e442d98066da8bac3 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sat, 25 May 2019 00:50:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BRenderList=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/hgl/graph/RenderList.h | 67 +++++++++++++++++++++ inc/hgl/graph/SceneNode.h | 7 +-- src/SceneGraph/RenderList.cpp | 107 +++++++++++++++++++++++----------- src/SceneGraph/SceneNode.cpp | 9 +-- 4 files changed, 147 insertions(+), 43 deletions(-) create mode 100644 inc/hgl/graph/RenderList.h diff --git a/inc/hgl/graph/RenderList.h b/inc/hgl/graph/RenderList.h new file mode 100644 index 00000000..831840e1 --- /dev/null +++ b/inc/hgl/graph/RenderList.h @@ -0,0 +1,67 @@ +#ifndef HGL_GRAPH_RENDER_LIST_INCLUDE +#define HGL_GRAPH_RENDER_LIST_INCLUDE + +#include +#include +namespace hgl +{ + namespace graph + { + class SceneNode; + + struct UBOMatrixData + { + Matrix4f projection; + Matrix4f modelview; + Matrix4f mvp; + };// + + struct UBOSkyLight + { + Vector4f sky_color; + };// + + class RenderList + { + vulkan::CommandBuffer *cmd_buf; + + Camera camera; + + Matrix4f projection_matrix, + modelview_matrix, + mvp_matrix; + + Frustum frustum; + + private: + + List SceneNodeList; + + vulkan::Pipeline * last_pipeline; + vulkan::DescriptorSets *last_desc_sets; + vulkan::Renderable * last_renderable; + + void Render(vulkan::RenderableInstance *,const Matrix4f &); + + public: + + RenderList() + { + cmd_buf=nullptr; + last_pipeline=nullptr; + last_desc_sets=nullptr; + last_renderable=nullptr; + } + + ~RenderList()=default; + + void Add (const SceneNode *node) {if(node)SceneNodeList.Add(node);} + void Clear () {SceneNodeList.ClearData();} + + void SetCamera(const Camera &); + + bool Render(); + };//class RenderList + }//namespace graph +}//namespace hgl +#endif//HGL_GRAPH_RENDER_LIST_INCLUDE diff --git a/inc/hgl/graph/SceneNode.h b/inc/hgl/graph/SceneNode.h index 2e0aca4e..775c1c2a 100644 --- a/inc/hgl/graph/SceneNode.h +++ b/inc/hgl/graph/SceneNode.h @@ -13,9 +13,8 @@ namespace hgl class SceneNode; struct Camera; class Frustum; - - using RenderList=List; ///<渲染列表类型重定义 - + class RenderList; + using RenderListCompFunc=float (*)(Camera *,SceneNode *,SceneNode *); ///<渲染列表排序比较函数 float CameraLengthComp(Camera *,SceneNode *,SceneNode *); ///<摄像机距离比较函数 @@ -98,7 +97,7 @@ namespace hgl bool ExpendToList(RenderList *rl,Frustum *f)const ///<展开到渲染列表(使用平截头裁剪) {return ExpendToList(rl,FrustumClipFilter,f);} - bool ExpendToList(RenderList *,const Matrix4f &,const Matrix4f &,RenderListCompFunc=nullptr)const; ///<展开到渲染列表(使用平截头裁剪并排序) +// bool ExpendToList(RenderList *,const Matrix4f &,const Matrix4f &,RenderListCompFunc=nullptr)const; ///<展开到渲染列表(使用平截头裁剪并排序) bool ExpendToList(RenderList *,Camera *,RenderListCompFunc=nullptr)const; ///<展开到渲染列表(使用摄像机平截头裁剪并排序) };//class SceneNode }//namespace graph diff --git a/src/SceneGraph/RenderList.cpp b/src/SceneGraph/RenderList.cpp index 6f3841d7..ab493379 100644 --- a/src/SceneGraph/RenderList.cpp +++ b/src/SceneGraph/RenderList.cpp @@ -1,11 +1,13 @@ -#include +#include +#include #include #include +#include #include -//#include - #include +#include + namespace hgl { namespace graph @@ -26,33 +28,85 @@ namespace hgl // return (((Frustum *)fc)->BoxIn(node->GetWorldBoundingBox())!=Frustum::OUTSIDE); //} - /** - * 使用指定矩阵渲染一个渲染列表 - * @param rl 渲染列表 - * @param proj 透视矩阵 - * @param mv 视图矩阵 - * @return 是否渲染成功 - */ - bool Render(const RenderList *rl,const Matrix4f *proj,const Matrix4f *mv) + void RenderList::SetCamera(const Camera &cam) { - if(!rl) + camera=cam; + + MakeCameraMatrix( &projection_matrix, + &modelview_matrix, + &camera); + + mvp_matrix=projection_matrix*modelview_matrix; + + CameraToFrustum(&frustum, + &camera); + } + + void RenderList::Render(vulkan::RenderableInstance *ri,const Matrix4f &fin_mvp) + { + if(last_pipeline!=ri->GetPipeline()) + { + cmd_buf->Bind(ri->GetPipeline()); + + last_pipeline=ri->GetPipeline(); + + cmd_buf->Bind(ri->GetDescriptorSets()); + } + else + { + if(last_desc_sets!=ri->GetDescriptorSets()) + { + cmd_buf->Bind(ri->GetDescriptorSets()); + + last_desc_sets=ri->GetDescriptorSets(); + } + } + + //更新fin_mvp + + vulkan::Renderable *obj=ri->GetRenderable(); + + if(obj!=last_renderable) + { + cmd_buf->Bind(obj); + + last_renderable=obj; + } + + const vulkan::IndexBuffer *ib=obj->GetIndexBuffer(); + + if(ib) + { + cmd_buf->DrawIndexed(ib->GetCount()); + } + else + { + cmd_buf->Draw(obj->GetDrawCount()); + } + } + + bool RenderList::Render() + { + if(!cmd_buf) return(false); - int count=rl->GetCount(); - const SceneNode **node=rl->GetData(); + last_pipeline=nullptr; + last_desc_sets=nullptr; + last_renderable=nullptr; + + int count=SceneNodeList.GetCount(); + const SceneNode **node=SceneNodeList.GetData(); for(int i=0;iGetLocalToWorldMatrix(); + const Matrix4f fin_mv=modelview_matrix*(*node)->GetLocalToWorldMatrix(); int sn=(*node)->RenderableList.GetCount(); RenderableInstance **p=(*node)->RenderableList.GetData(); for(int j=0;j +#include //#include namespace hgl { @@ -81,12 +82,12 @@ namespace hgl return(true); } - bool SceneNode::ExpendToList(RenderList *rl,const Matrix4f &proj,const Matrix4f &mv,RenderListCompFunc comp_func)const - { - if(!rl)return(false); + //bool SceneNode::ExpendToList(RenderList *rl,const Matrix4f &proj,const Matrix4f &mv,RenderListCompFunc comp_func)const + //{ + // if(!rl)return(false); - } + //} /** * 从当前节点展开输出到一个渲染列表