2020-12-02 20:27:36 +08:00
|
|
|
|
// 1.two_triangle
|
|
|
|
|
// 该范例主要演示使用CmdBindDescriptorSets偏移UBO绘图
|
|
|
|
|
|
|
|
|
|
#include"VulkanAppFramework.h"
|
|
|
|
|
#include<hgl/math/Math.h>
|
|
|
|
|
#include<hgl/filesystem/FileSystem.h>
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2022-02-22 20:53:39 +08:00
|
|
|
|
constexpr float position_data[VERTEX_COUNT][2]=
|
2020-12-02 20:27:36 +08:00
|
|
|
|
{
|
|
|
|
|
{SCREEN_WIDTH*0.5, SCREEN_HEIGHT*0.25},
|
|
|
|
|
{SCREEN_WIDTH*0.75, SCREEN_HEIGHT*0.75},
|
|
|
|
|
{SCREEN_WIDTH*0.25, SCREEN_HEIGHT*0.75}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constexpr float color_data[VERTEX_COUNT][4]=
|
|
|
|
|
{ {1,0,0,1},
|
|
|
|
|
{0,1,0,1},
|
|
|
|
|
{0,0,1,1}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class TestApp:public VulkanApplicationFramework
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
Camera cam;
|
|
|
|
|
|
2020-12-09 21:33:29 +08:00
|
|
|
|
SceneNode render_root;
|
2021-06-19 20:42:44 +08:00
|
|
|
|
RenderList * render_list =nullptr;
|
2020-12-09 21:33:29 +08:00
|
|
|
|
|
2021-06-16 21:03:52 +08:00
|
|
|
|
MaterialInstance * material_instance =nullptr;
|
2020-12-02 20:27:36 +08:00
|
|
|
|
RenderableInstance *render_instance =nullptr;
|
2021-05-28 21:24:48 +08:00
|
|
|
|
GPUBuffer * ubo_camera_info =nullptr;
|
2020-12-02 20:27:36 +08:00
|
|
|
|
|
|
|
|
|
Pipeline * pipeline =nullptr;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
bool InitMaterial()
|
|
|
|
|
{
|
|
|
|
|
material_instance=db->CreateMaterialInstance(OS_TEXT("res/material/VertexColor2D"));
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
return pipeline;
|
2020-12-02 20:27:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitUBO()
|
|
|
|
|
{
|
|
|
|
|
const VkExtent2D extent=sc_render_target->GetExtent();
|
|
|
|
|
|
|
|
|
|
cam.width=extent.width;
|
|
|
|
|
cam.height=extent.height;
|
|
|
|
|
|
2022-03-09 20:33:26 +08:00
|
|
|
|
cam.RefreshCameraInfo();
|
2020-12-02 20:27:36 +08:00
|
|
|
|
|
2021-05-13 17:34:40 +08:00
|
|
|
|
ubo_camera_info=db->CreateUBO(sizeof(CameraInfo),&cam.info);
|
2020-12-02 20:27:36 +08:00
|
|
|
|
|
2021-05-13 17:34:40 +08:00
|
|
|
|
if(!ubo_camera_info)
|
2020-12-02 20:27:36 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2022-03-09 20:33:26 +08:00
|
|
|
|
if(!material_instance->BindUBO(DescriptorSetsType::Global,"g_camera",ubo_camera_info))return(false);
|
2021-06-16 21:03:52 +08:00
|
|
|
|
|
2020-12-02 20:27:36 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
2020-12-09 21:33:29 +08:00
|
|
|
|
|
2020-12-02 20:27:36 +08:00
|
|
|
|
bool InitVBO()
|
|
|
|
|
{
|
|
|
|
|
Renderable *render_obj=db->CreateRenderable(VERTEX_COUNT);
|
|
|
|
|
if(!render_obj)return(false);
|
|
|
|
|
|
2022-02-22 20:53:39 +08:00
|
|
|
|
if(!render_obj->Set(VAN::Position, db->CreateVBO(VF_V2F,VERTEX_COUNT,position_data)))return(false);
|
2021-11-30 15:01:55 +08:00
|
|
|
|
if(!render_obj->Set(VAN::Color, db->CreateVBO(VF_V4F,VERTEX_COUNT,color_data)))return(false);
|
2020-12-02 20:27:36 +08:00
|
|
|
|
|
|
|
|
|
render_instance=db->CreateRenderableInstance(render_obj,material_instance,pipeline);
|
2020-12-09 21:33:29 +08:00
|
|
|
|
|
2021-06-15 21:20:57 +08:00
|
|
|
|
render_root.CreateSubNode(scale(0.5,0.5),render_instance);
|
2021-05-31 19:10:17 +08:00
|
|
|
|
|
2020-12-09 21:33:29 +08:00
|
|
|
|
render_root.RefreshMatrix();
|
2021-05-31 19:10:17 +08:00
|
|
|
|
|
2021-06-19 20:42:44 +08:00
|
|
|
|
render_list->Expend(cam.info,&render_root);
|
2021-05-31 19:10:17 +08:00
|
|
|
|
|
2020-12-02 20:27:36 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2021-06-20 02:04:00 +08:00
|
|
|
|
~TestApp()
|
2021-06-19 20:42:44 +08:00
|
|
|
|
{
|
|
|
|
|
SAFE_CLEAR(render_list);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 20:27:36 +08:00
|
|
|
|
bool Init()
|
|
|
|
|
{
|
|
|
|
|
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
|
|
|
|
|
return(false);
|
|
|
|
|
|
2021-06-19 20:42:44 +08:00
|
|
|
|
render_list=new RenderList(device);
|
|
|
|
|
|
2020-12-02 20:27:36 +08:00
|
|
|
|
if(!InitMaterial())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitUBO())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitVBO())
|
|
|
|
|
return(false);
|
|
|
|
|
|
2021-06-19 20:42:44 +08:00
|
|
|
|
BuildCommandBuffer(render_list);
|
2020-12-02 20:27:36 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Resize(int w,int h)override
|
|
|
|
|
{
|
|
|
|
|
cam.width=w;
|
|
|
|
|
cam.height=h;
|
|
|
|
|
|
2022-03-09 20:33:26 +08:00
|
|
|
|
cam.RefreshCameraInfo();
|
2020-12-02 20:27:36 +08:00
|
|
|
|
|
2021-05-13 17:34:40 +08:00
|
|
|
|
ubo_camera_info->Write(&cam.info);
|
2020-12-02 20:27:36 +08:00
|
|
|
|
|
2021-06-19 20:42:44 +08:00
|
|
|
|
BuildCommandBuffer(render_list);
|
2020-12-02 20:27:36 +08:00
|
|
|
|
}
|
|
|
|
|
};//class TestApp:public VulkanApplicationFramework
|
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
|
|
|
|
TestApp app;
|
|
|
|
|
|
|
|
|
|
if(!app.Init())
|
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
|
|
while(app.Run());
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|