2019-05-20 19:32:25 +08:00
|
|
|
|
// 2.texture_rect
|
|
|
|
|
// 该示例是1.indices_rect的进化,演示在矩形上贴上贴图
|
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>
|
2019-05-19 13:02:35 +08:00
|
|
|
|
#include<hgl/math/Math.h>
|
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
|
|
|
|
VK_NAMESPACE_BEGIN
|
2020-10-21 12:09:15 +08:00
|
|
|
|
Texture2D *CreateTextureFromFile(RenderDevice *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
|
|
|
|
|
|
|
|
|
constexpr uint32_t VERTEX_COUNT=4;
|
|
|
|
|
|
|
|
|
|
constexpr float vertex_data[VERTEX_COUNT][2]=
|
|
|
|
|
{
|
2019-05-20 17:52:23 +08:00
|
|
|
|
{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}
|
2019-05-19 13:02:35 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constexpr uint32_t INDEX_COUNT=6;
|
|
|
|
|
|
|
|
|
|
constexpr uint16 index_data[INDEX_COUNT]=
|
|
|
|
|
{
|
|
|
|
|
0,1,3,
|
|
|
|
|
0,3,2
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class TestApp:public VulkanApplicationFramework
|
|
|
|
|
{
|
2020-01-20 16:35:43 +08:00
|
|
|
|
Camera cam;
|
|
|
|
|
|
2019-05-19 13:02:35 +08:00
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
vulkan::Texture2D * texture =nullptr;
|
2019-05-20 17:52:23 +08:00
|
|
|
|
vulkan::Sampler * sampler =nullptr;
|
2020-01-20 16:35:43 +08:00
|
|
|
|
vulkan::MaterialInstance * material_instance =nullptr;
|
2020-09-21 19:31:34 +08:00
|
|
|
|
vulkan::RenderableInstance *renderable_instance =nullptr;
|
2020-10-21 12:09:15 +08:00
|
|
|
|
vulkan::GPUBuffer * ubo_world_matrix =nullptr;
|
2019-05-19 13:02:35 +08:00
|
|
|
|
vulkan::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);
|
2019-05-19 13:02:35 +08:00
|
|
|
|
|
2020-10-16 12:48:54 +08:00
|
|
|
|
pipeline=CreatePipeline(material_instance,vulkan::InlinePipeline::Solid2D);
|
2020-09-21 17:19:47 +08:00
|
|
|
|
if(!pipeline)return(false);
|
2019-05-19 13:02:35 +08:00
|
|
|
|
|
2019-12-03 20:48:42 +08:00
|
|
|
|
texture=vulkan::CreateTextureFromFile(device,OS_TEXT("res/image/lena.Tex2D"));
|
2020-09-21 17:19:47 +08:00
|
|
|
|
if(!texture)return(false);
|
2019-05-20 17:52:23 +08:00
|
|
|
|
|
2020-09-21 19:31:34 +08:00
|
|
|
|
db->Add(texture);
|
|
|
|
|
|
2020-09-21 17:19:47 +08:00
|
|
|
|
sampler=db->CreateSampler();
|
2019-05-20 17:52:23 +08:00
|
|
|
|
|
2020-01-20 16:35:43 +08:00
|
|
|
|
material_instance->BindSampler("tex",texture,sampler);
|
2019-05-20 17:52:23 +08:00
|
|
|
|
|
2019-05-19 13:02:35 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitUBO()
|
|
|
|
|
{
|
2019-07-16 19:59:53 +08:00
|
|
|
|
const VkExtent2D extent=sc_render_target->GetExtent();
|
2019-05-19 13:02:35 +08:00
|
|
|
|
|
2020-09-21 17:19:47 +08:00
|
|
|
|
cam.vp_width=cam.width=extent.width;
|
|
|
|
|
cam.vp_height=cam.height=extent.height;
|
2019-05-19 13:02:35 +08:00
|
|
|
|
|
2020-01-20 16:35:43 +08:00
|
|
|
|
cam.Refresh();
|
|
|
|
|
|
2020-09-21 17:19:47 +08:00
|
|
|
|
ubo_world_matrix=db->CreateUBO(sizeof(WorldMatrix),&cam.matrix);
|
2020-01-20 16:35:43 +08:00
|
|
|
|
|
2020-07-14 20:42:01 +08:00
|
|
|
|
if(!ubo_world_matrix)
|
2020-01-20 16:35:43 +08:00
|
|
|
|
return(false);
|
2019-05-19 13:02:35 +08:00
|
|
|
|
|
2020-09-21 17:19:47 +08:00
|
|
|
|
if(!material_instance->BindUBO("world",ubo_world_matrix))return(false);
|
|
|
|
|
material_instance->Update();
|
2020-01-20 16:35:43 +08:00
|
|
|
|
return(true);
|
2019-05-19 13:02:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-21 17:19:47 +08:00
|
|
|
|
bool InitVBO()
|
2019-05-19 13:02:35 +08:00
|
|
|
|
{
|
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);
|
2019-05-19 13:02:35 +08:00
|
|
|
|
|
2020-09-21 19:31:34 +08:00
|
|
|
|
if(!render_obj->Set(VAN::Position,db->CreateVAB(VAF_VEC2,VERTEX_COUNT,vertex_data)))return(false);
|
|
|
|
|
if(!render_obj->Set(VAN::TexCoord,db->CreateVAB(VAF_VEC2,VERTEX_COUNT,tex_coord_data)))return(false);
|
|
|
|
|
if(!render_obj->Set(db->CreateIBO16(INDEX_COUNT,index_data)))return(false);
|
2019-05-19 13:02:35 +08:00
|
|
|
|
|
2020-09-21 19:31:34 +08:00
|
|
|
|
renderable_instance=db->CreateRenderableInstance(render_obj,material_instance,pipeline);
|
2019-05-19 13:02:35 +08:00
|
|
|
|
|
2020-09-21 17:19:47 +08:00
|
|
|
|
return(true);
|
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(!InitUBO())
|
|
|
|
|
return(false);
|
2019-05-19 13:02:35 +08:00
|
|
|
|
|
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
|
|
|
|
|
2020-09-21 19:31:34 +08:00
|
|
|
|
BuildCommandBuffer(renderable_instance);
|
2019-05-19 13:02:35 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
2019-06-13 23:12:11 +08:00
|
|
|
|
|
2020-01-20 16:35:43 +08:00
|
|
|
|
void Resize(int w,int h)override
|
2019-06-13 23:12:11 +08:00
|
|
|
|
{
|
2020-01-20 16:35:43 +08:00
|
|
|
|
cam.width=w;
|
|
|
|
|
cam.height=h;
|
|
|
|
|
|
|
|
|
|
cam.Refresh();
|
|
|
|
|
|
2020-07-14 20:42:01 +08:00
|
|
|
|
ubo_world_matrix->Write(&cam.matrix);
|
2020-09-21 19:31:34 +08:00
|
|
|
|
|
|
|
|
|
BuildCommandBuffer(renderable_instance);
|
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
|
|
|
|
|
if(!vulkan::CheckStrideBytesByFormat())
|
|
|
|
|
return 0xff;
|
|
|
|
|
#endif//
|
|
|
|
|
|
|
|
|
|
TestApp app;
|
|
|
|
|
|
|
|
|
|
if(!app.Init())
|
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
|
|
while(app.Run());
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|