ULRE/example/Vulkan/SceneTree.cpp

189 lines
4.1 KiB
C++
Raw Normal View History

2022-03-09 20:06:06 +08:00
// SceneTree
2022-06-18 18:47:08 +08:00
// 用于测试树形排列的场景中,每一级节点对变换矩阵的处理是否正确
#include"VulkanAppFramework.h"
#include<hgl/filesystem/FileSystem.h>
#include<hgl/graph/InlineGeometry.h>
#include<hgl/graph/RenderList.h>
#include<hgl/Time.h>
using namespace hgl;
using namespace hgl::graph;
2020-06-12 15:55:51 +08:00
constexpr uint32_t SCREEN_WIDTH=1280;
constexpr uint32_t SCREEN_HEIGHT=720;
class TestApp:public CameraAppFramework
{
struct
{
Color4f diffuse; //虽然shader中写vec3但这里依然用Color4f
2022-03-09 20:06:06 +08:00
Color4f abiment;
}color_material;
Vector3f sun_direction;
private:
double start_time;
SceneNode render_root;
2022-03-09 20:33:26 +08:00
RenderList *render_list;
2022-03-09 20:06:06 +08:00
Material * material =nullptr;
MaterialInstance * material_instance =nullptr;
DeviceBuffer * ubo_color =nullptr;
DeviceBuffer * ubo_sun =nullptr;
Primitive * primitive =nullptr;
2022-03-09 20:06:06 +08:00
Pipeline * pipeline =nullptr;
public:
TestApp()
{
start_time=GetDoubleTime();
}
~TestApp()
{
SAFE_CLEAR(render_list);
}
private:
bool InitMaterial()
{
2022-06-18 18:47:08 +08:00
material=db->CreateMaterial(OS_TEXT("res/material/PhongFullyRough"));
if(!material)
return(false);
2022-03-09 20:06:06 +08:00
material_instance=db->CreateMaterialInstance(material);
return(true);
}
void CreateRenderObject()
{
primitive=inline_geometry::CreateSphere(db,material_instance->GetVIL(),40);
}
bool InitUBO()
{
color_material.diffuse.Set(1,1,1);
color_material.abiment.Set(0.25,0.25,0.25);
2022-03-09 20:06:06 +08:00
ubo_color=db->CreateUBO(sizeof(color_material),&color_material);
2022-03-09 20:06:06 +08:00
if(!ubo_color)return(false);
sun_direction=normalized(Vector3f(rand(),rand(),rand()));
ubo_sun=db->CreateUBO(sizeof(sun_direction),&sun_direction);
2022-03-09 20:06:06 +08:00
if(!ubo_sun)return(false);
{
MaterialParameters *mp=material_instance->GetMP(DescriptorSetsType::Value);
if(!mp)return(false);
mp->BindUBO("material",ubo_color);
mp->BindUBO("sun",ubo_sun);
mp->Update();
}
BindCameraUBO(material_instance);
return(true);
}
bool InitPipeline()
{
2022-03-09 20:06:06 +08:00
pipeline=CreatePipeline(material_instance,InlinePipeline::Solid3D,Prim::Triangles);
return pipeline;
}
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;
Renderable *ri=db->CreateRenderable(primitive,material_instance,pipeline);
for(uint i=0;i<360;i++)
{
2019-06-11 20:10:46 +08:00
size=(i+1)/100.0f;
2022-03-09 20:06:06 +08:00
cur_node=render_root.CreateSubNode( rotate(i/5.0f,camera->world_up)*
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++)
2022-06-18 18:47:08 +08:00
cur_node->CreateSubNode(translate(0,0,size*n*1.1),ri);
}
render_root.RefreshMatrix();
2022-03-09 20:33:26 +08:00
render_list->Expend(camera->info,&render_root);
return(true);
}
public:
bool Init()
{
if(!CameraAppFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
return(false);
render_list=new RenderList(device);
if(!InitMaterial())
return(false);
CreateRenderObject();
if(!InitUBO())
return(false);
if(!InitPipeline())
return(false);
if(!InitScene())
return(false);
return(true);
}
void Draw() override
{
CameraAppFramework::Draw();
Matrix4f rot=rotate(GetDoubleTime()-start_time,camera->world_up);
render_root.RefreshMatrix(&rot);
render_list->Expend(GetCameraInfo(),&render_root);
}
2019-06-18 00:48:05 +08:00
void BuildCommandBuffer(uint32 index)
{
VulkanApplicationFramework::BuildCommandBuffer(index,render_list);
}
};//class TestApp:public CameraAppFramework
int main(int,char **)
{
TestApp app;
if(!app.Init())
return(-1);
while(app.Run());
return 0;
}