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>
|
2019-05-27 16:54:08 +08:00
|
|
|
|
#include<hgl/graph/RenderableInstance.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);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-27 16:54:08 +08:00
|
|
|
|
void RenderList::SetMVP(const Matrix4f &proj,const Matrix4f &mv)
|
|
|
|
|
{
|
|
|
|
|
ubo_matrix.projection =proj;
|
|
|
|
|
ubo_matrix.modelview =mv;
|
|
|
|
|
ubo_matrix.mvp =proj*mv;
|
|
|
|
|
ubo_matrix.normal =ubo_matrix.modelview.Float3x3Part(); //法线矩阵为3x3
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderList::Render(RenderableInstance *ri)
|
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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-27 16:54:08 +08:00
|
|
|
|
void RenderList::Render(List<RenderableInstance *> &ri_list)
|
2019-05-25 00:50:04 +08:00
|
|
|
|
{
|
2019-05-27 16:54:08 +08:00
|
|
|
|
const int count=ri_list.GetCount();
|
|
|
|
|
RenderableInstance **ri=ri_list.GetData();
|
|
|
|
|
|
|
|
|
|
for(int i=0;i<count;i++)
|
|
|
|
|
{
|
|
|
|
|
Render(*ri);
|
|
|
|
|
++ri;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RenderList::Render(vulkan::CommandBuffer *cb)
|
|
|
|
|
{
|
|
|
|
|
if(!cb)
|
2019-05-21 21:28:33 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2019-05-27 16:54:08 +08:00
|
|
|
|
cmd_buf=cb;
|
|
|
|
|
|
2019-05-25 00:50:04 +08:00
|
|
|
|
last_pipeline=nullptr;
|
|
|
|
|
last_desc_sets=nullptr;
|
|
|
|
|
last_renderable=nullptr;
|
|
|
|
|
|
2019-05-27 16:54:08 +08:00
|
|
|
|
const int count=scene_node_list.GetCount();
|
|
|
|
|
SceneNode **node=scene_node_list.GetData();
|
2019-05-21 21:28:33 +08:00
|
|
|
|
|
|
|
|
|
for(int i=0;i<count;i++)
|
|
|
|
|
{
|
2019-05-27 16:54:08 +08:00
|
|
|
|
Render((*node)->renderable_instances);
|
|
|
|
|
++node;
|
2019-05-21 21:28:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
}//namespace graph
|
|
|
|
|
}//namespace hgl
|