ULRE/example/Basic/rf_test.cpp

109 lines
3.0 KiB
C++
Raw Normal View History

#include<hgl/WorkManager.h>
2025-01-24 13:23:57 +08:00
#include<hgl/graph/VKVertexInputConfig.h>
#include<hgl/graph/VKRenderResource.h>
2025-01-24 13:23:57 +08:00
#include<hgl/graph/mtl/Material2DCreateConfig.h>
#include<hgl/graph/VKMaterialInstance.h>
#include<hgl/graph/mtl/MaterialLibrary.h>
2025-04-23 00:27:43 +08:00
#include"AssetData.h"
2025-01-24 13:23:57 +08:00
using namespace hgl;
using namespace hgl::graph;
constexpr uint32_t SCREEN_WIDTH=1280;
constexpr uint32_t SCREEN_HEIGHT=720;
constexpr uint32_t VERTEX_COUNT=3;
2025-01-26 10:32:53 +08:00
constexpr float position_data[VERTEX_COUNT*2]=
2025-01-24 13:23:57 +08:00
{
0.0, -0.5,
-0.5, 0.5,
0.5, 0.5
};
constexpr float color_data[VERTEX_COUNT*4]=
{
1,0,0,1,
2025-01-24 13:23:57 +08:00
0,1,0,1,
0,0,1,1
};
class TestApp:public WorkObject
{
private:
Color4f clear_color =Color4f(0.2f,0.2f,0.2f,1.0f);
2025-01-24 13:23:57 +08:00
MaterialInstance * material_instance =nullptr;
Renderable * render_obj =nullptr;
Pipeline * pipeline =nullptr;
private:
bool InitAutoMaterial()
{
mtl::Material2DCreateConfig cfg(GetDeviceAttribute(),"VertexColor2D",PrimitiveType::Triangles);
2025-01-24 13:23:57 +08:00
cfg.coordinate_system=CoordinateSystem2D::NDC;
cfg.local_to_world=false;
//AutoDelete<mtl::MaterialCreateInfo> mci=mtl::CreateVertexColor2D(&cfg); //这个是直接创建
AutoDelete<mtl::MaterialCreateInfo> mci=mtl::CreateMaterialCreateInfo("VertexColor2D",&cfg); //这个是使用名称创建
//这两种方式都可以,上一种方式肯定是会快些,主要用于一些程序中直接写死的地方。
//而下面这种方式很明显是为了可以将使用的材质写入配置文件中。
2025-01-24 13:23:57 +08:00
material_instance=CreateMaterialInstance(mci);
2025-01-24 13:23:57 +08:00
return material_instance;
}
bool InitPipeline()
{
// pipeline=db->CreatePipeline(material_instance,sc_render_target,OS_TEXT("res/pipeline/solid2d"));
pipeline=CreatePipeline(material_instance,InlinePipeline::Solid2D,PrimitiveType::Triangles); //等同上一行为Framework重载默认使用swapchain的render target
2025-01-24 13:23:57 +08:00
return pipeline;
2025-01-24 13:23:57 +08:00
}
bool InitVBO()
{
render_obj=CreateRenderable("Triangle",VERTEX_COUNT,material_instance,pipeline,
{
{VAN::Position,VF_V2F,position_data},
{VAN::Color, VF_V4F,color_data}
});
2025-01-24 13:23:57 +08:00
return(render_obj);
}
public:
2025-01-29 17:42:06 +08:00
TestApp(RenderFramework *rf):WorkObject(rf,rf->GetSwapchainRenderTarget())
{
2025-01-24 13:23:57 +08:00
if(!InitAutoMaterial())
return;
2025-01-24 13:23:57 +08:00
if(!InitPipeline())
return;
2025-01-24 13:23:57 +08:00
if(!InitVBO())
return;
2025-01-24 13:23:57 +08:00
}
void Tick(double)override{}
2025-01-25 15:25:29 +08:00
void Render(double delta_time,graph::RenderCmdBuffer *cmd)
{
cmd->SetClearColor(0,clear_color);
cmd->BeginRenderPass();
cmd->Render(render_obj);
cmd->EndRenderPass();
}
2025-01-29 16:30:00 +08:00
};//class TestApp:public WorkObject
2025-01-24 13:23:57 +08:00
2025-01-29 16:30:00 +08:00
int os_main(int,os_char **)
2025-01-24 13:23:57 +08:00
{
2025-01-29 16:30:00 +08:00
return RunFramework<TestApp>(OS_TEXT("RenderFramework Test"));
2025-01-24 13:23:57 +08:00
}