2019-05-22 18:10:13 +08:00
|
|
|
|
// 3.Geometry2D
|
|
|
|
|
// 该范例有两个作用:
|
2019-05-27 20:05:22 +08:00
|
|
|
|
// 一、测试绘制2D几何体
|
|
|
|
|
// 二、试验动态合并材质渲染机制
|
|
|
|
|
// 三、试验SceneDB/SceneGraph
|
2019-05-22 00:30:42 +08:00
|
|
|
|
|
|
|
|
|
#include"VulkanAppFramework.h"
|
|
|
|
|
#include<hgl/filesystem/FileSystem.h>
|
2019-05-24 19:28:27 +08:00
|
|
|
|
#include<hgl/graph/InlineGeometry.h>
|
2019-05-27 15:15:39 +08:00
|
|
|
|
#include<hgl/graph/SceneDB.h>
|
2019-05-27 16:54:08 +08:00
|
|
|
|
#include<hgl/graph/RenderableInstance.h>
|
|
|
|
|
#include<hgl/graph/RenderList.h>
|
2019-05-22 00:30:42 +08:00
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
|
|
|
|
bool SaveToFile(const OSString &filename,VK_NAMESPACE::PipelineCreater *pc);
|
|
|
|
|
bool LoadFromFile(const OSString &filename,VK_NAMESPACE::PipelineCreater *pc);
|
|
|
|
|
|
2019-05-22 18:10:13 +08:00
|
|
|
|
constexpr uint32_t SCREEN_WIDTH=128;
|
|
|
|
|
constexpr uint32_t SCREEN_HEIGHT=128;
|
2019-05-22 00:30:42 +08:00
|
|
|
|
|
|
|
|
|
struct WorldConfig
|
|
|
|
|
{
|
|
|
|
|
Matrix4f mvp;
|
|
|
|
|
}world;
|
|
|
|
|
|
|
|
|
|
class TestApp:public VulkanApplicationFramework
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
|
2019-05-27 20:05:22 +08:00
|
|
|
|
SceneNode render_root;
|
|
|
|
|
RenderList render_list;
|
2019-05-27 15:15:39 +08:00
|
|
|
|
|
2019-05-22 00:30:42 +08:00
|
|
|
|
vulkan::Material * material =nullptr;
|
2019-05-23 19:23:49 +08:00
|
|
|
|
vulkan::DescriptorSets * descriptor_sets =nullptr;
|
2019-06-14 17:13:30 +08:00
|
|
|
|
|
2019-05-27 18:05:21 +08:00
|
|
|
|
vulkan::Renderable *ro_rectangle =nullptr,
|
|
|
|
|
*ro_circle =nullptr,
|
|
|
|
|
*ro_round_rectangle =nullptr;
|
2019-05-23 19:23:49 +08:00
|
|
|
|
|
2019-05-22 00:30:42 +08:00
|
|
|
|
vulkan::Buffer * ubo_mvp =nullptr;
|
|
|
|
|
|
|
|
|
|
vulkan::Pipeline * pipeline =nullptr;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
bool InitMaterial()
|
|
|
|
|
{
|
2019-07-10 21:21:17 +08:00
|
|
|
|
material=shader_manage->CreateMaterial(OS_TEXT("res/shader/OnlyPosition.vert.spv"),
|
|
|
|
|
OS_TEXT("res/shader/FlatColor.frag.spv"));
|
2019-05-22 00:30:42 +08:00
|
|
|
|
if(!material)
|
|
|
|
|
return(false);
|
|
|
|
|
|
2019-05-23 19:23:49 +08:00
|
|
|
|
descriptor_sets=material->CreateDescriptorSets();
|
2019-05-27 15:15:39 +08:00
|
|
|
|
|
|
|
|
|
db->Add(material);
|
|
|
|
|
db->Add(descriptor_sets);
|
2019-05-22 00:30:42 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-27 18:05:21 +08:00
|
|
|
|
void CreateRenderObject()
|
2019-05-22 18:10:13 +08:00
|
|
|
|
{
|
2019-05-27 18:05:21 +08:00
|
|
|
|
{
|
|
|
|
|
struct RectangleCreateInfo rci;
|
|
|
|
|
|
|
|
|
|
rci.scope.Set(10,10,20,20);
|
|
|
|
|
|
2019-06-18 00:48:05 +08:00
|
|
|
|
ro_rectangle=CreateRenderableRectangle(db,material,&rci);
|
2019-05-27 18:05:21 +08:00
|
|
|
|
}
|
2019-05-24 19:46:46 +08:00
|
|
|
|
|
2019-05-27 18:05:21 +08:00
|
|
|
|
{
|
|
|
|
|
struct RoundRectangleCreateInfo rrci;
|
|
|
|
|
|
|
|
|
|
rrci.scope.Set(SCREEN_WIDTH-30,10,20,20);
|
|
|
|
|
rrci.radius=5;
|
|
|
|
|
rrci.round_per=5;
|
2019-05-24 19:46:46 +08:00
|
|
|
|
|
2019-06-18 00:48:05 +08:00
|
|
|
|
ro_round_rectangle=CreateRenderableRoundRectangle(db,material,&rrci);
|
2019-05-27 18:05:21 +08:00
|
|
|
|
}
|
2019-05-22 18:10:13 +08:00
|
|
|
|
|
2019-05-27 18:05:21 +08:00
|
|
|
|
{
|
|
|
|
|
struct CircleCreateInfo cci;
|
2019-05-22 18:10:13 +08:00
|
|
|
|
|
2019-05-27 18:05:21 +08:00
|
|
|
|
cci.center.x=SCREEN_WIDTH/2;
|
|
|
|
|
cci.center.y=SCREEN_HEIGHT/2;
|
2019-05-22 18:10:13 +08:00
|
|
|
|
|
2019-05-27 18:05:21 +08:00
|
|
|
|
cci.radius.x=SCREEN_WIDTH*0.35;
|
|
|
|
|
cci.radius.y=SCREEN_HEIGHT*0.35;
|
|
|
|
|
|
|
|
|
|
cci.field_count=8;
|
|
|
|
|
|
2019-06-18 00:48:05 +08:00
|
|
|
|
ro_circle=CreateRenderableCircle(db,material,&cci);
|
2019-05-27 18:05:21 +08:00
|
|
|
|
}
|
2019-05-22 18:10:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-22 00:30:42 +08:00
|
|
|
|
bool InitUBO()
|
|
|
|
|
{
|
2019-07-16 19:59:53 +08:00
|
|
|
|
const VkExtent2D extent=sc_render_target->GetExtent();
|
2019-05-22 00:30:42 +08:00
|
|
|
|
|
|
|
|
|
world.mvp=ortho(extent.width,extent.height);
|
|
|
|
|
|
2019-05-27 15:15:39 +08:00
|
|
|
|
ubo_mvp=db->CreateUBO(sizeof(WorldConfig),&world);
|
2019-05-22 00:30:42 +08:00
|
|
|
|
|
|
|
|
|
if(!ubo_mvp)
|
|
|
|
|
return(false);
|
|
|
|
|
|
2019-07-06 16:46:19 +08:00
|
|
|
|
if(!descriptor_sets->BindUBO(material->GetUBO("world"),ubo_mvp))
|
2019-05-22 00:30:42 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2019-05-23 19:23:49 +08:00
|
|
|
|
descriptor_sets->Update();
|
2019-05-22 00:30:42 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
2019-06-14 17:13:30 +08:00
|
|
|
|
|
2019-05-22 00:30:42 +08:00
|
|
|
|
bool InitPipeline()
|
|
|
|
|
{
|
2019-07-05 17:03:28 +08:00
|
|
|
|
AutoDelete<vulkan::PipelineCreater>
|
2019-07-16 20:22:29 +08:00
|
|
|
|
pipeline_creater=new vulkan::PipelineCreater(device,material,sc_render_target);
|
2019-07-05 17:00:49 +08:00
|
|
|
|
pipeline_creater->CloseCullFace();
|
|
|
|
|
pipeline_creater->Set(PRIM_TRIANGLE_FAN);
|
2019-05-22 00:30:42 +08:00
|
|
|
|
|
2019-07-05 17:00:49 +08:00
|
|
|
|
pipeline=pipeline_creater->Create();
|
|
|
|
|
if(!pipeline)return(false);
|
2019-05-22 00:30:42 +08:00
|
|
|
|
|
2019-07-05 17:00:49 +08:00
|
|
|
|
db->Add(pipeline);
|
|
|
|
|
return(true);
|
2019-05-22 00:30:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-27 16:54:08 +08:00
|
|
|
|
bool InitScene()
|
2019-05-23 19:23:49 +08:00
|
|
|
|
{
|
2019-05-27 20:05:22 +08:00
|
|
|
|
render_root.Add(db->CreateRenderableInstance(pipeline,descriptor_sets,ro_rectangle));
|
|
|
|
|
render_root.Add(db->CreateRenderableInstance(pipeline,descriptor_sets,ro_round_rectangle));
|
|
|
|
|
render_root.Add(db->CreateRenderableInstance(pipeline,descriptor_sets,ro_circle));
|
2019-05-23 19:23:49 +08:00
|
|
|
|
|
2019-05-27 20:05:22 +08:00
|
|
|
|
render_root.ExpendToList(&render_list);
|
2019-06-11 23:14:13 +08:00
|
|
|
|
BuildCommandBuffer(&render_list);
|
2019-05-22 00:30:42 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
bool Init()
|
|
|
|
|
{
|
|
|
|
|
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitMaterial())
|
|
|
|
|
return(false);
|
|
|
|
|
|
2019-05-27 18:05:21 +08:00
|
|
|
|
CreateRenderObject();
|
2019-05-22 00:30:42 +08:00
|
|
|
|
|
2019-05-22 18:10:13 +08:00
|
|
|
|
if(!InitUBO())
|
|
|
|
|
return(false);
|
2019-06-14 17:13:30 +08:00
|
|
|
|
|
2019-05-22 00:30:42 +08:00
|
|
|
|
if(!InitPipeline())
|
|
|
|
|
return(false);
|
|
|
|
|
|
2019-05-27 16:54:08 +08:00
|
|
|
|
if(!InitScene())
|
|
|
|
|
return(false);
|
2019-05-23 19:23:49 +08:00
|
|
|
|
|
2019-05-22 00:30:42 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
2019-06-13 23:12:11 +08:00
|
|
|
|
|
2019-06-18 00:48:05 +08:00
|
|
|
|
void Resize(int w,int h)
|
2019-06-13 23:12:11 +08:00
|
|
|
|
{
|
2019-06-14 17:13:30 +08:00
|
|
|
|
BuildCommandBuffer(&render_list);
|
2019-06-13 23:12:11 +08:00
|
|
|
|
}
|
2019-05-22 00:30:42 +08:00
|
|
|
|
};//class TestApp:public VulkanApplicationFramework
|
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
|
|
|
|
TestApp app;
|
|
|
|
|
|
|
|
|
|
if(!app.Init())
|
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
|
|
while(app.Run());
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|