2023-10-13 19:22:11 +08:00
|
|
|
|
// RayPicking
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
2025-06-11 01:12:14 +08:00
|
|
|
|
#include<hgl/WorkManager.h>
|
2022-02-10 18:56:00 +08:00
|
|
|
|
#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}
|
|
|
|
|
};
|
|
|
|
|
|
2025-06-11 23:22:09 +08:00
|
|
|
|
static uint8 lumiance_data[2]={255,255};
|
2023-10-05 00:50:31 +08:00
|
|
|
|
|
|
|
|
|
static Color4f white_color(1,1,1,1);
|
|
|
|
|
static Color4f yellow_color(1,1,0,1);
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
2025-06-11 01:12:14 +08:00
|
|
|
|
class TestApp:public WorkObject
|
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-06-11 01:12:14 +08:00
|
|
|
|
mtl::Material3DCreateConfig cfg(PrimitiveType::Lines);
|
2023-10-05 00:50:31 +08:00
|
|
|
|
|
|
|
|
|
cfg.local_to_world=true;
|
|
|
|
|
|
2025-06-11 23:22:09 +08:00
|
|
|
|
VILConfig vil_config;
|
|
|
|
|
|
|
|
|
|
vil_config.Add(VAN::Luminance,VF_V1UN8);
|
|
|
|
|
|
2024-08-08 01:43:20 +08:00
|
|
|
|
{
|
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
|
|
|
|
|
|
|
|
|
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-06-12 00:00:14 +08:00
|
|
|
|
pipeline_plane_grid=CreatePipeline(mi_plane_grid,InlinePipeline::Solid3D);
|
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
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
2025-06-11 23:22:09 +08:00
|
|
|
|
mi_line=db->CreateMaterialInstance(mtl_line,&vil_config,&yellow_color);
|
2024-06-12 08:53:54 +08:00
|
|
|
|
if(!mi_line)return(false);
|
|
|
|
|
|
2025-06-12 00:00:14 +08:00
|
|
|
|
pipeline_line=CreatePipeline(mi_line,InlinePipeline::Solid3D);
|
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
|
|
|
|
|
2025-06-11 01:12:14 +08:00
|
|
|
|
Mesh *Add(SceneNode *parent_node,Primitive *r,MaterialInstance *mi,Pipeline *p)
|
2022-02-10 18:56:00 +08:00
|
|
|
|
{
|
2025-05-18 02:19:14 +08:00
|
|
|
|
Mesh *ri=db->CreateMesh(r,mi,p);
|
2023-10-05 00:50:31 +08:00
|
|
|
|
|
|
|
|
|
if(!ri)
|
|
|
|
|
{
|
2025-05-18 02:03:16 +08:00
|
|
|
|
LOG_ERROR(OS_TEXT("Create Mesh failed."));
|
2023-10-05 00:50:31 +08:00
|
|
|
|
return(nullptr);
|
|
|
|
|
}
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
2025-06-11 01:12:14 +08:00
|
|
|
|
parent_node->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
|
|
|
|
{
|
2025-06-11 01:12:14 +08:00
|
|
|
|
auto pc=GetPrimitiveCreater(mi_plane_grid);
|
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
|
|
|
|
|
2025-06-11 01:12:14 +08:00
|
|
|
|
prim_plane_grid=CreatePlaneGrid2D(pc,&pgci);
|
2022-02-10 18:56:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2025-06-11 23:22:09 +08:00
|
|
|
|
prim_line=CreatePrimitive("RayLine",2,mi_line->GetVIL(),
|
2025-06-11 01:17:04 +08:00
|
|
|
|
{
|
|
|
|
|
{VAN::Position, VF_V3F,position_data},
|
2025-06-11 23:22:09 +08:00
|
|
|
|
{VAN::Luminance,VF_V1UN8,lumiance_data}
|
2025-06-11 01:17:04 +08:00
|
|
|
|
});
|
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()
|
|
|
|
|
{
|
2025-06-11 01:12:14 +08:00
|
|
|
|
SceneNode *scene_root=GetSceneRoot(); //取得缺省场景根节点
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
2025-06-11 01:12:14 +08:00
|
|
|
|
Add(scene_root,prim_plane_grid,mi_plane_grid,pipeline_plane_grid);
|
|
|
|
|
Add(scene_root,prim_line,mi_line,pipeline_line);
|
|
|
|
|
|
|
|
|
|
CameraControl *camera_control=GetCameraControl();
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
2025-06-11 01:12:14 +08:00
|
|
|
|
camera_control->SetPosition(Vector3f(32,32,32));
|
|
|
|
|
camera_control->SetTarget(Vector3f(0,0,0));
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2025-06-11 01:12:14 +08:00
|
|
|
|
using WorkObject::WorkObject;
|
|
|
|
|
|
2024-06-02 12:16:33 +08:00
|
|
|
|
~TestApp()
|
|
|
|
|
{
|
2024-06-12 08:53:54 +08:00
|
|
|
|
SAFE_CLEAR(prim_plane_grid);
|
2024-06-02 12:16:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-06-11 01:12:14 +08:00
|
|
|
|
bool Init() override
|
|
|
|
|
{
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-11 01:12:14 +08:00
|
|
|
|
void Tick(double) override
|
2022-02-10 18:56:00 +08:00
|
|
|
|
{
|
2025-06-11 01:12:14 +08:00
|
|
|
|
Vector2i mouse_position;
|
|
|
|
|
|
|
|
|
|
if(!GetMouseCoord(&mouse_position))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
CameraControl *camera_control=GetCameraControl();
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
2025-06-11 01:12:14 +08:00
|
|
|
|
const CameraInfo *ci=camera_control->GetCameraInfo();
|
|
|
|
|
const ViewportInfo *vi=camera_control->GetViewportInfo();
|
|
|
|
|
|
|
|
|
|
ray.Set(mouse_position,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上这个点的位置
|
2025-06-11 01:12:14 +08:00
|
|
|
|
1); //这里的1代表的数据数量,不是字节数
|
2022-02-10 18:56:00 +08:00
|
|
|
|
}
|
|
|
|
|
};//class TestApp:public CameraAppFramework
|
|
|
|
|
|
2025-06-11 01:12:14 +08:00
|
|
|
|
int os_main(int,os_char **)
|
2022-02-10 18:56:00 +08:00
|
|
|
|
{
|
2025-06-11 01:12:14 +08:00
|
|
|
|
return RunFramework<TestApp>(OS_TEXT("RayPicking"),1280,720);
|
2022-02-10 18:56:00 +08:00
|
|
|
|
}
|