ULRE/example/Vulkan/indices_rect.cpp

148 lines
3.3 KiB
C++
Raw Normal View History

2020-01-20 15:02:40 +08:00
// indices_rect
// 该示例演示使用索引数据画一个矩形,并使用了颜色材质
2019-05-06 12:00:03 +08:00
#include"VulkanAppFramework.h"
#include<hgl/math/Math.h>
using namespace hgl;
using namespace hgl::graph;
2022-01-07 21:16:44 +08:00
constexpr uint32_t SCREEN_WIDTH=256;
constexpr uint32_t SCREEN_HEIGHT=256;
2019-05-06 12:00:03 +08:00
constexpr uint32_t VERTEX_COUNT=4;
2020-07-14 20:42:01 +08:00
static Vector4f color(1,1,1,1);
2020-01-20 15:02:40 +08:00
2019-05-06 12:00:03 +08:00
constexpr float vertex_data[VERTEX_COUNT][2]=
{
2020-09-21 17:19:47 +08:00
{0,0},
{SCREEN_WIDTH,0},
{0,SCREEN_HEIGHT},
{SCREEN_WIDTH,SCREEN_HEIGHT}
2019-05-06 12:00:03 +08:00
};
2020-07-16 12:00:02 +08:00
2019-05-06 12:00:03 +08:00
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
2020-09-21 17:19:47 +08:00
Camera cam;
2019-07-17 04:49:49 +08:00
2021-06-19 20:56:32 +08:00
MaterialInstance * material_instance =nullptr;
2020-10-21 12:47:06 +08:00
RenderableInstance *renderable_instance =nullptr;
2021-06-19 20:56:32 +08:00
GPUBuffer * ubo_camera_info =nullptr;
2019-05-06 12:00:03 +08:00
2020-10-21 12:47:06 +08:00
Pipeline * pipeline =nullptr;
2019-05-06 12:00:03 +08:00
private:
bool InitMaterial()
{
2020-09-21 17:19:47 +08:00
material_instance=db->CreateMaterialInstance(OS_TEXT("res/material/FragColor"));
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
return pipeline;
2019-05-06 12:00:03 +08:00
}
2020-09-21 17:19:47 +08:00
2019-05-06 12:00:03 +08:00
bool InitUBO()
{
const VkExtent2D extent=sc_render_target->GetExtent();
2019-05-06 12:00:03 +08:00
2020-09-21 17:19:47 +08:00
cam.vp_width=cam.width=extent.width;
cam.vp_height=cam.height=extent.height;
2020-07-14 20:42:01 +08:00
2020-09-21 17:19:47 +08:00
cam.Refresh();
ubo_camera_info=db->CreateUBO(sizeof(CameraInfo),&cam.info);
2020-09-21 17:19:47 +08:00
if(!ubo_camera_info)
2020-09-21 17:19:47 +08:00
return(false);
2021-06-19 20:56:32 +08:00
{
MaterialParameters *mp_global=material_instance->GetMP(DescriptorSetsType::Global);
2021-06-19 20:56:32 +08:00
if(!mp_global)
return(false);
2020-09-21 17:19:47 +08:00
2021-06-19 20:56:32 +08:00
if(!mp_global->BindUBO("g_camera",ubo_camera_info))return(false);
2019-05-06 12:00:03 +08:00
2021-06-19 20:56:32 +08:00
mp_global->Update();
}
return(true);
2019-05-06 12:00:03 +08:00
}
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);
2019-05-06 12:00:03 +08:00
if(!render_obj->Set(VAN::Position,db->CreateVBO(VF_V2F,VERTEX_COUNT,vertex_data)))return(false);
2020-09-21 19:31:34 +08:00
if(!render_obj->Set(db->CreateIBO16(INDEX_COUNT,index_data)))return(false);
2019-05-06 12:00:03 +08:00
2020-09-21 19:31:34 +08:00
renderable_instance=db->CreateRenderableInstance(render_obj,material_instance,pipeline);
2019-05-06 12:00:03 +08:00
2020-09-21 17:19:47 +08:00
return(true);
2019-05-06 12:00:03 +08:00
}
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);
2020-09-21 17:19:47 +08:00
if(!InitVBO())
2019-05-06 12:00:03 +08:00
return(false);
2020-09-21 19:31:34 +08:00
BuildCommandBuffer(renderable_instance);
2019-05-06 12:00:03 +08:00
return(true);
}
2020-09-21 19:31:34 +08:00
void Resize(int w,int h)override
{
2020-09-21 19:31:34 +08:00
cam.vp_width=w;
cam.vp_height=h;
cam.Refresh();
ubo_camera_info->Write(&cam.info);
2020-09-21 19:31:34 +08:00
BuildCommandBuffer(renderable_instance);
}
2019-05-06 12:00:03 +08:00
};//class TestApp:public VulkanApplicationFramework
int main(int,char **)
{
#ifdef _DEBUG
2020-10-21 12:47:06 +08:00
if(!CheckStrideBytesByFormat())
2019-05-06 12:00:03 +08:00
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;
}