2020-08-04 01:27:35 +08:00
|
|
|
|
// 2.RectanglePrimivate
|
2020-07-08 15:46:30 +08:00
|
|
|
|
// 该示例是texture_rect的进化,演示使用GeometryShader画矩形
|
|
|
|
|
|
|
|
|
|
#include"VulkanAppFramework.h"
|
2020-10-21 11:43:18 +08:00
|
|
|
|
#include<hgl/graph/VKTexture.h>
|
|
|
|
|
#include<hgl/graph/VKSampler.h>
|
2020-07-08 15:46:30 +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);
|
2020-07-08 15:46:30 +08:00
|
|
|
|
VK_NAMESPACE_END
|
|
|
|
|
|
2020-07-08 15:51:05 +08:00
|
|
|
|
constexpr uint32_t SCREEN_SIZE=512;
|
2020-07-08 15:46:30 +08:00
|
|
|
|
|
|
|
|
|
constexpr uint32_t VERTEX_COUNT=1;
|
|
|
|
|
|
2020-07-08 17:17:36 +08:00
|
|
|
|
constexpr float BORDER=0.1f;
|
2020-07-08 15:51:05 +08:00
|
|
|
|
|
2022-02-22 20:53:39 +08:00
|
|
|
|
constexpr float position_data[4]=
|
2020-07-08 15:46:30 +08:00
|
|
|
|
{
|
2020-07-08 15:51:05 +08:00
|
|
|
|
SCREEN_SIZE*BORDER, SCREEN_SIZE*BORDER,
|
|
|
|
|
SCREEN_SIZE*(1.0-BORDER), SCREEN_SIZE*(1.0-BORDER)
|
2020-07-08 15:46:30 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constexpr float tex_coord_data[4]=
|
|
|
|
|
{
|
|
|
|
|
0,0,1,1
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class TestApp:public VulkanApplicationFramework
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
|
2020-10-21 12:47:06 +08:00
|
|
|
|
Texture2D * texture =nullptr;
|
|
|
|
|
Sampler * sampler =nullptr;
|
2021-09-27 11:05:05 +08:00
|
|
|
|
MaterialInstance * material_instance =nullptr;
|
2022-06-24 21:17:28 +08:00
|
|
|
|
Primitive * primitive =nullptr;
|
|
|
|
|
Renderable * render_obj =nullptr;
|
2020-07-08 15:46:30 +08:00
|
|
|
|
|
2020-10-21 12:47:06 +08:00
|
|
|
|
Pipeline * pipeline =nullptr;
|
2020-07-08 15:46:30 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
bool InitMaterial()
|
|
|
|
|
{
|
2020-09-27 22:30:12 +08:00
|
|
|
|
material_instance=db->CreateMaterialInstance(OS_TEXT("res/material/TextureRect2D"));
|
2020-09-28 11:16:45 +08:00
|
|
|
|
if(!material_instance)return(false);
|
2020-07-08 15:46:30 +08:00
|
|
|
|
|
2022-03-09 20:55:09 +08:00
|
|
|
|
BindCameraUBO(material_instance);
|
|
|
|
|
|
2021-09-28 10:53:09 +08:00
|
|
|
|
pipeline=CreatePipeline(material_instance,InlinePipeline::Solid2D,Prim::SolidRectangles);
|
2020-09-28 11:16:45 +08:00
|
|
|
|
if(!pipeline)return(false);
|
2020-07-08 15:46:30 +08:00
|
|
|
|
|
2020-10-25 21:29:18 +08:00
|
|
|
|
texture=db->LoadTexture2D(OS_TEXT("res/image/lena.Tex2D"));
|
2020-09-28 11:16:45 +08:00
|
|
|
|
if(!texture)return(false);
|
2020-07-08 15:46:30 +08:00
|
|
|
|
|
2020-07-08 17:17:36 +08:00
|
|
|
|
sampler=db->CreateSampler();
|
2020-07-08 15:46:30 +08:00
|
|
|
|
|
2023-02-22 21:53:51 +08:00
|
|
|
|
if(!material_instance->BindSampler(DescriptorSetType::Value,"tex",texture,sampler))return(false);
|
2021-09-27 11:05:05 +08:00
|
|
|
|
|
2020-07-08 15:46:30 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-27 22:30:12 +08:00
|
|
|
|
bool InitVBO()
|
|
|
|
|
{
|
2022-06-24 18:00:22 +08:00
|
|
|
|
primitive=db->CreatePrimitive(VERTEX_COUNT);
|
2020-07-08 15:46:30 +08:00
|
|
|
|
|
2022-06-24 18:00:22 +08:00
|
|
|
|
if(!primitive)return(false);
|
2020-07-08 15:46:30 +08:00
|
|
|
|
|
2022-06-24 18:00:22 +08:00
|
|
|
|
primitive->Set(VAN::Position,db->CreateVBO(VF_V4F,VERTEX_COUNT,position_data));
|
|
|
|
|
primitive->Set(VAN::TexCoord,db->CreateVBO(VF_V4F,VERTEX_COUNT,tex_coord_data));
|
2020-09-27 22:30:12 +08:00
|
|
|
|
|
2022-06-24 21:17:28 +08:00
|
|
|
|
render_obj=db->CreateRenderable(primitive,material_instance,pipeline);
|
2020-07-08 15:46:30 +08:00
|
|
|
|
|
2022-06-24 21:17:28 +08:00
|
|
|
|
return(render_obj);
|
2020-07-08 15:46:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
bool Init()
|
|
|
|
|
{
|
2020-07-08 15:51:05 +08:00
|
|
|
|
if(!VulkanApplicationFramework::Init(SCREEN_SIZE,SCREEN_SIZE))
|
2020-07-08 15:46:30 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitMaterial())
|
|
|
|
|
return(false);
|
|
|
|
|
|
2020-09-27 22:30:12 +08:00
|
|
|
|
if(!InitVBO())
|
2020-07-08 15:46:30 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2022-06-24 21:17:28 +08:00
|
|
|
|
BuildCommandBuffer(render_obj);
|
2020-07-08 15:46:30 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Resize(int w,int h)override
|
|
|
|
|
{
|
2022-03-09 20:55:09 +08:00
|
|
|
|
VulkanApplicationFramework::Resize(w,h);
|
2020-09-27 22:30:12 +08:00
|
|
|
|
|
2022-06-24 21:17:28 +08:00
|
|
|
|
BuildCommandBuffer(render_obj);
|
2020-07-08 15:46:30 +08:00
|
|
|
|
}
|
|
|
|
|
};//class TestApp:public VulkanApplicationFramework
|
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
|
|
|
|
TestApp app;
|
|
|
|
|
|
|
|
|
|
if(!app.Init())
|
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
|
|
while(app.Run());
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|