ULRE/example/Vulkan/SceneTree.cpp

203 lines
4.6 KiB
C++
Raw Normal View History

// 5.SceneTree
// <20><><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еij<D0B5><C4B3><EFBFBD><EFBFBD>У<EFBFBD>ÿһ<C3BF><D2BB><EFBFBD>ڵ<EFBFBD><DAB5>Ա任<D4B1><E4BBBB><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ȷ
#include"VulkanAppFramework.h"
#include<hgl/filesystem/FileSystem.h>
#include<hgl/graph/InlineGeometry.h>
#include<hgl/graph/SceneDB.h>
#include<hgl/graph/RenderableInstance.h>
#include<hgl/graph/RenderList.h>
#include<hgl/Time.h>
using namespace hgl;
using namespace hgl::graph;
constexpr uint32_t SCREEN_WIDTH=128;
constexpr uint32_t SCREEN_HEIGHT=128;
class TestApp:public VulkanApplicationFramework
{
private:
double start_time;
SceneNode render_root;
RenderList render_list;
Camera camera;
vulkan::Material * material =nullptr;
vulkan::DescriptorSets * descriptor_sets =nullptr;
vulkan::Renderable * ro_cube =nullptr;
vulkan::Buffer * ubo_world_matrix =nullptr;
vulkan::Pipeline * pipeline_line =nullptr;
public:
TestApp()
{
start_time=GetDoubleTime();
}
~TestApp()=default;
private:
void InitCamera()
{
camera.type=CameraType::Perspective;
2019-06-11 20:10:46 +08:00
camera.center.Set(0,0,30,1);
camera.eye.Set(100,100,100,1);
camera.width=SCREEN_WIDTH;
camera.height=SCREEN_HEIGHT;
camera.Refresh(); //<2F><><EFBFBD>¾<EFBFBD><C2BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
bool InitMaterial()
{
material=shader_manage->CreateMaterial(OS_TEXT("OnlyPosition3D.vert.spv"),
OS_TEXT("FlatColor.frag.spv"));
if(!material)
return(false);
descriptor_sets=material->CreateDescriptorSets();
db->Add(material);
db->Add(descriptor_sets);
return(true);
}
void CreateRenderObject()
{
struct CubeCreateInfo cci;
ro_cube=CreateCube(db,material,&cci);
}
bool InitUBO()
{
const VkExtent2D extent=device->GetExtent();
ubo_world_matrix=db->CreateUBO(sizeof(WorldMatrix),&camera.matrix);
if(!ubo_world_matrix)
return(false);
if(!descriptor_sets->BindUBO(material->GetUBO("world"),*ubo_world_matrix))
return(false);
descriptor_sets->Update();
return(true);
}
bool InitPipeline()
{
constexpr os_char PIPELINE_FILENAME[]=OS_TEXT("2DSolid.pipeline");
{
vulkan::PipelineCreater *pipeline_creater=new vulkan::PipelineCreater(device,material,device->GetRenderPass(),device->GetExtent());
pipeline_creater->SetDepthTest(true);
pipeline_creater->SetDepthWrite(true);
pipeline_creater->CloseCullFace();
pipeline_creater->SetPolygonMode(VK_POLYGON_MODE_LINE);
pipeline_creater->Set(PRIM_TRIANGLES);
pipeline_line=pipeline_creater->Create();
if(!pipeline_line)
return(false);
db->Add(pipeline_line);
delete pipeline_creater;
}
return pipeline_line;
}
bool InitScene()
{
2019-06-11 20:10:46 +08:00
SceneNode *cur_node;
2019-06-11 20:10:46 +08:00
uint count;
float size;
RenderableInstance *ri=db->CreateRenderableInstance(pipeline_line,descriptor_sets,ro_cube);
for(uint i=0;i<360;i++)
{
2019-06-11 20:10:46 +08:00
size=(i+1)/100.0f;
cur_node=render_root.CreateSubNode( rotate(i/5.0f,camera.up_vector)*
2019-06-11 20:10:46 +08:00
translate(i/4.0f,0,0)*
scale(size));
count=(rand()%16)+1;
for(uint n=0;n<count;n++)
cur_node->Add(ri,translate(0,0,size*n*1.01));
}
render_root.RefreshMatrix();
render_list.Clear();
render_root.ExpendToList(&render_list);
BuildCommandBuffer(&render_list);
return(true);
}
public:
bool Init()
{
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
return(false);
InitCamera();
if(!InitMaterial())
return(false);
CreateRenderObject();
if(!InitUBO())
return(false);
if(!InitPipeline())
return(false);
if(!InitScene())
return(false);
return(true);
}
void Draw() override
{
VulkanApplicationFramework::Draw();
2019-06-11 20:10:46 +08:00
Matrix4f rot=rotate(GetDoubleTime()-start_time,camera.up_vector);
render_root.RefreshMatrix(&rot);
render_list.Clear();
render_root.ExpendToList(&render_list);
BuildCommandBuffer(&render_list);
}
};//class TestApp:public VulkanApplicationFramework
int main(int,char **)
{
TestApp app;
if(!app.Init())
return(-1);
while(app.Run());
return 0;
}