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-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=
|
|
|
|
|
{
|
|
|
|
|
Vector3f(1,1,1),
|
|
|
|
|
Vector3f(1,0.975,0.95)
|
|
|
|
|
};
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
Material * mtl_sun_light =nullptr;
|
|
|
|
|
MaterialInstance * mi_sphere =nullptr;
|
|
|
|
|
Pipeline * p_sphere =nullptr;
|
|
|
|
|
Primitive * prim_sphere =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()
|
|
|
|
|
{
|
|
|
|
|
sun_light.color=Vector3f(1,1,1);
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
mtl_sun_light=db->LoadMaterial("Std3D/BlinnPhong/SunLightPureColor",&cfg);
|
|
|
|
|
if(!mtl_sun_light)return(false);
|
|
|
|
|
|
2024-03-07 13:59:28 +08:00
|
|
|
|
mtl_sun_light->BindUBO(DescriptorSetType::Global,"sun",ubo_sun);
|
2024-03-06 13:54:05 +08:00
|
|
|
|
|
2024-03-04 13:13:33 +08:00
|
|
|
|
mi_sphere=db->CreateMaterialInstance(mtl_sun_light);
|
|
|
|
|
if(!mi_sphere)return(false);
|
|
|
|
|
|
|
|
|
|
p_sphere=CreatePipeline(mtl_sun_light,InlinePipeline::Solid3D,Prim::Triangles);
|
|
|
|
|
|
|
|
|
|
if(!p_sphere)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
2024-02-12 08:20:24 +08:00
|
|
|
|
|
2024-03-04 13:13:33 +08:00
|
|
|
|
Renderable *Add(Primitive *r,MaterialInstance *mi,Pipeline *p)
|
2024-02-12 08:20:24 +08:00
|
|
|
|
{
|
2024-03-04 13:13:33 +08:00
|
|
|
|
Renderable *ri=db->CreateRenderable(r,mi,p);
|
2024-02-12 08:20:24 +08:00
|
|
|
|
|
|
|
|
|
if(!ri)
|
|
|
|
|
{
|
|
|
|
|
LOG_ERROR(OS_TEXT("Create Renderable failed."));
|
|
|
|
|
return(nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render_root.CreateSubNode(ri);
|
|
|
|
|
|
|
|
|
|
return ri;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CreateRenderObject()
|
|
|
|
|
{
|
|
|
|
|
using namespace inline_geometry;
|
|
|
|
|
|
2024-03-04 13:13:33 +08:00
|
|
|
|
//Plane Grid
|
2024-02-12 08:20:24 +08:00
|
|
|
|
{
|
|
|
|
|
struct PlaneGridCreateInfo pgci;
|
|
|
|
|
|
|
|
|
|
pgci.grid_size.Set(32,32);
|
|
|
|
|
pgci.sub_count.Set(8,8);
|
|
|
|
|
|
|
|
|
|
pgci.lum=0.5;
|
|
|
|
|
pgci.sub_lum=0.75;
|
|
|
|
|
|
2024-03-04 13:13:33 +08:00
|
|
|
|
prim_plane_grid=CreatePlaneGrid(db,mtl_vertex_lum->GetDefaultVIL(),&pgci);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Sphere
|
|
|
|
|
{
|
|
|
|
|
prim_sphere=CreateSphere(db,mi_sphere->GetVIL(),16);
|
2024-02-12 08:20:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitScene()
|
|
|
|
|
{
|
2024-03-04 13:13:33 +08:00
|
|
|
|
Add(prim_plane_grid,mi_plane_grid,p_line);
|
|
|
|
|
Add(prim_sphere,mi_sphere,p_sphere);
|
2024-02-12 08:20:24 +08:00
|
|
|
|
|
2024-03-07 13:59:28 +08:00
|
|
|
|
camera->pos=Vector3f(32,15,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-02-13 22:27:12 +08:00
|
|
|
|
if(!InitVertexLumMP())
|
2024-02-12 08:20:24 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
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);
|
|
|
|
|
}
|