2024-03-06 13:54:05 +08:00
|
|
|
|
// BlinnPhong direction light
|
2024-02-12 08:20:24 +08:00
|
|
|
|
|
|
|
|
|
#include"VulkanAppFramework.h"
|
|
|
|
|
#include<hgl/filesystem/FileSystem.h>
|
|
|
|
|
#include<hgl/graph/InlineGeometry.h>
|
|
|
|
|
#include<hgl/graph/VKRenderResource.h>
|
|
|
|
|
#include<hgl/graph/RenderList.h>
|
|
|
|
|
#include<hgl/graph/Camera.h>
|
|
|
|
|
#include<hgl/graph/Ray.h>
|
|
|
|
|
#include<hgl/graph/VKVertexAttribBuffer.h>
|
|
|
|
|
#include<hgl/graph/mtl/Material3DCreateConfig.h>
|
2024-03-07 13:59:28 +08:00
|
|
|
|
#include<hgl/graph/mtl/BlinnPhong.h>
|
2024-05-25 03:14:26 +08:00
|
|
|
|
#include<hgl/graph/VertexDataManager.h>
|
2024-05-25 04:11:38 +08:00
|
|
|
|
#include<hgl/graph/PrimitiveCreater.h>
|
2024-02-12 08:20:24 +08:00
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
|
|
|
|
static float lumiance_data[2]={1,1};
|
|
|
|
|
|
|
|
|
|
static Color4f white_color(1,1,1,1);
|
|
|
|
|
|
2024-03-07 13:59:28 +08:00
|
|
|
|
static mtl::blinnphong::SunLight sun_light=
|
|
|
|
|
{
|
2024-03-12 22:31:58 +08:00
|
|
|
|
Vector4f(1,1,1,0), //direction
|
|
|
|
|
Vector4f(1,0.95,0.9,1) //color
|
2024-03-07 13:59:28 +08:00
|
|
|
|
};
|
|
|
|
|
|
2024-03-16 14:08:55 +08:00
|
|
|
|
constexpr const COLOR AxisColor[4]=
|
|
|
|
|
{
|
2024-05-30 21:07:13 +08:00
|
|
|
|
//COLOR::Red, //X轴颜色
|
|
|
|
|
//COLOR::Green, //Y轴颜色
|
|
|
|
|
//COLOR::Blue, //Z轴颜色
|
|
|
|
|
COLOR::White, //全局颜色
|
2024-05-31 22:04:02 +08:00
|
|
|
|
COLOR::GhostWhite,
|
|
|
|
|
COLOR::BlanchedAlmond,
|
|
|
|
|
COLOR::AntiqueWhite
|
2024-05-30 21:07:13 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constexpr const os_char *tex_filename[]=
|
|
|
|
|
{
|
|
|
|
|
OS_TEXT("eucalyptus-cross-versailles.Tex2D"),
|
|
|
|
|
OS_TEXT("tints-ashton-green-stretcher.Tex2D"),
|
|
|
|
|
OS_TEXT("worn-clay-cobble-in-desert-stretcher.Tex2D"),
|
|
|
|
|
OS_TEXT("plain-grey-sheer.Tex2D"),
|
2024-03-16 14:08:55 +08:00
|
|
|
|
};
|
|
|
|
|
|
2024-05-30 21:07:13 +08:00
|
|
|
|
constexpr const size_t TexCount=sizeof(tex_filename)/sizeof(os_char *);
|
|
|
|
|
|
2024-02-12 08:20:24 +08:00
|
|
|
|
class TestApp:public SceneAppFramework
|
|
|
|
|
{
|
2024-03-04 13:13:33 +08:00
|
|
|
|
private: //plane grid
|
2024-02-12 08:20:24 +08:00
|
|
|
|
|
2024-02-12 08:23:31 +08:00
|
|
|
|
Material * mtl_vertex_lum =nullptr;
|
2024-02-12 08:20:24 +08:00
|
|
|
|
MaterialInstance * mi_plane_grid =nullptr;
|
2024-02-13 22:27:12 +08:00
|
|
|
|
Pipeline * p_line =nullptr;
|
2024-05-25 03:14:26 +08:00
|
|
|
|
Primitive * prim_plane_grid =nullptr;
|
2024-03-04 13:13:33 +08:00
|
|
|
|
|
2024-03-07 13:59:28 +08:00
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
DeviceBuffer * ubo_sun =nullptr;
|
|
|
|
|
|
2024-05-30 21:07:13 +08:00
|
|
|
|
|
|
|
|
|
Texture2DArray * texture =nullptr;
|
|
|
|
|
Sampler * sampler =nullptr;
|
|
|
|
|
|
2024-03-04 13:13:33 +08:00
|
|
|
|
private: //sphere
|
|
|
|
|
|
2024-03-15 01:38:47 +08:00
|
|
|
|
Material * mtl_blinnphong =nullptr;
|
2024-05-25 04:11:38 +08:00
|
|
|
|
PrimitiveCreater * pc_blinnphong =nullptr;
|
2024-05-25 03:14:26 +08:00
|
|
|
|
VertexDataManager * vdm_blinnphong =nullptr;
|
2024-04-02 19:41:47 +08:00
|
|
|
|
|
2024-03-16 00:20:27 +08:00
|
|
|
|
MaterialInstance * mi_blinnphong[4]{};
|
2024-03-15 01:38:47 +08:00
|
|
|
|
Pipeline * p_blinnphong =nullptr;
|
|
|
|
|
|
2024-05-25 03:14:26 +08:00
|
|
|
|
Primitive * prim_sphere =nullptr;
|
|
|
|
|
Primitive * prim_cone =nullptr;
|
|
|
|
|
Primitive * prim_cylinder =nullptr;
|
2024-02-12 08:20:24 +08:00
|
|
|
|
|
|
|
|
|
private:
|
2024-05-30 21:07:13 +08:00
|
|
|
|
|
|
|
|
|
bool InitTexture()
|
|
|
|
|
{
|
|
|
|
|
texture=db->CreateTexture2DArray( "SeamlessBackground",
|
|
|
|
|
256,256, ///<纹理尺寸
|
|
|
|
|
TexCount, ///<纹理层数
|
|
|
|
|
PF_BC1_RGBUN, ///<纹理格式
|
|
|
|
|
false); ///<是否自动产生mipmaps
|
2024-06-26 01:51:13 +08:00
|
|
|
|
|
2024-05-30 21:07:13 +08:00
|
|
|
|
if(!texture)return(false);
|
|
|
|
|
|
|
|
|
|
OSString filename;
|
|
|
|
|
|
|
|
|
|
for(uint i=0;i<TexCount;i++)
|
|
|
|
|
{
|
|
|
|
|
filename=filesystem::MergeFilename(OS_TEXT("res/image/seamless"),tex_filename[i]);
|
|
|
|
|
|
|
|
|
|
if(!db->LoadTexture2DToArray(texture,i,filename))
|
|
|
|
|
return(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
2024-02-12 08:20:24 +08:00
|
|
|
|
|
2024-02-13 22:27:12 +08:00
|
|
|
|
bool InitVertexLumMP()
|
2024-02-12 08:20:24 +08:00
|
|
|
|
{
|
2025-05-17 20:26:36 +08:00
|
|
|
|
mtl::Material3DCreateConfig cfg(device->GetDevAttr(),"VertexLuminance3D",PrimitiveType::Lines);
|
2024-02-12 08:20:24 +08:00
|
|
|
|
|
|
|
|
|
cfg.local_to_world=true;
|
|
|
|
|
|
2024-02-12 08:23:31 +08:00
|
|
|
|
mtl_vertex_lum=db->LoadMaterial("Std3D/VertexLum3D",&cfg);
|
|
|
|
|
if(!mtl_vertex_lum)return(false);
|
2024-02-12 08:20:24 +08:00
|
|
|
|
|
2024-02-12 08:23:31 +08:00
|
|
|
|
mi_plane_grid=db->CreateMaterialInstance(mtl_vertex_lum,nullptr,&white_color);
|
2024-02-12 08:20:24 +08:00
|
|
|
|
if(!mi_plane_grid)return(false);
|
|
|
|
|
|
2025-05-04 23:36:28 +08:00
|
|
|
|
p_line=CreatePipeline(mtl_vertex_lum,InlinePipeline::Solid3D,PrimitiveType::Lines);
|
2024-02-12 08:20:24 +08:00
|
|
|
|
|
2024-02-12 08:23:31 +08:00
|
|
|
|
if(!p_line)
|
2024-02-12 08:20:24 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
2024-03-04 13:13:33 +08:00
|
|
|
|
|
2024-03-07 13:59:28 +08:00
|
|
|
|
bool CreateBlinnPhongUBO()
|
|
|
|
|
{
|
|
|
|
|
ubo_sun=db->CreateUBO("sun",sizeof(sun_light),&sun_light);
|
|
|
|
|
if(!ubo_sun)return(false);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-04 13:13:33 +08:00
|
|
|
|
bool InitBlinnPhongSunLightMP()
|
|
|
|
|
{
|
2025-05-17 20:26:36 +08:00
|
|
|
|
mtl::Material3DCreateConfig cfg(device->GetDevAttr(),"BlinnPhong3D",PrimitiveType::Triangles);
|
2024-03-04 13:13:33 +08:00
|
|
|
|
|
|
|
|
|
cfg.local_to_world=true;
|
2024-05-26 02:25:49 +08:00
|
|
|
|
cfg.material_instance=true;
|
2024-03-04 13:13:33 +08:00
|
|
|
|
|
2024-05-30 21:07:13 +08:00
|
|
|
|
mtl_blinnphong=db->LoadMaterial("Std3D/BlinnPhong/SunLightPureColorTexture",&cfg);
|
2024-03-15 01:38:47 +08:00
|
|
|
|
if(!mtl_blinnphong)return(false);
|
2024-03-04 13:13:33 +08:00
|
|
|
|
|
2024-03-15 01:38:47 +08:00
|
|
|
|
mtl_blinnphong->BindUBO(DescriptorSetType::Global,"sun",ubo_sun);
|
|
|
|
|
mtl_blinnphong->Update();
|
2024-05-30 21:07:13 +08:00
|
|
|
|
|
|
|
|
|
sampler=db->CreateSampler();
|
|
|
|
|
|
|
|
|
|
if(!mtl_blinnphong->BindImageSampler( DescriptorSetType::PerMaterial, ///<描述符合集
|
|
|
|
|
mtl::SamplerName::BaseColor, ///<采样器名称
|
|
|
|
|
texture, ///<纹理
|
|
|
|
|
sampler)) ///<采样器
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
struct MIData
|
|
|
|
|
{
|
|
|
|
|
Color4f color;
|
|
|
|
|
uint32 tex_id[4];
|
|
|
|
|
}mi_data;
|
|
|
|
|
|
|
|
|
|
constexpr const uint MIDataStride=sizeof(MIData);
|
2024-03-06 13:54:05 +08:00
|
|
|
|
|
2024-03-16 00:20:27 +08:00
|
|
|
|
for(uint i=0;i<4;i++)
|
2024-03-10 22:45:38 +08:00
|
|
|
|
{
|
2024-05-30 21:07:13 +08:00
|
|
|
|
mi_data.color=GetColor4f(AxisColor[i],4);
|
|
|
|
|
mi_data.tex_id[0]=i;
|
2024-03-10 22:45:38 +08:00
|
|
|
|
|
2024-04-03 00:14:09 +08:00
|
|
|
|
mi_blinnphong[i]=db->CreateMaterialInstance(mtl_blinnphong, //材质
|
|
|
|
|
nullptr, //顶点输入配置,这里使用nullptr,即代表会使用默认配置,那么结果将等同于上面的mtl_blinnphong->GetDefaultVIL()
|
|
|
|
|
&mi_data); //材质实例参数
|
2024-03-16 00:20:27 +08:00
|
|
|
|
if(!mi_blinnphong[i])return(false);
|
|
|
|
|
}
|
2024-03-04 13:13:33 +08:00
|
|
|
|
|
2025-05-04 23:36:28 +08:00
|
|
|
|
p_blinnphong=CreatePipeline(mtl_blinnphong,InlinePipeline::Solid3D,PrimitiveType::Triangles);
|
2024-03-04 13:13:33 +08:00
|
|
|
|
|
2024-03-15 01:38:47 +08:00
|
|
|
|
if(!p_blinnphong)
|
2024-03-04 13:13:33 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2024-04-03 00:14:09 +08:00
|
|
|
|
return(true);
|
2024-03-04 13:13:33 +08:00
|
|
|
|
}
|
2024-02-12 08:20:24 +08:00
|
|
|
|
|
2024-05-25 04:11:38 +08:00
|
|
|
|
bool InitVDMAndPC()
|
2024-05-25 03:14:26 +08:00
|
|
|
|
{
|
|
|
|
|
vdm_blinnphong=new VertexDataManager(device,mtl_blinnphong->GetDefaultVIL());
|
|
|
|
|
if(!vdm_blinnphong->Init( 1024*1024, //VAB最大容量
|
|
|
|
|
1024*1024, //索引最大容量
|
|
|
|
|
IndexType::U16)) //索引类型
|
|
|
|
|
{
|
|
|
|
|
delete vdm_blinnphong;
|
|
|
|
|
vdm_blinnphong=nullptr;
|
|
|
|
|
|
|
|
|
|
return(false);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-25 04:11:38 +08:00
|
|
|
|
pc_blinnphong=new PrimitiveCreater(vdm_blinnphong);
|
|
|
|
|
//pc_blinnphong=new PrimitiveCreater(device,mtl_blinnphong->GetDefaultVIL());
|
|
|
|
|
|
2024-05-25 03:14:26 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-12 08:20:24 +08:00
|
|
|
|
bool CreateRenderObject()
|
|
|
|
|
{
|
|
|
|
|
using namespace inline_geometry;
|
|
|
|
|
|
2024-03-13 00:13:58 +08:00
|
|
|
|
//Plane Grid
|
2024-05-25 04:11:38 +08:00
|
|
|
|
//{
|
|
|
|
|
// struct PlaneGridCreateInfo pgci;
|
2024-02-12 08:20:24 +08:00
|
|
|
|
|
2024-05-25 04:11:38 +08:00
|
|
|
|
// pgci.grid_size.Set(32,32);
|
|
|
|
|
// pgci.sub_count.Set(8,8);
|
2024-02-12 08:20:24 +08:00
|
|
|
|
|
2024-05-25 04:11:38 +08:00
|
|
|
|
// pgci.lum=0.5;
|
|
|
|
|
// pgci.sub_lum=0.75;
|
2024-02-12 08:20:24 +08:00
|
|
|
|
|
2024-05-25 04:11:38 +08:00
|
|
|
|
// prim_plane_grid=CreatePlaneGrid(pc_blinnphong,mtl_vertex_lum->GetDefaultVIL(),&pgci);
|
|
|
|
|
//}
|
2024-03-04 13:13:33 +08:00
|
|
|
|
|
|
|
|
|
//Sphere
|
2024-05-25 04:11:38 +08:00
|
|
|
|
prim_sphere=CreateSphere(pc_blinnphong,16);
|
2024-03-15 01:38:47 +08:00
|
|
|
|
|
|
|
|
|
//Cone
|
2024-03-04 13:13:33 +08:00
|
|
|
|
{
|
2024-03-15 01:38:47 +08:00
|
|
|
|
struct ConeCreateInfo cci;
|
|
|
|
|
|
|
|
|
|
cci.radius =1; //圆锥半径
|
|
|
|
|
cci.halfExtend =1; //圆锤一半高度
|
|
|
|
|
cci.numberSlices=16; //圆锥底部分割数
|
|
|
|
|
cci.numberStacks=8; //圆锥高度分割数
|
|
|
|
|
|
2024-05-25 04:11:38 +08:00
|
|
|
|
prim_cone=CreateCone(pc_blinnphong,&cci);
|
2024-03-15 01:38:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Cyliner
|
|
|
|
|
{
|
|
|
|
|
struct CylinderCreateInfo cci;
|
|
|
|
|
|
2024-07-28 23:34:04 +08:00
|
|
|
|
cci.halfExtend =1.25; //圆柱一半高度
|
2024-03-15 01:38:47 +08:00
|
|
|
|
cci.numberSlices=16; //圆柱底部分割数
|
2024-05-30 21:07:13 +08:00
|
|
|
|
cci.radius =1.25f; //圆柱半径
|
2024-03-15 01:38:47 +08:00
|
|
|
|
|
2024-05-25 04:11:38 +08:00
|
|
|
|
prim_cylinder=CreateCylinder(pc_blinnphong,&cci);
|
2024-02-12 08:20:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return(true);
|
2024-03-15 01:38:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-18 02:03:16 +08:00
|
|
|
|
Mesh *Add(Primitive *r,MaterialInstance *mi,Pipeline *p,const Matrix4f &mat=Identity4f)
|
2024-03-15 01:38:47 +08:00
|
|
|
|
{
|
2024-05-23 02:19:40 +08:00
|
|
|
|
if(!r)
|
|
|
|
|
return(nullptr);
|
|
|
|
|
if(!mi)
|
|
|
|
|
return(nullptr);
|
|
|
|
|
if(!p)
|
|
|
|
|
return(nullptr);
|
|
|
|
|
|
2025-05-18 02:19:14 +08:00
|
|
|
|
Mesh *ri=db->CreateMesh(r,mi,p);
|
2024-03-15 01:38:47 +08:00
|
|
|
|
|
|
|
|
|
if(!ri)
|
|
|
|
|
{
|
2025-05-18 02:03:16 +08:00
|
|
|
|
LOG_ERROR("Create Mesh failed! Primitive: "+r->GetName());
|
2024-03-15 01:38:47 +08:00
|
|
|
|
return(nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render_root.CreateSubNode(mat,ri);
|
|
|
|
|
|
|
|
|
|
return ri;
|
2024-02-12 08:20:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitScene()
|
|
|
|
|
{
|
2024-05-25 04:11:38 +08:00
|
|
|
|
//Add(prim_plane_grid,mi_plane_grid,p_line,Identity4f);
|
2024-03-15 01:38:47 +08:00
|
|
|
|
|
2024-03-16 00:20:27 +08:00
|
|
|
|
Add(prim_sphere, mi_blinnphong[0],p_blinnphong,translate(Vector3f(0,0,2)));
|
|
|
|
|
|
|
|
|
|
Add(prim_cone, mi_blinnphong[1],p_blinnphong);
|
|
|
|
|
Add(prim_cylinder, mi_blinnphong[2],p_blinnphong,translate(Vector3f(0,0,-5)));
|
2024-02-12 08:20:24 +08:00
|
|
|
|
|
2024-03-12 22:31:58 +08:00
|
|
|
|
camera->pos=Vector3f(32,32,32);
|
2024-02-12 08:20:24 +08:00
|
|
|
|
camera_control->SetTarget(Vector3f(0,0,0));
|
|
|
|
|
camera_control->Refresh();
|
|
|
|
|
|
|
|
|
|
render_root.RefreshMatrix();
|
|
|
|
|
render_list->Expend(&render_root);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
bool Init(uint w,uint h)
|
|
|
|
|
{
|
|
|
|
|
if(!SceneAppFramework::Init(w,h))
|
|
|
|
|
return(false);
|
|
|
|
|
|
2024-05-30 21:07:13 +08:00
|
|
|
|
if(!InitTexture())
|
2024-03-13 00:13:58 +08:00
|
|
|
|
return(false);
|
2024-05-30 21:07:13 +08:00
|
|
|
|
|
|
|
|
|
//if(!InitVertexLumMP())
|
|
|
|
|
// return(false);
|
2024-03-10 00:35:35 +08:00
|
|
|
|
|
|
|
|
|
if(!CreateBlinnPhongUBO())
|
|
|
|
|
return(false);
|
2024-02-12 08:20:24 +08:00
|
|
|
|
|
2024-03-04 13:13:33 +08:00
|
|
|
|
if(!InitBlinnPhongSunLightMP())
|
|
|
|
|
return(false);
|
|
|
|
|
|
2024-05-25 04:11:38 +08:00
|
|
|
|
if(!InitVDMAndPC())
|
2024-05-25 03:14:26 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2024-02-12 08:20:24 +08:00
|
|
|
|
if(!CreateRenderObject())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitScene())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
2024-05-25 03:14:26 +08:00
|
|
|
|
|
|
|
|
|
~TestApp()
|
|
|
|
|
{
|
|
|
|
|
SAFE_CLEAR(prim_cone)
|
|
|
|
|
SAFE_CLEAR(prim_cylinder)
|
|
|
|
|
SAFE_CLEAR(prim_sphere)
|
|
|
|
|
SAFE_CLEAR(prim_plane_grid)
|
|
|
|
|
|
2024-05-25 04:11:38 +08:00
|
|
|
|
SAFE_CLEAR(pc_blinnphong)
|
2024-05-25 03:14:26 +08:00
|
|
|
|
SAFE_CLEAR(vdm_blinnphong)
|
|
|
|
|
}
|
2024-02-12 08:20:24 +08:00
|
|
|
|
};//class TestApp:public CameraAppFramework
|
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
|
|
|
|
return RunApp<TestApp>(1280,720);
|
|
|
|
|
}
|