2022-03-09 20:06:06 +08:00
|
|
|
|
// SceneTree
|
2022-06-18 18:47:08 +08:00
|
|
|
|
// 用于测试树形排列的场景中,每一级节点对变换矩阵的处理是否正确
|
2019-06-11 19:48:59 +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;
|
2019-06-11 19:48:59 +08:00
|
|
|
|
|
2019-06-15 18:07:20 +08:00
|
|
|
|
class TestApp:public CameraAppFramework
|
2019-06-11 19:48:59 +08:00
|
|
|
|
{
|
2020-06-16 20:01:36 +08:00
|
|
|
|
struct
|
|
|
|
|
{
|
2022-03-28 16:10:24 +08:00
|
|
|
|
Color4f diffuse; //虽然shader中写vec3,但这里依然用Color4f
|
2022-03-09 20:06:06 +08:00
|
|
|
|
Color4f abiment;
|
2020-06-16 20:01:36 +08:00
|
|
|
|
}color_material;
|
|
|
|
|
|
|
|
|
|
Vector3f sun_direction;
|
2020-01-20 20:32:09 +08:00
|
|
|
|
|
2019-06-11 19:48:59 +08:00
|
|
|
|
private:
|
|
|
|
|
|
2019-06-11 23:14:13 +08:00
|
|
|
|
double start_time;
|
2019-06-11 19:48:59 +08:00
|
|
|
|
|
|
|
|
|
SceneNode render_root;
|
2022-03-09 20:33:26 +08:00
|
|
|
|
RenderList *render_list;
|
2019-06-11 19:48:59 +08:00
|
|
|
|
|
2022-03-09 20:06:06 +08:00
|
|
|
|
Material * material =nullptr;
|
|
|
|
|
MaterialInstance * material_instance =nullptr;
|
2019-06-11 19:48:59 +08:00
|
|
|
|
|
2022-10-14 17:52:35 +08:00
|
|
|
|
DeviceBuffer * ubo_color =nullptr;
|
|
|
|
|
DeviceBuffer * ubo_sun =nullptr;
|
2020-01-20 20:32:09 +08:00
|
|
|
|
|
2022-06-24 21:36:24 +08:00
|
|
|
|
Primitive * primitive =nullptr;
|
2019-06-11 19:48:59 +08:00
|
|
|
|
|
2022-03-09 20:06:06 +08:00
|
|
|
|
Pipeline * pipeline =nullptr;
|
2019-06-11 19:48:59 +08:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
TestApp()
|
|
|
|
|
{
|
|
|
|
|
start_time=GetDoubleTime();
|
|
|
|
|
}
|
2022-03-28 16:10:24 +08:00
|
|
|
|
|
|
|
|
|
~TestApp()
|
|
|
|
|
{
|
|
|
|
|
SAFE_CLEAR(render_list);
|
|
|
|
|
}
|
2019-06-11 19:48:59 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
bool InitMaterial()
|
|
|
|
|
{
|
2022-06-18 18:47:08 +08:00
|
|
|
|
material=db->CreateMaterial(OS_TEXT("res/material/PhongFullyRough"));
|
2019-06-11 19:48:59 +08:00
|
|
|
|
if(!material)
|
|
|
|
|
return(false);
|
|
|
|
|
|
2022-03-09 20:06:06 +08:00
|
|
|
|
material_instance=db->CreateMaterialInstance(material);
|
2019-06-11 19:48:59 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreateRenderObject()
|
|
|
|
|
{
|
2022-10-11 19:16:06 +08:00
|
|
|
|
primitive=inline_geometry::CreateSphere(db,material_instance->GetVIL(),40);
|
2019-06-11 19:48:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitUBO()
|
|
|
|
|
{
|
2022-03-28 16:10:24 +08:00
|
|
|
|
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
|
|
|
|
|
2022-03-28 16:10:24 +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()));
|
2020-06-16 20:01:36 +08:00
|
|
|
|
|
2022-03-28 16:10:24 +08:00
|
|
|
|
ubo_sun=db->CreateUBO(sizeof(sun_direction),&sun_direction);
|
2022-03-09 20:06:06 +08:00
|
|
|
|
if(!ubo_sun)return(false);
|
2020-06-16 20:01:36 +08:00
|
|
|
|
|
2022-03-28 16:10:24 +08:00
|
|
|
|
{
|
|
|
|
|
MaterialParameters *mp=material_instance->GetMP(DescriptorSetsType::Value);
|
|
|
|
|
|
|
|
|
|
if(!mp)return(false);
|
|
|
|
|
|
|
|
|
|
mp->BindUBO("material",ubo_color);
|
|
|
|
|
mp->BindUBO("sun",ubo_sun);
|
2019-06-11 19:48:59 +08:00
|
|
|
|
|
2022-03-28 16:10:24 +08:00
|
|
|
|
mp->Update();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BindCameraUBO(material_instance);
|
2019-06-11 19:48:59 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitPipeline()
|
2019-12-04 21:05:09 +08:00
|
|
|
|
{
|
2022-03-09 20:06:06 +08:00
|
|
|
|
pipeline=CreatePipeline(material_instance,InlinePipeline::Solid3D,Prim::Triangles);
|
|
|
|
|
|
|
|
|
|
return pipeline;
|
2019-06-11 19:48:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitScene()
|
|
|
|
|
{
|
2019-06-11 20:10:46 +08:00
|
|
|
|
SceneNode *cur_node;
|
2019-06-11 19:48:59 +08:00
|
|
|
|
|
2019-06-11 20:10:46 +08:00
|
|
|
|
uint count;
|
|
|
|
|
float size;
|
2019-06-11 19:48:59 +08:00
|
|
|
|
|
2022-06-24 21:36:24 +08:00
|
|
|
|
Renderable *ri=db->CreateRenderable(primitive,material_instance,pipeline);
|
2019-06-11 20:03:58 +08:00
|
|
|
|
|
2019-06-11 19:48:59 +08:00
|
|
|
|
for(uint i=0;i<360;i++)
|
|
|
|
|
{
|
2019-06-11 20:10:46 +08:00
|
|
|
|
size=(i+1)/100.0f;
|
2019-06-14 17:13:30 +08:00
|
|
|
|
|
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);
|
2019-06-11 19:48:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-11 23:14:13 +08:00
|
|
|
|
render_root.RefreshMatrix();
|
2022-03-09 20:33:26 +08:00
|
|
|
|
render_list->Expend(camera->info,&render_root);
|
2019-06-11 19:48:59 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
bool Init()
|
|
|
|
|
{
|
2019-06-15 18:07:20 +08:00
|
|
|
|
if(!CameraAppFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
|
2019-06-11 19:48:59 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2022-03-28 16:10:24 +08:00
|
|
|
|
render_list=new RenderList(device);
|
|
|
|
|
|
2019-06-11 19:48:59 +08:00
|
|
|
|
if(!InitMaterial())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
CreateRenderObject();
|
|
|
|
|
|
|
|
|
|
if(!InitUBO())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitPipeline())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitScene())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Draw() override
|
|
|
|
|
{
|
2019-06-15 18:07:20 +08:00
|
|
|
|
CameraAppFramework::Draw();
|
2019-06-11 19:48:59 +08:00
|
|
|
|
|
2022-03-28 16:10:24 +08:00
|
|
|
|
Matrix4f rot=rotate(GetDoubleTime()-start_time,camera->world_up);
|
2019-06-11 19:48:59 +08:00
|
|
|
|
|
|
|
|
|
render_root.RefreshMatrix(&rot);
|
2022-03-28 16:10:24 +08:00
|
|
|
|
render_list->Expend(GetCameraInfo(),&render_root);
|
2019-06-11 19:48:59 +08:00
|
|
|
|
}
|
2019-06-18 00:48:05 +08:00
|
|
|
|
|
|
|
|
|
void BuildCommandBuffer(uint32 index)
|
2019-06-13 23:12:11 +08:00
|
|
|
|
{
|
2022-03-28 16:10:24 +08:00
|
|
|
|
VulkanApplicationFramework::BuildCommandBuffer(index,render_list);
|
2019-06-13 23:12:11 +08:00
|
|
|
|
}
|
2019-06-15 18:07:20 +08:00
|
|
|
|
};//class TestApp:public CameraAppFramework
|
2019-06-11 19:48:59 +08:00
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
|
|
|
|
TestApp app;
|
|
|
|
|
|
|
|
|
|
if(!app.Init())
|
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
|
|
while(app.Run());
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|