2020-01-20 15:02:40 +08:00
|
|
|
|
// 该范例主要用于测试gl_FragCoord值
|
|
|
|
|
|
|
|
|
|
#include"VulkanAppFramework.h"
|
|
|
|
|
#include<hgl/math/Math.h>
|
|
|
|
|
#include<hgl/filesystem/FileSystem.h>
|
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
|
|
|
|
constexpr uint32_t SCREEN_WIDTH=128;
|
|
|
|
|
constexpr uint32_t SCREEN_HEIGHT=128;
|
|
|
|
|
|
|
|
|
|
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}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class TestApp:public VulkanApplicationFramework
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
Camera cam;
|
|
|
|
|
|
2020-01-20 15:39:39 +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;
|
2020-01-20 15:02:40 +08:00
|
|
|
|
|
|
|
|
|
vulkan::Pipeline * pipeline =nullptr;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
bool InitMaterial()
|
|
|
|
|
{
|
2020-09-21 12:37:44 +08:00
|
|
|
|
material_instance=db->CreateMaterialInstance(OS_TEXT("res/material/FragColor"));
|
|
|
|
|
|
|
|
|
|
if(!material_instance)
|
|
|
|
|
return(false);
|
|
|
|
|
|
2020-10-16 17:24:01 +08:00
|
|
|
|
pipeline=CreatePipeline(material_instance,vulkan::InlinePipeline::Solid2D,Prim::TriangleStrip);
|
2020-09-21 12:37:44 +08:00
|
|
|
|
|
|
|
|
|
if(!pipeline)
|
2020-01-20 15:02:40 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2020-01-20 15:13:08 +08:00
|
|
|
|
return(true);
|
2020-01-20 15:02:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitUBO()
|
|
|
|
|
{
|
|
|
|
|
const VkExtent2D extent=sc_render_target->GetExtent();
|
|
|
|
|
|
2020-06-11 19:24:13 +08:00
|
|
|
|
cam.vp_width=cam.width=extent.width;
|
|
|
|
|
cam.vp_height=cam.height=extent.height;
|
2020-01-20 15:02:40 +08:00
|
|
|
|
|
|
|
|
|
cam.Refresh();
|
2020-01-20 15:13:08 +08:00
|
|
|
|
|
2020-09-21 12:37:44 +08:00
|
|
|
|
ubo_world_matrix=db->CreateUBO(sizeof(WorldMatrix),&cam.matrix);
|
2020-01-20 15:13:08 +08:00
|
|
|
|
|
2020-07-14 20:42:01 +08:00
|
|
|
|
if(!ubo_world_matrix)
|
2020-01-23 21:00:35 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2020-09-21 12:37:44 +08:00
|
|
|
|
if(!material_instance->BindUBO("world",ubo_world_matrix))return(false);
|
|
|
|
|
if(!material_instance->BindUBO("frag_world",ubo_world_matrix))return(false);
|
2020-01-20 15:02:40 +08:00
|
|
|
|
|
2020-01-20 15:39:39 +08:00
|
|
|
|
material_instance->Update();
|
2020-01-20 15:02:40 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
2020-01-23 21:00:35 +08:00
|
|
|
|
|
2020-09-21 12:37:44 +08:00
|
|
|
|
bool InitVBO()
|
2020-01-20 15:02:40 +08:00
|
|
|
|
{
|
2020-09-21 19:31:34 +08:00
|
|
|
|
auto render_obj=db->CreateRenderable(VERTEX_COUNT);
|
2020-09-21 12:37:44 +08:00
|
|
|
|
if(!render_obj)return(false);
|
2020-01-20 15:02:40 +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);
|
2020-01-20 15:02:40 +08:00
|
|
|
|
|
2020-09-21 19:31:34 +08:00
|
|
|
|
renderable_instance=db->CreateRenderableInstance(render_obj,material_instance,pipeline);
|
2020-09-21 12:37:44 +08:00
|
|
|
|
return(true);
|
2020-01-20 15:02:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
bool Init()
|
|
|
|
|
{
|
|
|
|
|
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitMaterial())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitUBO())
|
|
|
|
|
return(false);
|
|
|
|
|
|
2020-09-21 12:37:44 +08:00
|
|
|
|
if(!InitVBO())
|
2020-01-20 15:02:40 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2020-09-21 19:31:34 +08:00
|
|
|
|
BuildCommandBuffer(renderable_instance);
|
2020-01-20 15:02:40 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Resize(int w,int h)override
|
|
|
|
|
{
|
2020-06-11 19:24:13 +08:00
|
|
|
|
cam.vp_width=w;
|
|
|
|
|
cam.vp_height=h;
|
2020-01-20 15:02:40 +08:00
|
|
|
|
|
|
|
|
|
cam.Refresh();
|
|
|
|
|
|
2020-07-14 20:42:01 +08:00
|
|
|
|
ubo_world_matrix->Write(&cam.matrix);
|
2020-01-20 15:02:40 +08:00
|
|
|
|
|
2020-09-21 19:31:34 +08:00
|
|
|
|
BuildCommandBuffer(renderable_instance);
|
2020-01-20 15:02:40 +08:00
|
|
|
|
}
|
|
|
|
|
};//class TestApp:public VulkanApplicationFramework
|
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
|
|
|
|
TestApp app;
|
|
|
|
|
|
|
|
|
|
if(!app.Init())
|
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
|
|
while(app.Run());
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|