ULRE/example/Gizmo/GizmoTest.cpp

105 lines
2.2 KiB
C++
Raw Normal View History

2024-07-01 23:31:18 +08:00
#include"VulkanAppFramework.h"
#include"Gizmo.h"
#include<hgl/graph/Ray.h>
2024-07-01 23:31:18 +08:00
using namespace hgl;
using namespace hgl::graph;
/**
*
*/
Vector2f WorldToScreen(const Vector3f &world_pos,const CameraInfo &ci,const ViewportInfo &vi)
{
Vector4f pos=ci.vp*Vector4f(world_pos,1);
pos/=pos.w;
return Vector2f((pos.x+1)*vi.GetViewportWidth()/2,(1-pos.y)*vi.GetViewportHeight()/2);
}
2024-07-01 23:31:18 +08:00
class TestApp:public SceneAppFramework
{
2024-07-31 00:11:24 +08:00
SceneNode root;
2024-07-30 23:54:25 +08:00
StaticMesh *sm_move=nullptr;
StaticMesh *sm_rotate=nullptr;
Renderable *face_torus=nullptr;
2024-07-01 23:31:18 +08:00
private:
bool InitGizmo()
{
if(!InitGizmoResource(db))
2024-07-01 23:31:18 +08:00
return(false);
2024-07-30 23:54:25 +08:00
sm_move =GetGizmoMoveStaticMesh();
sm_rotate =GetGizmoRotateStaticMesh();
face_torus =GetGizmoRenderable(GizmoShape::Torus,GizmoColor::White);
2024-07-01 23:31:18 +08:00
return(true);
}
public:
bool Init(uint w,uint h)
{
if(!SceneAppFramework::Init(w,h))
return(false);
if(!InitGizmo())
return(false);
camera->pos=Vector3f(32,32,32);
camera_control->SetTarget(Vector3f(0,0,0));
return(true);
}
~TestApp()
{
FreeGizmoResource();
}
void BuildCommandBuffer(uint32 index) override
{
camera_control->Refresh();
const CameraInfo &ci=camera_control->GetCameraInfo();
const ViewportInfo &vi=GetViewportInfo();
const float screen_height=vi.GetViewportHeight();
root.Clear();
const Vector3f GizmoPosition(0,0,0);
const Vector4f pos=ci.Project(GizmoPosition);
root.SetLocalMatrix(scale(pos.w*16.0f/screen_height));
2024-07-31 00:11:24 +08:00
root.CreateSubNode(sm_move->GetScene());
root.CreateSubNode(sm_rotate->GetScene());
{
Transform tm;
tm.SetRotation(CalculateFacingRotationQuat(GizmoPosition,ci.view,AxisVector::X));
tm.SetScale(15);
root.CreateSubNode(tm,face_torus);
}
2024-07-31 00:11:24 +08:00
root.RefreshMatrix();
render_list->Expend(&root);
2024-07-01 23:31:18 +08:00
SceneAppFramework::BuildCommandBuffer(index);
2024-07-01 23:31:18 +08:00
}
};//class TestApp:public SceneAppFramework
int main(int,char **)
{
2024-07-31 00:11:24 +08:00
return RunApp<TestApp>(1024,1024);
2024-07-01 23:31:18 +08:00
}