ULRE/example/Vulkan/texture_rect.cpp

183 lines
4.3 KiB
C++
Raw Normal View History

2019-05-20 19:32:25 +08:00
// 2.texture_rect
// 该示例是1.indices_rect的进化演示在矩形上贴上贴图
#include"VulkanAppFramework.h"
#include<hgl/graph/VKTexture.h>
#include<hgl/graph/VKSampler.h>
#include<hgl/graph/VKInlinePipeline.h>
#include<hgl/math/Math.h>
using namespace hgl;
using namespace hgl::graph;
VK_NAMESPACE_BEGIN
Texture2D *CreateTexture2DFromFile(GPUDevice *device,const OSString &filename);
VK_NAMESPACE_END
2020-09-21 19:31:34 +08:00
constexpr uint32_t SCREEN_WIDTH=256;
constexpr uint32_t SCREEN_HEIGHT=256;
constexpr uint32_t VERTEX_COUNT=4;
constexpr float vertex_data[VERTEX_COUNT][2]=
{
{0, 0},
{SCREEN_WIDTH, 0},
{0, SCREEN_HEIGHT},
{SCREEN_WIDTH, SCREEN_HEIGHT}
};
constexpr float tex_coord_data[VERTEX_COUNT][2]=
{
{0,0},
{1,0},
{0,1},
{1,1}
};
constexpr uint32_t INDEX_COUNT=6;
constexpr uint16 index_data[INDEX_COUNT]=
{
0,1,3,
0,3,2
};
class TestApp:public VulkanApplicationFramework
{
Camera cam;
private:
2020-10-21 12:47:06 +08:00
Texture2D * texture =nullptr;
Sampler * sampler =nullptr;
MaterialInstance * material_instance =nullptr;
2020-10-21 12:47:06 +08:00
RenderableInstance *renderable_instance =nullptr;
GPUBuffer * ubo_camera_info =nullptr;
2020-10-21 12:47:06 +08:00
Pipeline * pipeline =nullptr;
private:
bool InitMaterial()
{
2020-09-21 17:19:47 +08:00
material_instance=db->CreateMaterialInstance(OS_TEXT("res/material/Texture2D"));
if(!material_instance)return(false);
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
if(!pipeline)
return(false);
2020-10-24 19:14:31 +08:00
texture=db->LoadTexture2D(OS_TEXT("res/image/lena.Tex2D"));
2020-09-21 17:19:47 +08:00
if(!texture)return(false);
2020-09-21 17:19:47 +08:00
sampler=db->CreateSampler();
{
MaterialParameters *mp_texture=material_instance->GetMP(DescriptorSetsType::Value);
if(!mp_texture)
return(false);
if(!mp_texture->BindSampler("tex",texture,sampler))return(false);
mp_texture->Update();
}
return(true);
}
bool InitUBO()
{
const VkExtent2D extent=sc_render_target->GetExtent();
2020-09-21 17:19:47 +08:00
cam.vp_width=cam.width=extent.width;
cam.vp_height=cam.height=extent.height;
cam.Refresh();
ubo_camera_info=db->CreateUBO(sizeof(CameraInfo),&cam.info);
if(!ubo_camera_info)
return(false);
{
MaterialParameters *mp_global=material_instance->GetMP(DescriptorSetsType::Global);
if(!mp_global)
return(false);
if(!mp_global->BindUBO("g_camera",ubo_camera_info))return(false);
mp_global->Update();
}
return(true);
}
2020-09-21 17:19:47 +08:00
bool InitVBO()
{
2020-09-21 19:31:34 +08:00
auto render_obj=db->CreateRenderable(VERTEX_COUNT);
2020-09-21 17:19:47 +08:00
if(!render_obj)return(false);
if(!render_obj->Set(VAN::Position,db->CreateVBO(VF_V2F,VERTEX_COUNT,vertex_data)))return(false);
if(!render_obj->Set(VAN::TexCoord,db->CreateVBO(VF_V2F,VERTEX_COUNT,tex_coord_data)))return(false);
2020-09-21 19:31:34 +08:00
if(!render_obj->Set(db->CreateIBO16(INDEX_COUNT,index_data)))return(false);
2020-09-21 19:31:34 +08:00
renderable_instance=db->CreateRenderableInstance(render_obj,material_instance,pipeline);
2020-09-21 17:19:47 +08:00
return(true);
}
public:
bool Init()
{
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
return(false);
2020-09-21 17:19:47 +08:00
if(!InitMaterial())
return(false);
2020-09-21 17:19:47 +08:00
if(!InitUBO())
return(false);
2020-09-21 17:19:47 +08:00
if(!InitVBO())
return(false);
2020-09-21 19:31:34 +08:00
BuildCommandBuffer(renderable_instance);
return(true);
}
void Resize(int w,int h)override
{
cam.width=w;
cam.height=h;
cam.Refresh();
ubo_camera_info->Write(&cam.info);
2020-09-21 19:31:34 +08:00
BuildCommandBuffer(renderable_instance);
}
};//class TestApp:public VulkanApplicationFramework
int main(int,char **)
{
#ifdef _DEBUG
2020-10-21 12:47:06 +08:00
if(!CheckStrideBytesByFormat())
return 0xff;
#endif//
TestApp app;
if(!app.Init())
return(-1);
while(app.Run());
return 0;
}