ULRE/example/Vulkan/InlineGeometryScene.cpp

313 lines
7.9 KiB
C++
Raw Normal View History

// 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>
#include<hgl/graph/VKRenderable.h>
2020-10-21 22:16:50 +08:00
#include<hgl/graph/VKTexture.h>
2019-06-17 12:17:59 +08:00
#include<hgl/graph/RenderList.h>
using namespace hgl;
using namespace hgl::graph;
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:
2021-09-22 18:36:54 +08:00
SceneNode render_root;
RenderList * render_list =nullptr;
2020-09-21 20:34:47 +08:00
2020-10-21 12:47:06 +08:00
Material * material =nullptr;
2021-06-24 19:25:43 +08:00
MaterialInstance * material_instance =nullptr;
2020-09-27 20:58:25 +08:00
2020-10-21 18:24:00 +08:00
Material * axis_material =nullptr;
2021-06-24 19:25:43 +08:00
MaterialInstance * axis_mi =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
DeviceBuffer * ubo_light =nullptr;
DeviceBuffer * ubo_phong =nullptr;
2019-06-17 12:17:59 +08:00
2020-10-21 22:16:50 +08:00
struct
{
Sampler * sampler =nullptr;
Texture2D * color =nullptr;
Texture2D * normal =nullptr;
}texture;
2022-06-24 17:51:05 +08:00
Primitive *ro_axis,
2020-10-21 18:24:00 +08:00
*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
{
light.color=Vector4f(1,1,1,1);
light.position=Vector4f(1000,1000,1000,1.0);
2020-10-21 21:05:03 +08:00
phong.BaseColor=Vector4f(1,1,1,1);
phong.ambient=0.5;
phong.specular=Vector4f(0.3,0.3,0.3,32);
2020-10-21 21:05:03 +08:00
2020-10-21 22:16:50 +08:00
{
axis_material=db->CreateMaterial(OS_TEXT("res/material/VertexColor3D"));
if(!axis_material)return(false);
2020-10-21 18:24:00 +08:00
2020-10-21 22:16:50 +08:00
axis_mi=db->CreateMaterialInstance(axis_material);
if(!axis_mi)return(false);
2020-10-21 18:24:00 +08:00
axis_pipeline=CreatePipeline(axis_mi,InlinePipeline::Solid3D,Prim::Lines);
2020-10-21 22:16:50 +08:00
if(!axis_pipeline)return(false);
}
{
2020-10-24 21:50:36 +08:00
texture.color =db->LoadTexture2D(OS_TEXT("res/image/Brickwall/Albedo.Tex2D"),true);
texture.normal =db->LoadTexture2D(OS_TEXT("res/image/Brickwall/Normal.Tex2D"),true);
2021-09-24 00:53:59 +08:00
if(!texture.color
||!texture.normal)
return(false);
2020-10-24 19:14:31 +08:00
VkSamplerCreateInfo sampler_create_info=
{
VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
nullptr,
0,
VK_FILTER_LINEAR,
VK_FILTER_LINEAR,
VK_SAMPLER_MIPMAP_MODE_LINEAR,
VK_SAMPLER_ADDRESS_MODE_REPEAT,
VK_SAMPLER_ADDRESS_MODE_REPEAT,
VK_SAMPLER_ADDRESS_MODE_REPEAT,
0.0f,
2020-10-24 21:50:36 +08:00
VK_TRUE,
device->GetPhyDevice()->GetMaxSamplerAnisotropy(),
2020-10-24 19:14:31 +08:00
false,
VK_COMPARE_OP_NEVER,
0.0f,
2020-10-24 21:50:36 +08:00
static_cast<float>(texture.color->GetMipLevel()),
2020-10-24 19:14:31 +08:00
VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK,
false
};
texture.sampler =db->CreateSampler(&sampler_create_info);
2020-10-21 22:16:50 +08:00
}
{
material=db->CreateMaterial(OS_TEXT("res/material/TextureNormal"));
if(!material)return(false);
material_instance=db->CreateMaterialInstance(material);
if(!material_instance)return(false);
{
MaterialParameters *mp_texture=material_instance->GetMP(DescriptorSetType::Value);
if(!mp_texture)
return(false);
2023-02-23 13:25:05 +08:00
mp_texture->BindImageSampler("TexColor" ,texture.color, texture.sampler);
mp_texture->BindImageSampler("TexNormal" ,texture.normal, texture.sampler);
}
2020-10-21 22:16:50 +08:00
}
2019-06-17 12:17:59 +08:00
2022-01-07 16:28:00 +08:00
pipeline_solid=CreatePipeline(material_instance,InlinePipeline::Solid3D,Prim::Triangles);
2020-09-27 20:58:25 +08:00
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()
{
using namespace inline_geometry;
2019-06-17 12:17:59 +08:00
{
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
ro_axis=CreateAxis(db,axis_mi->GetVIL(),&aci);
2019-06-17 12:17:59 +08:00
}
const VIL *vil=material_instance->GetVIL();
2019-06-17 12:17:59 +08:00
{
2020-09-27 20:58:25 +08:00
struct CubeCreateInfo cci;
cci.normal=true;
cci.tangent=true;
cci.tex_coord=true;
cci.color_type=CubeCreateInfo::ColorType::SameColor;
cci.color[0]=Vector4f(1,1,1,1);
ro_cube=CreateCube(db,vil,&cci);
2019-06-17 12:17:59 +08:00
}
{
ro_sphere=CreateSphere(db,vil,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
2020-10-24 19:14:31 +08:00
tci.uv_scale.x=4;
tci.uv_scale.y=1;
ro_torus=CreateTorus(db,vil,&tci);
2019-06-17 20:25:44 +08:00
}
{
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=CreateCylinder(db,vil,&cci);
2019-06-17 20:25:44 +08:00
}
{
ConeCreateInfo cci;
cci.halfExtend=10;
cci.radius=10;
2020-10-21 21:05:03 +08:00
cci.numberSlices=128;
cci.numberStacks=32;
2019-06-17 20:25:44 +08:00
ro_cone=CreateCone(db,vil,&cci);
2019-06-17 20:25:44 +08:00
}
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);
2021-09-22 18:36:54 +08:00
{
MaterialParameters *mp_value=material_instance->GetMP(DescriptorSetType::Value);
2021-09-22 18:36:54 +08:00
if(!mp_value)
return(false);
2020-01-21 10:48:25 +08:00
2021-09-22 18:36:54 +08:00
mp_value->BindUBO("light",ubo_light);
mp_value->BindUBO("phong",ubo_phong);
mp_value->Update();
}
2020-10-21 22:16:50 +08:00
BindCameraUBO(material_instance);
2020-10-21 18:24:00 +08:00
2019-06-17 12:17:59 +08:00
return(true);
}
2020-09-21 20:34:47 +08:00
2022-06-24 17:51:05 +08:00
void Add(Primitive *r,Pipeline *pl)
2020-09-21 20:34:47 +08:00
{
auto ri=db->CreateRenderable(r,material_instance,pl);
2020-09-21 20:34:47 +08:00
render_root.CreateSubNode(ri);
2020-09-21 20:34:47 +08:00
}
2022-06-24 17:51:05 +08:00
void Add(Primitive *r,Pipeline *pl,const Matrix4f &mat)
2020-09-21 20:34:47 +08:00
{
auto ri=db->CreateRenderable(r,material_instance,pl);
2020-09-21 20:34:47 +08:00
render_root.CreateSubNode(mat,ri);
2020-09-21 20:34:47 +08:00
}
2019-06-17 12:17:59 +08:00
bool InitScene()
{
render_root.CreateSubNode(db->CreateRenderable(ro_axis,axis_mi,axis_pipeline));
Add(ro_torus ,pipeline_solid);
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();
2021-09-22 18:36:54 +08:00
render_list->Expend(GetCameraInfo(),&render_root);
2019-06-17 12:17:59 +08:00
return(true);
}
public:
2021-09-22 18:36:54 +08:00
~TestApp()
{
SAFE_CLEAR(render_list);
}
2019-06-17 12:17:59 +08:00
bool Init()
{
if(!CameraAppFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
return(false);
2020-09-21 20:34:47 +08:00
2021-09-22 18:36:54 +08:00
render_list=new RenderList(device);
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();
2021-09-22 18:36:54 +08:00
render_list->Expend(GetCameraInfo(),&render_root);
2019-06-18 00:48:05 +08:00
2021-09-22 18:36:54 +08:00
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;
}