2023-09-27 11:36:39 +08:00
|
|
|
|
// 画一个带纹理的矩形,2D模式专用
|
2019-05-19 13:02:35 +08:00
|
|
|
|
|
|
|
|
|
#include"VulkanAppFramework.h"
|
2020-10-21 11:43:18 +08:00
|
|
|
|
#include<hgl/graph/VKTexture.h>
|
|
|
|
|
#include<hgl/graph/VKSampler.h>
|
|
|
|
|
#include<hgl/graph/VKInlinePipeline.h>
|
2024-05-31 23:17:08 +08:00
|
|
|
|
#include<hgl/graph/PrimitiveCreater.h>
|
2023-10-07 20:59:44 +08:00
|
|
|
|
#include<hgl/graph/mtl/Material2DCreateConfig.h>
|
2019-05-19 13:02:35 +08:00
|
|
|
|
#include<hgl/math/Math.h>
|
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
|
|
|
|
VK_NAMESPACE_BEGIN
|
2021-12-14 15:34:21 +08:00
|
|
|
|
Texture2D *CreateTexture2DFromFile(GPUDevice *device,const OSString &filename);
|
2019-05-19 13:02:35 +08:00
|
|
|
|
VK_NAMESPACE_END
|
|
|
|
|
|
2020-09-21 19:31:34 +08:00
|
|
|
|
constexpr uint32_t SCREEN_WIDTH=256;
|
|
|
|
|
constexpr uint32_t SCREEN_HEIGHT=256;
|
2019-05-19 13:02:35 +08:00
|
|
|
|
|
2023-09-21 21:36:55 +08:00
|
|
|
|
constexpr float position_data[4]=
|
2019-05-19 13:02:35 +08:00
|
|
|
|
{
|
2023-09-22 01:08:32 +08:00
|
|
|
|
0, //left
|
|
|
|
|
0, //top
|
2023-09-22 11:32:26 +08:00
|
|
|
|
1, //right
|
|
|
|
|
1 //bottom
|
2019-05-20 17:52:23 +08:00
|
|
|
|
};
|
|
|
|
|
|
2023-09-21 21:36:55 +08:00
|
|
|
|
constexpr float tex_coord_data[4]=
|
2019-05-20 17:52:23 +08:00
|
|
|
|
{
|
2023-09-21 21:36:55 +08:00
|
|
|
|
0,0,
|
|
|
|
|
1,1
|
2019-05-19 13:02:35 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class TestApp:public VulkanApplicationFramework
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
|
2020-10-21 12:47:06 +08:00
|
|
|
|
Texture2D * texture =nullptr;
|
|
|
|
|
Sampler * sampler =nullptr;
|
2023-09-26 14:50:38 +08:00
|
|
|
|
Material * material =nullptr;
|
2021-06-19 21:04:02 +08:00
|
|
|
|
MaterialInstance * material_instance =nullptr;
|
2023-09-21 21:36:55 +08:00
|
|
|
|
Renderable * render_obj =nullptr;
|
2020-10-21 12:47:06 +08:00
|
|
|
|
Pipeline * pipeline =nullptr;
|
2019-05-19 13:02:35 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
bool InitMaterial()
|
|
|
|
|
{
|
2023-09-26 21:49:37 +08:00
|
|
|
|
mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"RectTexture2D",Prim::SolidRectangles);
|
2023-09-21 21:36:55 +08:00
|
|
|
|
|
2023-09-22 01:08:32 +08:00
|
|
|
|
cfg.coordinate_system=CoordinateSystem2D::ZeroToOne;
|
2023-09-21 21:36:55 +08:00
|
|
|
|
cfg.local_to_world=false;
|
|
|
|
|
|
2023-10-11 19:02:17 +08:00
|
|
|
|
material=db->LoadMaterial("Std2D/RectTexture2D",&cfg);
|
2023-09-21 21:36:55 +08:00
|
|
|
|
|
2023-09-26 14:50:38 +08:00
|
|
|
|
if(!material)
|
2023-09-21 21:36:55 +08:00
|
|
|
|
return(false);
|
2022-03-09 20:55:09 +08:00
|
|
|
|
|
2021-09-22 17:22:17 +08:00
|
|
|
|
// pipeline=db->CreatePipeline(material_instance,sc_render_target,OS_TEXT("res/pipeline/solid2d"));
|
2023-09-26 14:50:38 +08:00
|
|
|
|
pipeline=CreatePipeline(material,InlinePipeline::Solid2D,Prim::SolidRectangles); //等同上一行,为Framework重载,默认使用swapchain的render target
|
2023-09-21 21:36:55 +08:00
|
|
|
|
|
2021-09-22 17:22:17 +08:00
|
|
|
|
if(!pipeline)
|
2021-09-15 19:17:56 +08:00
|
|
|
|
return(false);
|
2019-05-19 13:02:35 +08:00
|
|
|
|
|
2021-12-15 14:24:35 +08:00
|
|
|
|
texture=db->LoadTexture2D(OS_TEXT("res/image/lena.Tex2D"),true);
|
2020-09-21 17:19:47 +08:00
|
|
|
|
if(!texture)return(false);
|
2019-05-20 17:52:23 +08:00
|
|
|
|
|
2020-09-21 17:19:47 +08:00
|
|
|
|
sampler=db->CreateSampler();
|
2019-05-20 17:52:23 +08:00
|
|
|
|
|
2023-09-26 14:50:38 +08:00
|
|
|
|
if(!material->BindImageSampler( DescriptorSetType::PerMaterial, ///<描述符合集
|
2024-05-31 23:17:08 +08:00
|
|
|
|
mtl::SamplerName::BaseColor, ///<采样器名称
|
2023-09-26 14:50:38 +08:00
|
|
|
|
texture, ///<纹理
|
|
|
|
|
sampler)) ///<采样器
|
2023-09-21 21:36:55 +08:00
|
|
|
|
return(false);
|
2019-05-20 17:52:23 +08:00
|
|
|
|
|
2023-09-26 14:50:38 +08:00
|
|
|
|
material_instance=db->CreateMaterialInstance(material);
|
|
|
|
|
|
2019-05-19 13:02:35 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-21 17:19:47 +08:00
|
|
|
|
bool InitVBO()
|
2019-05-19 13:02:35 +08:00
|
|
|
|
{
|
2024-05-31 23:17:08 +08:00
|
|
|
|
PrimitiveCreater rpc(device,material_instance->GetVIL());
|
|
|
|
|
|
|
|
|
|
rpc.Init("Rectangle",1);
|
2019-05-19 13:02:35 +08:00
|
|
|
|
|
2024-05-31 23:17:08 +08:00
|
|
|
|
if(!rpc.WriteVAB(VAN::Position,VF_V4F,position_data))return(false);
|
|
|
|
|
if(!rpc.WriteVAB(VAN::TexCoord,VF_V4F,tex_coord_data))return(false);
|
2019-05-19 13:02:35 +08:00
|
|
|
|
|
2024-05-31 23:17:08 +08:00
|
|
|
|
render_obj=db->CreateRenderable(&rpc,material_instance,pipeline);
|
2023-09-21 21:36:55 +08:00
|
|
|
|
return(render_obj);
|
2019-05-19 13:02:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
bool Init()
|
|
|
|
|
{
|
|
|
|
|
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
|
|
|
|
|
return(false);
|
2020-09-21 17:19:47 +08:00
|
|
|
|
|
2019-05-21 12:02:57 +08:00
|
|
|
|
if(!InitMaterial())
|
2019-05-19 13:02:35 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2020-09-21 17:19:47 +08:00
|
|
|
|
if(!InitVBO())
|
2019-05-19 13:02:35 +08:00
|
|
|
|
return(false);
|
2020-01-20 16:35:43 +08:00
|
|
|
|
|
2023-09-21 21:36:55 +08:00
|
|
|
|
BuildCommandBuffer(render_obj);
|
2019-05-19 13:02:35 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
2019-06-13 23:12:11 +08:00
|
|
|
|
|
2024-03-06 23:24:57 +08:00
|
|
|
|
void Resize(uint w,uint h)override
|
2019-06-13 23:12:11 +08:00
|
|
|
|
{
|
2022-03-09 20:55:09 +08:00
|
|
|
|
VulkanApplicationFramework::Resize(w,h);
|
2020-09-21 19:31:34 +08:00
|
|
|
|
|
2023-09-21 21:36:55 +08:00
|
|
|
|
BuildCommandBuffer(render_obj);
|
2019-06-13 23:12:11 +08:00
|
|
|
|
}
|
2019-05-19 13:02:35 +08:00
|
|
|
|
};//class TestApp:public VulkanApplicationFramework
|
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
|
|
|
|
#ifdef _DEBUG
|
2020-10-21 12:47:06 +08:00
|
|
|
|
if(!CheckStrideBytesByFormat())
|
2019-05-19 13:02:35 +08:00
|
|
|
|
return 0xff;
|
|
|
|
|
#endif//
|
|
|
|
|
|
|
|
|
|
TestApp app;
|
|
|
|
|
|
|
|
|
|
if(!app.Init())
|
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
|
|
while(app.Run());
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|