2019-06-14 17:13:30 +08:00
|
|
|
|
// 5.SceneTree
|
|
|
|
|
// 用于测试树形排列的场景中,每一级节点对变换矩阵的处理是否正确
|
2019-06-11 19:48:59 +08:00
|
|
|
|
|
|
|
|
|
#include"VulkanAppFramework.h"
|
|
|
|
|
#include<hgl/filesystem/FileSystem.h>
|
|
|
|
|
#include<hgl/graph/InlineGeometry.h>
|
2020-09-02 18:16:15 +08:00
|
|
|
|
#include<hgl/graph/vulkan/VKDatabase.h>
|
2019-06-11 19:48:59 +08:00
|
|
|
|
#include<hgl/graph/RenderableInstance.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
|
|
|
|
|
{
|
|
|
|
|
Vector4f color;
|
|
|
|
|
Vector4f abiment;
|
|
|
|
|
}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;
|
|
|
|
|
RenderList render_list;
|
|
|
|
|
|
|
|
|
|
vulkan::Material * material =nullptr;
|
2020-01-20 20:00:03 +08:00
|
|
|
|
vulkan::MaterialInstance * material_instance =nullptr;
|
2019-06-11 19:48:59 +08:00
|
|
|
|
|
2020-01-20 20:32:09 +08:00
|
|
|
|
vulkan::Buffer * ubo_color =nullptr;
|
2020-06-16 20:01:36 +08:00
|
|
|
|
vulkan::Buffer * ubo_sun =nullptr;
|
2020-01-20 20:32:09 +08:00
|
|
|
|
|
2019-06-17 10:37:59 +08:00
|
|
|
|
vulkan::Renderable * renderable_object =nullptr;
|
2019-06-11 19:48:59 +08:00
|
|
|
|
|
|
|
|
|
vulkan::Pipeline * pipeline_line =nullptr;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
TestApp()
|
|
|
|
|
{
|
|
|
|
|
start_time=GetDoubleTime();
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-11 23:14:13 +08:00
|
|
|
|
~TestApp()=default;
|
2019-06-11 19:48:59 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
bool InitMaterial()
|
|
|
|
|
{
|
2020-06-20 15:25:57 +08:00
|
|
|
|
material=shader_manage->CreateMaterial(OS_TEXT("res/shader/VertexLight.vert"),
|
2020-06-16 20:01:36 +08:00
|
|
|
|
OS_TEXT("res/shader/VertexColor.frag"));
|
2019-06-11 19:48:59 +08:00
|
|
|
|
if(!material)
|
|
|
|
|
return(false);
|
|
|
|
|
|
2020-01-20 20:00:03 +08:00
|
|
|
|
material_instance=material->CreateInstance();
|
2019-06-11 19:48:59 +08:00
|
|
|
|
|
|
|
|
|
db->Add(material);
|
2020-01-20 20:00:03 +08:00
|
|
|
|
db->Add(material_instance);
|
2019-06-11 19:48:59 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreateRenderObject()
|
|
|
|
|
{
|
2020-06-16 20:01:36 +08:00
|
|
|
|
renderable_object=CreateRenderableSphere(db,material,40);
|
2019-06-11 19:48:59 +08:00
|
|
|
|
|
2020-06-16 20:01:36 +08:00
|
|
|
|
db->Add(renderable_object);
|
2019-06-11 19:48:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitUBO()
|
|
|
|
|
{
|
2020-06-16 20:01:36 +08:00
|
|
|
|
LCG lcg;
|
2020-01-20 20:32:09 +08:00
|
|
|
|
|
2020-06-16 20:01:36 +08:00
|
|
|
|
color_material.color=Vector4f(1,1,1,1.0);
|
|
|
|
|
color_material.abiment.Set(0.25,0.25,0.25,1.0);
|
|
|
|
|
ubo_color=device->CreateUBO(sizeof(color_material),&color_material);
|
|
|
|
|
|
|
|
|
|
sun_direction=Vector3f::RandomDir(lcg);
|
|
|
|
|
ubo_sun=device->CreateUBO(sizeof(sun_direction),&sun_direction);
|
|
|
|
|
|
|
|
|
|
material_instance->BindUBO("world",GetCameraMatrixBuffer());
|
2020-01-20 20:32:09 +08:00
|
|
|
|
material_instance->BindUBO("color_material",ubo_color);
|
2020-06-16 20:01:36 +08:00
|
|
|
|
material_instance->BindUBO("sun",ubo_sun);
|
2019-06-11 19:48:59 +08:00
|
|
|
|
|
2020-01-20 20:00:03 +08:00
|
|
|
|
material_instance->Update();
|
2020-06-16 20:01:36 +08:00
|
|
|
|
|
|
|
|
|
db->Add(ubo_color);
|
|
|
|
|
db->Add(ubo_sun);
|
2019-06-11 19:48:59 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitPipeline()
|
2019-12-04 21:05:09 +08:00
|
|
|
|
{
|
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->SetDepthTest(true);
|
|
|
|
|
pipeline_creater->SetDepthWrite(true);
|
|
|
|
|
pipeline_creater->CloseCullFace();
|
2020-06-16 20:01:36 +08:00
|
|
|
|
pipeline_creater->SetPolygonMode(VK_POLYGON_MODE_FILL);
|
2020-09-02 19:06:12 +08:00
|
|
|
|
pipeline_creater->Set(Prim::Triangles);
|
2019-07-05 17:00:49 +08:00
|
|
|
|
|
|
|
|
|
pipeline_line=pipeline_creater->Create();
|
|
|
|
|
if(!pipeline_line)
|
|
|
|
|
return(false);
|
2019-06-11 19:48:59 +08:00
|
|
|
|
|
2019-07-05 17:00:49 +08:00
|
|
|
|
db->Add(pipeline_line);
|
|
|
|
|
return(true);
|
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
|
|
|
|
|
2020-01-20 20:00:03 +08:00
|
|
|
|
RenderableInstance *ri=db->CreateRenderableInstance(pipeline_line,material_instance,renderable_object);
|
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
|
|
|
|
|
2019-06-11 20:16:44 +08:00
|
|
|
|
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));
|
2019-06-11 19:48:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-11 23:14:13 +08:00
|
|
|
|
render_root.RefreshMatrix();
|
|
|
|
|
render_list.Clear();
|
|
|
|
|
render_root.ExpendToList(&render_list);
|
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);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-06-11 20:10:46 +08:00
|
|
|
|
Matrix4f rot=rotate(GetDoubleTime()-start_time,camera.up_vector);
|
2019-06-11 19:48:59 +08:00
|
|
|
|
|
|
|
|
|
render_root.RefreshMatrix(&rot);
|
2019-06-11 19:56:01 +08:00
|
|
|
|
render_list.Clear();
|
2019-06-11 19:48:59 +08:00
|
|
|
|
render_root.ExpendToList(&render_list);
|
|
|
|
|
}
|
2019-06-18 00:48:05 +08:00
|
|
|
|
|
|
|
|
|
void BuildCommandBuffer(uint32 index)
|
2019-06-13 23:12:11 +08:00
|
|
|
|
{
|
2019-06-18 00:48:05 +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;
|
|
|
|
|
}
|