ULRE/example/Vulkan/DrawText.cpp

152 lines
3.7 KiB
C++
Raw Normal View History

2020-07-08 21:56:39 +08:00
#include"VulkanAppFramework.h"
#include<hgl/graph/vulkan/VKTexture.h>
#include<hgl/graph/vulkan/VKSampler.h>
#include<hgl/math/Math.h>
#include<hgl/graph/font/TextLayout.h>
2020-06-28 22:16:07 +08:00
using namespace hgl;
2020-07-08 21:56:39 +08:00
using namespace hgl::graph;
VK_NAMESPACE_BEGIN
Texture2D *CreateTextureFromFile(Device *device,const OSString &filename);
VK_NAMESPACE_END
constexpr uint32_t SCREEN_SIZE=512;
2020-06-28 22:16:07 +08:00
2020-07-08 21:56:39 +08:00
class TestApp:public VulkanApplicationFramework
2020-06-28 22:16:07 +08:00
{
2020-07-08 21:56:39 +08:00
Camera cam;
private:
TextLayout text_layout;
private:
vulkan::Material * material =nullptr;
vulkan::Texture2D * texture =nullptr;
vulkan::Sampler * sampler =nullptr;
vulkan::MaterialInstance * material_instance =nullptr;
vulkan::Renderable * render_obj =nullptr;
vulkan::Buffer * ubo_mvp =nullptr;
vulkan::Pipeline * pipeline =nullptr;
vulkan::VertexBuffer * vertex_buffer =nullptr;
vulkan::VertexBuffer * tex_coord_buffer =nullptr;
private:
bool InitMaterial()
{
material=shader_manage->CreateMaterial( OS_TEXT("res/shader/DrawRect2D.vert"),
OS_TEXT("res/shader/DrawRect2D.geom"),
OS_TEXT("res/shader/FlatTexture.frag"));
if(!material)
return(false);
render_obj=material->CreateRenderable(VERTEX_COUNT);
material_instance=material->CreateInstance();
texture=vulkan::CreateTextureFromFile(device,OS_TEXT("res/image/lena.Tex2D"));
sampler=db->CreateSampler();
material_instance->BindSampler("tex",texture,sampler);
material_instance->BindUBO("world",ubo_mvp);
material_instance->Update();
db->Add(material);
db->Add(material_instance);
db->Add(texture);
db->Add(render_obj);
return(true);
}
bool InitUBO()
{
const VkExtent2D extent=sc_render_target->GetExtent();
2020-06-28 22:16:07 +08:00
2020-07-08 21:56:39 +08:00
cam.width=extent.width;
cam.height=extent.height;
2020-06-28 22:16:07 +08:00
2020-07-08 21:56:39 +08:00
cam.Refresh();
2020-06-28 22:16:07 +08:00
2020-07-08 21:56:39 +08:00
ubo_mvp=db->CreateUBO(sizeof(WorldMatrix),&cam.matrix);
2020-06-28 22:16:07 +08:00
2020-07-08 21:56:39 +08:00
if(!ubo_mvp)
return(false);
2020-06-28 22:16:07 +08:00
2020-07-08 21:56:39 +08:00
return(true);
}
2020-06-28 22:16:07 +08:00
2020-07-08 21:56:39 +08:00
void InitVBO()
{
vertex_buffer =db->CreateVBO(FMT_RGBA32F,VERTEX_COUNT,vertex_data);
tex_coord_buffer=db->CreateVBO(FMT_RGBA32F,VERTEX_COUNT,tex_coord_data);
2020-06-28 22:16:07 +08:00
2020-07-08 21:56:39 +08:00
render_obj->Set("Vertex",vertex_buffer);
render_obj->Set("TexCoord",tex_coord_buffer);
}
bool InitPipeline()
{
AutoDelete<vulkan::PipelineCreater>
pipeline_creater=new vulkan::PipelineCreater(device,material,sc_render_target);
pipeline_creater->CloseCullFace();
pipeline_creater->Set(PRIM_RECTANGLES);
pipeline=pipeline_creater->Create();
db->Add(pipeline);
return pipeline;
}
public:
bool Init()
{
if(!VulkanApplicationFramework::Init(SCREEN_SIZE,SCREEN_SIZE))
return(false);
if(!InitUBO())
return(false);
if(!InitMaterial())
return(false);
InitVBO();
if(!InitPipeline())
return(false);
BuildCommandBuffer(pipeline,material_instance,render_obj);
return(true);
}
void Resize(int w,int h)override
{
cam.width=w;
cam.height=h;
cam.Refresh();
ubo_mvp->Write(&cam.matrix);
BuildCommandBuffer(pipeline,material_instance,render_obj);
}
};//class TestApp:public VulkanApplicationFramework
int os_main(int,os_char **)
{
TestApp app;
2020-06-28 22:16:07 +08:00
2020-07-08 21:56:39 +08:00
if(!app.Init())
return(-1);
2020-06-28 22:16:07 +08:00
2020-07-08 21:56:39 +08:00
while(app.Run());
2020-06-28 22:16:07 +08:00
2020-07-08 21:56:39 +08:00
return 0;
}