ULRE/example/Vulkan/third_triangle.cpp

134 lines
3.0 KiB
C++
Raw Normal View History

2022-06-24 16:27:43 +08:00
// second_triangle
// 该范例主要演示使用场景树系统绘制三角形
#include"VulkanAppFramework.h"
#include<hgl/math/Math.h>
#include<hgl/filesystem/FileSystem.h>
#include<hgl/graph/VKRenderablePrimitiveCreater.h>
#include<hgl/graph/mtl/2d/VertexColor2D.h>
2023-03-24 22:14:43 +08:00
#include<hgl/graph/RenderList2D.h>
2022-06-24 16:27:43 +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;
constexpr float position_data[VERTEX_COUNT][2]=
{
{0.5, 0.25},
{0.25, 0.75},
{0.75, 0.75}
2022-06-24 16:27:43 +08:00
};
constexpr float color_data[VERTEX_COUNT][4]=
{ {1,0,0,1},
{0,1,0,1},
{0,0,1,1}
};
class TestApp:public VulkanApplicationFramework
{
private:
SceneNode render_root;
2023-03-24 22:14:43 +08:00
RenderList2D * render_list =nullptr;
2022-06-24 16:27:43 +08:00
MaterialInstance * material_instance =nullptr;
Renderable * render_obj =nullptr;
2022-06-24 16:27:43 +08:00
Pipeline * pipeline =nullptr;
private:
bool InitMaterial()
{
{
mtl::Material2DConfig cfg;
cfg.coordinate_system=mtl::CoordinateSystem2D::ZeroToOne;
cfg.local_to_world=true;
AutoDelete<MaterialCreateInfo> mci=mtl::CreateVertexColor2D(&cfg);
material_instance=db->CreateMaterialInstance(mci);
}
2022-06-24 16:27:43 +08:00
if(!material_instance)
return(false);
// 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
return pipeline;
}
bool InitVBO()
{
RenderablePrimitiveCreater rpc(db,VERTEX_COUNT);
2022-06-24 16:27:43 +08:00
if(!rpc.SetVBO(VAN::Position, VF_V2F, position_data))return(false);
if(!rpc.SetVBO(VAN::Color, VF_V4F, color_data ))return(false);
2022-06-24 16:27:43 +08:00
render_obj=rpc.Create(material_instance,pipeline);
2022-06-24 16:27:43 +08:00
if(!render_obj)
return(false);
render_root.CreateSubNode(translate(-0.25,0),render_obj);
render_root.CreateSubNode(translate( 0.25,0),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-03-24 22:14:43 +08:00
render_list=new RenderList2D(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;
}