2023-09-27 11:36:39 +08:00
|
|
|
|
// 该范例主要演示使用RenderList系统绘制多个三角形,并利用RenderList进行排序以及自动合并进行Instance渲染
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
|
|
|
|
#include"VulkanAppFramework.h"
|
|
|
|
|
#include<hgl/math/Math.h>
|
|
|
|
|
#include<hgl/filesystem/FileSystem.h>
|
2023-03-22 19:41:06 +08:00
|
|
|
|
#include<hgl/graph/VKRenderablePrimitiveCreater.h>
|
2023-08-18 18:39:25 +08:00
|
|
|
|
#include<hgl/graph/VKVertexInputConfig.h>
|
2023-06-02 20:45:19 +08:00
|
|
|
|
#include<hgl/graph/mtl/2d/Material2DCreateConfig.h>
|
2023-05-07 01:07:26 +08:00
|
|
|
|
#include<hgl/graph/RenderList.h>
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
2023-05-07 01:07:26 +08:00
|
|
|
|
constexpr uint32_t SCREEN_WIDTH=1024;
|
|
|
|
|
constexpr uint32_t SCREEN_HEIGHT=1024;
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
|
|
|
|
constexpr uint32_t VERTEX_COUNT=3;
|
|
|
|
|
|
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]=
|
|
|
|
|
{ {255,0,0,255},
|
|
|
|
|
{0,255,0,255},
|
|
|
|
|
{0,0,255,255}
|
2022-06-24 16:27:43 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class TestApp:public VulkanApplicationFramework
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
SceneNode render_root;
|
2023-05-07 01:07:26 +08:00
|
|
|
|
RenderList * render_list =nullptr;
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2023-09-26 21:49:37 +08:00
|
|
|
|
mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"VertexColor2D",Prim::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"));
|
|
|
|
|
pipeline=CreatePipeline(material_instance,InlinePipeline::Solid2D,Prim::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()
|
|
|
|
|
{
|
2023-03-22 19:41:06 +08:00
|
|
|
|
RenderablePrimitiveCreater rpc(db,VERTEX_COUNT);
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
2023-08-18 18:39:25 +08:00
|
|
|
|
if(!rpc.SetVBO(VAN::Position, VF_V2F, position_data))return(false);
|
|
|
|
|
if(!rpc.SetVBO(VAN::Color, VF_V4UN8, color_data ))return(false);
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
2023-03-22 19:41:06 +08:00
|
|
|
|
render_obj=rpc.Create(material_instance,pipeline);
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
2023-02-13 11:48:53 +08:00
|
|
|
|
if(!render_obj)
|
|
|
|
|
return(false);
|
2023-05-07 00:42:06 +08:00
|
|
|
|
|
|
|
|
|
for(uint i=0;i<12;i++)
|
|
|
|
|
render_root.CreateSubNode(rotate(deg2rad(30*i),Vector3f(0,0,1)),render_obj);
|
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:
|
|
|
|
|
|
|
|
|
|
~TestApp()
|
|
|
|
|
{
|
|
|
|
|
SAFE_CLEAR(render_list);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Init()
|
|
|
|
|
{
|
|
|
|
|
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
|
|
|
|
|
return(false);
|
|
|
|
|
|
2023-05-07 01:07:26 +08:00
|
|
|
|
render_list=new RenderList(device);
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
|
|
|
|
if(!InitMaterial())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitVBO())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
BuildCommandBuffer(render_list);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Resize(int w,int h)override
|
|
|
|
|
{
|
|
|
|
|
VulkanApplicationFramework::Resize(w,h);
|
|
|
|
|
|
|
|
|
|
BuildCommandBuffer(render_list);
|
|
|
|
|
}
|
|
|
|
|
};//class TestApp:public VulkanApplicationFramework
|
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
|
|
|
|
TestApp app;
|
|
|
|
|
|
|
|
|
|
if(!app.Init())
|
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
|
|
while(app.Run());
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|