ULRE/example/LightBasic/BlinnPhongDirectionLight.cpp

191 lines
4.5 KiB
C++
Raw Normal View History

2024-03-06 13:54:05 +08:00
// BlinnPhong direction light
#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>
#include<hgl/graph/mtl/BlinnPhong.h>
using namespace hgl;
using namespace hgl::graph;
static float lumiance_data[2]={1,1};
static Color4f white_color(1,1,1,1);
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
};
class TestApp:public SceneAppFramework
{
2024-03-04 13:13:33 +08:00
private: //plane grid
Material * mtl_vertex_lum =nullptr;
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;
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;
private:
2024-02-13 22:27:12 +08:00
bool InitVertexLumMP()
{
mtl::Material3DCreateConfig cfg(device->GetDeviceAttribute(),"VertexLuminance3D",Prim::Lines);
cfg.local_to_world=true;
mtl_vertex_lum=db->LoadMaterial("Std3D/VertexLum3D",&cfg);
if(!mtl_vertex_lum)return(false);
mi_plane_grid=db->CreateMaterialInstance(mtl_vertex_lum,nullptr,&white_color);
if(!mi_plane_grid)return(false);
p_line=CreatePipeline(mtl_vertex_lum,InlinePipeline::Solid3D,Prim::Lines);
if(!p_line)
return(false);
return(true);
}
2024-03-04 13:13:33 +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;
mtl_sun_light=db->LoadMaterial("Std3D/BlinnPhong/SunLightPureColor",&cfg);
if(!mtl_sun_light)return(false);
mtl_sun_light->BindUBO(DescriptorSetType::Global,"sun",ubo_sun);
2024-03-10 00:35:35 +08:00
mtl_sun_light->Update();
2024-03-06 13:54:05 +08:00
float mi_data[4]=
{
1,0,0, //color
4 //gloss
};
mi_sphere=db->CreateMaterialInstance(mtl_sun_light,nullptr,&mi_data);
2024-03-04 13:13:33 +08:00
if(!mi_sphere)return(false);
p_sphere=CreatePipeline(mtl_sun_light,InlinePipeline::Solid3D,Prim::Triangles);
if(!p_sphere)
return(false);
return(true);
}
2024-03-04 13:13:33 +08:00
Renderable *Add(Primitive *r,MaterialInstance *mi,Pipeline *p)
{
2024-03-04 13:13:33 +08:00
Renderable *ri=db->CreateRenderable(r,mi,p);
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-12 23:29:38 +08:00
////Plane Grid
//{
// struct PlaneGridCreateInfo pgci;
2024-03-12 23:29:38 +08:00
// pgci.grid_size.Set(32,32);
// pgci.sub_count.Set(8,8);
2024-03-12 23:29:38 +08:00
// pgci.lum=0.5;
// pgci.sub_lum=0.75;
2024-03-12 23:29:38 +08:00
// prim_plane_grid=CreatePlaneGrid(db,mtl_vertex_lum->GetDefaultVIL(),&pgci);
//}
2024-03-04 13:13:33 +08:00
//Sphere
{
prim_sphere=CreateSphere(db,mi_sphere->GetVIL(),32);
}
return(true);
}
bool InitScene()
{
2024-03-12 23:29:38 +08:00
//Add(prim_plane_grid,mi_plane_grid,p_line);
2024-03-04 13:13:33 +08:00
Add(prim_sphere,mi_sphere,p_sphere);
2024-03-12 22:31:58 +08:00
camera->pos=Vector3f(32,32,32);
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-12 23:29:38 +08:00
//if(!InitVertexLumMP())
// return(false);
2024-03-10 00:35:35 +08:00
if(!CreateBlinnPhongUBO())
return(false);
2024-03-04 13:13:33 +08:00
if(!InitBlinnPhongSunLightMP())
return(false);
if(!CreateRenderObject())
return(false);
if(!InitScene())
return(false);
return(true);
}
};//class TestApp:public CameraAppFramework
int main(int,char **)
{
return RunApp<TestApp>(1280,720);
}