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>
|
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:
|
|
|
|
|
|
|
|
|
|
Material * material =nullptr;
|
2023-10-05 00:50:31 +08:00
|
|
|
|
MaterialInstance * mi_plane_grid =nullptr;
|
|
|
|
|
MaterialInstance * mi_line =nullptr;
|
|
|
|
|
|
2022-02-10 18:56:00 +08:00
|
|
|
|
Pipeline * pipeline =nullptr;
|
|
|
|
|
|
2024-03-04 13:13:33 +08:00
|
|
|
|
Primitive * prim_plane_grid =nullptr;
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
2023-10-05 00:50:31 +08:00
|
|
|
|
Primitive * ro_line =nullptr;
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
|
|
|
|
VBO * vbo_pos =nullptr;
|
|
|
|
|
|
|
|
|
|
Ray ray;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
2023-10-05 00:50:31 +08:00
|
|
|
|
bool InitMaterialAndPipeline()
|
2022-02-10 18:56:00 +08:00
|
|
|
|
{
|
2023-10-05 00:50:31 +08:00
|
|
|
|
mtl::Material3DCreateConfig cfg(device->GetDeviceAttribute(),"VertexLuminance3D",Prim::Lines);
|
|
|
|
|
|
|
|
|
|
cfg.local_to_world=true;
|
|
|
|
|
|
2023-10-11 19:02:17 +08:00
|
|
|
|
material=db->LoadMaterial("Std3D/VertexLum3D",&cfg);
|
2022-02-10 18:56:00 +08:00
|
|
|
|
if(!material)return(false);
|
|
|
|
|
|
2023-10-11 19:14:24 +08:00
|
|
|
|
mi_plane_grid=db->CreateMaterialInstance(material,nullptr,&white_color);
|
2023-10-05 00:50:31 +08:00
|
|
|
|
if(!mi_plane_grid)return(false);
|
|
|
|
|
|
2023-10-11 19:14:24 +08:00
|
|
|
|
mi_line=db->CreateMaterialInstance(material,nullptr,&yellow_color);
|
2023-10-05 00:50:31 +08:00
|
|
|
|
if(!mi_line)return(false);
|
|
|
|
|
|
|
|
|
|
pipeline=CreatePipeline(material,InlinePipeline::Solid3D,Prim::Lines);
|
|
|
|
|
|
2022-02-10 18:56:00 +08:00
|
|
|
|
if(!pipeline)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-05 00:50:31 +08:00
|
|
|
|
Renderable *Add(Primitive *r,MaterialInstance *mi)
|
2022-02-10 18:56:00 +08:00
|
|
|
|
{
|
2023-10-05 00:50:31 +08:00
|
|
|
|
Renderable *ri=db->CreateRenderable(r,mi,pipeline);
|
|
|
|
|
|
|
|
|
|
if(!ri)
|
|
|
|
|
{
|
|
|
|
|
LOG_ERROR(OS_TEXT("Create Renderable failed."));
|
|
|
|
|
return(nullptr);
|
|
|
|
|
}
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
2023-10-05 00:50:31 +08:00
|
|
|
|
render_root.CreateSubNode(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;
|
|
|
|
|
|
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
|
|
|
|
|
2023-10-05 00:50:31 +08:00
|
|
|
|
pgci.lum=0.5;
|
|
|
|
|
pgci.sub_lum=0.75;
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
2024-03-04 13:13:33 +08:00
|
|
|
|
prim_plane_grid=CreatePlaneGrid(db,material->GetDefaultVIL(),&pgci);
|
2022-02-10 18:56:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2023-10-12 05:55:39 +08:00
|
|
|
|
ro_line=db->CreatePrimitive("Line",2);
|
2022-02-10 18:56:00 +08:00
|
|
|
|
if(!ro_line)return(false);
|
|
|
|
|
|
2024-04-24 01:38:55 +08:00
|
|
|
|
if(!ro_line->Set(VAN::Position, vbo_pos= db->CreateVAB(VF_V3F,2,position_data )))return(false);
|
|
|
|
|
if(!ro_line->Set(VAN::Luminance, db->CreateVAB(VF_V1F,2,lumiance_data )))return(false);
|
2022-02-10 18:56:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitScene()
|
|
|
|
|
{
|
2024-03-04 13:13:33 +08:00
|
|
|
|
Add(prim_plane_grid,mi_plane_grid);
|
2023-10-05 00:50:31 +08:00
|
|
|
|
Add(ro_line,mi_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:
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
const CameraInfo &ci=GetCameraInfo();
|
2023-10-05 00:50:31 +08:00
|
|
|
|
const ViewportInfo &vi=GetViewportInfo();
|
2022-02-10 18:56:00 +08:00
|
|
|
|
|
2023-10-05 00:50:31 +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)最近的点的坐标
|
|
|
|
|
|
|
|
|
|
vbo_pos->Write(&pos,3*sizeof(float)); //更新VBO上这个点的位置
|
|
|
|
|
|
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
|
|
|
|
}
|