198 lines
7.0 KiB
C++
198 lines
7.0 KiB
C++
#pragma once
|
||
#include<hgl/type/object/TickObject.h>
|
||
#include<hgl/graph/RenderFramework.h>
|
||
#include<hgl/graph/VKRenderResource.h>
|
||
#include<hgl/graph/mtl/MaterialLibrary.h>
|
||
#include<hgl/graph/Renderer.h>
|
||
#include<hgl/graph/Scene.h>
|
||
#include<hgl/Time.h>
|
||
//#include<iostream>
|
||
|
||
namespace hgl
|
||
{
|
||
namespace graph::mtl
|
||
{
|
||
class MaterialCreateInfo;
|
||
}
|
||
|
||
/**
|
||
* 工作对象</p>
|
||
*
|
||
* WorkObject被定义为工作对象,所有的渲染控制都需要被写在WorkObject的Render函数下。
|
||
*/
|
||
class WorkObject:public TickObject
|
||
{
|
||
graph::RenderFramework *render_framework=nullptr;
|
||
|
||
bool destroy_flag=false;
|
||
bool render_dirty=true;
|
||
|
||
protected:
|
||
|
||
//以下数据均取自RenderFramework
|
||
|
||
graph::RenderResource *db=nullptr; //暂时的,未来会被更好的机制替代
|
||
|
||
graph::Scene * scene=nullptr; //场景
|
||
graph::Renderer * renderer=nullptr; //渲染器
|
||
|
||
public:
|
||
|
||
graph::RenderFramework * GetRenderFramework (){return render_framework;}
|
||
graph::VulkanDevice * GetDevice (){return render_framework->GetDevice();}
|
||
graph::VulkanDevAttr * GetDevAttr (){return render_framework->GetDevAttr();}
|
||
graph::TextureManager * GetTextureManager (){return render_framework->GetTextureManager();}
|
||
|
||
const VkExtent2D & GetExtent (){return renderer->GetExtent();}
|
||
|
||
graph::Scene * GetScene (){return scene;}
|
||
graph::SceneNode * GetSceneRoot (){return scene->GetRootNode();}
|
||
graph::Renderer * GetRenderer (){return renderer;}
|
||
graph::Camera * GetCamera (){return renderer->GetCamera();}
|
||
graph::CameraControl * GetCameraControl (){return render_framework->GetDefaultCameraControl();}
|
||
|
||
bool GetMouseCoord (Vector2i *mc)const{return render_framework->GetMouseCoord(mc);}
|
||
|
||
public:
|
||
|
||
const bool IsDestroy ()const{return destroy_flag;}
|
||
void MarkDestory(){destroy_flag=true;}
|
||
|
||
const bool IsRenderDirty ()const{return render_dirty;}
|
||
void MarkRenderDirty(){render_dirty=true;}
|
||
|
||
public:
|
||
|
||
WorkObject(graph::RenderFramework *,graph::Renderer *r=nullptr);
|
||
virtual ~WorkObject()=default;
|
||
|
||
virtual bool Init()=0;
|
||
|
||
virtual void OnRendererChange(graph::RenderFramework *rf,graph::Renderer *r);
|
||
|
||
virtual void OnResize(const VkExtent2D &){}
|
||
|
||
virtual void Tick(double){}
|
||
|
||
virtual void Render(double delta_time);
|
||
|
||
public:
|
||
|
||
template<typename ...ARGS>
|
||
graph::Pipeline *CreatePipeline(ARGS...args)
|
||
{
|
||
return renderer->GetRenderPass()->CreatePipeline(args...);
|
||
}
|
||
|
||
graph::MaterialInstance *CreateMaterialInstance(const AnsiString &mi_name,const graph::mtl::MaterialCreateInfo *mci,const graph::VILConfig *vil_cfg=nullptr)
|
||
{
|
||
return db->CreateMaterialInstance(mi_name,mci,vil_cfg);
|
||
}
|
||
|
||
graph::MaterialInstance *CreateMaterialInstance(const AnsiString &mtl_name,graph::mtl::MaterialCreateConfig *mtl_cfg,const graph::VILConfig *vil_cfg=nullptr)
|
||
{
|
||
AutoDelete<graph::mtl::MaterialCreateInfo> mci=graph::mtl::CreateMaterialCreateInfo(GetDevAttr(),mtl_name,mtl_cfg);
|
||
|
||
return db->CreateMaterialInstance(mtl_name,mci,vil_cfg);
|
||
}
|
||
|
||
AutoDelete<graph::PrimitiveCreater> GetPrimitiveCreater(graph::Material *mtl)
|
||
{
|
||
if(!mtl)
|
||
return(nullptr);
|
||
|
||
return(new graph::PrimitiveCreater(GetDevice(),mtl->GetDefaultVIL()));
|
||
}
|
||
|
||
AutoDelete<graph::PrimitiveCreater> GetPrimitiveCreater(graph::MaterialInstance *mi)
|
||
{
|
||
if(!mi)
|
||
return(nullptr);
|
||
|
||
return(new graph::PrimitiveCreater(GetDevice(),mi->GetVIL()));
|
||
}
|
||
|
||
graph::Primitive *CreatePrimitive( const AnsiString &name,
|
||
const uint32_t vertices_count,
|
||
const graph::VIL *vil,
|
||
const std::initializer_list<graph::VertexAttribDataPtr> &vad_list);
|
||
|
||
graph::Mesh *CreateMesh(const AnsiString &name,
|
||
const uint32_t vertices_count,
|
||
graph::MaterialInstance *mi,
|
||
graph::Pipeline *pipeline,
|
||
const std::initializer_list<graph::VertexAttribDataPtr> &vad_list);
|
||
|
||
public: //Component 相关
|
||
|
||
template<typename C,typename ...ARGS>
|
||
inline C *CreateComponent(ARGS...args)
|
||
{
|
||
auto manager=C::GetDefaultManager(); //取得默认管理器
|
||
|
||
if(!manager)
|
||
{
|
||
// LOG_ERROR(OS_TEXT("CreateComponent failed, no default manager!"));
|
||
return(nullptr);
|
||
}
|
||
|
||
return manager->CreateComponent(args...); //创建组件
|
||
}
|
||
|
||
template<typename C,typename ...ARGS>
|
||
inline C *CreateComponent(graph::SceneNode *parent_node,ARGS...args)
|
||
{
|
||
if(!parent_node)
|
||
{
|
||
// LOG_ERROR(OS_TEXT("CreateComponent failed, parent node is null!"));
|
||
return(nullptr);
|
||
}
|
||
|
||
C *c=this->CreateComponent<C>(args...); //创建组件
|
||
|
||
if(!c)
|
||
{
|
||
// LOG_ERROR(OS_TEXT("CreateComponent failed, create component failed!"));
|
||
return(nullptr);
|
||
}
|
||
|
||
parent_node->AttachComponent(c); //将组件附加到父节点
|
||
|
||
return c;
|
||
}
|
||
|
||
template<typename C,typename ...ARGS>
|
||
inline C *CreateComponent(const graph::Matrix4f &mat,graph::SceneNode *parent_node,ARGS...args)
|
||
{
|
||
if(!parent_node)
|
||
{
|
||
// LOG_ERROR(OS_TEXT("CreateComponent failed, parent node is null!"));
|
||
return(nullptr);
|
||
}
|
||
|
||
C *c=this->CreateComponent<C>(args...); //创建组件
|
||
|
||
if(!c)
|
||
{
|
||
// LOG_ERROR(OS_TEXT("CreateComponent failed, create component failed!"));
|
||
return(nullptr);
|
||
}
|
||
|
||
parent_node->AttachComponent(c); //将组件附加到父节点
|
||
|
||
c->graph::SceneOrient::SetLocalMatrix(mat);
|
||
|
||
return c;
|
||
}
|
||
};//class WorkObject
|
||
|
||
/**
|
||
* 但我们认为游戏开发者不应该关注如何控制渲染,而应该关注如何处理游戏逻辑.
|
||
* 所以我们在WorkObject的基础上再提供RenderWorkObject派生类,用于直接封装好的渲染场景树控制。
|
||
*
|
||
* 开发者仅需要将要渲染的物件放置于场景树即可。
|
||
|
||
* 但开发者也可以直接使用WorkObject,自行管理这些事。
|
||
* */
|
||
}//namespcae hgl
|