2019-06-24 20:18:01 +08:00
|
|
|
|
// 7.InlineGeometryScene
|
2019-06-17 12:17:59 +08:00
|
|
|
|
// 全内置几何体场景
|
|
|
|
|
|
|
|
|
|
#include"VulkanAppFramework.h"
|
|
|
|
|
#include<hgl/filesystem/FileSystem.h>
|
|
|
|
|
#include<hgl/graph/InlineGeometry.h>
|
2020-10-21 12:39:22 +08:00
|
|
|
|
#include<hgl/graph/VKRenderResource.h>
|
2020-10-21 11:43:18 +08:00
|
|
|
|
#include<hgl/graph/VKRenderableInstance.h>
|
2019-06-17 12:17:59 +08:00
|
|
|
|
#include<hgl/graph/RenderList.h>
|
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
2020-06-16 20:01:36 +08:00
|
|
|
|
constexpr uint32_t SCREEN_WIDTH=1280;
|
|
|
|
|
constexpr uint32_t SCREEN_HEIGHT=720;
|
2019-06-17 12:17:59 +08:00
|
|
|
|
|
2020-10-21 21:05:03 +08:00
|
|
|
|
struct PhongLight
|
|
|
|
|
{
|
|
|
|
|
Vector4f color;
|
|
|
|
|
Vector4f position;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct PhongMaterial
|
|
|
|
|
{
|
|
|
|
|
Vector4f BaseColor;
|
2020-10-21 21:30:06 +08:00
|
|
|
|
Vector4f specular;
|
2020-10-21 21:05:03 +08:00
|
|
|
|
float ambient;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constexpr size_t v3flen=sizeof(PhongLight);
|
|
|
|
|
|
2019-06-17 12:17:59 +08:00
|
|
|
|
class TestApp:public CameraAppFramework
|
|
|
|
|
{
|
2020-10-21 21:05:03 +08:00
|
|
|
|
PhongLight light;
|
|
|
|
|
PhongMaterial phong;
|
2020-01-21 10:48:25 +08:00
|
|
|
|
|
2019-06-17 12:17:59 +08:00
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
SceneNode render_root;
|
|
|
|
|
RenderList render_list;
|
2020-09-21 20:34:47 +08:00
|
|
|
|
|
2020-10-21 12:47:06 +08:00
|
|
|
|
Material * material =nullptr;
|
|
|
|
|
MaterialInstance * material_instance =nullptr;
|
2020-09-27 20:58:25 +08:00
|
|
|
|
|
2020-10-21 18:24:00 +08:00
|
|
|
|
Material * axis_material =nullptr;
|
|
|
|
|
MaterialInstance * axis_mi =nullptr;
|
|
|
|
|
|
2020-10-21 12:47:06 +08:00
|
|
|
|
PipelineData * pipeline_data =nullptr;
|
2020-10-21 18:24:00 +08:00
|
|
|
|
Pipeline * axis_pipeline =nullptr;
|
2020-10-21 12:47:06 +08:00
|
|
|
|
Pipeline * pipeline_solid =nullptr;
|
2019-06-17 12:17:59 +08:00
|
|
|
|
|
2020-10-21 21:05:03 +08:00
|
|
|
|
GPUBuffer * ubo_light =nullptr;
|
|
|
|
|
GPUBuffer * ubo_phong =nullptr;
|
2019-06-17 12:17:59 +08:00
|
|
|
|
|
2020-10-21 18:24:00 +08:00
|
|
|
|
Renderable *ro_axis,
|
|
|
|
|
*ro_cube,
|
|
|
|
|
*ro_sphere,
|
|
|
|
|
*ro_torus,
|
|
|
|
|
*ro_cylinder,
|
|
|
|
|
*ro_cone;
|
2019-06-17 12:17:59 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
2020-09-27 20:58:25 +08:00
|
|
|
|
bool InitMaterial()
|
2019-06-17 12:17:59 +08:00
|
|
|
|
{
|
2020-10-21 21:05:03 +08:00
|
|
|
|
light.color.Set(1,1,1,1);
|
|
|
|
|
light.position.Set(1000,1000,1000,1.0);
|
|
|
|
|
|
2020-10-21 21:30:06 +08:00
|
|
|
|
phong.BaseColor.Set(1,0,0,1);
|
|
|
|
|
phong.ambient=0.05;
|
|
|
|
|
phong.specular.Set(0.3,0.3,0.3,32);
|
2020-10-21 21:05:03 +08:00
|
|
|
|
|
2020-10-21 18:24:00 +08:00
|
|
|
|
axis_material=db->CreateMaterial(OS_TEXT("res/material/VertexColor3D"));
|
|
|
|
|
if(!axis_material)return(false);
|
|
|
|
|
|
|
|
|
|
axis_mi=db->CreateMaterialInstance(axis_material);
|
|
|
|
|
if(!axis_mi)return(false);
|
|
|
|
|
|
|
|
|
|
axis_pipeline=CreatePipeline(axis_material,InlinePipeline::Solid3D,Prim::Lines);
|
|
|
|
|
if(!axis_pipeline)return(false);
|
|
|
|
|
|
2020-10-21 21:05:03 +08:00
|
|
|
|
material=db->CreateMaterial(OS_TEXT("res/material/VertexNormal"));
|
2020-09-27 20:58:25 +08:00
|
|
|
|
if(!material)return(false);
|
2020-09-21 20:34:47 +08:00
|
|
|
|
|
2020-09-27 20:58:25 +08:00
|
|
|
|
material_instance=db->CreateMaterialInstance(material);
|
|
|
|
|
if(!material_instance)return(false);
|
2019-06-17 12:17:59 +08:00
|
|
|
|
|
2020-10-21 12:47:06 +08:00
|
|
|
|
pipeline_data=GetPipelineData(InlinePipeline::Solid3D);
|
2020-09-27 20:58:25 +08:00
|
|
|
|
if(!pipeline_data)return(false);
|
2019-06-17 12:17:59 +08:00
|
|
|
|
|
2020-09-27 20:58:25 +08:00
|
|
|
|
|
|
|
|
|
pipeline_solid=CreatePipeline(material,pipeline_data,Prim::Triangles);
|
|
|
|
|
if(!pipeline_solid)return(false);
|
2020-09-21 20:34:47 +08:00
|
|
|
|
|
2019-06-17 12:17:59 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreateRenderObject()
|
|
|
|
|
{
|
|
|
|
|
{
|
2020-10-21 18:24:00 +08:00
|
|
|
|
struct AxisCreateInfo aci;
|
2019-06-17 12:17:59 +08:00
|
|
|
|
|
2020-10-21 18:24:00 +08:00
|
|
|
|
aci.size=200;
|
2019-06-17 12:17:59 +08:00
|
|
|
|
|
2020-10-21 18:24:00 +08:00
|
|
|
|
ro_axis=CreateRenderableAxis(db,axis_material,&aci);
|
2019-06-17 12:17:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2020-09-27 20:58:25 +08:00
|
|
|
|
struct CubeCreateInfo cci;
|
|
|
|
|
cci.has_color=true;
|
|
|
|
|
cci.color.Set(1,1,1,1);
|
2019-06-17 21:59:54 +08:00
|
|
|
|
ro_cube=CreateRenderableCube(db,material,&cci);
|
2019-06-17 12:17:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2020-10-21 21:05:03 +08:00
|
|
|
|
ro_sphere=CreateRenderableSphere(db,material,64);
|
2019-06-17 12:17:59 +08:00
|
|
|
|
}
|
2019-06-17 20:25:44 +08:00
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
TorusCreateInfo tci;
|
|
|
|
|
|
|
|
|
|
tci.innerRadius=50;
|
|
|
|
|
tci.outerRadius=70;
|
|
|
|
|
|
2020-10-21 21:05:03 +08:00
|
|
|
|
tci.numberSlices=128;
|
2020-10-21 21:30:06 +08:00
|
|
|
|
tci.numberStacks=64;
|
2019-06-17 20:25:44 +08:00
|
|
|
|
|
|
|
|
|
ro_torus=CreateRenderableTorus(db,material,&tci);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
CylinderCreateInfo cci;
|
|
|
|
|
|
|
|
|
|
cci.halfExtend=10;
|
|
|
|
|
cci.radius=10;
|
2020-10-21 21:05:03 +08:00
|
|
|
|
cci.numberSlices=32;
|
2019-06-17 20:25:44 +08:00
|
|
|
|
|
|
|
|
|
ro_cylinder=CreateRenderableCylinder(db,material,&cci);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
ConeCreateInfo cci;
|
|
|
|
|
|
|
|
|
|
cci.halfExtend=10;
|
|
|
|
|
cci.radius=10;
|
2020-10-21 21:05:03 +08:00
|
|
|
|
cci.numberSlices=128;
|
2019-06-17 20:25:44 +08:00
|
|
|
|
cci.numberStacks=1;
|
|
|
|
|
|
|
|
|
|
ro_cone=CreateRenderableCone(db,material,&cci);
|
|
|
|
|
}
|
2019-06-17 12:17:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitUBO()
|
|
|
|
|
{
|
2020-10-21 21:05:03 +08:00
|
|
|
|
ubo_light=db->CreateUBO(sizeof(PhongLight),&light);
|
|
|
|
|
ubo_phong=db->CreateUBO(sizeof(PhongMaterial),&phong);
|
2020-01-21 10:48:25 +08:00
|
|
|
|
|
2020-10-21 21:05:03 +08:00
|
|
|
|
material_instance->BindUBO("light",ubo_light);
|
|
|
|
|
material_instance->BindUBO("phong",ubo_phong);
|
2020-01-21 10:48:25 +08:00
|
|
|
|
|
2020-06-16 20:01:36 +08:00
|
|
|
|
if(!material_instance->BindUBO("world",GetCameraMatrixBuffer()))
|
2019-06-17 12:17:59 +08:00
|
|
|
|
return(false);
|
2020-10-21 21:05:03 +08:00
|
|
|
|
if(!material_instance->BindUBO("fs_world",GetCameraMatrixBuffer()))
|
|
|
|
|
return(false);
|
2019-06-17 12:17:59 +08:00
|
|
|
|
|
2020-01-20 20:00:03 +08:00
|
|
|
|
material_instance->Update();
|
2020-10-21 18:24:00 +08:00
|
|
|
|
|
|
|
|
|
if(!axis_mi->BindUBO("world",GetCameraMatrixBuffer()))
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
axis_mi->Update();
|
2019-06-17 12:17:59 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
2020-09-21 20:34:47 +08:00
|
|
|
|
|
2020-10-21 12:47:06 +08:00
|
|
|
|
void Add(Renderable *r,Pipeline *pl)
|
2020-09-21 20:34:47 +08:00
|
|
|
|
{
|
2020-09-27 20:58:25 +08:00
|
|
|
|
auto ri=db->CreateRenderableInstance(r,material_instance,pl);
|
2020-09-21 20:34:47 +08:00
|
|
|
|
|
|
|
|
|
render_root.Add(ri);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 12:47:06 +08:00
|
|
|
|
void Add(Renderable *r,Pipeline *pl,const Matrix4f &mat)
|
2020-09-21 20:34:47 +08:00
|
|
|
|
{
|
2020-09-27 20:58:25 +08:00
|
|
|
|
auto ri=db->CreateRenderableInstance(r,material_instance,pl);
|
2020-09-21 20:34:47 +08:00
|
|
|
|
|
|
|
|
|
render_root.Add(ri,mat);
|
|
|
|
|
}
|
2019-06-17 12:17:59 +08:00
|
|
|
|
|
|
|
|
|
bool InitScene()
|
|
|
|
|
{
|
2020-10-21 18:24:00 +08:00
|
|
|
|
render_root.Add(db->CreateRenderableInstance(ro_axis,axis_mi,axis_pipeline));
|
2020-10-21 21:05:03 +08:00
|
|
|
|
Add(ro_torus ,pipeline_solid);//,rotate(90,Vector3f(1,0,0)));
|
2020-09-27 20:58:25 +08:00
|
|
|
|
Add(ro_cube ,pipeline_solid,translate(-10, 0, 5)*scale(10,10,10));
|
|
|
|
|
Add(ro_sphere ,pipeline_solid,translate( 10, 0, 5)*scale(10,10,10));
|
|
|
|
|
Add(ro_cylinder ,pipeline_solid,translate( 0, 16, 0));
|
|
|
|
|
Add(ro_cone ,pipeline_solid,translate( 0,-16, 0));
|
2019-06-17 12:17:59 +08:00
|
|
|
|
|
|
|
|
|
render_root.RefreshMatrix();
|
|
|
|
|
render_root.ExpendToList(&render_list);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
bool Init()
|
|
|
|
|
{
|
|
|
|
|
if(!CameraAppFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
|
|
|
|
|
return(false);
|
2020-09-21 20:34:47 +08:00
|
|
|
|
|
2020-09-27 20:58:25 +08:00
|
|
|
|
if(!InitMaterial())
|
2020-09-21 20:34:47 +08:00
|
|
|
|
return(false);
|
2019-06-17 12:17:59 +08:00
|
|
|
|
|
|
|
|
|
if(!InitUBO())
|
|
|
|
|
return(false);
|
|
|
|
|
|
2020-09-27 20:58:25 +08:00
|
|
|
|
CreateRenderObject();
|
2019-06-17 12:17:59 +08:00
|
|
|
|
|
|
|
|
|
if(!InitScene())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
2019-06-18 00:48:05 +08:00
|
|
|
|
|
|
|
|
|
void BuildCommandBuffer(uint32_t index) override
|
2019-06-17 12:17:59 +08:00
|
|
|
|
{
|
2019-06-18 00:48:05 +08:00
|
|
|
|
render_root.RefreshMatrix();
|
|
|
|
|
render_list.Clear();
|
|
|
|
|
render_root.ExpendToList(&render_list);
|
|
|
|
|
|
|
|
|
|
VulkanApplicationFramework::BuildCommandBuffer(index,&render_list);
|
2019-06-17 12:17:59 +08:00
|
|
|
|
}
|
|
|
|
|
};//class TestApp:public CameraAppFramework
|
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
|
|
|
|
TestApp app;
|
|
|
|
|
|
|
|
|
|
if(!app.Init())
|
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
|
|
while(app.Run());
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|