改进RenderList类

This commit is contained in:
hyzboy 2019-05-25 00:50:04 +08:00
parent c3729961da
commit 2ab09c6367
4 changed files with 147 additions and 43 deletions

View File

@ -0,0 +1,67 @@
#ifndef HGL_GRAPH_RENDER_LIST_INCLUDE
#define HGL_GRAPH_RENDER_LIST_INCLUDE
#include<hgl/graph/vulkan/VK.h>
#include<hgl/graph/Camera.h>
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<const SceneNode *> 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

View File

@ -13,9 +13,8 @@ namespace hgl
class SceneNode;
struct Camera;
class Frustum;
using RenderList=List<const SceneNode *>; ///<渲染列表类型重定义
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

View File

@ -1,11 +1,13 @@
#include<hgl/graph/Camera.h>
#include<hgl/graph/RenderList.h>
#include<hgl/graph/Camera.h>
#include<hgl/graph/SceneNode.h>
#include<hgl/graph/vulkan/VKRenderable.h>
#include<hgl/graph/vulkan/VKCommandBuffer.h>
#include<hgl/graph/VertexBuffer.h>
//#include<hgl/graph/Frustum.h>
#include<hgl/math/Math.h>
#include<hgl/graph/vulkan/VKRenderableInstance.h>
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;i<count;i++)
{
const Matrix4f fin_mv=(*mv)*(*node)->GetLocalToWorldMatrix();
const Matrix4f fin_mv=modelview_matrix*(*node)->GetLocalToWorldMatrix();
int sn=(*node)->RenderableList.GetCount();
RenderableInstance **p=(*node)->RenderableList.GetData();
for(int j=0;j<sn;j++)
{
DirectRender( (*p),
proj,
&fin_mv);
Render(*p,projection_matrix*fin_mv);
p++;
}
@ -62,22 +116,5 @@ namespace hgl
return(true);
}
/**
*
* @param rl
* @param cam
* @return
*/
bool Render(const RenderList *rl,const Camera *cam)
{
if(!rl||!cam)return(false);
Matrix4f proj,mv;
MakeCameraMatrix(&proj,&mv,cam);
return Render(rl,&proj,&mv);
}
}//namespace graph
}//namespace hgl

View File

@ -1,4 +1,5 @@
#include<hgl/graph/SceneNode.h>
#include<hgl/graph/RenderList.h>
//#include<hgl/graph/Frustum.h>
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);
}
//}
/**
*