2023-10-13 19:22:11 +08:00
|
|
|
|
// RayPicking
|
2022-02-10 18:56:00 +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>
|
2023-10-07 20:59:44 +08:00
|
|
|
|
#include<hgl/graph/mtl/Material3DCreateConfig.h>
|
2024-06-02 12:16:33 +08:00
|
|
|
|
#include<hgl/graph/VertexDataManager.h>
|
2024-07-10 02:00:18 +08:00
|
|
|
|
#include<hgl/graph/VKVertexInputConfig.h>
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
|
|
|
|
static float position_data[2][3]=
|
|
|
|
|
{
|
|
|
|
|
{100,100,100},
|
|
|
|
|
{0,0,0}
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-05 00:50:31 +08:00
|
|
|
|
static float lumiance_data[2]={1,1};
|
|
|
|
|
|
|
|
|
|
static Color4f white_color(1,1,1,1);
|
|
|
|
|
static Color4f yellow_color(1,1,0,1);
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
2023-10-11 18:49:29 +08:00
|
|
|
|
class TestApp:public SceneAppFramework
|
2022-02-10 18:56:00 +08:00
|
|
|
|
{
|
|
|
|
|
Color4f color;
|
|
|
|
|
|
2022-10-14 17:52:35 +08:00
|
|
|
|
DeviceBuffer *ubo_color=nullptr;
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
2024-06-12 08:53:54 +08:00
|
|
|
|
Material * mtl_plane_grid =nullptr;
|
2023-10-05 00:50:31 +08:00
|
|
|
|
MaterialInstance * mi_plane_grid =nullptr;
|
2024-06-12 08:53:54 +08:00
|
|
|
|
Pipeline * pipeline_plane_grid =nullptr;
|
2024-04-24 01:44:01 +08:00
|
|
|
|
Primitive * prim_plane_grid =nullptr;
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
2024-06-12 08:53:54 +08:00
|
|
|
|
Material * mtl_line =nullptr;
|
|
|
|
|
MaterialInstance * mi_line =nullptr;
|
|
|
|
|
Pipeline * pipeline_line =nullptr;
|
2024-06-02 12:16:33 +08:00
|
|
|
|
Primitive * prim_line =nullptr;
|
2024-06-12 02:14:56 +08:00
|
|
|
|
VABMap * prim_line_vab_map =nullptr;
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
|
|
|
|
Ray ray;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
2023-10-05 00:50:31 +08:00
|
|
|
|
bool InitMaterialAndPipeline()
|
2022-02-10 18:56:00 +08:00
|
|
|
|
{
|
2025-05-17 20:26:36 +08:00
|
|
|
|
mtl::Material3DCreateConfig cfg(device->GetDevAttr(),"VertexLuminance2D",PrimitiveType::Lines);
|
2023-10-05 00:50:31 +08:00
|
|
|
|
|
|
|
|
|
cfg.local_to_world=true;
|
|
|
|
|
|
2024-08-08 01:43:20 +08:00
|
|
|
|
{
|
2024-06-12 09:55:50 +08:00
|
|
|
|
cfg.mtl_name="VertexLuminance2D"; //注意必须用不同名字,未来改名材质文件名+cfg hash名
|
2024-06-12 08:53:54 +08:00
|
|
|
|
cfg.position_format=VAT_VEC2;
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
2024-06-12 09:55:50 +08:00
|
|
|
|
mtl_plane_grid=db->LoadMaterial("Std3D/VertexLum3D",&cfg);
|
2024-06-12 08:53:54 +08:00
|
|
|
|
if(!mtl_plane_grid)return(false);
|
|
|
|
|
|
2024-07-10 02:00:18 +08:00
|
|
|
|
VILConfig vil_config;
|
|
|
|
|
|
|
|
|
|
vil_config.Add(VAN::Luminance,VF_V1UN8);
|
|
|
|
|
|
|
|
|
|
mi_plane_grid=db->CreateMaterialInstance(mtl_plane_grid,&vil_config,&white_color);
|
2024-06-12 08:53:54 +08:00
|
|
|
|
if(!mi_plane_grid)return(false);
|
2023-10-05 00:50:31 +08:00
|
|
|
|
|
2025-05-04 23:36:28 +08:00
|
|
|
|
pipeline_plane_grid=CreatePipeline(mi_plane_grid,InlinePipeline::Solid3D,PrimitiveType::Lines);
|
2024-06-12 08:53:54 +08:00
|
|
|
|
if(!pipeline_plane_grid)return(false);
|
|
|
|
|
}
|
2023-10-05 00:50:31 +08:00
|
|
|
|
|
2024-06-12 08:53:54 +08:00
|
|
|
|
{
|
2024-06-12 08:56:01 +08:00
|
|
|
|
cfg.mtl_name="VertexLuminance3D"; //注意必须用不同名字,未来改名材质文件名+cfg hash名
|
2024-06-12 08:53:54 +08:00
|
|
|
|
cfg.position_format=VAT_VEC3;
|
2023-10-05 00:50:31 +08:00
|
|
|
|
|
2024-06-12 08:53:54 +08:00
|
|
|
|
mtl_line=db->LoadMaterial("Std3D/VertexLum3D",&cfg);
|
|
|
|
|
if(!mtl_line)return(false);
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
2024-06-12 08:53:54 +08:00
|
|
|
|
mi_line=db->CreateMaterialInstance(mtl_line,nullptr,&yellow_color);
|
|
|
|
|
if(!mi_line)return(false);
|
|
|
|
|
|
2025-05-04 23:36:28 +08:00
|
|
|
|
pipeline_line=CreatePipeline(mtl_line,InlinePipeline::Solid3D,PrimitiveType::Lines);
|
2024-06-12 08:53:54 +08:00
|
|
|
|
|
|
|
|
|
if(!pipeline_line)
|
|
|
|
|
return(false);
|
|
|
|
|
}
|
2024-06-02 12:16:33 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
2024-06-12 08:53:54 +08:00
|
|
|
|
|
|
|
|
|
Renderable *Add(Primitive *r,MaterialInstance *mi,Pipeline *p)
|
2022-02-10 18:56:00 +08:00
|
|
|
|
{
|
2024-06-12 08:53:54 +08:00
|
|
|
|
Renderable *ri=db->CreateRenderable(r,mi,p);
|
2023-10-05 00:50:31 +08:00
|
|
|
|
|
|
|
|
|
if(!ri)
|
|
|
|
|
{
|
|
|
|
|
LOG_ERROR(OS_TEXT("Create Renderable failed."));
|
|
|
|
|
return(nullptr);
|
|
|
|
|
}
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
2024-10-06 14:51:38 +08:00
|
|
|
|
render_root.Add(new SceneNode(ri));
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
|
|
|
|
return ri;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CreateRenderObject()
|
|
|
|
|
{
|
2022-06-24 21:36:24 +08:00
|
|
|
|
using namespace inline_geometry;
|
2024-06-12 08:53:54 +08:00
|
|
|
|
|
2022-02-10 18:56:00 +08:00
|
|
|
|
{
|
2024-07-10 02:00:18 +08:00
|
|
|
|
PrimitiveCreater pc(device,mi_plane_grid->GetVIL());
|
2024-06-12 08:53:54 +08:00
|
|
|
|
|
2022-02-10 18:56:00 +08:00
|
|
|
|
struct PlaneGridCreateInfo pgci;
|
|
|
|
|
|
2024-02-12 08:20:24 +08:00
|
|
|
|
pgci.grid_size.Set(32,32);
|
|
|
|
|
pgci.sub_count.Set(8,8);
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
2024-07-10 02:00:18 +08:00
|
|
|
|
pgci.lum=128;
|
|
|
|
|
pgci.sub_lum=196;
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
2024-07-10 02:00:18 +08:00
|
|
|
|
prim_plane_grid=CreatePlaneGrid2D(&pc,&pgci);
|
2022-02-10 18:56:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2024-06-12 08:53:54 +08:00
|
|
|
|
PrimitiveCreater pc(device,mtl_line->GetDefaultVIL());
|
|
|
|
|
|
|
|
|
|
if(!pc.Init("Line",2))
|
2024-06-02 12:16:33 +08:00
|
|
|
|
return(false);
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
2024-06-12 08:53:54 +08:00
|
|
|
|
if(!pc.WriteVAB(VAN::Position, VF_V3F,position_data))return(false);
|
|
|
|
|
if(!pc.WriteVAB(VAN::Luminance,VF_V1F,lumiance_data))return(false);
|
2024-06-02 12:16:33 +08:00
|
|
|
|
|
2024-06-12 08:53:54 +08:00
|
|
|
|
prim_line=pc.Create();
|
2024-06-02 12:16:33 +08:00
|
|
|
|
|
2024-06-12 02:14:56 +08:00
|
|
|
|
prim_line_vab_map=prim_line->GetVABMap(VAN::Position);
|
2022-02-10 18:56:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitScene()
|
|
|
|
|
{
|
2024-06-12 08:53:54 +08:00
|
|
|
|
Add(prim_plane_grid,mi_plane_grid,pipeline_plane_grid);
|
|
|
|
|
Add(prim_line,mi_line,pipeline_line);
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
2023-10-05 00:50:31 +08:00
|
|
|
|
camera->pos=Vector3f(32,32,32);
|
2022-02-10 18:56:00 +08:00
|
|
|
|
camera_control->SetTarget(Vector3f(0,0,0));
|
|
|
|
|
camera_control->Refresh();
|
|
|
|
|
|
|
|
|
|
render_root.RefreshMatrix();
|
2023-10-05 00:50:31 +08:00
|
|
|
|
render_list->Expend(&render_root);
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2024-06-02 12:16:33 +08:00
|
|
|
|
~TestApp()
|
|
|
|
|
{
|
2024-06-12 08:53:54 +08:00
|
|
|
|
SAFE_CLEAR(prim_plane_grid);
|
|
|
|
|
SAFE_CLEAR(prim_line);
|
2024-06-02 12:16:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-11 19:06:18 +08:00
|
|
|
|
bool Init(uint w,uint h)
|
|
|
|
|
{
|
|
|
|
|
if(!SceneAppFramework::Init(w,h))
|
2022-02-10 18:56:00 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2023-10-05 00:50:31 +08:00
|
|
|
|
if(!InitMaterialAndPipeline())
|
2022-02-10 18:56:00 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!CreateRenderObject())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitScene())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-07 16:14:34 +08:00
|
|
|
|
void BuildCommandBuffer(uint32 index) override
|
2022-02-10 18:56:00 +08:00
|
|
|
|
{
|
2024-08-08 01:43:20 +08:00
|
|
|
|
const CameraInfo *ci=GetCameraInfo();
|
|
|
|
|
const ViewportInfo *vi=GetViewportInfo();
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
2024-08-08 01:43:20 +08:00
|
|
|
|
ray.Set(GetMouseCoord(),ci,vi); //设置射线查询的屏幕坐标点
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
|
|
|
|
const Vector3f pos=ray.ClosestPoint(Vector3f(0,0,0)); //求射线上与点(0,0,0)最近的点的坐标
|
|
|
|
|
|
2024-06-12 08:53:54 +08:00
|
|
|
|
prim_line_vab_map->Write(&pos, //更新VAB上这个点的位置
|
|
|
|
|
1); //这里的1代表的数据数量,不是字节数
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
2023-10-11 19:06:18 +08:00
|
|
|
|
SceneAppFramework::BuildCommandBuffer(index);
|
2022-02-10 18:56:00 +08:00
|
|
|
|
}
|
|
|
|
|
};//class TestApp:public CameraAppFramework
|
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
2023-10-11 19:06:18 +08:00
|
|
|
|
return RunApp<TestApp>(1280,720);
|
2022-02-10 18:56:00 +08:00
|
|
|
|
}
|