ULRE/example/Vulkan/indices_rect.cpp

163 lines
3.7 KiB
C++
Raw Normal View History

2019-05-06 12:00:03 +08:00
// 1.indices_rect
// 该示例是0.triangle的进化演示使用索引数据画一个矩形
#include"VulkanAppFramework.h"
#include<hgl/math/Math.h>
using namespace hgl;
using namespace hgl::graph;
constexpr uint32_t SCREEN_WIDTH=128;
constexpr uint32_t SCREEN_HEIGHT=128;
struct WorldConfig
{
Matrix4f mvp;
2019-05-06 12:00:03 +08:00
}world;
constexpr uint32_t VERTEX_COUNT=4;
constexpr float vertex_data[VERTEX_COUNT][2]=
{
{SCREEN_WIDTH*0.25, SCREEN_HEIGHT*0.25},
{SCREEN_WIDTH*0.75, SCREEN_HEIGHT*0.25},
{SCREEN_WIDTH*0.25, SCREEN_HEIGHT*0.75},
{SCREEN_WIDTH*0.75, SCREEN_HEIGHT*0.75}
};
constexpr uint32_t INDEX_COUNT=6;
constexpr uint16 index_data[INDEX_COUNT]=
{
0,1,3,
0,3,2
};
class TestApp:public VulkanApplicationFramework
{
2019-05-07 03:18:39 +08:00
private:
2019-05-06 12:00:03 +08:00
vulkan::Material * material =nullptr;
2019-05-23 19:23:49 +08:00
vulkan::DescriptorSets * descriptor_sets =nullptr;
2019-05-07 03:18:39 +08:00
vulkan::Renderable * render_obj =nullptr;
2019-05-06 12:00:03 +08:00
vulkan::Buffer * ubo_mvp =nullptr;
vulkan::Pipeline * pipeline =nullptr;
vulkan::VertexBuffer * vertex_buffer =nullptr;
vulkan::IndexBuffer * index_buffer =nullptr;
public:
~TestApp()
{
SAFE_CLEAR(index_buffer);
SAFE_CLEAR(vertex_buffer);
SAFE_CLEAR(pipeline);
SAFE_CLEAR(ubo_mvp);
SAFE_CLEAR(render_obj);
2019-05-23 19:23:49 +08:00
SAFE_CLEAR(descriptor_sets);
2019-05-06 12:00:03 +08:00
SAFE_CLEAR(material);
}
private:
bool InitMaterial()
{
material=shader_manage->CreateMaterial(OS_TEXT("OnlyPosition.vert.spv"),
OS_TEXT("FlatColor.frag.spv"));
if(!material)
return(false);
2019-05-24 19:28:27 +08:00
render_obj=material->CreateRenderable(VERTEX_COUNT);
2019-05-23 19:23:49 +08:00
descriptor_sets=material->CreateDescriptorSets();
2019-05-06 12:00:03 +08:00
return(true);
}
bool InitUBO()
{
const VkExtent2D extent=device->GetExtent();
world.mvp=ortho(extent.width,extent.height);
ubo_mvp=device->CreateUBO(sizeof(WorldConfig),&world);
if(!ubo_mvp)
return(false);
2019-05-23 19:23:49 +08:00
if(!descriptor_sets->BindUBO(material->GetUBO("world"),*ubo_mvp))
return(false);
2019-05-23 19:23:49 +08:00
descriptor_sets->Update();
return(true);
2019-05-06 12:00:03 +08:00
}
void InitVBO()
2019-05-07 03:18:39 +08:00
{
2019-05-06 12:00:03 +08:00
vertex_buffer =device->CreateVBO(FMT_RG32F,VERTEX_COUNT,vertex_data);
index_buffer =device->CreateIBO16(INDEX_COUNT,index_data);
render_obj->Set("Vertex",vertex_buffer);
render_obj->Set(index_buffer);
}
bool InitPipeline()
{
SharedPtr<vulkan::PipelineCreater>
2019-07-01 17:04:02 +08:00
pipeline_creater=new vulkan::PipelineCreater(device,material,device->GetMainRenderPass(),device->GetExtent());
2019-05-06 12:00:03 +08:00
pipeline_creater->SetDepthTest(false);
pipeline_creater->SetDepthWrite(false);
pipeline_creater->CloseCullFace();
pipeline_creater->Set(PRIM_TRIANGLES);
pipeline=pipeline_creater->Create();
return pipeline;
}
public:
2019-05-07 03:18:39 +08:00
bool Init()
2019-05-06 12:00:03 +08:00
{
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
return(false);
if(!InitMaterial())
return(false);
if(!InitUBO())
return(false);
InitVBO();
if(!InitPipeline())
return(false);
BuildCommandBuffer(pipeline,descriptor_sets,render_obj);
2019-05-06 12:00:03 +08:00
return(true);
}
void Resize(int,int)override
{
BuildCommandBuffer(pipeline,descriptor_sets,render_obj);
}
2019-05-06 12:00:03 +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());
2019-05-07 03:18:39 +08:00
2019-05-06 12:00:03 +08:00
return 0;
}