2019-05-06 12:00:03 +08:00
|
|
|
|
// 0.triangle
|
|
|
|
|
// 该范例主要演示直接绘制一个渐变色的三角形
|
|
|
|
|
|
|
|
|
|
#include"VulkanAppFramework.h"
|
2019-04-23 02:46:47 +08:00
|
|
|
|
#include<hgl/math/Math.h>
|
2019-05-17 19:22:13 +08:00
|
|
|
|
#include<hgl/filesystem/FileSystem.h>
|
2021-05-24 20:44:20 +08:00
|
|
|
|
#include<hgl/graph/SceneInfo.h>
|
2019-04-15 22:19:09 +08:00
|
|
|
|
|
2019-04-10 01:13:31 +08:00
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
2019-04-09 00:22:26 +08:00
|
|
|
|
|
2020-06-09 19:40:08 +08:00
|
|
|
|
constexpr uint32_t SCREEN_WIDTH=1280;
|
|
|
|
|
constexpr uint32_t SCREEN_HEIGHT=720;
|
2019-04-23 02:46:47 +08:00
|
|
|
|
|
2019-04-28 17:02:38 +08:00
|
|
|
|
constexpr uint32_t VERTEX_COUNT=3;
|
|
|
|
|
|
|
|
|
|
constexpr float vertex_data[VERTEX_COUNT][2]=
|
2019-04-23 02:46:47 +08:00
|
|
|
|
{
|
2019-04-28 17:02:38 +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}
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-24 19:14:31 +08:00
|
|
|
|
constexpr float color_data[VERTEX_COUNT][4]=
|
|
|
|
|
{ {1,0,0,1},
|
|
|
|
|
{0,1,0,1},
|
|
|
|
|
{0,0,1,1}
|
2019-04-23 02:46:47 +08:00
|
|
|
|
};
|
2019-04-20 17:18:02 +08:00
|
|
|
|
|
2019-04-28 18:18:56 +08:00
|
|
|
|
class TestApp:public VulkanApplicationFramework
|
2019-04-09 00:22:26 +08:00
|
|
|
|
{
|
2019-05-05 14:22:58 +08:00
|
|
|
|
private:
|
2019-05-04 16:31:28 +08:00
|
|
|
|
|
2020-01-20 15:45:12 +08:00
|
|
|
|
Camera cam;
|
2019-07-17 04:49:49 +08:00
|
|
|
|
|
2021-06-16 20:29:25 +08:00
|
|
|
|
MaterialInstance * material_instance =nullptr;
|
2020-10-21 12:47:06 +08:00
|
|
|
|
RenderableInstance *render_instance =nullptr;
|
2021-05-28 21:24:48 +08:00
|
|
|
|
GPUBuffer * ubo_camera_info =nullptr;
|
2019-04-16 14:49:55 +08:00
|
|
|
|
|
2020-10-21 12:47:06 +08:00
|
|
|
|
Pipeline * pipeline =nullptr;
|
2019-04-09 00:22:26 +08:00
|
|
|
|
|
2019-04-28 18:18:56 +08:00
|
|
|
|
private:
|
2019-04-12 16:39:22 +08:00
|
|
|
|
|
2021-06-20 02:15:17 +08:00
|
|
|
|
bool RecreatePipeline()
|
|
|
|
|
{
|
|
|
|
|
// pipeline=db->CreatePipeline(material_instance,sc_render_target,OS_TEXT("res/pipeline/solid2d"));
|
|
|
|
|
pipeline=CreatePipeline(material_instance,InlinePipeline::Solid2D); //等同上一行,为Framework重载,默认使用swapchain的render target
|
|
|
|
|
|
|
|
|
|
return pipeline;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-28 18:18:56 +08:00
|
|
|
|
bool InitMaterial()
|
2019-04-12 16:39:22 +08:00
|
|
|
|
{
|
2020-09-20 03:59:42 +08:00
|
|
|
|
material_instance=db->CreateMaterialInstance(OS_TEXT("res/material/VertexColor2D"));
|
|
|
|
|
|
|
|
|
|
if(!material_instance)
|
2019-04-28 18:18:56 +08:00
|
|
|
|
return(false);
|
2020-09-20 21:17:16 +08:00
|
|
|
|
|
2021-06-20 02:15:17 +08:00
|
|
|
|
return RecreatePipeline();
|
2019-04-12 16:39:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-28 18:18:56 +08:00
|
|
|
|
bool InitUBO()
|
2019-04-13 21:44:26 +08:00
|
|
|
|
{
|
2021-05-24 20:44:20 +08:00
|
|
|
|
MVPMatrix *mvp=new MVPMatrix;
|
|
|
|
|
|
2019-07-16 19:59:53 +08:00
|
|
|
|
const VkExtent2D extent=sc_render_target->GetExtent();
|
2019-04-13 21:44:26 +08:00
|
|
|
|
|
2020-01-20 15:45:12 +08:00
|
|
|
|
cam.width=extent.width;
|
|
|
|
|
cam.height=extent.height;
|
2019-04-28 18:18:56 +08:00
|
|
|
|
|
2020-01-20 15:45:12 +08:00
|
|
|
|
cam.Refresh();
|
2019-04-28 18:18:56 +08:00
|
|
|
|
|
2021-05-13 17:34:40 +08:00
|
|
|
|
ubo_camera_info=db->CreateUBO(sizeof(CameraInfo),&cam.info);
|
2019-04-28 18:45:26 +08:00
|
|
|
|
|
2021-05-13 17:34:40 +08:00
|
|
|
|
if(!ubo_camera_info)
|
2019-05-21 12:02:57 +08:00
|
|
|
|
return(false);
|
2021-06-16 20:29:25 +08:00
|
|
|
|
|
|
|
|
|
{
|
2021-06-22 14:48:08 +08:00
|
|
|
|
MaterialParameters *mp_global=material_instance->GetMP(DescriptorSetType::Global);
|
2021-06-16 20:29:25 +08:00
|
|
|
|
|
|
|
|
|
if(!mp_global)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
mp_global->BindUBO("g_camera",ubo_camera_info);
|
|
|
|
|
mp_global->Update();
|
|
|
|
|
}
|
2019-05-21 12:02:57 +08:00
|
|
|
|
return(true);
|
2019-04-13 21:44:26 +08:00
|
|
|
|
}
|
2019-07-17 04:49:49 +08:00
|
|
|
|
|
2020-06-10 17:11:24 +08:00
|
|
|
|
bool InitVBO()
|
2019-05-05 14:22:58 +08:00
|
|
|
|
{
|
2020-10-21 12:47:06 +08:00
|
|
|
|
Renderable *render_obj=db->CreateRenderable(VERTEX_COUNT);
|
2020-09-20 21:17:16 +08:00
|
|
|
|
if(!render_obj)return(false);
|
|
|
|
|
|
2020-09-21 19:31:34 +08:00
|
|
|
|
if(!render_obj->Set(VAN::Position, db->CreateVAB(VAF_VEC2,VERTEX_COUNT,vertex_data)))return(false);
|
2020-10-24 19:14:31 +08:00
|
|
|
|
if(!render_obj->Set(VAN::Color, db->CreateVAB(VAF_VEC4,VERTEX_COUNT,color_data)))return(false);
|
2020-09-21 19:31:34 +08:00
|
|
|
|
|
|
|
|
|
render_instance=db->CreateRenderableInstance(render_obj,material_instance,pipeline);
|
2020-06-10 17:11:24 +08:00
|
|
|
|
return(true);
|
2019-04-28 18:18:56 +08:00
|
|
|
|
}
|
2019-04-18 21:02:42 +08:00
|
|
|
|
|
2019-04-28 18:18:56 +08:00
|
|
|
|
public:
|
2019-04-20 16:12:22 +08:00
|
|
|
|
|
2019-05-05 14:22:58 +08:00
|
|
|
|
bool Init()
|
2019-04-28 18:18:56 +08:00
|
|
|
|
{
|
|
|
|
|
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
|
|
|
|
|
return(false);
|
2019-04-23 00:02:59 +08:00
|
|
|
|
|
2019-04-28 18:18:56 +08:00
|
|
|
|
if(!InitMaterial())
|
|
|
|
|
return(false);
|
2019-04-20 02:28:57 +08:00
|
|
|
|
|
2019-04-28 18:18:56 +08:00
|
|
|
|
if(!InitUBO())
|
|
|
|
|
return(false);
|
2019-04-20 02:28:57 +08:00
|
|
|
|
|
2020-06-10 17:11:24 +08:00
|
|
|
|
if(!InitVBO())
|
|
|
|
|
return(false);
|
2019-04-20 16:12:22 +08:00
|
|
|
|
|
2020-10-24 19:14:31 +08:00
|
|
|
|
if(!BuildCommandBuffer(render_instance))
|
|
|
|
|
return(false);
|
2019-04-28 18:18:56 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
2019-06-13 23:12:11 +08:00
|
|
|
|
|
2019-07-17 04:49:49 +08:00
|
|
|
|
void Resize(int w,int h)override
|
2019-06-13 23:12:11 +08:00
|
|
|
|
{
|
2020-01-20 15:45:12 +08:00
|
|
|
|
cam.width=w;
|
|
|
|
|
cam.height=h;
|
|
|
|
|
|
|
|
|
|
cam.Refresh();
|
2019-07-17 04:49:49 +08:00
|
|
|
|
|
2021-05-13 17:34:40 +08:00
|
|
|
|
ubo_camera_info->Write(&cam.info);
|
2019-07-17 04:49:49 +08:00
|
|
|
|
|
2021-06-20 02:15:17 +08:00
|
|
|
|
RecreatePipeline();
|
|
|
|
|
render_instance->UpdatePipeline(pipeline);
|
2020-09-21 19:31:34 +08:00
|
|
|
|
BuildCommandBuffer(render_instance);
|
2019-06-13 23:12:11 +08:00
|
|
|
|
}
|
2019-04-28 18:18:56 +08:00
|
|
|
|
};//class TestApp:public VulkanApplicationFramework
|
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
|
|
|
|
TestApp app;
|
|
|
|
|
|
|
|
|
|
if(!app.Init())
|
|
|
|
|
return(-1);
|
2019-04-20 16:12:22 +08:00
|
|
|
|
|
2019-05-04 16:52:44 +08:00
|
|
|
|
while(app.Run());
|
2019-05-05 14:22:58 +08:00
|
|
|
|
|
2019-04-09 00:22:26 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|