2023-09-27 11:36:39 +08:00
|
|
|
|
// 该范例主要演示使用RenderList系统绘制多个三角形,并利用RenderList进行排序以及自动合并进行Instance渲染
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
2025-03-13 23:41:51 +08:00
|
|
|
|
#include<hgl/WorkManager.h>
|
2022-06-24 16:27:43 +08:00
|
|
|
|
#include<hgl/math/Math.h>
|
2024-05-25 22:47:26 +08:00
|
|
|
|
#include<hgl/graph/PrimitiveCreater.h>
|
2023-08-18 18:39:25 +08:00
|
|
|
|
#include<hgl/graph/VKVertexInputConfig.h>
|
2023-10-07 20:59:44 +08:00
|
|
|
|
#include<hgl/graph/mtl/Material2DCreateConfig.h>
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
|
|
|
|
constexpr uint32_t VERTEX_COUNT=3;
|
|
|
|
|
|
2024-05-25 22:47:26 +08:00
|
|
|
|
constexpr uint32_t TRIANGLE_NUMBER=12;
|
|
|
|
|
|
2023-05-07 00:42:06 +08:00
|
|
|
|
constexpr float position_data[VERTEX_COUNT*2]=
|
2022-06-24 16:27:43 +08:00
|
|
|
|
{
|
2023-05-07 00:42:06 +08:00
|
|
|
|
0.0, 0.0,
|
|
|
|
|
-0.1, 0.9,
|
|
|
|
|
0.1, 0.9
|
2022-06-24 16:27:43 +08:00
|
|
|
|
};
|
|
|
|
|
|
2023-08-18 18:39:25 +08:00
|
|
|
|
constexpr uint8 color_data[VERTEX_COUNT][4]=
|
2024-04-23 03:32:25 +08:00
|
|
|
|
{
|
|
|
|
|
{255,0,0,255},
|
2023-08-18 18:39:25 +08:00
|
|
|
|
{0,255,0,255},
|
|
|
|
|
{0,0,255,255}
|
2022-06-24 16:27:43 +08:00
|
|
|
|
};
|
|
|
|
|
|
2025-03-13 23:41:51 +08:00
|
|
|
|
class TestApp:public WorkObject
|
2022-06-24 16:27:43 +08:00
|
|
|
|
{
|
2025-03-13 23:41:51 +08:00
|
|
|
|
Color4f clear_color =Color4f(0.2f,0.2f,0.2f,1.0f);
|
|
|
|
|
|
2022-06-24 16:27:43 +08:00
|
|
|
|
private:
|
|
|
|
|
|
2025-03-13 23:56:43 +08:00
|
|
|
|
AutoDelete<RenderList> render_list =nullptr;
|
|
|
|
|
|
2022-06-24 16:27:43 +08:00
|
|
|
|
SceneNode render_root;
|
|
|
|
|
|
|
|
|
|
MaterialInstance * material_instance =nullptr;
|
2022-06-24 21:17:28 +08:00
|
|
|
|
Renderable * render_obj =nullptr;
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
|
|
|
|
Pipeline * pipeline =nullptr;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
bool InitMaterial()
|
|
|
|
|
{
|
2023-05-06 22:19:50 +08:00
|
|
|
|
{
|
2025-05-17 20:26:36 +08:00
|
|
|
|
mtl::Material2DCreateConfig cfg(GetDevAttr(),"VertexColor2D",PrimitiveType::Triangles);
|
2023-03-22 19:41:06 +08:00
|
|
|
|
|
2023-05-16 15:21:32 +08:00
|
|
|
|
cfg.coordinate_system=CoordinateSystem2D::NDC;
|
2023-05-06 22:19:50 +08:00
|
|
|
|
cfg.local_to_world=true;
|
|
|
|
|
|
2023-05-16 15:21:32 +08:00
|
|
|
|
AutoDelete<mtl::MaterialCreateInfo> mci=mtl::CreateVertexColor2D(&cfg);
|
2023-05-06 22:19:50 +08:00
|
|
|
|
|
2023-08-18 18:39:25 +08:00
|
|
|
|
VILConfig vil_config;
|
|
|
|
|
|
|
|
|
|
vil_config.Add(VAN::Color,VF_V4UN8);
|
|
|
|
|
|
|
|
|
|
material_instance=db->CreateMaterialInstance(mci,&vil_config);
|
2023-05-06 22:19:50 +08:00
|
|
|
|
}
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
|
|
|
|
if(!material_instance)
|
|
|
|
|
return(false);
|
2023-08-18 18:39:25 +08:00
|
|
|
|
|
2022-06-24 16:27:43 +08:00
|
|
|
|
// pipeline=db->CreatePipeline(material_instance,sc_render_target,OS_TEXT("res/pipeline/solid2d"));
|
2025-05-04 23:36:28 +08:00
|
|
|
|
pipeline=CreatePipeline(material_instance,InlinePipeline::Solid2D,PrimitiveType::Triangles); //等同上一行,为Framework重载,默认使用swapchain的render target
|
2023-08-18 18:39:25 +08:00
|
|
|
|
|
2022-06-24 16:27:43 +08:00
|
|
|
|
return pipeline;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitVBO()
|
|
|
|
|
{
|
2025-03-13 23:41:51 +08:00
|
|
|
|
render_obj=CreateRenderable("Triangle",VERTEX_COUNT,material_instance,pipeline,
|
|
|
|
|
{
|
|
|
|
|
{VAN::Position, VF_V2F, position_data},
|
|
|
|
|
{VAN::Color, VF_V4UN8, color_data }
|
|
|
|
|
});
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
2023-02-13 11:48:53 +08:00
|
|
|
|
if(!render_obj)
|
|
|
|
|
return(false);
|
2024-05-25 22:47:26 +08:00
|
|
|
|
|
|
|
|
|
double rad;
|
|
|
|
|
Matrix4f mat;
|
2023-05-07 00:42:06 +08:00
|
|
|
|
|
2024-05-25 22:47:26 +08:00
|
|
|
|
for(uint i=0;i<TRIANGLE_NUMBER;i++)
|
|
|
|
|
{
|
2025-03-13 23:41:51 +08:00
|
|
|
|
rad=deg2rad<double>((360.0f/double(TRIANGLE_NUMBER))*i); //这里一定要加<float>或<float>,否则结果用int保存会出现问题
|
2024-05-25 22:47:26 +08:00
|
|
|
|
mat=rotate(rad,Vector3f(0,0,1));
|
|
|
|
|
|
2024-10-06 02:30:59 +08:00
|
|
|
|
render_root.Add(new SceneNode(mat,render_obj));
|
2024-05-25 22:47:26 +08:00
|
|
|
|
}
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
|
|
|
|
render_root.RefreshMatrix();
|
|
|
|
|
|
2023-03-24 22:14:43 +08:00
|
|
|
|
render_list->Expend(&render_root);
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2025-03-13 23:41:51 +08:00
|
|
|
|
TestApp(RenderFramework *rf):WorkObject(rf,rf->GetSwapchainRenderTarget())
|
2022-06-24 16:27:43 +08:00
|
|
|
|
{
|
2025-03-13 23:41:51 +08:00
|
|
|
|
render_list=rf->CreateRenderList();
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
|
|
|
|
if(!InitMaterial())
|
2025-03-13 23:41:51 +08:00
|
|
|
|
return;
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
|
|
|
|
if(!InitVBO())
|
2025-03-13 23:41:51 +08:00
|
|
|
|
return;
|
2022-06-24 16:27:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 23:41:51 +08:00
|
|
|
|
void Render(double delta_time,graph::RenderCmdBuffer *cmd)override
|
2022-06-24 16:27:43 +08:00
|
|
|
|
{
|
2025-03-13 23:41:51 +08:00
|
|
|
|
cmd->SetClearColor(0,clear_color);
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
2025-03-13 23:41:51 +08:00
|
|
|
|
cmd->BeginRenderPass();
|
|
|
|
|
render_list->Render(cmd);
|
|
|
|
|
cmd->EndRenderPass();
|
2022-06-24 16:27:43 +08:00
|
|
|
|
}
|
2025-03-13 23:41:51 +08:00
|
|
|
|
};//class TestApp:public WorkObject
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
2025-03-13 23:41:51 +08:00
|
|
|
|
int os_main(int,os_char **)
|
2022-06-24 16:27:43 +08:00
|
|
|
|
{
|
2025-03-13 23:41:51 +08:00
|
|
|
|
return RunFramework<TestApp>(OS_TEXT("AutoInstance"),1024,1024);
|
2022-06-24 16:27:43 +08:00
|
|
|
|
}
|