2023-09-27 11:36:39 +08:00
|
|
|
|
// 该范例主要演示使用2D坐系统直接绘制一个渐变色的三角形,使用UBO传递Viewport信息
|
2020-12-02 20:27:36 +08:00
|
|
|
|
|
|
|
|
|
#include"VulkanAppFramework.h"
|
|
|
|
|
#include<hgl/math/Math.h>
|
|
|
|
|
#include<hgl/filesystem/FileSystem.h>
|
2022-06-24 16:27:43 +08:00
|
|
|
|
#include<hgl/graph/SceneInfo.h>
|
2023-10-12 15:00:39 +08:00
|
|
|
|
#include<hgl/graph/VKVertexInputConfig.h>
|
2023-03-22 18:15:45 +08:00
|
|
|
|
#include<hgl/graph/VKRenderablePrimitiveCreater.h>
|
2023-10-07 20:59:44 +08:00
|
|
|
|
#include<hgl/graph/mtl/Material2DCreateConfig.h>
|
2020-12-02 20:27:36 +08:00
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
|
|
|
|
constexpr uint32_t VERTEX_COUNT=3;
|
|
|
|
|
|
2023-10-12 15:00:39 +08:00
|
|
|
|
static float position_data_float[VERTEX_COUNT][2]=
|
2020-12-02 20:27:36 +08:00
|
|
|
|
{
|
2023-03-22 17:56:36 +08:00
|
|
|
|
{0.5, 0.25},
|
|
|
|
|
{0.75, 0.75},
|
|
|
|
|
{0.25, 0.75}
|
2020-12-02 20:27:36 +08:00
|
|
|
|
};
|
|
|
|
|
|
2023-10-12 15:00:39 +08:00
|
|
|
|
static uint16 position_data_u16[VERTEX_COUNT][2]={};
|
|
|
|
|
|
|
|
|
|
constexpr uint8 color_data[VERTEX_COUNT*4]=
|
2022-10-14 17:52:35 +08:00
|
|
|
|
{
|
2023-10-12 15:00:39 +08:00
|
|
|
|
255,0,0,255,
|
|
|
|
|
0,255,0,255,
|
|
|
|
|
0,0,255,255
|
2020-12-02 20:27:36 +08:00
|
|
|
|
};
|
|
|
|
|
|
2023-03-28 21:52:08 +08:00
|
|
|
|
//#define USE_ZERO2ONE_COORD //使用左上角0,0右下角1,1的坐标系
|
2023-03-22 17:56:36 +08:00
|
|
|
|
|
2020-12-02 20:27:36 +08:00
|
|
|
|
class TestApp:public VulkanApplicationFramework
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
|
2021-06-16 21:03:52 +08:00
|
|
|
|
MaterialInstance * material_instance =nullptr;
|
2022-06-24 21:17:28 +08:00
|
|
|
|
Renderable * render_obj =nullptr;
|
2020-12-02 20:27:36 +08:00
|
|
|
|
|
|
|
|
|
Pipeline * pipeline =nullptr;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
bool InitMaterial()
|
2023-03-22 17:56:36 +08:00
|
|
|
|
{
|
2023-09-26 21:49:37 +08:00
|
|
|
|
mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"VertexColor2D",Prim::Triangles);
|
2023-03-22 19:39:53 +08:00
|
|
|
|
|
2023-10-12 15:00:39 +08:00
|
|
|
|
VILConfig vil_config;
|
|
|
|
|
|
2023-03-22 17:56:36 +08:00
|
|
|
|
#ifdef USE_ZERO2ONE_COORD
|
2023-05-16 15:21:32 +08:00
|
|
|
|
cfg.coordinate_system=CoordinateSystem2D::ZeroToOne;
|
2023-03-22 17:56:36 +08:00
|
|
|
|
#else
|
2023-05-16 15:21:32 +08:00
|
|
|
|
cfg.coordinate_system=CoordinateSystem2D::Ortho;
|
2023-10-12 15:00:39 +08:00
|
|
|
|
|
|
|
|
|
cfg.position_format =VAT_UVEC2; //这里指定shader中使用uvec2当做顶点输入格式
|
|
|
|
|
// ^
|
|
|
|
|
// + 这上下两种格式要配套,否则会出错
|
|
|
|
|
// v
|
|
|
|
|
vil_config.Add(VAN::Position,VF_V2U16); //这里指定VBO中使用RG16U当做顶点数据格式
|
2023-03-22 17:56:36 +08:00
|
|
|
|
#endif//USE_ZERO2ONE_COORD
|
2023-03-22 15:58:59 +08:00
|
|
|
|
|
2023-10-12 15:00:39 +08:00
|
|
|
|
vil_config.Add(VAN::Color,VF_V4UN8); //这里指定VBO中使用RGBA8UNorm当做颜色数据格式
|
|
|
|
|
|
2023-05-08 09:50:45 +08:00
|
|
|
|
cfg.local_to_world=false;
|
|
|
|
|
|
2023-05-16 15:21:32 +08:00
|
|
|
|
AutoDelete<mtl::MaterialCreateInfo> mci=mtl::CreateVertexColor2D(&cfg);
|
2023-03-22 15:58:59 +08:00
|
|
|
|
|
2023-10-12 15:00:39 +08:00
|
|
|
|
material_instance=db->CreateMaterialInstance(mci,&vil_config);
|
2023-03-22 15:58:59 +08:00
|
|
|
|
|
2020-12-02 20:27:36 +08:00
|
|
|
|
if(!material_instance)
|
|
|
|
|
return(false);
|
2023-10-11 18:49:29 +08:00
|
|
|
|
|
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
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
2021-09-22 17:22:17 +08:00
|
|
|
|
return pipeline;
|
2020-12-02 20:27:36 +08:00
|
|
|
|
}
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
2020-12-02 20:27:36 +08:00
|
|
|
|
bool InitVBO()
|
|
|
|
|
{
|
2023-10-12 10:39:43 +08:00
|
|
|
|
RenderablePrimitiveCreater rpc(db,"Triangle",VERTEX_COUNT);
|
2020-12-02 20:27:36 +08:00
|
|
|
|
|
2023-10-12 15:00:39 +08:00
|
|
|
|
#ifdef USE_ZERO2ONE_COORD //使用0 to 1坐标系
|
|
|
|
|
if(!rpc.SetVBO(VAN::Position, VF_V2F, position_data_float ))return(false);
|
|
|
|
|
#else //使用ortho坐标系
|
|
|
|
|
if(!rpc.SetVBO(VAN::Position, VF_V2U16, position_data_u16 ))return(false);
|
2023-03-22 17:56:36 +08:00
|
|
|
|
#endif//USE_ZERO2ONE_COORD
|
|
|
|
|
|
2023-10-12 15:00:39 +08:00
|
|
|
|
if(!rpc.SetVBO(VAN::Color, VF_V4UN8, color_data ))return(false);
|
2020-12-02 20:27:36 +08:00
|
|
|
|
|
2023-03-22 18:15:45 +08:00
|
|
|
|
render_obj=rpc.Create(material_instance,pipeline);
|
2020-12-02 20:27:36 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2023-10-12 15:00:39 +08:00
|
|
|
|
bool Init(uint w,uint h)
|
2020-12-02 20:27:36 +08:00
|
|
|
|
{
|
2023-10-12 15:00:39 +08:00
|
|
|
|
if(!VulkanApplicationFramework::Init(w,h))
|
2020-12-02 20:27:36 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2023-10-12 15:00:39 +08:00
|
|
|
|
#ifndef USE_ZERO2ONE_COORD
|
|
|
|
|
for(uint i=0;i<VERTEX_COUNT;i++)
|
|
|
|
|
{
|
|
|
|
|
position_data_u16[i][0]=position_data_float[i][0]*w;
|
|
|
|
|
position_data_u16[i][1]=position_data_float[i][1]*h;
|
|
|
|
|
}
|
|
|
|
|
#endif//
|
|
|
|
|
|
2020-12-02 20:27:36 +08:00
|
|
|
|
if(!InitMaterial())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitVBO())
|
|
|
|
|
return(false);
|
|
|
|
|
|
2022-06-24 21:17:28 +08:00
|
|
|
|
if(!BuildCommandBuffer(render_obj))
|
2022-06-24 16:27:43 +08:00
|
|
|
|
return(false);
|
2020-12-02 20:27:36 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Resize(int w,int h)override
|
|
|
|
|
{
|
2022-03-09 20:55:09 +08:00
|
|
|
|
VulkanApplicationFramework::Resize(w,h);
|
2020-12-02 20:27:36 +08:00
|
|
|
|
|
2022-06-24 21:17:28 +08:00
|
|
|
|
BuildCommandBuffer(render_obj);
|
2020-12-02 20:27:36 +08:00
|
|
|
|
}
|
|
|
|
|
};//class TestApp:public VulkanApplicationFramework
|
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
2023-10-12 15:00:39 +08:00
|
|
|
|
RunApp<TestApp>(1280,720);
|
2020-12-02 20:27:36 +08:00
|
|
|
|
}
|