2020-09-30 14:21:24 +08:00
|
|
|
|
#include"VulkanAppFramework.h"
|
|
|
|
|
#include<hgl/math/Math.h>
|
|
|
|
|
#include<hgl/filesystem/FileSystem.h>
|
|
|
|
|
#include<iostream>
|
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
|
|
|
|
constexpr uint32_t SCREEN_WIDTH=1280;
|
|
|
|
|
constexpr uint32_t SCREEN_HEIGHT=720;
|
|
|
|
|
|
|
|
|
|
constexpr uint32_t VERTEX_COUNT=4;
|
|
|
|
|
|
2022-02-22 20:53:39 +08:00
|
|
|
|
constexpr float position_data[VERTEX_COUNT][2]=
|
2020-09-30 14:21:24 +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},
|
|
|
|
|
{SCREEN_WIDTH*0.5, SCREEN_HEIGHT*0.25},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static Vector4f color(1,1,1,1);
|
|
|
|
|
|
|
|
|
|
struct Line2DConfig
|
|
|
|
|
{
|
2020-10-19 22:26:42 +08:00
|
|
|
|
float width=20.0f;
|
|
|
|
|
float border=5.0f;
|
2020-09-30 14:21:24 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-10-14 21:05:12 +08:00
|
|
|
|
static Line2DConfig line_2d_config;
|
2020-09-30 14:21:24 +08:00
|
|
|
|
|
|
|
|
|
class TestApp:public VulkanApplicationFramework
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
Camera cam;
|
|
|
|
|
|
2021-12-01 15:17:30 +08:00
|
|
|
|
MaterialInstance * material_instance =nullptr;
|
2022-06-24 21:17:28 +08:00
|
|
|
|
Renderable * render_obj =nullptr;
|
2023-05-04 19:11:18 +08:00
|
|
|
|
DeviceBuffer * ubo_camera_info =nullptr;
|
|
|
|
|
DeviceBuffer * ubo_color_material =nullptr;
|
|
|
|
|
DeviceBuffer * ubo_line_config =nullptr;
|
2020-09-30 14:21:24 +08:00
|
|
|
|
|
2020-10-21 12:47:06 +08:00
|
|
|
|
Pipeline * pipeline =nullptr;
|
2020-09-30 14:21:24 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
bool InitMaterial()
|
|
|
|
|
{
|
|
|
|
|
material_instance=db->CreateMaterialInstance(OS_TEXT("res/material/Line2D"));
|
|
|
|
|
|
|
|
|
|
if(!material_instance)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
// pipeline=db->CreatePipeline(material_instance,sc_render_target,OS_TEXT("res/pipeline/solid2d"));
|
2022-06-25 22:15:25 +08:00
|
|
|
|
//pipeline=CreatePipeline(material_instance,OS_TEXT("res/pipeline/alpha2d"),Prim::LineStrip); //等同上一行,为Framework重载,默认使用swapchain的render target
|
|
|
|
|
pipeline=CreatePipeline(material_instance,InlinePipeline::Alpha2D,Prim::LineStrip); //等同上一行,为Framework重载,默认使用swapchain的render target
|
2020-09-30 14:21:24 +08:00
|
|
|
|
|
|
|
|
|
if(!pipeline)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 17:52:35 +08:00
|
|
|
|
DeviceBuffer *CreateUBO(const AnsiString &name,const VkDeviceSize size,void *data)
|
2020-09-30 14:21:24 +08:00
|
|
|
|
{
|
2022-10-14 17:52:35 +08:00
|
|
|
|
DeviceBuffer *ubo=db->CreateUBO(size,data);
|
2020-09-30 14:21:24 +08:00
|
|
|
|
|
|
|
|
|
if(!ubo)
|
|
|
|
|
return(nullptr);
|
|
|
|
|
|
|
|
|
|
return ubo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitUBO()
|
|
|
|
|
{
|
|
|
|
|
const VkExtent2D extent=sc_render_target->GetExtent();
|
|
|
|
|
|
|
|
|
|
cam.width=extent.width;
|
|
|
|
|
cam.height=extent.height;
|
|
|
|
|
|
2022-02-22 20:53:39 +08:00
|
|
|
|
cam.RefreshCameraInfo();
|
2021-12-01 15:17:30 +08:00
|
|
|
|
|
|
|
|
|
{
|
2023-02-22 21:53:51 +08:00
|
|
|
|
MaterialParameters *mp_global=material_instance->GetMP(DescriptorSetType::Global);
|
2021-12-01 15:17:30 +08:00
|
|
|
|
|
|
|
|
|
if(!mp_global)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
ubo_camera_info =db->CreateUBO(sizeof(CameraInfo), &cam.info);
|
|
|
|
|
|
|
|
|
|
mp_global->BindUBO("g_camera",ubo_camera_info);
|
|
|
|
|
mp_global->Update();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2020-09-30 14:21:24 +08:00
|
|
|
|
|
2023-02-22 21:53:51 +08:00
|
|
|
|
MaterialParameters *mp_value=material_instance->GetMP(DescriptorSetType::Value);
|
2021-12-01 15:17:30 +08:00
|
|
|
|
|
|
|
|
|
if(!mp_value)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
ubo_color_material =db->CreateUBO(sizeof(Vector4f), &color);
|
|
|
|
|
ubo_line_config =db->CreateUBO(sizeof(Line2DConfig), &line_2d_config);
|
|
|
|
|
|
|
|
|
|
mp_value->BindUBO("color_material",ubo_color_material);
|
|
|
|
|
mp_value->BindUBO("line2d_config",ubo_line_config);
|
|
|
|
|
mp_value->Update();
|
|
|
|
|
}
|
2020-09-30 14:21:24 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitVBO()
|
|
|
|
|
{
|
2022-06-24 18:00:22 +08:00
|
|
|
|
Primitive *primitive=db->CreatePrimitive(VERTEX_COUNT);
|
|
|
|
|
if(!primitive)return(false);
|
2020-09-30 14:21:24 +08:00
|
|
|
|
|
2024-04-24 01:38:55 +08:00
|
|
|
|
if(!primitive->Set(VAN::Position, db->CreateVAB(VF_V2F,VERTEX_COUNT,position_data)))return(false);
|
2020-09-30 14:21:24 +08:00
|
|
|
|
|
2022-06-24 21:17:28 +08:00
|
|
|
|
render_obj=db->CreateRenderable(primitive,material_instance,pipeline);
|
2020-09-30 14:21:24 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
bool Init()
|
|
|
|
|
{
|
|
|
|
|
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitMaterial())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitUBO())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitVBO())
|
|
|
|
|
return(false);
|
|
|
|
|
|
2022-06-24 21:17:28 +08:00
|
|
|
|
BuildCommandBuffer(render_obj);
|
2020-09-30 14:21:24 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-06 23:24:57 +08:00
|
|
|
|
void Resize(uint w,uint h)override
|
2020-09-30 14:21:24 +08:00
|
|
|
|
{
|
|
|
|
|
cam.width=w;
|
|
|
|
|
cam.height=h;
|
|
|
|
|
|
2022-02-22 20:53:39 +08:00
|
|
|
|
cam.RefreshCameraInfo();
|
2020-09-30 14:21:24 +08:00
|
|
|
|
|
2021-05-13 17:34:40 +08:00
|
|
|
|
ubo_camera_info->Write(&cam.info);
|
2020-09-30 14:21:24 +08:00
|
|
|
|
|
2022-06-24 21:17:28 +08:00
|
|
|
|
BuildCommandBuffer(render_obj);
|
2020-09-30 14:21:24 +08:00
|
|
|
|
}
|
|
|
|
|
};//class TestApp:public VulkanApplicationFramework
|
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
|
|
|
|
TestApp app;
|
|
|
|
|
|
|
|
|
|
if(!app.Init())
|
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
|
|
while(app.Run());
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|