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-03-16 00:20:27 +08:00
|
|
|
|
#include<hgl/color/Color.h>
|
2024-04-01 23:32:18 +08:00
|
|
|
|
#include<hgl/type/DataChain.h>
|
|
|
|
|
#include<hgl/type/MemoryBlock.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]=
|
|
|
|
|
{
|
|
|
|
|
COLOR::Red, //X轴颜色
|
|
|
|
|
COLOR::Green, //Y轴颜色
|
|
|
|
|
COLOR::Blue, //Z轴颜色
|
|
|
|
|
COLOR::White //全局颜色
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class VertexDataManager
|
|
|
|
|
{
|
2024-03-29 12:58:58 +08:00
|
|
|
|
protected:
|
|
|
|
|
|
|
|
|
|
uint vi_count; ///<顶点输入流数量
|
|
|
|
|
const VIF * vif_list; ///<顶点输入格式列表
|
|
|
|
|
|
|
|
|
|
VkDeviceSize vbo_max_size; ///<顶点缓冲区分配空间大小
|
|
|
|
|
VkDeviceSize vbo_cur_size; ///<顶点缓冲区当前使用大小
|
|
|
|
|
VBO ** vbo; ///<顶点缓冲区列表
|
|
|
|
|
|
|
|
|
|
VkDeviceSize ibo_cur_size; ///<索引缓冲区当前使用大小
|
|
|
|
|
IndexBuffer * ibo; ///<索引缓冲区
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
2024-04-01 23:32:18 +08:00
|
|
|
|
DataChain data_chain; ///<数据链
|
2024-03-29 12:58:58 +08:00
|
|
|
|
|
2024-03-16 14:08:55 +08:00
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
VertexDataManager(const VIL &_vil)
|
|
|
|
|
{
|
|
|
|
|
vi_count=_vil.GetCount();
|
2024-04-01 23:32:18 +08:00
|
|
|
|
vif_list=_vil.GetVIFList(); //来自于Material,不会被释放,所以指针有效
|
2024-03-16 14:08:55 +08:00
|
|
|
|
|
|
|
|
|
vbo_max_size=0;
|
|
|
|
|
vbo_cur_size=0;
|
2024-03-18 13:30:06 +08:00
|
|
|
|
vbo=hgl_zero_new<VBO *>(vi_count);
|
2024-03-16 14:08:55 +08:00
|
|
|
|
|
|
|
|
|
ibo_cur_size=0;
|
|
|
|
|
ibo=nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 23:32:18 +08:00
|
|
|
|
~VertexDataManager()
|
|
|
|
|
{
|
|
|
|
|
SAFE_CLEAR_OBJECT_ARRAY(vbo,vi_count);
|
|
|
|
|
SAFE_CLEAR(ibo);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-29 12:58:58 +08:00
|
|
|
|
const VkDeviceSize GetVBOMaxCount ()const{return vbo_max_size;} ///<取得顶点缓冲区分配的空间最大数量
|
|
|
|
|
const VkDeviceSize GetVBOCurCount ()const{return vbo_cur_size;} ///<取得顶点缓冲区当前数量
|
2024-03-16 14:08:55 +08:00
|
|
|
|
|
|
|
|
|
const IndexType GetIBOType ()const{return ibo?ibo->GetType():IndexType::ERR;} ///<取得索引缓冲区类型
|
|
|
|
|
const VkDeviceSize GetIBOMaxCount ()const{return ibo?ibo->GetCount():-1;} ///<取得索引缓冲区分配的空间最大数量
|
|
|
|
|
const VkDeviceSize GetIBOCurCount ()const{return ibo?ibo_cur_size:-1;} ///<取得索引缓冲区当前数量
|
2024-03-29 12:58:58 +08:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
|
2024-03-16 14:08:55 +08:00
|
|
|
|
};//class VertexDataManager
|
2024-03-16 00:20:27 +08:00
|
|
|
|
|
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-03-04 13:13:33 +08:00
|
|
|
|
Primitive * prim_plane_grid =nullptr;
|
|
|
|
|
|
2024-03-07 13:59:28 +08:00
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
DeviceBuffer * ubo_sun =nullptr;
|
|
|
|
|
|
2024-03-04 13:13:33 +08:00
|
|
|
|
private: //sphere
|
|
|
|
|
|
2024-03-15 01:38:47 +08:00
|
|
|
|
Material * mtl_blinnphong =nullptr;
|
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-03-04 13:13:33 +08:00
|
|
|
|
Primitive * prim_sphere =nullptr;
|
2024-03-15 01:38:47 +08:00
|
|
|
|
Primitive * prim_cone =nullptr;
|
|
|
|
|
Primitive * prim_cylinder =nullptr;
|
2024-02-12 08:20:24 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
2024-02-13 22:27:12 +08:00
|
|
|
|
bool InitVertexLumMP()
|
2024-02-12 08:20:24 +08:00
|
|
|
|
{
|
|
|
|
|
mtl::Material3DCreateConfig cfg(device->GetDeviceAttribute(),"VertexLuminance3D",Prim::Lines);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2024-02-12 08:23:31 +08:00
|
|
|
|
p_line=CreatePipeline(mtl_vertex_lum,InlinePipeline::Solid3D,Prim::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()
|
|
|
|
|
{
|
|
|
|
|
mtl::Material3DCreateConfig cfg(device->GetDeviceAttribute(),"BlinnPhong3D",Prim::Triangles);
|
|
|
|
|
|
|
|
|
|
cfg.local_to_world=true;
|
|
|
|
|
|
2024-03-15 01:38:47 +08:00
|
|
|
|
mtl_blinnphong=db->LoadMaterial("Std3D/BlinnPhong/SunLightPureColor",&cfg);
|
|
|
|
|
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-03-06 13:54:05 +08:00
|
|
|
|
|
2024-03-16 00:20:27 +08:00
|
|
|
|
Color4f mi_data;
|
|
|
|
|
for(uint i=0;i<4;i++)
|
2024-03-10 22:45:38 +08:00
|
|
|
|
{
|
2024-03-16 00:20:27 +08:00
|
|
|
|
mi_data=GetColor4f(AxisColor[i],4);
|
2024-03-10 22:45:38 +08:00
|
|
|
|
|
2024-03-16 00:20:27 +08:00
|
|
|
|
mi_blinnphong[i]=db->CreateMaterialInstance(mtl_blinnphong,nullptr,&mi_data);
|
|
|
|
|
if(!mi_blinnphong[i])return(false);
|
|
|
|
|
}
|
2024-03-04 13:13:33 +08:00
|
|
|
|
|
2024-03-15 01:38:47 +08:00
|
|
|
|
p_blinnphong=CreatePipeline(mtl_blinnphong,InlinePipeline::Solid3D,Prim::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);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
struct PlaneGridCreateInfo pgci;
|
2024-02-12 08:20:24 +08:00
|
|
|
|
|
2024-03-13 00:13:58 +08:00
|
|
|
|
pgci.grid_size.Set(32,32);
|
|
|
|
|
pgci.sub_count.Set(8,8);
|
2024-02-12 08:20:24 +08:00
|
|
|
|
|
2024-03-13 00:13:58 +08:00
|
|
|
|
pgci.lum=0.5;
|
|
|
|
|
pgci.sub_lum=0.75;
|
2024-02-12 08:20:24 +08:00
|
|
|
|
|
2024-03-13 00:13:58 +08:00
|
|
|
|
prim_plane_grid=CreatePlaneGrid(db,mtl_vertex_lum->GetDefaultVIL(),&pgci);
|
|
|
|
|
}
|
2024-03-04 13:13:33 +08:00
|
|
|
|
|
|
|
|
|
//Sphere
|
2024-03-16 00:20:27 +08:00
|
|
|
|
prim_sphere=CreateSphere(db,mi_blinnphong[0]->GetVIL(),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-03-16 00:20:27 +08:00
|
|
|
|
prim_cone=CreateCone(db,mi_blinnphong[1]->GetVIL(),&cci);
|
2024-03-15 01:38:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Cyliner
|
|
|
|
|
{
|
|
|
|
|
struct CylinderCreateInfo cci;
|
|
|
|
|
|
|
|
|
|
cci.halfExtend =4; //圆柱一半高度
|
|
|
|
|
cci.numberSlices=16; //圆柱底部分割数
|
|
|
|
|
cci.radius =0.25f; //圆柱半径
|
|
|
|
|
|
2024-03-16 00:20:27 +08:00
|
|
|
|
prim_cylinder=CreateCylinder(db,mi_blinnphong[2]->GetVIL(),&cci);
|
2024-02-12 08:20:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return(true);
|
2024-03-15 01:38:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-16 00:20:27 +08:00
|
|
|
|
Renderable *Add(Primitive *r,MaterialInstance *mi,Pipeline *p,const Matrix4f &mat=Identity4f)
|
2024-03-15 01:38:47 +08:00
|
|
|
|
{
|
|
|
|
|
Renderable *ri=db->CreateRenderable(r,mi,p);
|
|
|
|
|
|
|
|
|
|
if(!ri)
|
|
|
|
|
{
|
|
|
|
|
LOG_ERROR(OS_TEXT("Create Renderable failed."));
|
|
|
|
|
return(nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render_root.CreateSubNode(mat,ri);
|
|
|
|
|
|
|
|
|
|
return ri;
|
2024-02-12 08:20:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitScene()
|
|
|
|
|
{
|
2024-03-15 01:38:47 +08:00
|
|
|
|
Add(prim_plane_grid,mi_plane_grid,p_line,Identity4f);
|
|
|
|
|
|
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-03-13 00:13:58 +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-02-12 08:20:24 +08:00
|
|
|
|
if(!CreateRenderObject())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitScene())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
};//class TestApp:public CameraAppFramework
|
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
|
|
|
|
return RunApp<TestApp>(1280,720);
|
|
|
|
|
}
|