ULRE/example/Vulkan/EquirectangularMap.cpp

147 lines
3.3 KiB
C++
Raw Normal View History

2022-02-11 18:07:47 +08:00
// Equirectangular ( or spherical)
// 等距矩形(或球形)贴图反射测试
#include"VulkanAppFramework.h"
#include<hgl/filesystem/FileSystem.h>
#include<hgl/graph/InlineGeometry.h>
#include<hgl/graph/VKRenderResource.h>
#include<hgl/graph/VKRenderable.h>
2022-02-11 18:07:47 +08:00
#include<hgl/graph/VKTexture.h>
#include<hgl/graph/RenderList.h>
using namespace hgl;
using namespace hgl::graph;
constexpr uint32_t SCREEN_WIDTH=1280;
constexpr uint32_t SCREEN_HEIGHT=720;
2022-02-11 18:07:47 +08:00
class TestApp:public CameraAppFramework
{
private:
SceneNode render_root;
RenderList * render_list =nullptr;
MaterialInstance * envmap_mi =nullptr;
Pipeline * solid_pipeline =nullptr;
DeviceBuffer * ubo_light =nullptr;
DeviceBuffer * ubo_phong =nullptr;
2022-02-11 18:07:47 +08:00
Sampler * sampler =nullptr;
Texture2D * texture =nullptr;
2022-06-24 17:51:05 +08:00
Primitive * ro_sphere =nullptr;
2022-02-11 18:07:47 +08:00
private:
bool InitMaterial()
{
{
// photo source: https://www.hqreslib.com
// license: CY-BY
texture=db->LoadTexture2D(OS_TEXT("res/equirectangular/jifu.Tex2D"),false);
2022-02-11 18:07:47 +08:00
if(!texture)
return(false);
sampler=db->CreateSampler();
2022-02-11 18:07:47 +08:00
}
{
envmap_mi=db->CreateMaterialInstance(OS_TEXT("res/material/EnvEquirectangularMap"));
2022-02-11 18:07:47 +08:00
if(!envmap_mi)return(false);
2023-02-23 13:25:05 +08:00
if(!envmap_mi->BindImageSampler(DescriptorSetType::Value,"Envmap" ,texture, sampler))return(false);
2022-02-11 18:07:47 +08:00
}
solid_pipeline=CreatePipeline(envmap_mi,InlinePipeline::Solid3D,Prim::Triangles);
2022-02-11 18:07:47 +08:00
return(true);
}
void CreateRenderObject()
{
ro_sphere=inline_geometry::CreateSphere(db,envmap_mi->GetVIL(),128);
2022-02-11 18:07:47 +08:00
}
bool InitUBO()
{
if(!BindCameraUBO(envmap_mi))return(false);
return(true);
}
2022-06-24 17:51:05 +08:00
SceneNode *Add(Primitive *r,MaterialInstance *mi,Pipeline *pl)
2022-02-11 18:07:47 +08:00
{
auto ri=db->CreateRenderable(r,mi,pl);
2022-02-11 18:07:47 +08:00
return render_root.CreateSubNode(ri);
}
bool InitScene()
{
2022-02-11 19:53:01 +08:00
auto sn_sphere=Add(ro_sphere,envmap_mi,solid_pipeline);
2022-02-11 18:07:47 +08:00
sn_sphere->SetLocalMatrix(scale(5,5,5));
render_root.RefreshMatrix();
render_list->Expend(GetCameraInfo(),&render_root);
return(true);
}
public:
~TestApp()
{
SAFE_CLEAR(render_list);
}
bool Init()
{
if(!CameraAppFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
return(false);
camera->pos=Vector3f(10,10,0);
camera_control->SetTarget(Vector3f(0,0,0));
render_list=new RenderList(device);
if(!InitMaterial())
return(false);
if(!InitUBO())
return(false);
CreateRenderObject();
if(!InitScene())
return(false);
return(true);
}
void BuildCommandBuffer(uint32_t index) override
{
render_root.RefreshMatrix();
render_list->Expend(GetCameraInfo(),&render_root);
VulkanApplicationFramework::BuildCommandBuffer(index,render_list);
}
};//class TestApp:public CameraAppFramework
int main(int,char **)
{
TestApp app;
if(!app.Init())
return(-1);
while(app.Run());
return 0;
}