ULRE/src/SceneGraph/RenderList.cpp

112 lines
3.0 KiB
C++
Raw Normal View History

2019-05-25 00:50:04 +08:00
#include<hgl/graph/RenderList.h>
#include<hgl/graph/Camera.h>
2019-05-21 21:28:33 +08:00
#include<hgl/graph/SceneNode.h>
2019-05-24 21:43:59 +08:00
#include<hgl/graph/vulkan/VKRenderable.h>
2019-05-25 00:50:04 +08:00
#include<hgl/graph/vulkan/VKCommandBuffer.h>
2019-05-21 21:28:33 +08:00
#include<hgl/graph/VertexBuffer.h>
#include<hgl/graph/RenderableNode.h>
2019-05-25 00:50:04 +08:00
2019-05-21 21:28:33 +08:00
namespace hgl
{
namespace graph
{
2019-05-24 21:43:59 +08:00
float CameraLengthComp(Camera *cam,SceneNode *obj_one,SceneNode *obj_two)
2019-05-21 21:28:33 +08:00
{
if(!cam||!obj_one||!obj_two)
return(0);
2019-05-24 21:43:59 +08:00
return( length_squared(obj_one->GetCenter(),cam->eye)-
2019-05-21 21:28:33 +08:00
length_squared(obj_two->GetCenter(),cam->eye));
}
2019-05-24 21:43:59 +08:00
//bool FrustumClipFilter(const SceneNode *node,void *fc)
//{
// if(!node||!fc)return(false);
2019-05-21 21:28:33 +08:00
2019-05-24 21:43:59 +08:00
// return (((Frustum *)fc)->BoxIn(node->GetWorldBoundingBox())!=Frustum::OUTSIDE);
//}
2019-05-21 21:28:33 +08:00
2019-05-25 00:50:04 +08:00
void RenderList::SetCamera(const Camera &cam)
{
camera=cam;
2019-05-25 01:06:33 +08:00
MakeCameraMatrix( &ubo_matrix.projection,
&ubo_matrix.modelview,
2019-05-25 00:50:04 +08:00
&camera);
2019-05-25 01:06:33 +08:00
ubo_matrix.mvp =ubo_matrix.projection*ubo_matrix.modelview;
ubo_matrix.normal =ubo_matrix.modelview.Float3x3Part(); //法线矩阵为3x3
2019-05-25 00:50:04 +08:00
CameraToFrustum(&frustum,
&camera);
}
void RenderList::Render(RenderableNode *ri,const Matrix4f &fin_mvp)
2019-05-21 21:28:33 +08:00
{
2019-05-25 00:50:04 +08:00
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)
2019-05-21 21:28:33 +08:00
return(false);
2019-05-25 00:50:04 +08:00
last_pipeline=nullptr;
last_desc_sets=nullptr;
last_renderable=nullptr;
const int count=renderable_node_list.GetCount();
RenderableNode **node=renderable_node_list.GetData();
2019-05-21 21:28:33 +08:00
for(int i=0;i<count;i++)
{
2019-05-25 01:06:33 +08:00
const Matrix4f fin_mv=ubo_matrix.modelview*(*node)->GetLocalToWorldMatrix();
2019-05-21 21:28:33 +08:00
Render(*node,ubo_matrix.projection*fin_mv);
2019-05-21 21:28:33 +08:00
node++;
}
return(true);
}
}//namespace graph
}//namespace hgl