ULRE/example/Gizmo/RayPicking.cpp

205 lines
4.8 KiB
C++
Raw Normal View History

2022-02-10 18:56:00 +08:00
// 18.RayPicking
#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>
2022-02-10 18:56:00 +08:00
using namespace hgl;
using namespace hgl::graph;
constexpr uint32_t SCREEN_WIDTH=1280;
constexpr uint32_t SCREEN_HEIGHT=720;
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
class TestApp:public CameraAppFramework
{
Color4f color;
DeviceBuffer *ubo_color=nullptr;
2022-02-10 18:56:00 +08:00
private:
SceneNode render_root;
RenderList * render_list =nullptr;
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;
2023-10-05 00:50:31 +08:00
Primitive * ro_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;
AutoDelete<mtl::MaterialCreateInfo> mci=mtl::CreateVertexLuminance3D(&cfg);
material=db->CreateMaterial(mci);
2022-02-10 18:56:00 +08:00
if(!material)return(false);
2023-10-05 00:50:31 +08:00
db->global_descriptor.Bind(material);
mi_plane_grid=db->CreateMaterialInstance(material);
if(!mi_plane_grid)return(false);
mi_plane_grid->WriteMIData(white_color);
mi_line=db->CreateMaterialInstance(material);
if(!mi_line)return(false);
mi_line->WriteMIData(yellow_color);
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()
{
using namespace inline_geometry;
2022-02-10 18:56:00 +08:00
{
struct PlaneGridCreateInfo pgci;
2023-10-05 00:50:31 +08:00
pgci.grid_size.width =32;
pgci.grid_size.height=32;
2022-02-10 18:56:00 +08:00
2023-10-05 00:50:31 +08:00
pgci.sub_count.width =8;
pgci.sub_count.height=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
2023-10-05 00:50:31 +08:00
ro_plane_grid=CreatePlaneGrid(db,material->GetDefaultVIL(),&pgci);
2022-02-10 18:56:00 +08:00
}
{
2022-06-24 17:51:05 +08:00
ro_line=db->CreatePrimitive(2);
2022-02-10 18:56:00 +08:00
if(!ro_line)return(false);
2023-10-05 00:50:31 +08:00
if(!ro_line->Set(VAN::Position, vbo_pos= db->CreateVBO(VF_V3F,2,position_data )))return(false);
if(!ro_line->Set(VAN::Luminance, db->CreateVBO(VF_V1F,2,lumiance_data )))return(false);
2022-02-10 18:56:00 +08:00
}
return(true);
}
bool InitScene()
{
2023-10-05 00:50:31 +08:00
Add(ro_plane_grid,mi_plane_grid);
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:
~TestApp()
{
SAFE_CLEAR(render_list);
}
bool Init()
{
if(!CameraAppFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
return(false);
render_list=new RenderList(device);
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上这个点的位置
VulkanApplicationFramework::BuildCommandBuffer(index,render_list);
}
2023-10-07 16:14:34 +08:00
void Resize(int w,int h) override
2022-02-10 18:56:00 +08:00
{
CameraAppFramework::Resize(w,h);
VulkanApplicationFramework::BuildCommandBuffer(render_list);
}
};//class TestApp:public CameraAppFramework
int main(int,char **)
{
TestApp app;
if(!app.Init())
return(-1);
while(app.Run());
return 0;
}