ULRE/example/Basic/draw_triangle_use_UBO.cpp

116 lines
3.3 KiB
C++
Raw Normal View History

2023-09-27 11:36:39 +08:00
// 该范例主要演示使用2D坐系统直接绘制一个渐变色的三角形,使用UBO传递Viewport信息
2020-12-02 20:27:36 +08:00
2025-02-01 16:32:08 +08:00
#include<hgl/WorkManager.h>
#include<hgl/graph/VKVertexInputConfig.h>
2024-05-31 23:07:05 +08:00
#include<hgl/graph/PrimitiveCreater.h>
#include<hgl/graph/mtl/Material2DCreateConfig.h>
2020-12-02 20:27:36 +08:00
using namespace hgl;
using namespace hgl::graph;
constexpr uint32_t VERTEX_COUNT=3;
static float position_data_float[VERTEX_COUNT][2]=
2020-12-02 20:27:36 +08:00
{
{0.5, 0.25},
{0.75, 0.75},
{0.25, 0.75}
2020-12-02 20:27:36 +08:00
};
2025-03-12 02:08:20 +08:00
static uint16 position_data[VERTEX_COUNT][2]={};
constexpr uint8 color_data[VERTEX_COUNT*4]=
{
255,0,0,255,
0,255,0,255,
0,0,255,255
2020-12-02 20:27:36 +08:00
};
2025-02-01 16:32:08 +08:00
class TestApp:public WorkObject
2020-12-02 20:27:36 +08:00
{
private:
2025-02-01 16:32:08 +08:00
Color4f clear_color =Color4f(0.2f,0.2f,0.2f,1.0f);
MaterialInstance * material_instance =nullptr;
Renderable * render_obj =nullptr;
2020-12-02 20:27:36 +08:00
Pipeline * pipeline =nullptr;
private:
bool InitMaterial()
{
2025-02-01 16:32:08 +08:00
mtl::Material2DCreateConfig cfg(GetDeviceAttribute(),"VertexColor2D",Prim::Triangles);
VILConfig vil_config;
cfg.coordinate_system=CoordinateSystem2D::Ortho;
cfg.position_format =VAT_UVEC2; //这里指定shader中使用uvec2当做顶点输入格式
// ^
// + 这上下两种格式要配套,否则会出错
// v
2024-04-24 01:44:01 +08:00
vil_config.Add(VAN::Position,VF_V2U16); //这里指定VAB中使用RG16U当做顶点数据格式
2023-03-22 15:58:59 +08:00
2024-04-24 01:44:01 +08:00
vil_config.Add(VAN::Color,VF_V4UN8); //这里指定VAB中使用RGBA8UNorm当做颜色数据格式
cfg.local_to_world=false;
AutoDelete<mtl::MaterialCreateInfo> mci=mtl::CreateVertexColor2D(&cfg);
2023-03-22 15:58:59 +08:00
material_instance=db->CreateMaterialInstance(mci,&vil_config);
2023-03-22 15:58:59 +08:00
2020-12-02 20:27:36 +08:00
if(!material_instance)
return(false);
2021-09-22 17:22:17 +08:00
// pipeline=db->CreatePipeline(material_instance,sc_render_target,OS_TEXT("res/pipeline/solid2d"));
pipeline=CreatePipeline(material_instance,InlinePipeline::Solid2D,Prim::Triangles); //等同上一行为Framework重载默认使用swapchain的render target
2022-06-24 16:27:43 +08:00
2021-09-22 17:22:17 +08:00
return pipeline;
2020-12-02 20:27:36 +08:00
}
2022-06-24 16:27:43 +08:00
2020-12-02 20:27:36 +08:00
bool InitVBO()
{
const auto ext=GetExtent2D();
for(uint i=0;i<VERTEX_COUNT;i++)
{
position_data[i][0]=position_data_float[i][0]*ext.width;
position_data[i][1]=position_data_float[i][1]*ext.height;
}
2025-02-01 16:32:08 +08:00
render_obj=CreateRenderable("Triangle",VERTEX_COUNT,material_instance,pipeline,
{
2025-03-12 02:08:20 +08:00
{VAN::Position,VF_V2U16,position_data},
{VAN::Color, VF_V4UN8,color_data}
2025-02-01 16:32:08 +08:00
});
return(render_obj);
2020-12-02 20:27:36 +08:00
}
public:
2025-02-01 16:32:08 +08:00
TestApp(RenderFramework *rf):WorkObject(rf,rf->GetSwapchainRenderTarget())
2020-12-02 20:27:36 +08:00
{
if(!InitMaterial())
2025-02-01 16:32:08 +08:00
return;
2020-12-02 20:27:36 +08:00
if(!InitVBO())
2025-02-01 16:32:08 +08:00
return;
2020-12-02 20:27:36 +08:00
}
void Render(double delta_time,graph::RenderCmdBuffer *cmd)override
2020-12-02 20:27:36 +08:00
{
2025-02-01 16:32:08 +08:00
cmd->SetClearColor(0,clear_color);
2020-12-02 20:27:36 +08:00
2025-02-01 16:32:08 +08:00
cmd->BeginRenderPass();
cmd->Render(render_obj);
cmd->EndRenderPass();
2020-12-02 20:27:36 +08:00
}
2025-02-01 16:32:08 +08:00
};//class TestApp:public WorkObject
2020-12-02 20:27:36 +08:00
2025-02-01 16:32:08 +08:00
int os_main(int,os_char **)
2020-12-02 20:27:36 +08:00
{
2025-02-01 16:32:08 +08:00
return RunFramework<TestApp>(OS_TEXT("Draw triangle use UBO"));
2020-12-02 20:27:36 +08:00
}